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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #include <asm/unaligned.h> |
| 53 | |
| 54 | #include <net/bluetooth/bluetooth.h> |
| 55 | #include <net/bluetooth/hci_core.h> |
| 56 | #include <net/bluetooth/l2cap.h> |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 57 | #include <net/bluetooth/smp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 59 | bool disable_ertm; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 60 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 61 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 62 | static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 64 | static LIST_HEAD(chan_list); |
| 65 | static DEFINE_RWLOCK(chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
| 68 | u8 code, u8 ident, u16 dlen, void *data); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 69 | static void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, |
| 70 | void *data); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 71 | 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] | 72 | static void l2cap_send_disconn_req(struct l2cap_conn *conn, |
| 73 | struct l2cap_chan *chan, int err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 75 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 76 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 77 | 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] | 78 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 79 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 80 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 81 | list_for_each_entry(c, &conn->chan_l, list) { |
| 82 | if (c->dcid == cid) |
| 83 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 84 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 85 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 88 | 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] | 89 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 90 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 91 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 92 | list_for_each_entry(c, &conn->chan_l, list) { |
| 93 | if (c->scid == cid) |
| 94 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 95 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 96 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* Find channel with given SCID. |
| 100 | * Returns locked socket */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 101 | 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] | 102 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 103 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 104 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 105 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 106 | c = __l2cap_get_chan_by_scid(conn, cid); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 107 | mutex_unlock(&conn->chan_lock); |
| 108 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 109 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 112 | 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] | 113 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 114 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 115 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 116 | list_for_each_entry(c, &conn->chan_l, list) { |
| 117 | if (c->ident == ident) |
| 118 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 119 | } |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 120 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 121 | } |
| 122 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 123 | 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] | 124 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 125 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 126 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 127 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 128 | c = __l2cap_get_chan_by_ident(conn, ident); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 129 | mutex_unlock(&conn->chan_lock); |
| 130 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 131 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 134 | 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] | 135 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 136 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 137 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 138 | list_for_each_entry(c, &chan_list, global_l) { |
| 139 | if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 140 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 141 | } |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 142 | return NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 146 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 147 | int err; |
| 148 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 149 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 150 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 151 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 152 | err = -EADDRINUSE; |
| 153 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 154 | } |
| 155 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 156 | if (psm) { |
| 157 | chan->psm = psm; |
| 158 | chan->sport = psm; |
| 159 | err = 0; |
| 160 | } else { |
| 161 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 162 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 163 | err = -EINVAL; |
| 164 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 165 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 166 | chan->psm = cpu_to_le16(p); |
| 167 | chan->sport = cpu_to_le16(p); |
| 168 | err = 0; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | done: |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 174 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 175 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 179 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 180 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 181 | |
| 182 | chan->scid = scid; |
| 183 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 184 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 189 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 190 | { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 191 | u16 cid = L2CAP_CID_DYN_START; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 192 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 193 | for (; cid < L2CAP_CID_DYN_END; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 194 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 195 | return cid; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 201 | static void __l2cap_state_change(struct l2cap_chan *chan, int state) |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 202 | { |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 203 | 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] | 204 | state_to_string(state)); |
| 205 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 206 | chan->state = state; |
| 207 | chan->ops->state_change(chan->data, state); |
| 208 | } |
| 209 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 210 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
| 211 | { |
| 212 | struct sock *sk = chan->sk; |
| 213 | |
| 214 | lock_sock(sk); |
| 215 | __l2cap_state_change(chan, state); |
| 216 | release_sock(sk); |
| 217 | } |
| 218 | |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 219 | static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 220 | { |
| 221 | struct sock *sk = chan->sk; |
| 222 | |
| 223 | sk->sk_err = err; |
| 224 | } |
| 225 | |
| 226 | static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) |
| 227 | { |
| 228 | struct sock *sk = chan->sk; |
| 229 | |
| 230 | lock_sock(sk); |
| 231 | __l2cap_chan_set_err(chan, err); |
| 232 | release_sock(sk); |
| 233 | } |
| 234 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 235 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 236 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 237 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 238 | chan_timer.work); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 239 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 240 | int reason; |
| 241 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 242 | 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] | 243 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 244 | mutex_lock(&conn->chan_lock); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 245 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 246 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 247 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 248 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 249 | else if (chan->state == BT_CONNECT && |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 250 | chan->sec_level != BT_SECURITY_SDP) |
| 251 | reason = ECONNREFUSED; |
| 252 | else |
| 253 | reason = ETIMEDOUT; |
| 254 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 255 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 256 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 257 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 258 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 259 | chan->ops->close(chan->data); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 260 | mutex_unlock(&conn->chan_lock); |
| 261 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 262 | l2cap_chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 263 | } |
| 264 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 265 | struct l2cap_chan *l2cap_chan_create(void) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 266 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 267 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 268 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 269 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 270 | if (!chan) |
| 271 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 272 | |
Andrei Emeltchenko | c03b355 | 2012-02-21 12:54:56 +0200 | [diff] [blame] | 273 | mutex_init(&chan->lock); |
| 274 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 275 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 276 | list_add(&chan->global_l, &chan_list); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 277 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 278 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 279 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 280 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 281 | chan->state = BT_OPEN; |
| 282 | |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 283 | atomic_set(&chan->refcnt, 1); |
| 284 | |
Gustavo Padovan | eef1d9b | 2012-03-25 13:59:16 -0300 | [diff] [blame] | 285 | BT_DBG("chan %p", chan); |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 286 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 287 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 290 | void l2cap_chan_destroy(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 291 | { |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 292 | write_lock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 293 | list_del(&chan->global_l); |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 294 | write_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 295 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 296 | l2cap_chan_put(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 297 | } |
| 298 | |
Andrei Emeltchenko | bd4b165 | 2012-03-28 16:31:25 +0300 | [diff] [blame^] | 299 | void l2cap_chan_set_defaults(struct l2cap_chan *chan) |
| 300 | { |
| 301 | chan->fcs = L2CAP_FCS_CRC16; |
| 302 | chan->max_tx = L2CAP_DEFAULT_MAX_TX; |
| 303 | chan->tx_win = L2CAP_DEFAULT_TX_WINDOW; |
| 304 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 305 | chan->sec_level = BT_SECURITY_LOW; |
| 306 | |
| 307 | set_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 308 | } |
| 309 | |
Andrei Emeltchenko | 14a2849 | 2012-03-23 16:31:49 +0200 | [diff] [blame] | 310 | static void __l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 311 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 312 | BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 313 | __le16_to_cpu(chan->psm), chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 314 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 315 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 316 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 317 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 318 | |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 319 | switch (chan->chan_type) { |
| 320 | case L2CAP_CHAN_CONN_ORIENTED: |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 321 | if (conn->hcon->type == LE_LINK) { |
| 322 | /* LE connection */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 323 | chan->omtu = L2CAP_LE_DEFAULT_MTU; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 324 | chan->scid = L2CAP_CID_LE_DATA; |
| 325 | chan->dcid = L2CAP_CID_LE_DATA; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 326 | } else { |
| 327 | /* Alloc CID for connection-oriented socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 328 | chan->scid = l2cap_alloc_cid(conn); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 329 | chan->omtu = L2CAP_DEFAULT_MTU; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 330 | } |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 331 | break; |
| 332 | |
| 333 | case L2CAP_CHAN_CONN_LESS: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 334 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 335 | chan->scid = L2CAP_CID_CONN_LESS; |
| 336 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 337 | chan->omtu = L2CAP_DEFAULT_MTU; |
Andrei Emeltchenko | 5491120 | 2012-02-06 15:04:00 +0200 | [diff] [blame] | 338 | break; |
| 339 | |
| 340 | default: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 341 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 342 | chan->scid = L2CAP_CID_SIGNALING; |
| 343 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 344 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 347 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 348 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 349 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 350 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 351 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
| 352 | chan->local_flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 353 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 354 | l2cap_chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 355 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 356 | list_add(&chan->list, &conn->chan_l); |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Andrei Emeltchenko | 14a2849 | 2012-03-23 16:31:49 +0200 | [diff] [blame] | 359 | static void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Andrei Emeltchenko | 643162a | 2012-02-22 17:11:55 +0200 | [diff] [blame] | 360 | { |
| 361 | mutex_lock(&conn->chan_lock); |
| 362 | __l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 363 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 366 | static void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 367 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 368 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 369 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 370 | struct sock *parent = bt_sk(sk)->parent; |
| 371 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 372 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 373 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 374 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 375 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 376 | if (conn) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 377 | /* Delete from channel list */ |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 378 | list_del(&chan->list); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 379 | |
Ulisses Furquim | 371fd83 | 2011-12-21 20:02:36 -0200 | [diff] [blame] | 380 | l2cap_chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 381 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 382 | chan->conn = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 383 | hci_conn_put(conn->hcon); |
| 384 | } |
| 385 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 386 | lock_sock(sk); |
| 387 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 388 | __l2cap_state_change(chan, BT_CLOSED); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 389 | sock_set_flag(sk, SOCK_ZAPPED); |
| 390 | |
| 391 | if (err) |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 392 | __l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 393 | |
| 394 | if (parent) { |
| 395 | bt_accept_unlink(sk); |
| 396 | parent->sk_data_ready(parent, 0); |
| 397 | } else |
| 398 | sk->sk_state_change(sk); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 399 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 400 | release_sock(sk); |
| 401 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 402 | if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) && |
| 403 | test_bit(CONF_INPUT_DONE, &chan->conf_state))) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 404 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 405 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 406 | skb_queue_purge(&chan->tx_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 407 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 408 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 409 | struct srej_list *l, *tmp; |
| 410 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 411 | __clear_retrans_timer(chan); |
| 412 | __clear_monitor_timer(chan); |
| 413 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 414 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 415 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 416 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 417 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 418 | list_del(&l->list); |
| 419 | kfree(l); |
| 420 | } |
| 421 | } |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 424 | static void l2cap_chan_cleanup_listen(struct sock *parent) |
| 425 | { |
| 426 | struct sock *sk; |
| 427 | |
| 428 | BT_DBG("parent %p", parent); |
| 429 | |
| 430 | /* Close not yet accepted channels */ |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 431 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 432 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 433 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 434 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 435 | __clear_chan_timer(chan); |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 436 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 437 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 438 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 439 | chan->ops->close(chan->data); |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 440 | } |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 441 | } |
| 442 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 443 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 444 | { |
| 445 | struct l2cap_conn *conn = chan->conn; |
| 446 | struct sock *sk = chan->sk; |
| 447 | |
Andrei Emeltchenko | e05dcc3 | 2012-02-17 11:40:56 +0200 | [diff] [blame] | 448 | BT_DBG("chan %p state %s sk %p", chan, |
| 449 | state_to_string(chan->state), sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 450 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 451 | switch (chan->state) { |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 452 | case BT_LISTEN: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 453 | lock_sock(sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 454 | l2cap_chan_cleanup_listen(sk); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 455 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 456 | __l2cap_state_change(chan, BT_CLOSED); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 457 | sock_set_flag(sk, SOCK_ZAPPED); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 458 | release_sock(sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 459 | break; |
| 460 | |
| 461 | case BT_CONNECTED: |
| 462 | case BT_CONFIG: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 463 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 464 | conn->hcon->type == ACL_LINK) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 465 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 466 | l2cap_send_disconn_req(conn, chan, reason); |
| 467 | } else |
| 468 | l2cap_chan_del(chan, reason); |
| 469 | break; |
| 470 | |
| 471 | case BT_CONNECT2: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 472 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 473 | conn->hcon->type == ACL_LINK) { |
| 474 | struct l2cap_conn_rsp rsp; |
| 475 | __u16 result; |
| 476 | |
| 477 | if (bt_sk(sk)->defer_setup) |
| 478 | result = L2CAP_CR_SEC_BLOCK; |
| 479 | else |
| 480 | result = L2CAP_CR_BAD_PSM; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 481 | l2cap_state_change(chan, BT_DISCONN); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 482 | |
| 483 | rsp.scid = cpu_to_le16(chan->dcid); |
| 484 | rsp.dcid = cpu_to_le16(chan->scid); |
| 485 | rsp.result = cpu_to_le16(result); |
| 486 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 487 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 488 | sizeof(rsp), &rsp); |
| 489 | } |
| 490 | |
| 491 | l2cap_chan_del(chan, reason); |
| 492 | break; |
| 493 | |
| 494 | case BT_CONNECT: |
| 495 | case BT_DISCONN: |
| 496 | l2cap_chan_del(chan, reason); |
| 497 | break; |
| 498 | |
| 499 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 500 | lock_sock(sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 501 | sock_set_flag(sk, SOCK_ZAPPED); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 502 | release_sock(sk); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | } |
| 506 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 507 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 508 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 509 | if (chan->chan_type == L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 510 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 511 | case BT_SECURITY_HIGH: |
| 512 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 513 | case BT_SECURITY_MEDIUM: |
| 514 | return HCI_AT_DEDICATED_BONDING; |
| 515 | default: |
| 516 | return HCI_AT_NO_BONDING; |
| 517 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 518 | } else if (chan->psm == cpu_to_le16(0x0001)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 519 | if (chan->sec_level == BT_SECURITY_LOW) |
| 520 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 521 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 522 | if (chan->sec_level == BT_SECURITY_HIGH) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 523 | return HCI_AT_NO_BONDING_MITM; |
| 524 | else |
| 525 | return HCI_AT_NO_BONDING; |
| 526 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 527 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 528 | case BT_SECURITY_HIGH: |
| 529 | return HCI_AT_GENERAL_BONDING_MITM; |
| 530 | case BT_SECURITY_MEDIUM: |
| 531 | return HCI_AT_GENERAL_BONDING; |
| 532 | default: |
| 533 | return HCI_AT_NO_BONDING; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 538 | /* Service level security */ |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 539 | int l2cap_chan_check_security(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 540 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 541 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 542 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 543 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 544 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 545 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 546 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 549 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 550 | { |
| 551 | u8 id; |
| 552 | |
| 553 | /* Get next available identificator. |
| 554 | * 1 - 128 are used by kernel. |
| 555 | * 129 - 199 are reserved. |
| 556 | * 200 - 254 are used by utilities like l2ping, etc. |
| 557 | */ |
| 558 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 559 | spin_lock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 560 | |
| 561 | if (++conn->tx_ident > 128) |
| 562 | conn->tx_ident = 1; |
| 563 | |
| 564 | id = conn->tx_ident; |
| 565 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 566 | spin_unlock(&conn->lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 567 | |
| 568 | return id; |
| 569 | } |
| 570 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 571 | 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] | 572 | { |
| 573 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 574 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 575 | |
| 576 | BT_DBG("code 0x%2.2x", code); |
| 577 | |
| 578 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 579 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 580 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 581 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 582 | flags = ACL_START_NO_FLUSH; |
| 583 | else |
| 584 | flags = ACL_START; |
| 585 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 586 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 587 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 588 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 589 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 592 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 593 | { |
| 594 | struct hci_conn *hcon = chan->conn->hcon; |
| 595 | u16 flags; |
| 596 | |
| 597 | BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, |
| 598 | skb->priority); |
| 599 | |
| 600 | if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
| 601 | lmp_no_flush_capable(hcon->hdev)) |
| 602 | flags = ACL_START_NO_FLUSH; |
| 603 | else |
| 604 | flags = ACL_START; |
| 605 | |
| 606 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 607 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 610 | 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] | 611 | { |
| 612 | struct sk_buff *skb; |
| 613 | struct l2cap_hdr *lh; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 614 | struct l2cap_conn *conn = chan->conn; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 615 | int count, hlen; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 616 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 617 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 618 | return; |
| 619 | |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 620 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 621 | hlen = L2CAP_EXT_HDR_SIZE; |
| 622 | else |
| 623 | hlen = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 624 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 625 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 626 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 627 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 628 | BT_DBG("chan %p, control 0x%8.8x", chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 629 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 630 | count = min_t(unsigned int, conn->mtu, hlen); |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 631 | |
| 632 | control |= __set_sframe(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 633 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 634 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 635 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 636 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 637 | if (test_and_clear_bit(CONN_SEND_PBIT, &chan->conn_state)) |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 638 | control |= __set_ctrl_poll(chan); |
Gustavo F. Padovan | f0946cc | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 639 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 640 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 641 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 642 | return; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 643 | |
| 644 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 645 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 646 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 647 | |
| 648 | __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 649 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 650 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 651 | u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE); |
| 652 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 653 | } |
| 654 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 655 | skb->priority = HCI_PRIO_MAX; |
| 656 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 657 | } |
| 658 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 659 | 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] | 660 | { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 661 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 662 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 663 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 664 | } else |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 665 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 666 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 667 | control |= __set_reqseq(chan, chan->buffer_seq); |
Gustavo F. Padovan | 2ab25cd | 2009-10-03 02:34:40 -0300 | [diff] [blame] | 668 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 669 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 670 | } |
| 671 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 672 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 673 | { |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 674 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 675 | } |
| 676 | |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 677 | static void l2cap_send_conn_req(struct l2cap_chan *chan) |
| 678 | { |
| 679 | struct l2cap_conn *conn = chan->conn; |
| 680 | struct l2cap_conn_req req; |
| 681 | |
| 682 | req.scid = cpu_to_le16(chan->scid); |
| 683 | req.psm = chan->psm; |
| 684 | |
| 685 | chan->ident = l2cap_get_ident(conn); |
| 686 | |
| 687 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
| 688 | |
| 689 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, sizeof(req), &req); |
| 690 | } |
| 691 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 692 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 693 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 694 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 695 | |
| 696 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 697 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 698 | return; |
| 699 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 700 | if (l2cap_chan_check_security(chan) && |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 701 | __l2cap_no_conn_pending(chan)) |
| 702 | l2cap_send_conn_req(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 703 | } else { |
| 704 | struct l2cap_info_req req; |
| 705 | req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 706 | |
| 707 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 708 | conn->info_ident = l2cap_get_ident(conn); |
| 709 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 710 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 711 | |
| 712 | l2cap_send_cmd(conn, conn->info_ident, |
| 713 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 714 | } |
| 715 | } |
| 716 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 717 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 718 | { |
| 719 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 720 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 721 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 722 | |
| 723 | switch (mode) { |
| 724 | case L2CAP_MODE_ERTM: |
| 725 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 726 | case L2CAP_MODE_STREAMING: |
| 727 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 728 | default: |
| 729 | return 0x00; |
| 730 | } |
| 731 | } |
| 732 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 733 | 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] | 734 | { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 735 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 736 | struct l2cap_disconn_req req; |
| 737 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 738 | if (!conn) |
| 739 | return; |
| 740 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 741 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 742 | __clear_retrans_timer(chan); |
| 743 | __clear_monitor_timer(chan); |
| 744 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 745 | } |
| 746 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 747 | req.dcid = cpu_to_le16(chan->dcid); |
| 748 | req.scid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 749 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 750 | L2CAP_DISCONN_REQ, sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 751 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 752 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 753 | __l2cap_state_change(chan, BT_DISCONN); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 754 | __l2cap_chan_set_err(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 755 | release_sock(sk); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 756 | } |
| 757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 759 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 760 | { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 761 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 762 | |
| 763 | BT_DBG("conn %p", conn); |
| 764 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 765 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 766 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 767 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 768 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 769 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 770 | l2cap_chan_lock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 771 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 772 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 773 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 774 | continue; |
| 775 | } |
| 776 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 777 | if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 778 | if (!l2cap_chan_check_security(chan) || |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 779 | !__l2cap_no_conn_pending(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 780 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 781 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 782 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 783 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 784 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
| 785 | && test_bit(CONF_STATE2_DEVICE, |
| 786 | &chan->conf_state)) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 787 | l2cap_chan_close(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 788 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 789 | continue; |
| 790 | } |
| 791 | |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 792 | l2cap_send_conn_req(chan); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 793 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 794 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 795 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 796 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 797 | rsp.scid = cpu_to_le16(chan->dcid); |
| 798 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 799 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 800 | if (l2cap_chan_check_security(chan)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 801 | lock_sock(sk); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 802 | if (bt_sk(sk)->defer_setup) { |
| 803 | struct sock *parent = bt_sk(sk)->parent; |
| 804 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 805 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Ilia Kolomisnky | 05e9a2f | 2011-07-15 18:30:21 +0000 | [diff] [blame] | 806 | if (parent) |
| 807 | parent->sk_data_ready(parent, 0); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 808 | |
| 809 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 810 | __l2cap_state_change(chan, BT_CONFIG); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 811 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 812 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 813 | } |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 814 | release_sock(sk); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 815 | } else { |
| 816 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 817 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
| 818 | } |
| 819 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 820 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 821 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 822 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 823 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 824 | rsp.result != L2CAP_CR_SUCCESS) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 825 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 826 | continue; |
| 827 | } |
| 828 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 829 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 830 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 831 | l2cap_build_conf_req(chan, buf), buf); |
| 832 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 833 | } |
| 834 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 835 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 836 | } |
| 837 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 838 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 839 | } |
| 840 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 841 | /* Find socket with cid and source bdaddr. |
| 842 | * Returns closest match, locked. |
| 843 | */ |
Andrei Emeltchenko | d9b8870 | 2012-03-12 12:13:08 +0200 | [diff] [blame] | 844 | static struct l2cap_chan *l2cap_global_chan_by_scid(int state, u16 cid, |
| 845 | bdaddr_t *src) |
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 | struct l2cap_chan *c, *c1 = NULL; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 848 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 849 | read_lock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 850 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 851 | list_for_each_entry(c, &chan_list, global_l) { |
| 852 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 853 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 854 | if (state && c->state != state) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 855 | continue; |
| 856 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 857 | if (c->scid == cid) { |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 858 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 859 | if (!bacmp(&bt_sk(sk)->src, src)) { |
| 860 | read_unlock(&chan_list_lock); |
| 861 | return c; |
| 862 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 863 | |
| 864 | /* Closest match */ |
| 865 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 866 | c1 = c; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 867 | } |
| 868 | } |
Gustavo F. Padovan | 280f294 | 2011-04-13 19:01:22 -0300 | [diff] [blame] | 869 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 870 | read_unlock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 871 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 872 | return c1; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 876 | { |
Gustavo F. Padovan | c916fbe | 2011-04-04 16:00:55 -0300 | [diff] [blame] | 877 | struct sock *parent, *sk; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 878 | struct l2cap_chan *chan, *pchan; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 879 | |
| 880 | BT_DBG(""); |
| 881 | |
| 882 | /* Check if we have socket listening on cid */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 883 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 884 | conn->src); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 885 | if (!pchan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 886 | return; |
| 887 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 888 | parent = pchan->sk; |
| 889 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 890 | lock_sock(parent); |
Gustavo F. Padovan | 62f3a2c | 2011-04-14 18:34:34 -0300 | [diff] [blame] | 891 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 892 | /* Check for backlog size */ |
| 893 | if (sk_acceptq_is_full(parent)) { |
| 894 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
| 895 | goto clean; |
| 896 | } |
| 897 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 898 | chan = pchan->ops->new_connection(pchan->data); |
| 899 | if (!chan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 900 | goto clean; |
| 901 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 902 | sk = chan->sk; |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 903 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 904 | hci_conn_hold(conn->hcon); |
| 905 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 906 | bacpy(&bt_sk(sk)->src, conn->src); |
| 907 | bacpy(&bt_sk(sk)->dst, conn->dst); |
| 908 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 909 | bt_accept_enqueue(parent, sk); |
| 910 | |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 911 | l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 912 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 913 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 914 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 915 | __l2cap_state_change(chan, BT_CONNECTED); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 916 | parent->sk_data_ready(parent, 0); |
| 917 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 918 | clean: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 919 | release_sock(parent); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 920 | } |
| 921 | |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 922 | static void l2cap_chan_ready(struct l2cap_chan *chan) |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 923 | { |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 924 | struct sock *sk = chan->sk; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 925 | struct sock *parent; |
| 926 | |
| 927 | lock_sock(sk); |
| 928 | |
| 929 | parent = bt_sk(sk)->parent; |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 930 | |
| 931 | BT_DBG("sk %p, parent %p", sk, parent); |
| 932 | |
| 933 | chan->conf_state = 0; |
| 934 | __clear_chan_timer(chan); |
| 935 | |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 936 | __l2cap_state_change(chan, BT_CONNECTED); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 937 | sk->sk_state_change(sk); |
| 938 | |
| 939 | if (parent) |
| 940 | parent->sk_data_ready(parent, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 941 | |
| 942 | release_sock(sk); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 943 | } |
| 944 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 945 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 946 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 947 | struct l2cap_chan *chan; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 948 | |
| 949 | BT_DBG("conn %p", conn); |
| 950 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 951 | if (!conn->hcon->out && conn->hcon->type == LE_LINK) |
| 952 | l2cap_le_conn_ready(conn); |
| 953 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 954 | if (conn->hcon->out && conn->hcon->type == LE_LINK) |
| 955 | smp_conn_security(conn, conn->hcon->pending_sec_level); |
| 956 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 957 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 958 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 959 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 960 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 961 | l2cap_chan_lock(chan); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 962 | |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 963 | if (conn->hcon->type == LE_LINK) { |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 964 | if (smp_conn_security(conn, chan->sec_level)) |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 965 | l2cap_chan_ready(chan); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 966 | |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 967 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 968 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 969 | __clear_chan_timer(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 970 | lock_sock(sk); |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 971 | __l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 972 | sk->sk_state_change(sk); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 973 | release_sock(sk); |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 974 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 975 | } else if (chan->state == BT_CONNECT) |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 976 | l2cap_do_start(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 977 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 978 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 979 | } |
| 980 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 981 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 982 | } |
| 983 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 984 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 985 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 986 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 987 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 988 | |
| 989 | BT_DBG("conn %p", conn); |
| 990 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 991 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 992 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 993 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 994 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 995 | __l2cap_chan_set_err(chan, err); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 996 | } |
| 997 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 998 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 999 | } |
| 1000 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1001 | static void l2cap_info_timeout(struct work_struct *work) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1002 | { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame] | 1003 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
Gustavo F. Padovan | 030013d | 2011-12-20 10:57:28 -0200 | [diff] [blame] | 1004 | info_timer.work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1005 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1006 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 1007 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 1008 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1009 | l2cap_conn_start(conn); |
| 1010 | } |
| 1011 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1012 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
| 1013 | { |
| 1014 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 1015 | struct l2cap_chan *chan, *l; |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1016 | |
| 1017 | if (!conn) |
| 1018 | return; |
| 1019 | |
| 1020 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1021 | |
| 1022 | kfree_skb(conn->rx_skb); |
| 1023 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1024 | mutex_lock(&conn->chan_lock); |
| 1025 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1026 | /* Kill channels */ |
| 1027 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1028 | l2cap_chan_lock(chan); |
| 1029 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1030 | l2cap_chan_del(chan, err); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1031 | |
| 1032 | l2cap_chan_unlock(chan); |
| 1033 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1034 | chan->ops->close(chan->data); |
| 1035 | } |
| 1036 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1037 | mutex_unlock(&conn->chan_lock); |
| 1038 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1039 | hci_chan_del(conn->hchan); |
| 1040 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1041 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1042 | cancel_delayed_work_sync(&conn->info_timer); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1043 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1044 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags)) { |
Ulisses Furquim | 127074b | 2012-01-30 18:26:29 -0200 | [diff] [blame] | 1045 | cancel_delayed_work_sync(&conn->security_timer); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1046 | smp_chan_destroy(conn); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1047 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1048 | |
| 1049 | hcon->l2cap_data = NULL; |
| 1050 | kfree(conn); |
| 1051 | } |
| 1052 | |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1053 | static void security_timeout(struct work_struct *work) |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1054 | { |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1055 | struct l2cap_conn *conn = container_of(work, struct l2cap_conn, |
| 1056 | security_timer.work); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1057 | |
| 1058 | l2cap_conn_del(conn->hcon, ETIMEDOUT); |
| 1059 | } |
| 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) |
| 1062 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1063 | struct l2cap_conn *conn = hcon->l2cap_data; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1064 | struct hci_chan *hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1066 | if (conn || status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | return conn; |
| 1068 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1069 | hchan = hci_chan_create(hcon); |
| 1070 | if (!hchan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1073 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); |
| 1074 | if (!conn) { |
| 1075 | hci_chan_del(hchan); |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | hcon->l2cap_data = conn; |
| 1080 | conn->hcon = hcon; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1081 | conn->hchan = hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1083 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1084 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1085 | if (hcon->hdev->le_mtu && hcon->type == LE_LINK) |
| 1086 | conn->mtu = hcon->hdev->le_mtu; |
| 1087 | else |
| 1088 | conn->mtu = hcon->hdev->acl_mtu; |
| 1089 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | conn->src = &hcon->hdev->bdaddr; |
| 1091 | conn->dst = &hcon->dst; |
| 1092 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1093 | conn->feat_mask = 0; |
| 1094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | spin_lock_init(&conn->lock); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1096 | mutex_init(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1097 | |
| 1098 | INIT_LIST_HEAD(&conn->chan_l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1100 | if (hcon->type == LE_LINK) |
Gustavo F. Padovan | 6c9d42a | 2011-12-20 10:57:27 -0200 | [diff] [blame] | 1101 | INIT_DELAYED_WORK(&conn->security_timer, security_timeout); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1102 | else |
Gustavo F. Padovan | 030013d | 2011-12-20 10:57:28 -0200 | [diff] [blame] | 1103 | INIT_DELAYED_WORK(&conn->info_timer, l2cap_info_timeout); |
Dave Young | 45054dc | 2009-10-18 20:28:30 +0000 | [diff] [blame] | 1104 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 1105 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | return conn; |
| 1108 | } |
| 1109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
| 1112 | /* Find socket with psm and source bdaddr. |
| 1113 | * Returns closest match. |
| 1114 | */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1115 | 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] | 1116 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1117 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1119 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1120 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1121 | list_for_each_entry(c, &chan_list, global_l) { |
| 1122 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1123 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1124 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | continue; |
| 1126 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1127 | if (c->psm == psm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1129 | if (!bacmp(&bt_sk(sk)->src, src)) { |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1130 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1131 | return c; |
| 1132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | |
| 1134 | /* Closest match */ |
| 1135 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1136 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | } |
| 1138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1140 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1141 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1142 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Johan Hedberg | cbe8fed | 2012-01-08 22:51:16 +0200 | [diff] [blame] | 1145 | 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] | 1146 | { |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 1147 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | bdaddr_t *src = &bt_sk(sk)->src; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | struct l2cap_conn *conn; |
| 1150 | struct hci_conn *hcon; |
| 1151 | struct hci_dev *hdev; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1152 | __u8 auth_type; |
Marcel Holtmann | 44d0e48 | 2009-04-20 07:09:16 +0200 | [diff] [blame] | 1153 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | |
Marcel Holtmann | f29972d | 2009-02-12 05:07:45 +0100 | [diff] [blame] | 1155 | BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 1156 | __le16_to_cpu(chan->psm)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1158 | hdev = hci_get_route(dst, src); |
| 1159 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | return -EHOSTUNREACH; |
| 1161 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1162 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1164 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1165 | |
| 1166 | /* PSM must be odd and lsb of upper byte must be 0 */ |
| 1167 | if ((__le16_to_cpu(psm) & 0x0101) != 0x0001 && !cid && |
| 1168 | chan->chan_type != L2CAP_CHAN_RAW) { |
| 1169 | err = -EINVAL; |
| 1170 | goto done; |
| 1171 | } |
| 1172 | |
| 1173 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && !(psm || cid)) { |
| 1174 | err = -EINVAL; |
| 1175 | goto done; |
| 1176 | } |
| 1177 | |
| 1178 | switch (chan->mode) { |
| 1179 | case L2CAP_MODE_BASIC: |
| 1180 | break; |
| 1181 | case L2CAP_MODE_ERTM: |
| 1182 | case L2CAP_MODE_STREAMING: |
| 1183 | if (!disable_ertm) |
| 1184 | break; |
| 1185 | /* fall through */ |
| 1186 | default: |
| 1187 | err = -ENOTSUPP; |
| 1188 | goto done; |
| 1189 | } |
| 1190 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1191 | lock_sock(sk); |
| 1192 | |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1193 | switch (sk->sk_state) { |
| 1194 | case BT_CONNECT: |
| 1195 | case BT_CONNECT2: |
| 1196 | case BT_CONFIG: |
| 1197 | /* Already connecting */ |
| 1198 | err = 0; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1199 | release_sock(sk); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1200 | goto done; |
| 1201 | |
| 1202 | case BT_CONNECTED: |
| 1203 | /* Already connected */ |
| 1204 | err = -EISCONN; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1205 | release_sock(sk); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1206 | goto done; |
| 1207 | |
| 1208 | case BT_OPEN: |
| 1209 | case BT_BOUND: |
| 1210 | /* Can connect */ |
| 1211 | break; |
| 1212 | |
| 1213 | default: |
| 1214 | err = -EBADFD; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1215 | release_sock(sk); |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1216 | goto done; |
| 1217 | } |
| 1218 | |
| 1219 | /* Set destination address and psm */ |
Gustavo F. Padovan | 9219b2a | 2012-01-02 20:08:04 -0200 | [diff] [blame] | 1220 | bacpy(&bt_sk(sk)->dst, dst); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1221 | |
| 1222 | release_sock(sk); |
| 1223 | |
Gustavo F. Padovan | 03a0019 | 2011-12-09 04:48:17 -0200 | [diff] [blame] | 1224 | chan->psm = psm; |
| 1225 | chan->dcid = cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1227 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1228 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1229 | if (chan->dcid == L2CAP_CID_LE_DATA) |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1230 | hcon = hci_connect(hdev, LE_LINK, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1231 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1232 | else |
| 1233 | hcon = hci_connect(hdev, ACL_LINK, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1234 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1235 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1236 | if (IS_ERR(hcon)) { |
| 1237 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
| 1241 | conn = l2cap_conn_add(hcon, 0); |
| 1242 | if (!conn) { |
| 1243 | hci_conn_put(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1244 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | goto done; |
| 1246 | } |
| 1247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | /* Update source addr of the socket */ |
| 1249 | bacpy(src, conn->src); |
| 1250 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1251 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1252 | l2cap_chan_add(conn, chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1253 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1254 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1255 | l2cap_state_change(chan, BT_CONNECT); |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1256 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
| 1258 | if (hcon->state == BT_CONNECTED) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1259 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1260 | __clear_chan_timer(chan); |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1261 | if (l2cap_chan_check_security(chan)) |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1262 | l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1263 | } else |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1264 | l2cap_do_start(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1267 | err = 0; |
| 1268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1270 | l2cap_chan_unlock(chan); |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1271 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | hci_dev_put(hdev); |
| 1273 | return err; |
| 1274 | } |
| 1275 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1276 | int __l2cap_wait_ack(struct sock *sk) |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1277 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1278 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1279 | DECLARE_WAITQUEUE(wait, current); |
| 1280 | int err = 0; |
| 1281 | int timeo = HZ/5; |
| 1282 | |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1283 | add_wait_queue(sk_sleep(sk), &wait); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1284 | set_current_state(TASK_INTERRUPTIBLE); |
| 1285 | while (chan->unacked_frames > 0 && chan->conn) { |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1286 | if (!timeo) |
| 1287 | timeo = HZ/5; |
| 1288 | |
| 1289 | if (signal_pending(current)) { |
| 1290 | err = sock_intr_errno(timeo); |
| 1291 | break; |
| 1292 | } |
| 1293 | |
| 1294 | release_sock(sk); |
| 1295 | timeo = schedule_timeout(timeo); |
| 1296 | lock_sock(sk); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1297 | set_current_state(TASK_INTERRUPTIBLE); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1298 | |
| 1299 | err = sock_error(sk); |
| 1300 | if (err) |
| 1301 | break; |
| 1302 | } |
| 1303 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1304 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1305 | return err; |
| 1306 | } |
| 1307 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1308 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1309 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1310 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 1311 | monitor_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1312 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1313 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1314 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1315 | l2cap_chan_lock(chan); |
| 1316 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1317 | if (chan->retry_count >= chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1318 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1319 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1320 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1321 | return; |
| 1322 | } |
| 1323 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1324 | chan->retry_count++; |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1325 | __set_monitor_timer(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1326 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1327 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1328 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1329 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1330 | } |
| 1331 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1332 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1333 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1334 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 1335 | retrans_timer.work); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1336 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1337 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1338 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1339 | l2cap_chan_lock(chan); |
| 1340 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1341 | chan->retry_count = 1; |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1342 | __set_monitor_timer(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1343 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1344 | set_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1345 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1346 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 1347 | |
| 1348 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 8d7e1c7 | 2012-03-23 09:42:15 +0200 | [diff] [blame] | 1349 | l2cap_chan_put(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1350 | } |
| 1351 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1352 | static void l2cap_drop_acked_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1353 | { |
| 1354 | struct sk_buff *skb; |
| 1355 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1356 | while ((skb = skb_peek(&chan->tx_q)) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1357 | chan->unacked_frames) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1358 | if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1359 | break; |
| 1360 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1361 | skb = skb_dequeue(&chan->tx_q); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1362 | kfree_skb(skb); |
| 1363 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1364 | chan->unacked_frames--; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1365 | } |
| 1366 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1367 | if (!chan->unacked_frames) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1368 | __clear_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1369 | } |
| 1370 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1371 | static void l2cap_streaming_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1372 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1373 | struct sk_buff *skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1374 | u32 control; |
| 1375 | u16 fcs; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1376 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1377 | while ((skb = skb_dequeue(&chan->tx_q))) { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1378 | control = __get_control(chan, skb->data + L2CAP_HDR_SIZE); |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1379 | control |= __set_txseq(chan, chan->next_tx_seq); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1380 | __put_control(chan, control, skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1381 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1382 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1383 | fcs = crc16(0, (u8 *)skb->data, |
| 1384 | skb->len - L2CAP_FCS_SIZE); |
| 1385 | put_unaligned_le16(fcs, |
| 1386 | skb->data + skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1387 | } |
| 1388 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1389 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1390 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1391 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1392 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1393 | } |
| 1394 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1395 | 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] | 1396 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1397 | struct sk_buff *skb, *tx_skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1398 | u16 fcs; |
| 1399 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1400 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1401 | skb = skb_peek(&chan->tx_q); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1402 | if (!skb) |
| 1403 | return; |
| 1404 | |
Szymon Janc | d1726b6 | 2011-11-16 09:32:20 +0100 | [diff] [blame] | 1405 | while (bt_cb(skb)->tx_seq != tx_seq) { |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1406 | if (skb_queue_is_last(&chan->tx_q, skb)) |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1407 | return; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1408 | |
Szymon Janc | d1726b6 | 2011-11-16 09:32:20 +0100 | [diff] [blame] | 1409 | skb = skb_queue_next(&chan->tx_q, skb); |
| 1410 | } |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1411 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1412 | if (chan->remote_max_tx && |
| 1413 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1414 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1415 | return; |
| 1416 | } |
| 1417 | |
| 1418 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1419 | bt_cb(skb)->retries++; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1420 | |
| 1421 | control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1422 | control &= __get_sar_mask(chan); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1423 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1424 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 1425 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1426 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1427 | control |= __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1428 | control |= __set_txseq(chan, tx_seq); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1429 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1430 | __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1431 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1432 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1433 | fcs = crc16(0, (u8 *)tx_skb->data, |
| 1434 | tx_skb->len - L2CAP_FCS_SIZE); |
| 1435 | put_unaligned_le16(fcs, |
| 1436 | tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1437 | } |
| 1438 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1439 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1440 | } |
| 1441 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1442 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1443 | { |
| 1444 | struct sk_buff *skb, *tx_skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1445 | u16 fcs; |
| 1446 | u32 control; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1447 | int nsent = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1448 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1449 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1450 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1451 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1452 | while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1453 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1454 | if (chan->remote_max_tx && |
| 1455 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1456 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1457 | break; |
| 1458 | } |
| 1459 | |
Andrei Emeltchenko | e420aba | 2009-12-23 13:07:14 +0200 | [diff] [blame] | 1460 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1461 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1462 | bt_cb(skb)->retries++; |
| 1463 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1464 | control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1465 | control &= __get_sar_mask(chan); |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1466 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1467 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 1468 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1469 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1470 | control |= __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1471 | control |= __set_txseq(chan, chan->next_tx_seq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1472 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1473 | __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1474 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1475 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1476 | fcs = crc16(0, (u8 *)skb->data, |
| 1477 | tx_skb->len - L2CAP_FCS_SIZE); |
| 1478 | put_unaligned_le16(fcs, skb->data + |
| 1479 | tx_skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1480 | } |
| 1481 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1482 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1483 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1484 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1485 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1486 | bt_cb(skb)->tx_seq = chan->next_tx_seq; |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1487 | |
| 1488 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1489 | |
Szymon Janc | 8ed7a0a | 2012-02-07 15:43:01 +0100 | [diff] [blame] | 1490 | if (bt_cb(skb)->retries == 1) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1491 | chan->unacked_frames++; |
Szymon Janc | 930fa4a | 2012-02-07 15:43:02 +0100 | [diff] [blame] | 1492 | |
| 1493 | if (!nsent++) |
| 1494 | __clear_ack_timer(chan); |
Szymon Janc | 8ed7a0a | 2012-02-07 15:43:01 +0100 | [diff] [blame] | 1495 | } |
Suraj Sumangala | 23e9fde | 2011-03-09 14:44:05 +0530 | [diff] [blame] | 1496 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1497 | chan->frames_sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1498 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1499 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 1500 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1501 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1502 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1503 | } |
| 1504 | |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1505 | return nsent; |
| 1506 | } |
| 1507 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1508 | static int l2cap_retransmit_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1509 | { |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1510 | int ret; |
| 1511 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1512 | if (!skb_queue_empty(&chan->tx_q)) |
| 1513 | chan->tx_send_head = chan->tx_q.next; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1514 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1515 | chan->next_tx_seq = chan->expected_ack_seq; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1516 | ret = l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1517 | return ret; |
| 1518 | } |
| 1519 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 1520 | static void __l2cap_send_ack(struct l2cap_chan *chan) |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1521 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1522 | u32 control = 0; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1523 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1524 | control |= __set_reqseq(chan, chan->buffer_seq); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1525 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1526 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 1527 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1528 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1529 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1530 | return; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1531 | } |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1532 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1533 | if (l2cap_ertm_send(chan) > 0) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1534 | return; |
| 1535 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 1536 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1537 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1538 | } |
| 1539 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 1540 | static void l2cap_send_ack(struct l2cap_chan *chan) |
| 1541 | { |
| 1542 | __clear_ack_timer(chan); |
| 1543 | __l2cap_send_ack(chan); |
| 1544 | } |
| 1545 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1546 | static void l2cap_send_srejtail(struct l2cap_chan *chan) |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1547 | { |
| 1548 | struct srej_list *tail; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1549 | u32 control; |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1550 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 1551 | control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 1552 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1553 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 1554 | tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1555 | control |= __set_reqseq(chan, tail->tx_seq); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1556 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1557 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1558 | } |
| 1559 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1560 | static inline int l2cap_skbuff_fromiovec(struct l2cap_chan *chan, |
| 1561 | struct msghdr *msg, int len, |
| 1562 | int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | { |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 1564 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1565 | struct sk_buff **frag; |
| 1566 | int err, sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | |
Gustavo F. Padovan | 59203a2 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1568 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1569 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
| 1571 | sent += count; |
| 1572 | len -= count; |
| 1573 | |
| 1574 | /* Continuation fragments (no L2CAP header) */ |
| 1575 | frag = &skb_shinfo(skb)->frag_list; |
| 1576 | while (len) { |
| 1577 | count = min_t(unsigned int, conn->mtu, len); |
| 1578 | |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1579 | *frag = chan->ops->alloc_skb(chan, count, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1580 | msg->msg_flags & MSG_DONTWAIT, |
| 1581 | &err); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | if (!*frag) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1584 | return err; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1585 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
| 1586 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1588 | (*frag)->priority = skb->priority; |
| 1589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | sent += count; |
| 1591 | len -= count; |
| 1592 | |
| 1593 | frag = &(*frag)->next; |
| 1594 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | |
| 1596 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1599 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
| 1600 | struct msghdr *msg, size_t len, |
| 1601 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1602 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1603 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1604 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1605 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1606 | struct l2cap_hdr *lh; |
| 1607 | |
Andrei Emeltchenko | 6d5922b | 2012-02-06 15:04:01 +0200 | [diff] [blame] | 1608 | 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] | 1609 | |
| 1610 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1611 | |
| 1612 | skb = chan->ops->alloc_skb(chan, count + hlen, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1613 | msg->msg_flags & MSG_DONTWAIT, &err); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1614 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1615 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1616 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1617 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1618 | skb->priority = priority; |
| 1619 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1620 | /* Create L2CAP header */ |
| 1621 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1622 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1623 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 1624 | put_unaligned(chan->psm, skb_put(skb, 2)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1625 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 1626 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1627 | if (unlikely(err < 0)) { |
| 1628 | kfree_skb(skb); |
| 1629 | return ERR_PTR(err); |
| 1630 | } |
| 1631 | return skb; |
| 1632 | } |
| 1633 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1634 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
| 1635 | struct msghdr *msg, size_t len, |
| 1636 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1637 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1638 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1639 | struct sk_buff *skb; |
| 1640 | int err, count, hlen = L2CAP_HDR_SIZE; |
| 1641 | struct l2cap_hdr *lh; |
| 1642 | |
Andrei Emeltchenko | 6d5922b | 2012-02-06 15:04:01 +0200 | [diff] [blame] | 1643 | BT_DBG("chan %p len %d", chan, (int)len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1644 | |
| 1645 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1646 | |
| 1647 | skb = chan->ops->alloc_skb(chan, count + hlen, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1648 | msg->msg_flags & MSG_DONTWAIT, &err); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1649 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1650 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1651 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1652 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1653 | skb->priority = priority; |
| 1654 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1655 | /* Create L2CAP header */ |
| 1656 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1657 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1658 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 1659 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 1660 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1661 | if (unlikely(err < 0)) { |
| 1662 | kfree_skb(skb); |
| 1663 | return ERR_PTR(err); |
| 1664 | } |
| 1665 | return skb; |
| 1666 | } |
| 1667 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 1668 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
| 1669 | struct msghdr *msg, size_t len, |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1670 | u32 control, u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1671 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1672 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1673 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 1674 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1675 | struct l2cap_hdr *lh; |
| 1676 | |
Andrei Emeltchenko | 6d5922b | 2012-02-06 15:04:01 +0200 | [diff] [blame] | 1677 | BT_DBG("chan %p len %d", chan, (int)len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1678 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 1679 | if (!conn) |
| 1680 | return ERR_PTR(-ENOTCONN); |
| 1681 | |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 1682 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1683 | hlen = L2CAP_EXT_HDR_SIZE; |
| 1684 | else |
| 1685 | hlen = L2CAP_ENH_HDR_SIZE; |
| 1686 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1687 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1688 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1689 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1690 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1691 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1692 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1693 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
Andrei Emeltchenko | 2f7719c | 2012-01-20 14:08:03 +0200 | [diff] [blame] | 1694 | |
| 1695 | skb = chan->ops->alloc_skb(chan, count + hlen, |
| 1696 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1697 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1698 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1699 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1700 | |
| 1701 | /* Create L2CAP header */ |
| 1702 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1703 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1704 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1705 | |
| 1706 | __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); |
| 1707 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1708 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1709 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1710 | |
Andrei Emeltchenko | 0952a57 | 2012-01-13 17:21:43 +0200 | [diff] [blame] | 1711 | err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1712 | if (unlikely(err < 0)) { |
| 1713 | kfree_skb(skb); |
| 1714 | return ERR_PTR(err); |
| 1715 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1716 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1717 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1718 | put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1719 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1720 | bt_cb(skb)->retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1721 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | } |
| 1723 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1724 | 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] | 1725 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1726 | struct sk_buff *skb; |
| 1727 | struct sk_buff_head sar_queue; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1728 | u32 control; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1729 | size_t size = 0; |
| 1730 | |
Gustavo F. Padovan | ff12fd6 | 2010-05-05 22:09:15 -0300 | [diff] [blame] | 1731 | skb_queue_head_init(&sar_queue); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1732 | control = __set_ctrl_sar(chan, L2CAP_SAR_START); |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1733 | 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] | 1734 | if (IS_ERR(skb)) |
| 1735 | return PTR_ERR(skb); |
| 1736 | |
| 1737 | __skb_queue_tail(&sar_queue, skb); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1738 | len -= chan->remote_mps; |
| 1739 | size += chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1740 | |
| 1741 | while (len > 0) { |
| 1742 | size_t buflen; |
| 1743 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1744 | if (len > chan->remote_mps) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1745 | control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1746 | buflen = chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1747 | } else { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1748 | control = __set_ctrl_sar(chan, L2CAP_SAR_END); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1749 | buflen = len; |
| 1750 | } |
| 1751 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1752 | skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1753 | if (IS_ERR(skb)) { |
| 1754 | skb_queue_purge(&sar_queue); |
| 1755 | return PTR_ERR(skb); |
| 1756 | } |
| 1757 | |
| 1758 | __skb_queue_tail(&sar_queue, skb); |
| 1759 | len -= buflen; |
| 1760 | size += buflen; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1761 | } |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1762 | skb_queue_splice_tail(&sar_queue, &chan->tx_q); |
| 1763 | if (chan->tx_send_head == NULL) |
| 1764 | chan->tx_send_head = sar_queue.next; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1765 | |
| 1766 | return size; |
| 1767 | } |
| 1768 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1769 | int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, |
| 1770 | u32 priority) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1771 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1772 | struct sk_buff *skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1773 | u32 control; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1774 | int err; |
| 1775 | |
| 1776 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1777 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1778 | skb = l2cap_create_connless_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1779 | if (IS_ERR(skb)) |
| 1780 | return PTR_ERR(skb); |
| 1781 | |
| 1782 | l2cap_do_send(chan, skb); |
| 1783 | return len; |
| 1784 | } |
| 1785 | |
| 1786 | switch (chan->mode) { |
| 1787 | case L2CAP_MODE_BASIC: |
| 1788 | /* Check outgoing MTU */ |
| 1789 | if (len > chan->omtu) |
| 1790 | return -EMSGSIZE; |
| 1791 | |
| 1792 | /* Create a basic PDU */ |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1793 | skb = l2cap_create_basic_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1794 | if (IS_ERR(skb)) |
| 1795 | return PTR_ERR(skb); |
| 1796 | |
| 1797 | l2cap_do_send(chan, skb); |
| 1798 | err = len; |
| 1799 | break; |
| 1800 | |
| 1801 | case L2CAP_MODE_ERTM: |
| 1802 | case L2CAP_MODE_STREAMING: |
| 1803 | /* Entire SDU fits into one PDU */ |
| 1804 | if (len <= chan->remote_mps) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1805 | control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1806 | skb = l2cap_create_iframe_pdu(chan, msg, len, control, |
| 1807 | 0); |
| 1808 | if (IS_ERR(skb)) |
| 1809 | return PTR_ERR(skb); |
| 1810 | |
| 1811 | __skb_queue_tail(&chan->tx_q, skb); |
| 1812 | |
| 1813 | if (chan->tx_send_head == NULL) |
| 1814 | chan->tx_send_head = skb; |
| 1815 | |
| 1816 | } else { |
| 1817 | /* Segment SDU into multiples PDUs */ |
| 1818 | err = l2cap_sar_segment_sdu(chan, msg, len); |
| 1819 | if (err < 0) |
| 1820 | return err; |
| 1821 | } |
| 1822 | |
| 1823 | if (chan->mode == L2CAP_MODE_STREAMING) { |
| 1824 | l2cap_streaming_send(chan); |
| 1825 | err = len; |
| 1826 | break; |
| 1827 | } |
| 1828 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1829 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 1830 | test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1831 | err = len; |
| 1832 | break; |
| 1833 | } |
| 1834 | |
| 1835 | err = l2cap_ertm_send(chan); |
| 1836 | if (err >= 0) |
| 1837 | err = len; |
| 1838 | |
| 1839 | break; |
| 1840 | |
| 1841 | default: |
| 1842 | BT_DBG("bad state %1.1x", chan->mode); |
| 1843 | err = -EBADFD; |
| 1844 | } |
| 1845 | |
| 1846 | return err; |
| 1847 | } |
| 1848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | /* Copy frame to all raw sockets on that connection */ |
| 1850 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1851 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1853 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | |
| 1855 | BT_DBG("conn %p", conn); |
| 1856 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1857 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 1858 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1859 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1860 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1861 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | continue; |
| 1863 | |
| 1864 | /* Don't send frame to the socket it came from */ |
| 1865 | if (skb->sk == sk) |
| 1866 | continue; |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1867 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 1868 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | continue; |
| 1870 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 1871 | if (chan->ops->recv(chan->data, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | kfree_skb(nskb); |
| 1873 | } |
Gustavo F. Padovan | 3d57dc6 | 2011-12-17 10:56:45 -0200 | [diff] [blame] | 1874 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 1875 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | /* ---- L2CAP signalling commands ---- */ |
| 1879 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
| 1880 | u8 code, u8 ident, u16 dlen, void *data) |
| 1881 | { |
| 1882 | struct sk_buff *skb, **frag; |
| 1883 | struct l2cap_cmd_hdr *cmd; |
| 1884 | struct l2cap_hdr *lh; |
| 1885 | int len, count; |
| 1886 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1887 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", |
| 1888 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | |
| 1890 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 1891 | count = min_t(unsigned int, conn->mtu, len); |
| 1892 | |
| 1893 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 1894 | if (!skb) |
| 1895 | return NULL; |
| 1896 | |
| 1897 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1898 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 1899 | |
| 1900 | if (conn->hcon->type == LE_LINK) |
| 1901 | lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
| 1902 | else |
| 1903 | lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | |
| 1905 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 1906 | cmd->code = code; |
| 1907 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1908 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | |
| 1910 | if (dlen) { |
| 1911 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 1912 | memcpy(skb_put(skb, count), data, count); |
| 1913 | data += count; |
| 1914 | } |
| 1915 | |
| 1916 | len -= skb->len; |
| 1917 | |
| 1918 | /* Continuation fragments (no L2CAP header) */ |
| 1919 | frag = &skb_shinfo(skb)->frag_list; |
| 1920 | while (len) { |
| 1921 | count = min_t(unsigned int, conn->mtu, len); |
| 1922 | |
| 1923 | *frag = bt_skb_alloc(count, GFP_ATOMIC); |
| 1924 | if (!*frag) |
| 1925 | goto fail; |
| 1926 | |
| 1927 | memcpy(skb_put(*frag, count), data, count); |
| 1928 | |
| 1929 | len -= count; |
| 1930 | data += count; |
| 1931 | |
| 1932 | frag = &(*frag)->next; |
| 1933 | } |
| 1934 | |
| 1935 | return skb; |
| 1936 | |
| 1937 | fail: |
| 1938 | kfree_skb(skb); |
| 1939 | return NULL; |
| 1940 | } |
| 1941 | |
| 1942 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) |
| 1943 | { |
| 1944 | struct l2cap_conf_opt *opt = *ptr; |
| 1945 | int len; |
| 1946 | |
| 1947 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 1948 | *ptr += len; |
| 1949 | |
| 1950 | *type = opt->type; |
| 1951 | *olen = opt->len; |
| 1952 | |
| 1953 | switch (opt->len) { |
| 1954 | case 1: |
| 1955 | *val = *((u8 *) opt->val); |
| 1956 | break; |
| 1957 | |
| 1958 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1959 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | break; |
| 1961 | |
| 1962 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1963 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | break; |
| 1965 | |
| 1966 | default: |
| 1967 | *val = (unsigned long) opt->val; |
| 1968 | break; |
| 1969 | } |
| 1970 | |
| 1971 | BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); |
| 1972 | return len; |
| 1973 | } |
| 1974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 1976 | { |
| 1977 | struct l2cap_conf_opt *opt = *ptr; |
| 1978 | |
| 1979 | BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); |
| 1980 | |
| 1981 | opt->type = type; |
| 1982 | opt->len = len; |
| 1983 | |
| 1984 | switch (len) { |
| 1985 | case 1: |
| 1986 | *((u8 *) opt->val) = val; |
| 1987 | break; |
| 1988 | |
| 1989 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1990 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | break; |
| 1992 | |
| 1993 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1994 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | break; |
| 1996 | |
| 1997 | default: |
| 1998 | memcpy(opt->val, (void *) val, len); |
| 1999 | break; |
| 2000 | } |
| 2001 | |
| 2002 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 2003 | } |
| 2004 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2005 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 2006 | { |
| 2007 | struct l2cap_conf_efs efs; |
| 2008 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 2009 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2010 | case L2CAP_MODE_ERTM: |
| 2011 | efs.id = chan->local_id; |
| 2012 | efs.stype = chan->local_stype; |
| 2013 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2014 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 2015 | efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
| 2016 | efs.flush_to = cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO); |
| 2017 | break; |
| 2018 | |
| 2019 | case L2CAP_MODE_STREAMING: |
| 2020 | efs.id = 1; |
| 2021 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 2022 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 2023 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 2024 | efs.acc_lat = 0; |
| 2025 | efs.flush_to = 0; |
| 2026 | break; |
| 2027 | |
| 2028 | default: |
| 2029 | return; |
| 2030 | } |
| 2031 | |
| 2032 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 2033 | (unsigned long) &efs); |
| 2034 | } |
| 2035 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 2036 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2037 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 2038 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 2039 | ack_timer.work); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2040 | |
Gustavo F. Padovan | 2fb9b3d | 2011-12-22 16:56:05 -0200 | [diff] [blame] | 2041 | BT_DBG("chan %p", chan); |
| 2042 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2043 | l2cap_chan_lock(chan); |
| 2044 | |
Szymon Janc | b17e73b | 2012-01-11 10:59:47 +0100 | [diff] [blame] | 2045 | __l2cap_send_ack(chan); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2046 | |
| 2047 | l2cap_chan_unlock(chan); |
Szymon Janc | 09bfb2e | 2012-01-11 10:59:49 +0100 | [diff] [blame] | 2048 | |
| 2049 | l2cap_chan_put(chan); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2050 | } |
| 2051 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2052 | static inline void l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2053 | { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2054 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2055 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2056 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2057 | chan->num_acked = 0; |
| 2058 | chan->frames_sent = 0; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2059 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 2060 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 2061 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 2062 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2063 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 2064 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2065 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 2066 | INIT_LIST_HEAD(&chan->srej_l); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2067 | } |
| 2068 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2069 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 2070 | { |
| 2071 | switch (mode) { |
| 2072 | case L2CAP_MODE_STREAMING: |
| 2073 | case L2CAP_MODE_ERTM: |
| 2074 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 2075 | return mode; |
| 2076 | /* fall through */ |
| 2077 | default: |
| 2078 | return L2CAP_MODE_BASIC; |
| 2079 | } |
| 2080 | } |
| 2081 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2082 | static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) |
| 2083 | { |
| 2084 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; |
| 2085 | } |
| 2086 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2087 | static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) |
| 2088 | { |
| 2089 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; |
| 2090 | } |
| 2091 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2092 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 2093 | { |
| 2094 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2095 | __l2cap_ews_supported(chan)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2096 | /* use extended control field */ |
| 2097 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2098 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 2099 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2100 | chan->tx_win = min_t(u16, chan->tx_win, |
| 2101 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2102 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 2103 | } |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2104 | } |
| 2105 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2106 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2109 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2111 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 2113 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2115 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2116 | goto done; |
| 2117 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2118 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2119 | case L2CAP_MODE_STREAMING: |
| 2120 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2121 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2122 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2123 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2124 | if (__l2cap_efs_supported(chan)) |
| 2125 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 2126 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 2127 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2128 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2129 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2130 | break; |
| 2131 | } |
| 2132 | |
| 2133 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2134 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 2135 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 2136 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2137 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2138 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2139 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
| 2140 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2141 | break; |
| 2142 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 2143 | rfc.mode = L2CAP_MODE_BASIC; |
| 2144 | rfc.txwin_size = 0; |
| 2145 | rfc.max_transmit = 0; |
| 2146 | rfc.retrans_timeout = 0; |
| 2147 | rfc.monitor_timeout = 0; |
| 2148 | rfc.max_pdu_size = 0; |
| 2149 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2150 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 2151 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2152 | break; |
| 2153 | |
| 2154 | case L2CAP_MODE_ERTM: |
| 2155 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2156 | rfc.max_transmit = chan->max_tx; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2157 | rfc.retrans_timeout = 0; |
| 2158 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2159 | |
| 2160 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
| 2161 | L2CAP_EXT_HDR_SIZE - |
| 2162 | L2CAP_SDULEN_SIZE - |
| 2163 | L2CAP_FCS_SIZE); |
| 2164 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2165 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2166 | l2cap_txwin_setup(chan); |
| 2167 | |
| 2168 | rfc.txwin_size = min_t(u16, chan->tx_win, |
| 2169 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2170 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2171 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 2172 | (unsigned long) &rfc); |
| 2173 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2174 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 2175 | l2cap_add_opt_efs(&ptr, chan); |
| 2176 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2177 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2178 | break; |
| 2179 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2180 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2181 | test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2182 | chan->fcs = L2CAP_FCS_NONE; |
| 2183 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2184 | } |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2185 | |
| 2186 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2187 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
| 2188 | chan->tx_win); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2189 | break; |
| 2190 | |
| 2191 | case L2CAP_MODE_STREAMING: |
| 2192 | rfc.mode = L2CAP_MODE_STREAMING; |
| 2193 | rfc.txwin_size = 0; |
| 2194 | rfc.max_transmit = 0; |
| 2195 | rfc.retrans_timeout = 0; |
| 2196 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2197 | |
| 2198 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
| 2199 | L2CAP_EXT_HDR_SIZE - |
| 2200 | L2CAP_SDULEN_SIZE - |
| 2201 | L2CAP_FCS_SIZE); |
| 2202 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2203 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2204 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 2205 | (unsigned long) &rfc); |
| 2206 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2207 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 2208 | l2cap_add_opt_efs(&ptr, chan); |
| 2209 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2210 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2211 | break; |
| 2212 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2213 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2214 | test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2215 | chan->fcs = L2CAP_FCS_NONE; |
| 2216 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2217 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2218 | break; |
| 2219 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2221 | req->dcid = cpu_to_le16(chan->dcid); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2222 | req->flags = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | |
| 2224 | return ptr - data; |
| 2225 | } |
| 2226 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2227 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2229 | struct l2cap_conf_rsp *rsp = data; |
| 2230 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2231 | void *req = chan->conf_req; |
| 2232 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2233 | int type, hint, olen; |
| 2234 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2235 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2236 | struct l2cap_conf_efs efs; |
| 2237 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 2238 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2239 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2240 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2242 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 2243 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2244 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2245 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 2247 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 2248 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2249 | |
| 2250 | switch (type) { |
| 2251 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 2252 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2253 | break; |
| 2254 | |
| 2255 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2256 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2257 | break; |
| 2258 | |
| 2259 | case L2CAP_CONF_QOS: |
| 2260 | break; |
| 2261 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2262 | case L2CAP_CONF_RFC: |
| 2263 | if (olen == sizeof(rfc)) |
| 2264 | memcpy(&rfc, (void *) val, olen); |
| 2265 | break; |
| 2266 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2267 | case L2CAP_CONF_FCS: |
| 2268 | if (val == L2CAP_FCS_NONE) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2269 | set_bit(CONF_NO_FCS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2270 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2271 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2272 | case L2CAP_CONF_EFS: |
| 2273 | remote_efs = 1; |
| 2274 | if (olen == sizeof(efs)) |
| 2275 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2276 | break; |
| 2277 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2278 | case L2CAP_CONF_EWS: |
| 2279 | if (!enable_hs) |
| 2280 | return -ECONNREFUSED; |
| 2281 | |
| 2282 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 2283 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2284 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2285 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2286 | break; |
| 2287 | |
| 2288 | default: |
| 2289 | if (hint) |
| 2290 | break; |
| 2291 | |
| 2292 | result = L2CAP_CONF_UNKNOWN; |
| 2293 | *((u8 *) ptr++) = type; |
| 2294 | break; |
| 2295 | } |
| 2296 | } |
| 2297 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2298 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2299 | goto done; |
| 2300 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2301 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2302 | case L2CAP_MODE_STREAMING: |
| 2303 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2304 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2305 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2306 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2307 | break; |
| 2308 | } |
| 2309 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2310 | if (remote_efs) { |
| 2311 | if (__l2cap_efs_supported(chan)) |
| 2312 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 2313 | else |
| 2314 | return -ECONNREFUSED; |
| 2315 | } |
| 2316 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2317 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2318 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 2319 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2320 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2324 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2325 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2326 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2327 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2328 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2329 | return -ECONNREFUSED; |
| 2330 | |
| 2331 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2332 | sizeof(rfc), (unsigned long) &rfc); |
| 2333 | } |
| 2334 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2335 | if (result == L2CAP_CONF_SUCCESS) { |
| 2336 | /* Configure output options and let the other side know |
| 2337 | * which ones we don't like. */ |
| 2338 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2339 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 2340 | result = L2CAP_CONF_UNACCEPT; |
| 2341 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2342 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2343 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2344 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2345 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2346 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2347 | if (remote_efs) { |
| 2348 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
| 2349 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 2350 | efs.stype != chan->local_stype) { |
| 2351 | |
| 2352 | result = L2CAP_CONF_UNACCEPT; |
| 2353 | |
| 2354 | if (chan->num_conf_req >= 1) |
| 2355 | return -ECONNREFUSED; |
| 2356 | |
| 2357 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2358 | sizeof(efs), |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2359 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2360 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2361 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2362 | result = L2CAP_CONF_PENDING; |
| 2363 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2364 | } |
| 2365 | } |
| 2366 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2367 | switch (rfc.mode) { |
| 2368 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2369 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2370 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2371 | break; |
| 2372 | |
| 2373 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2374 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 2375 | chan->remote_tx_win = rfc.txwin_size; |
| 2376 | else |
| 2377 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 2378 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 2379 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 2380 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2381 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
| 2382 | chan->conn->mtu - |
| 2383 | L2CAP_EXT_HDR_SIZE - |
| 2384 | L2CAP_SDULEN_SIZE - |
| 2385 | L2CAP_FCS_SIZE); |
| 2386 | rfc.max_pdu_size = cpu_to_le16(size); |
| 2387 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2388 | |
Gustavo F. Padovan | 10467e9 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 2389 | rfc.retrans_timeout = |
Andrei Emeltchenko | 4fd21a8 | 2012-03-12 12:13:10 +0200 | [diff] [blame] | 2390 | __constant_cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
Gustavo F. Padovan | 10467e9 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 2391 | rfc.monitor_timeout = |
Andrei Emeltchenko | 4fd21a8 | 2012-03-12 12:13:10 +0200 | [diff] [blame] | 2392 | __constant_cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2393 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2394 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 2395 | |
| 2396 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2397 | sizeof(rfc), (unsigned long) &rfc); |
| 2398 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2399 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 2400 | chan->remote_id = efs.id; |
| 2401 | chan->remote_stype = efs.stype; |
| 2402 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 2403 | chan->remote_flush_to = |
| 2404 | le32_to_cpu(efs.flush_to); |
| 2405 | chan->remote_acc_lat = |
| 2406 | le32_to_cpu(efs.acc_lat); |
| 2407 | chan->remote_sdu_itime = |
| 2408 | le32_to_cpu(efs.sdu_itime); |
| 2409 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
| 2410 | sizeof(efs), (unsigned long) &efs); |
| 2411 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2412 | break; |
| 2413 | |
| 2414 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2415 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
| 2416 | chan->conn->mtu - |
| 2417 | L2CAP_EXT_HDR_SIZE - |
| 2418 | L2CAP_SDULEN_SIZE - |
| 2419 | L2CAP_FCS_SIZE); |
| 2420 | rfc.max_pdu_size = cpu_to_le16(size); |
| 2421 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2422 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2423 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 2424 | |
| 2425 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2426 | sizeof(rfc), (unsigned long) &rfc); |
| 2427 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2428 | break; |
| 2429 | |
| 2430 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2431 | result = L2CAP_CONF_UNACCEPT; |
| 2432 | |
| 2433 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2434 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2435 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2436 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2437 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2438 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2439 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2440 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2441 | rsp->result = cpu_to_le16(result); |
| 2442 | rsp->flags = cpu_to_le16(0x0000); |
| 2443 | |
| 2444 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2447 | 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] | 2448 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2449 | struct l2cap_conf_req *req = data; |
| 2450 | void *ptr = req->data; |
| 2451 | int type, olen; |
| 2452 | unsigned long val; |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 2453 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2454 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2455 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2456 | 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] | 2457 | |
| 2458 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2459 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 2460 | |
| 2461 | switch (type) { |
| 2462 | case L2CAP_CONF_MTU: |
| 2463 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 2464 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2465 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2466 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2467 | chan->imtu = val; |
| 2468 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2469 | break; |
| 2470 | |
| 2471 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2472 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2473 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2474 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2475 | break; |
| 2476 | |
| 2477 | case L2CAP_CONF_RFC: |
| 2478 | if (olen == sizeof(rfc)) |
| 2479 | memcpy(&rfc, (void *)val, olen); |
| 2480 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2481 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2482 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2483 | return -ECONNREFUSED; |
| 2484 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2485 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2486 | |
| 2487 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2488 | sizeof(rfc), (unsigned long) &rfc); |
| 2489 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2490 | |
| 2491 | case L2CAP_CONF_EWS: |
| 2492 | chan->tx_win = min_t(u16, val, |
| 2493 | L2CAP_DEFAULT_EXT_WINDOW); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2494 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
| 2495 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2496 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2497 | |
| 2498 | case L2CAP_CONF_EFS: |
| 2499 | if (olen == sizeof(efs)) |
| 2500 | memcpy(&efs, (void *)val, olen); |
| 2501 | |
| 2502 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
| 2503 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 2504 | efs.stype != chan->local_stype) |
| 2505 | return -ECONNREFUSED; |
| 2506 | |
| 2507 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
| 2508 | sizeof(efs), (unsigned long) &efs); |
| 2509 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2513 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 2514 | return -ECONNREFUSED; |
| 2515 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2516 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 2517 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2518 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2519 | switch (rfc.mode) { |
| 2520 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2521 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 2522 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 2523 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2524 | |
| 2525 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 2526 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 2527 | chan->local_sdu_itime = |
| 2528 | le32_to_cpu(efs.sdu_itime); |
| 2529 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 2530 | chan->local_flush_to = |
| 2531 | le32_to_cpu(efs.flush_to); |
| 2532 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2533 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2534 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2535 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2536 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2537 | } |
| 2538 | } |
| 2539 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2540 | req->dcid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2541 | req->flags = cpu_to_le16(0x0000); |
| 2542 | |
| 2543 | return ptr - data; |
| 2544 | } |
| 2545 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2546 | 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] | 2547 | { |
| 2548 | struct l2cap_conf_rsp *rsp = data; |
| 2549 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2551 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2553 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2554 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2555 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | |
| 2557 | return ptr - data; |
| 2558 | } |
| 2559 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2560 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2561 | { |
| 2562 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2563 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2564 | u8 buf[128]; |
| 2565 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2566 | rsp.scid = cpu_to_le16(chan->dcid); |
| 2567 | rsp.dcid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2568 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 2569 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 2570 | l2cap_send_cmd(conn, chan->ident, |
| 2571 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 2572 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2573 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2574 | return; |
| 2575 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2576 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
| 2577 | l2cap_build_conf_req(chan, buf), buf); |
| 2578 | chan->num_conf_req++; |
| 2579 | } |
| 2580 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2581 | 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] | 2582 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2583 | int type, olen; |
| 2584 | unsigned long val; |
| 2585 | struct l2cap_conf_rfc rfc; |
| 2586 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2587 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2588 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2589 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2590 | return; |
| 2591 | |
| 2592 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2593 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 2594 | |
| 2595 | switch (type) { |
| 2596 | case L2CAP_CONF_RFC: |
| 2597 | if (olen == sizeof(rfc)) |
| 2598 | memcpy(&rfc, (void *)val, olen); |
| 2599 | goto done; |
| 2600 | } |
| 2601 | } |
| 2602 | |
Mat Martineau | 36e999a | 2011-12-08 17:23:21 -0800 | [diff] [blame] | 2603 | /* Use sane default values in case a misbehaving remote device |
| 2604 | * did not send an RFC option. |
| 2605 | */ |
| 2606 | rfc.mode = chan->mode; |
| 2607 | rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO); |
| 2608 | rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO); |
| 2609 | rfc.max_pdu_size = cpu_to_le16(chan->imtu); |
| 2610 | |
| 2611 | BT_ERR("Expected RFC option was not found, using defaults"); |
| 2612 | |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2613 | done: |
| 2614 | switch (rfc.mode) { |
| 2615 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2616 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 2617 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 2618 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2619 | break; |
| 2620 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2621 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2622 | } |
| 2623 | } |
| 2624 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2625 | static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2626 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2627 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2628 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2629 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2630 | return 0; |
| 2631 | |
| 2632 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
| 2633 | cmd->ident == conn->info_ident) { |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 2634 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2635 | |
| 2636 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2637 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2638 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2639 | l2cap_conn_start(conn); |
| 2640 | } |
| 2641 | |
| 2642 | return 0; |
| 2643 | } |
| 2644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2646 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 2648 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2649 | struct l2cap_chan *chan = NULL, *pchan; |
Nathan Holstein | d793fe8 | 2010-10-15 11:54:02 -0400 | [diff] [blame] | 2650 | struct sock *parent, *sk = NULL; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2651 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | |
| 2653 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2654 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 2656 | BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | |
| 2658 | /* Check if we have socket listening on psm */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2659 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); |
| 2660 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | result = L2CAP_CR_BAD_PSM; |
| 2662 | goto sendresp; |
| 2663 | } |
| 2664 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2665 | parent = pchan->sk; |
| 2666 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2667 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2668 | lock_sock(parent); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 2669 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2670 | /* Check if the ACL is secure enough (if not SDP) */ |
| 2671 | if (psm != cpu_to_le16(0x0001) && |
| 2672 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 2673 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2674 | result = L2CAP_CR_SEC_BLOCK; |
| 2675 | goto response; |
| 2676 | } |
| 2677 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2678 | result = L2CAP_CR_NO_MEM; |
| 2679 | |
| 2680 | /* Check for backlog size */ |
| 2681 | if (sk_acceptq_is_full(parent)) { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2682 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | goto response; |
| 2684 | } |
| 2685 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 2686 | chan = pchan->ops->new_connection(pchan->data); |
| 2687 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | goto response; |
| 2689 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 2690 | sk = chan->sk; |
| 2691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | /* Check if we already have channel with that dcid */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2693 | if (__l2cap_get_chan_by_dcid(conn, scid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | sock_set_flag(sk, SOCK_ZAPPED); |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 2695 | chan->ops->close(chan->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2696 | goto response; |
| 2697 | } |
| 2698 | |
| 2699 | hci_conn_hold(conn->hcon); |
| 2700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | bacpy(&bt_sk(sk)->src, conn->src); |
| 2702 | bacpy(&bt_sk(sk)->dst, conn->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2703 | chan->psm = psm; |
| 2704 | chan->dcid = scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 2706 | bt_accept_enqueue(parent, sk); |
| 2707 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2708 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2709 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2710 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 2712 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2714 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2716 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 2717 | if (l2cap_chan_check_security(chan)) { |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2718 | if (bt_sk(sk)->defer_setup) { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 2719 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2720 | result = L2CAP_CR_PEND; |
| 2721 | status = L2CAP_CS_AUTHOR_PEND; |
| 2722 | parent->sk_data_ready(parent, 0); |
| 2723 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 2724 | __l2cap_state_change(chan, BT_CONFIG); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2725 | result = L2CAP_CR_SUCCESS; |
| 2726 | status = L2CAP_CS_NO_INFO; |
| 2727 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2728 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 2729 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2730 | result = L2CAP_CR_PEND; |
| 2731 | status = L2CAP_CS_AUTHEN_PEND; |
| 2732 | } |
| 2733 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 2734 | __l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2735 | result = L2CAP_CR_PEND; |
| 2736 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | } |
| 2738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | response: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2740 | release_sock(parent); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2741 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | |
| 2743 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2744 | rsp.scid = cpu_to_le16(scid); |
| 2745 | rsp.dcid = cpu_to_le16(dcid); |
| 2746 | rsp.result = cpu_to_le16(result); |
| 2747 | rsp.status = cpu_to_le16(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2749 | |
| 2750 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 2751 | struct l2cap_info_req info; |
| 2752 | info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 2753 | |
| 2754 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 2755 | conn->info_ident = l2cap_get_ident(conn); |
| 2756 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 2757 | schedule_delayed_work(&conn->info_timer, L2CAP_INFO_TIMEOUT); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2758 | |
| 2759 | l2cap_send_cmd(conn, conn->info_ident, |
| 2760 | L2CAP_INFO_REQ, sizeof(info), &info); |
| 2761 | } |
| 2762 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2763 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2764 | result == L2CAP_CR_SUCCESS) { |
| 2765 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2766 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2767 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2768 | l2cap_build_conf_req(chan, buf), buf); |
| 2769 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2770 | } |
| 2771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | return 0; |
| 2773 | } |
| 2774 | |
| 2775 | static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2776 | { |
| 2777 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 2778 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2779 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | u8 req[128]; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2781 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | |
| 2783 | scid = __le16_to_cpu(rsp->scid); |
| 2784 | dcid = __le16_to_cpu(rsp->dcid); |
| 2785 | result = __le16_to_cpu(rsp->result); |
| 2786 | status = __le16_to_cpu(rsp->status); |
| 2787 | |
Andrei Emeltchenko | 1b009c9 | 2012-02-21 12:54:54 +0200 | [diff] [blame] | 2788 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", |
| 2789 | dcid, scid, result, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2791 | mutex_lock(&conn->chan_lock); |
| 2792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | if (scid) { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2794 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 2795 | if (!chan) { |
| 2796 | err = -EFAULT; |
| 2797 | goto unlock; |
| 2798 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | } else { |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2800 | chan = __l2cap_get_chan_by_ident(conn, cmd->ident); |
| 2801 | if (!chan) { |
| 2802 | err = -EFAULT; |
| 2803 | goto unlock; |
| 2804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | } |
| 2806 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2807 | err = 0; |
| 2808 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2809 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2811 | switch (result) { |
| 2812 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2813 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2814 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2815 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2816 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 2817 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2818 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2819 | break; |
| 2820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2822 | l2cap_build_conf_req(chan, req), req); |
| 2823 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | break; |
| 2825 | |
| 2826 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2827 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | break; |
| 2829 | |
| 2830 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2831 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | break; |
| 2833 | } |
| 2834 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2835 | l2cap_chan_unlock(chan); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 2836 | |
| 2837 | unlock: |
| 2838 | mutex_unlock(&conn->chan_lock); |
| 2839 | |
| 2840 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2843 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2844 | { |
| 2845 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 2846 | * sides request it. |
| 2847 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2848 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2849 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2850 | else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2851 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2852 | } |
| 2853 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2854 | 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] | 2855 | { |
| 2856 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 2857 | u16 dcid, flags; |
| 2858 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2859 | struct l2cap_chan *chan; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2860 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | |
| 2862 | dcid = __le16_to_cpu(req->dcid); |
| 2863 | flags = __le16_to_cpu(req->flags); |
| 2864 | |
| 2865 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 2866 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2867 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2868 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | return -ENOENT; |
| 2870 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2871 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2872 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 2873 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2874 | struct l2cap_cmd_rej_cid rej; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2875 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2876 | rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); |
| 2877 | rej.scid = cpu_to_le16(chan->scid); |
| 2878 | rej.dcid = cpu_to_le16(chan->dcid); |
| 2879 | |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2880 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 2881 | sizeof(rej), &rej); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2882 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2883 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2884 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2885 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2886 | len = cmd_len - sizeof(*req); |
Dan Rosenberg | 7ac2881 | 2011-06-24 08:38:05 -0400 | [diff] [blame] | 2887 | if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2888 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2889 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2890 | L2CAP_CONF_REJECT, flags), rsp); |
| 2891 | goto unlock; |
| 2892 | } |
| 2893 | |
| 2894 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2895 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 2896 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2897 | |
| 2898 | if (flags & 0x0001) { |
| 2899 | /* Incomplete config. Send empty response. */ |
| 2900 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2901 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2902 | L2CAP_CONF_SUCCESS, 0x0001), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2903 | goto unlock; |
| 2904 | } |
| 2905 | |
| 2906 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2907 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2908 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2909 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2911 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2913 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2914 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2915 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2916 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2917 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2918 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2919 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2920 | goto unlock; |
| 2921 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2922 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2923 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2924 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2925 | l2cap_state_change(chan, BT_CONNECTED); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2926 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2927 | chan->next_tx_seq = 0; |
| 2928 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2929 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2930 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2931 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2932 | |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 2933 | l2cap_chan_ready(chan); |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2934 | goto unlock; |
| 2935 | } |
| 2936 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2937 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2938 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2940 | l2cap_build_conf_req(chan, buf), buf); |
| 2941 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2944 | /* Got Conf Rsp PENDING from remote side and asume we sent |
| 2945 | Conf Rsp PENDING in the code above */ |
| 2946 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
| 2947 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 2948 | |
| 2949 | /* check compatibility */ |
| 2950 | |
| 2951 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 2952 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 2953 | |
| 2954 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2955 | l2cap_build_conf_rsp(chan, rsp, |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2956 | L2CAP_CONF_SUCCESS, 0x0000), rsp); |
| 2957 | } |
| 2958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | unlock: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2960 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | return 0; |
| 2962 | } |
| 2963 | |
| 2964 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2965 | { |
| 2966 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 2967 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2968 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 2969 | int len = le16_to_cpu(cmd->len) - sizeof(*rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | |
| 2971 | scid = __le16_to_cpu(rsp->scid); |
| 2972 | flags = __le16_to_cpu(rsp->flags); |
| 2973 | result = __le16_to_cpu(rsp->result); |
| 2974 | |
Andrei Emeltchenko | 61386cb | 2012-03-12 12:13:07 +0200 | [diff] [blame] | 2975 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x len %d", scid, flags, |
| 2976 | result, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2978 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2979 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | return 0; |
| 2981 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 2982 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2983 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | switch (result) { |
| 2985 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2986 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2987 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | break; |
| 2989 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2990 | case L2CAP_CONF_PENDING: |
| 2991 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 2992 | |
| 2993 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 2994 | char buf[64]; |
| 2995 | |
| 2996 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
| 2997 | buf, &result); |
| 2998 | if (len < 0) { |
| 2999 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
| 3000 | goto done; |
| 3001 | } |
| 3002 | |
| 3003 | /* check compatibility */ |
| 3004 | |
| 3005 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 3006 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 3007 | |
| 3008 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 3009 | l2cap_build_conf_rsp(chan, buf, |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 3010 | L2CAP_CONF_SUCCESS, 0x0000), buf); |
| 3011 | } |
| 3012 | goto done; |
| 3013 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3015 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3016 | char req[64]; |
| 3017 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 3018 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3019 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 3020 | goto done; |
| 3021 | } |
| 3022 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3023 | /* throw out any old stored conf requests */ |
| 3024 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 3025 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
| 3026 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3027 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3028 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3029 | goto done; |
| 3030 | } |
| 3031 | |
| 3032 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 3033 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 3034 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 3035 | if (result != L2CAP_CONF_SUCCESS) |
| 3036 | goto done; |
| 3037 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | } |
| 3039 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 3040 | default: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3041 | l2cap_chan_set_err(chan, ECONNRESET); |
Andrei Emeltchenko | 2e0052e | 2012-02-21 12:54:58 +0200 | [diff] [blame] | 3042 | |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 3043 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3044 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | goto done; |
| 3046 | } |
| 3047 | |
| 3048 | if (flags & 0x01) |
| 3049 | goto done; |
| 3050 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3051 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 3053 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3054 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3055 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 3056 | l2cap_state_change(chan, BT_CONNECTED); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3057 | chan->next_tx_seq = 0; |
| 3058 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 3059 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3060 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3061 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 3062 | |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 3063 | l2cap_chan_ready(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3064 | } |
| 3065 | |
| 3066 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3067 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | return 0; |
| 3069 | } |
| 3070 | |
| 3071 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3072 | { |
| 3073 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 3074 | struct l2cap_disconn_rsp rsp; |
| 3075 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3076 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | struct sock *sk; |
| 3078 | |
| 3079 | scid = __le16_to_cpu(req->scid); |
| 3080 | dcid = __le16_to_cpu(req->dcid); |
| 3081 | |
| 3082 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 3083 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3084 | mutex_lock(&conn->chan_lock); |
| 3085 | |
| 3086 | chan = __l2cap_get_chan_by_scid(conn, dcid); |
| 3087 | if (!chan) { |
| 3088 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3090 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3092 | l2cap_chan_lock(chan); |
| 3093 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3094 | sk = chan->sk; |
| 3095 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3096 | rsp.dcid = cpu_to_le16(chan->scid); |
| 3097 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 3099 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3100 | lock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | sk->sk_shutdown = SHUTDOWN_MASK; |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3102 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3104 | l2cap_chan_del(chan, ECONNRESET); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3105 | |
| 3106 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 3108 | chan->ops->close(chan->data); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3109 | |
| 3110 | mutex_unlock(&conn->chan_lock); |
| 3111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | return 0; |
| 3113 | } |
| 3114 | |
| 3115 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3116 | { |
| 3117 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 3118 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3119 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | |
| 3121 | scid = __le16_to_cpu(rsp->scid); |
| 3122 | dcid = __le16_to_cpu(rsp->dcid); |
| 3123 | |
| 3124 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 3125 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3126 | mutex_lock(&conn->chan_lock); |
| 3127 | |
| 3128 | chan = __l2cap_get_chan_by_scid(conn, scid); |
| 3129 | if (!chan) { |
| 3130 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | return 0; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3134 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3135 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3136 | l2cap_chan_del(chan, 0); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 3137 | |
| 3138 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3139 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 3140 | chan->ops->close(chan->data); |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 3141 | |
| 3142 | mutex_unlock(&conn->chan_lock); |
| 3143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | return 0; |
| 3145 | } |
| 3146 | |
| 3147 | static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3148 | { |
| 3149 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | u16 type; |
| 3151 | |
| 3152 | type = __le16_to_cpu(req->type); |
| 3153 | |
| 3154 | BT_DBG("type 0x%4.4x", type); |
| 3155 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3156 | if (type == L2CAP_IT_FEAT_MASK) { |
| 3157 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 3158 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3159 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
| 3160 | rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 3161 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 3162 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3163 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
| 3164 | | L2CAP_FEAT_FCS; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 3165 | if (enable_hs) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3166 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
| 3167 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 3168 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 3169 | put_unaligned_le32(feat_mask, rsp->data); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3170 | l2cap_send_cmd(conn, cmd->ident, |
| 3171 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3172 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 3173 | u8 buf[12]; |
| 3174 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 3175 | |
| 3176 | if (enable_hs) |
| 3177 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; |
| 3178 | else |
| 3179 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; |
| 3180 | |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3181 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 3182 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Andrei Emeltchenko | c6337ea | 2011-10-20 17:02:44 +0300 | [diff] [blame] | 3183 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3184 | l2cap_send_cmd(conn, cmd->ident, |
| 3185 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3186 | } else { |
| 3187 | struct l2cap_info_rsp rsp; |
| 3188 | rsp.type = cpu_to_le16(type); |
| 3189 | rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); |
| 3190 | l2cap_send_cmd(conn, cmd->ident, |
| 3191 | L2CAP_INFO_RSP, sizeof(rsp), &rsp); |
| 3192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3193 | |
| 3194 | return 0; |
| 3195 | } |
| 3196 | |
| 3197 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3198 | { |
| 3199 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 3200 | u16 type, result; |
| 3201 | |
| 3202 | type = __le16_to_cpu(rsp->type); |
| 3203 | result = __le16_to_cpu(rsp->result); |
| 3204 | |
| 3205 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 3206 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 3207 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 3208 | if (cmd->ident != conn->info_ident || |
| 3209 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
| 3210 | return 0; |
| 3211 | |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 3212 | cancel_delayed_work(&conn->info_timer); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3213 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 3214 | if (result != L2CAP_IR_SUCCESS) { |
| 3215 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 3216 | conn->info_ident = 0; |
| 3217 | |
| 3218 | l2cap_conn_start(conn); |
| 3219 | |
| 3220 | return 0; |
| 3221 | } |
| 3222 | |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 3223 | switch (type) { |
| 3224 | case L2CAP_IT_FEAT_MASK: |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 3225 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3226 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3227 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3228 | struct l2cap_info_req req; |
| 3229 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 3230 | |
| 3231 | conn->info_ident = l2cap_get_ident(conn); |
| 3232 | |
| 3233 | l2cap_send_cmd(conn, conn->info_ident, |
| 3234 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 3235 | } else { |
| 3236 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 3237 | conn->info_ident = 0; |
| 3238 | |
| 3239 | l2cap_conn_start(conn); |
| 3240 | } |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 3241 | break; |
| 3242 | |
| 3243 | case L2CAP_IT_FIXED_CHAN: |
| 3244 | conn->fixed_chan_mask = rsp->data[0]; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3245 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3246 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3247 | |
| 3248 | l2cap_conn_start(conn); |
Andrei Emeltchenko | 978c93b | 2012-02-29 10:41:41 +0200 | [diff] [blame] | 3249 | break; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3250 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3252 | return 0; |
| 3253 | } |
| 3254 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 3255 | static inline int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 3256 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3257 | void *data) |
| 3258 | { |
| 3259 | struct l2cap_create_chan_req *req = data; |
| 3260 | struct l2cap_create_chan_rsp rsp; |
| 3261 | u16 psm, scid; |
| 3262 | |
| 3263 | if (cmd_len != sizeof(*req)) |
| 3264 | return -EPROTO; |
| 3265 | |
| 3266 | if (!enable_hs) |
| 3267 | return -EINVAL; |
| 3268 | |
| 3269 | psm = le16_to_cpu(req->psm); |
| 3270 | scid = le16_to_cpu(req->scid); |
| 3271 | |
| 3272 | BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id); |
| 3273 | |
| 3274 | /* Placeholder: Always reject */ |
| 3275 | rsp.dcid = 0; |
| 3276 | rsp.scid = cpu_to_le16(scid); |
Andrei Emeltchenko | 8ce0c49 | 2012-03-12 12:13:09 +0200 | [diff] [blame] | 3277 | rsp.result = __constant_cpu_to_le16(L2CAP_CR_NO_MEM); |
| 3278 | rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 3279 | |
| 3280 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 3281 | sizeof(rsp), &rsp); |
| 3282 | |
| 3283 | return 0; |
| 3284 | } |
| 3285 | |
| 3286 | static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn, |
| 3287 | struct l2cap_cmd_hdr *cmd, void *data) |
| 3288 | { |
| 3289 | BT_DBG("conn %p", conn); |
| 3290 | |
| 3291 | return l2cap_connect_rsp(conn, cmd, data); |
| 3292 | } |
| 3293 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 3294 | static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident, |
| 3295 | u16 icid, u16 result) |
| 3296 | { |
| 3297 | struct l2cap_move_chan_rsp rsp; |
| 3298 | |
| 3299 | BT_DBG("icid %d, result %d", icid, result); |
| 3300 | |
| 3301 | rsp.icid = cpu_to_le16(icid); |
| 3302 | rsp.result = cpu_to_le16(result); |
| 3303 | |
| 3304 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp); |
| 3305 | } |
| 3306 | |
| 3307 | static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn, |
| 3308 | struct l2cap_chan *chan, u16 icid, u16 result) |
| 3309 | { |
| 3310 | struct l2cap_move_chan_cfm cfm; |
| 3311 | u8 ident; |
| 3312 | |
| 3313 | BT_DBG("icid %d, result %d", icid, result); |
| 3314 | |
| 3315 | ident = l2cap_get_ident(conn); |
| 3316 | if (chan) |
| 3317 | chan->ident = ident; |
| 3318 | |
| 3319 | cfm.icid = cpu_to_le16(icid); |
| 3320 | cfm.result = cpu_to_le16(result); |
| 3321 | |
| 3322 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm); |
| 3323 | } |
| 3324 | |
| 3325 | static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident, |
| 3326 | u16 icid) |
| 3327 | { |
| 3328 | struct l2cap_move_chan_cfm_rsp rsp; |
| 3329 | |
| 3330 | BT_DBG("icid %d", icid); |
| 3331 | |
| 3332 | rsp.icid = cpu_to_le16(icid); |
| 3333 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 3334 | } |
| 3335 | |
| 3336 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
| 3337 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3338 | { |
| 3339 | struct l2cap_move_chan_req *req = data; |
| 3340 | u16 icid = 0; |
| 3341 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 3342 | |
| 3343 | if (cmd_len != sizeof(*req)) |
| 3344 | return -EPROTO; |
| 3345 | |
| 3346 | icid = le16_to_cpu(req->icid); |
| 3347 | |
| 3348 | BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id); |
| 3349 | |
| 3350 | if (!enable_hs) |
| 3351 | return -EINVAL; |
| 3352 | |
| 3353 | /* Placeholder: Always refuse */ |
| 3354 | l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result); |
| 3355 | |
| 3356 | return 0; |
| 3357 | } |
| 3358 | |
| 3359 | static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 3360 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3361 | { |
| 3362 | struct l2cap_move_chan_rsp *rsp = data; |
| 3363 | u16 icid, result; |
| 3364 | |
| 3365 | if (cmd_len != sizeof(*rsp)) |
| 3366 | return -EPROTO; |
| 3367 | |
| 3368 | icid = le16_to_cpu(rsp->icid); |
| 3369 | result = le16_to_cpu(rsp->result); |
| 3370 | |
| 3371 | BT_DBG("icid %d, result %d", icid, result); |
| 3372 | |
| 3373 | /* Placeholder: Always unconfirmed */ |
| 3374 | l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED); |
| 3375 | |
| 3376 | return 0; |
| 3377 | } |
| 3378 | |
| 3379 | static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 3380 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3381 | { |
| 3382 | struct l2cap_move_chan_cfm *cfm = data; |
| 3383 | u16 icid, result; |
| 3384 | |
| 3385 | if (cmd_len != sizeof(*cfm)) |
| 3386 | return -EPROTO; |
| 3387 | |
| 3388 | icid = le16_to_cpu(cfm->icid); |
| 3389 | result = le16_to_cpu(cfm->result); |
| 3390 | |
| 3391 | BT_DBG("icid %d, result %d", icid, result); |
| 3392 | |
| 3393 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 3394 | |
| 3395 | return 0; |
| 3396 | } |
| 3397 | |
| 3398 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
| 3399 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3400 | { |
| 3401 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
| 3402 | u16 icid; |
| 3403 | |
| 3404 | if (cmd_len != sizeof(*rsp)) |
| 3405 | return -EPROTO; |
| 3406 | |
| 3407 | icid = le16_to_cpu(rsp->icid); |
| 3408 | |
| 3409 | BT_DBG("icid %d", icid); |
| 3410 | |
| 3411 | return 0; |
| 3412 | } |
| 3413 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 3414 | 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] | 3415 | u16 to_multiplier) |
| 3416 | { |
| 3417 | u16 max_latency; |
| 3418 | |
| 3419 | if (min > max || min < 6 || max > 3200) |
| 3420 | return -EINVAL; |
| 3421 | |
| 3422 | if (to_multiplier < 10 || to_multiplier > 3200) |
| 3423 | return -EINVAL; |
| 3424 | |
| 3425 | if (max >= to_multiplier * 8) |
| 3426 | return -EINVAL; |
| 3427 | |
| 3428 | max_latency = (to_multiplier * 8 / max) - 1; |
| 3429 | if (latency > 499 || latency > max_latency) |
| 3430 | return -EINVAL; |
| 3431 | |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
| 3436 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3437 | { |
| 3438 | struct hci_conn *hcon = conn->hcon; |
| 3439 | struct l2cap_conn_param_update_req *req; |
| 3440 | struct l2cap_conn_param_update_rsp rsp; |
| 3441 | u16 min, max, latency, to_multiplier, cmd_len; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 3442 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3443 | |
| 3444 | if (!(hcon->link_mode & HCI_LM_MASTER)) |
| 3445 | return -EINVAL; |
| 3446 | |
| 3447 | cmd_len = __le16_to_cpu(cmd->len); |
| 3448 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 3449 | return -EPROTO; |
| 3450 | |
| 3451 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 3452 | min = __le16_to_cpu(req->min); |
| 3453 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3454 | latency = __le16_to_cpu(req->latency); |
| 3455 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 3456 | |
| 3457 | BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", |
| 3458 | min, max, latency, to_multiplier); |
| 3459 | |
| 3460 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 3461 | |
| 3462 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); |
| 3463 | if (err) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3464 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
| 3465 | else |
| 3466 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
| 3467 | |
| 3468 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
| 3469 | sizeof(rsp), &rsp); |
| 3470 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 3471 | if (!err) |
| 3472 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); |
| 3473 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3474 | return 0; |
| 3475 | } |
| 3476 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3477 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
| 3478 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
| 3479 | { |
| 3480 | int err = 0; |
| 3481 | |
| 3482 | switch (cmd->code) { |
| 3483 | case L2CAP_COMMAND_REJ: |
| 3484 | l2cap_command_rej(conn, cmd, data); |
| 3485 | break; |
| 3486 | |
| 3487 | case L2CAP_CONN_REQ: |
| 3488 | err = l2cap_connect_req(conn, cmd, data); |
| 3489 | break; |
| 3490 | |
| 3491 | case L2CAP_CONN_RSP: |
| 3492 | err = l2cap_connect_rsp(conn, cmd, data); |
| 3493 | break; |
| 3494 | |
| 3495 | case L2CAP_CONF_REQ: |
| 3496 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 3497 | break; |
| 3498 | |
| 3499 | case L2CAP_CONF_RSP: |
| 3500 | err = l2cap_config_rsp(conn, cmd, data); |
| 3501 | break; |
| 3502 | |
| 3503 | case L2CAP_DISCONN_REQ: |
| 3504 | err = l2cap_disconnect_req(conn, cmd, data); |
| 3505 | break; |
| 3506 | |
| 3507 | case L2CAP_DISCONN_RSP: |
| 3508 | err = l2cap_disconnect_rsp(conn, cmd, data); |
| 3509 | break; |
| 3510 | |
| 3511 | case L2CAP_ECHO_REQ: |
| 3512 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 3513 | break; |
| 3514 | |
| 3515 | case L2CAP_ECHO_RSP: |
| 3516 | break; |
| 3517 | |
| 3518 | case L2CAP_INFO_REQ: |
| 3519 | err = l2cap_information_req(conn, cmd, data); |
| 3520 | break; |
| 3521 | |
| 3522 | case L2CAP_INFO_RSP: |
| 3523 | err = l2cap_information_rsp(conn, cmd, data); |
| 3524 | break; |
| 3525 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 3526 | case L2CAP_CREATE_CHAN_REQ: |
| 3527 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 3528 | break; |
| 3529 | |
| 3530 | case L2CAP_CREATE_CHAN_RSP: |
| 3531 | err = l2cap_create_channel_rsp(conn, cmd, data); |
| 3532 | break; |
| 3533 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 3534 | case L2CAP_MOVE_CHAN_REQ: |
| 3535 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 3536 | break; |
| 3537 | |
| 3538 | case L2CAP_MOVE_CHAN_RSP: |
| 3539 | err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
| 3540 | break; |
| 3541 | |
| 3542 | case L2CAP_MOVE_CHAN_CFM: |
| 3543 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 3544 | break; |
| 3545 | |
| 3546 | case L2CAP_MOVE_CHAN_CFM_RSP: |
| 3547 | err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
| 3548 | break; |
| 3549 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3550 | default: |
| 3551 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 3552 | err = -EINVAL; |
| 3553 | break; |
| 3554 | } |
| 3555 | |
| 3556 | return err; |
| 3557 | } |
| 3558 | |
| 3559 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
| 3560 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3561 | { |
| 3562 | switch (cmd->code) { |
| 3563 | case L2CAP_COMMAND_REJ: |
| 3564 | return 0; |
| 3565 | |
| 3566 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3567 | return l2cap_conn_param_update_req(conn, cmd, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3568 | |
| 3569 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
| 3570 | return 0; |
| 3571 | |
| 3572 | default: |
| 3573 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
| 3574 | return -EINVAL; |
| 3575 | } |
| 3576 | } |
| 3577 | |
| 3578 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
| 3579 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3580 | { |
| 3581 | u8 *data = skb->data; |
| 3582 | int len = skb->len; |
| 3583 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3584 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3585 | |
| 3586 | l2cap_raw_recv(conn, skb); |
| 3587 | |
| 3588 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3589 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 3591 | data += L2CAP_CMD_HDR_SIZE; |
| 3592 | len -= L2CAP_CMD_HDR_SIZE; |
| 3593 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3594 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3595 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3596 | 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] | 3597 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3598 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3599 | BT_DBG("corrupted command"); |
| 3600 | break; |
| 3601 | } |
| 3602 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3603 | if (conn->hcon->type == LE_LINK) |
| 3604 | err = l2cap_le_sig_cmd(conn, &cmd, data); |
| 3605 | else |
| 3606 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3607 | |
| 3608 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3609 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 3610 | |
| 3611 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3612 | |
| 3613 | /* FIXME: Map err to a valid reason */ |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3614 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3615 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); |
| 3616 | } |
| 3617 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3618 | data += cmd_len; |
| 3619 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | kfree_skb(skb); |
| 3623 | } |
| 3624 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3625 | 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] | 3626 | { |
| 3627 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 3628 | int hdr_size; |
| 3629 | |
| 3630 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3631 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 3632 | else |
| 3633 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3634 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3635 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 3636 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3637 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 3638 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 3639 | |
| 3640 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 3641 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3642 | } |
| 3643 | return 0; |
| 3644 | } |
| 3645 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3646 | 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] | 3647 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3648 | u32 control = 0; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3649 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3650 | chan->frames_sent = 0; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3651 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3652 | control |= __set_reqseq(chan, chan->buffer_seq); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3653 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3654 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3655 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3656 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3657 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3658 | } |
| 3659 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3660 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3661 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3662 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3663 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3664 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3665 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3666 | chan->frames_sent == 0) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3667 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3668 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3669 | } |
| 3670 | } |
| 3671 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3672 | 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] | 3673 | { |
| 3674 | struct sk_buff *next_skb; |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3675 | int tx_seq_offset, next_tx_seq_offset; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3676 | |
| 3677 | bt_cb(skb)->tx_seq = tx_seq; |
| 3678 | bt_cb(skb)->sar = sar; |
| 3679 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3680 | next_skb = skb_peek(&chan->srej_q); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3681 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3682 | 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] | 3683 | |
Szymon Janc | 039d957 | 2011-11-16 09:32:19 +0100 | [diff] [blame] | 3684 | while (next_skb) { |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3685 | if (bt_cb(next_skb)->tx_seq == tx_seq) |
| 3686 | return -EINVAL; |
| 3687 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3688 | next_tx_seq_offset = __seq_offset(chan, |
| 3689 | bt_cb(next_skb)->tx_seq, chan->buffer_seq); |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3690 | |
| 3691 | if (next_tx_seq_offset > tx_seq_offset) { |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3692 | __skb_queue_before(&chan->srej_q, next_skb, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3693 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3694 | } |
| 3695 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3696 | if (skb_queue_is_last(&chan->srej_q, next_skb)) |
Szymon Janc | 039d957 | 2011-11-16 09:32:19 +0100 | [diff] [blame] | 3697 | next_skb = NULL; |
| 3698 | else |
| 3699 | next_skb = skb_queue_next(&chan->srej_q, next_skb); |
| 3700 | } |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3701 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3702 | __skb_queue_tail(&chan->srej_q, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3703 | |
| 3704 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3705 | } |
| 3706 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3707 | static void append_skb_frag(struct sk_buff *skb, |
| 3708 | struct sk_buff *new_frag, struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3709 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3710 | /* skb->len reflects data in skb as well as all fragments |
| 3711 | * skb->data_len reflects only data in fragments |
| 3712 | */ |
| 3713 | if (!skb_has_frag_list(skb)) |
| 3714 | skb_shinfo(skb)->frag_list = new_frag; |
| 3715 | |
| 3716 | new_frag->next = NULL; |
| 3717 | |
| 3718 | (*last_frag)->next = new_frag; |
| 3719 | *last_frag = new_frag; |
| 3720 | |
| 3721 | skb->len += new_frag->len; |
| 3722 | skb->data_len += new_frag->len; |
| 3723 | skb->truesize += new_frag->truesize; |
| 3724 | } |
| 3725 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3726 | 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] | 3727 | { |
| 3728 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3729 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3730 | switch (__get_ctrl_sar(chan, control)) { |
| 3731 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3732 | if (chan->sdu) |
| 3733 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3734 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3735 | err = chan->ops->recv(chan->data, skb); |
| 3736 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3737 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3738 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3739 | if (chan->sdu) |
| 3740 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3741 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3742 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 3743 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3744 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3745 | if (chan->sdu_len > chan->imtu) { |
| 3746 | err = -EMSGSIZE; |
| 3747 | break; |
| 3748 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3749 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3750 | if (skb->len >= chan->sdu_len) |
| 3751 | break; |
| 3752 | |
| 3753 | chan->sdu = skb; |
| 3754 | chan->sdu_last_frag = skb; |
| 3755 | |
| 3756 | skb = NULL; |
| 3757 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3758 | break; |
| 3759 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3760 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3761 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3762 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3763 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3764 | append_skb_frag(chan->sdu, skb, |
| 3765 | &chan->sdu_last_frag); |
| 3766 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3767 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3768 | if (chan->sdu->len >= chan->sdu_len) |
| 3769 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3770 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3771 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3772 | break; |
| 3773 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3774 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3775 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3776 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3777 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3778 | append_skb_frag(chan->sdu, skb, |
| 3779 | &chan->sdu_last_frag); |
| 3780 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3781 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3782 | if (chan->sdu->len != chan->sdu_len) |
| 3783 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3784 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3785 | err = chan->ops->recv(chan->data, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3786 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3787 | if (!err) { |
| 3788 | /* Reassembly complete */ |
| 3789 | chan->sdu = NULL; |
| 3790 | chan->sdu_last_frag = NULL; |
| 3791 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3792 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3793 | break; |
| 3794 | } |
| 3795 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3796 | if (err) { |
| 3797 | kfree_skb(skb); |
| 3798 | kfree_skb(chan->sdu); |
| 3799 | chan->sdu = NULL; |
| 3800 | chan->sdu_last_frag = NULL; |
| 3801 | chan->sdu_len = 0; |
| 3802 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3803 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3804 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3805 | } |
| 3806 | |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3807 | static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3808 | { |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3809 | BT_DBG("chan %p, Enter local busy", chan); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3810 | |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3811 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 3812 | |
Szymon Janc | 77f918b | 2012-01-11 10:59:48 +0100 | [diff] [blame] | 3813 | __set_ack_timer(chan); |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan) |
| 3817 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3818 | u32 control; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3819 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3820 | if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3821 | goto done; |
| 3822 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3823 | control = __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 3824 | control |= __set_ctrl_poll(chan); |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3825 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3826 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3827 | chan->retry_count = 1; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3828 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3829 | __clear_retrans_timer(chan); |
| 3830 | __set_monitor_timer(chan); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3831 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3832 | set_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3833 | |
| 3834 | done: |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3835 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 3836 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3837 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3838 | BT_DBG("chan %p, Exit local busy", chan); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3839 | } |
| 3840 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3841 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3842 | { |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3843 | if (chan->mode == L2CAP_MODE_ERTM) { |
| 3844 | if (busy) |
| 3845 | l2cap_ertm_enter_local_busy(chan); |
| 3846 | else |
| 3847 | l2cap_ertm_exit_local_busy(chan); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3848 | } |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3849 | } |
| 3850 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3851 | 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] | 3852 | { |
| 3853 | struct sk_buff *skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3854 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3855 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3856 | while ((skb = skb_peek(&chan->srej_q)) && |
| 3857 | !test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 3858 | int err; |
| 3859 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3860 | if (bt_cb(skb)->tx_seq != tx_seq) |
| 3861 | break; |
| 3862 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3863 | skb = skb_dequeue(&chan->srej_q); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3864 | control = __set_ctrl_sar(chan, bt_cb(skb)->sar); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3865 | err = l2cap_reassemble_sdu(chan, skb, control); |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3866 | |
| 3867 | if (err < 0) { |
| 3868 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
| 3869 | break; |
| 3870 | } |
| 3871 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3872 | chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej); |
| 3873 | tx_seq = __next_seq(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3874 | } |
| 3875 | } |
| 3876 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3877 | 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] | 3878 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3879 | struct srej_list *l, *tmp; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3880 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3881 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3882 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3883 | if (l->tx_seq == tx_seq) { |
| 3884 | list_del(&l->list); |
| 3885 | kfree(l); |
| 3886 | return; |
| 3887 | } |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3888 | control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3889 | control |= __set_reqseq(chan, l->tx_seq); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3890 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3891 | list_del(&l->list); |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3892 | list_add_tail(&l->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3893 | } |
| 3894 | } |
| 3895 | |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3896 | 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] | 3897 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3898 | struct srej_list *new; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3899 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3900 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3901 | while (tx_seq != chan->expected_tx_seq) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3902 | control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3903 | control |= __set_reqseq(chan, chan->expected_tx_seq); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3904 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3905 | |
| 3906 | new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3907 | if (!new) |
| 3908 | return -ENOMEM; |
| 3909 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3910 | new->tx_seq = chan->expected_tx_seq; |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3911 | |
| 3912 | chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); |
| 3913 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3914 | list_add_tail(&new->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3915 | } |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3916 | |
| 3917 | chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3918 | |
| 3919 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3920 | } |
| 3921 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3922 | 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] | 3923 | { |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3924 | u16 tx_seq = __get_txseq(chan, rx_control); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3925 | u16 req_seq = __get_reqseq(chan, rx_control); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3926 | u8 sar = __get_ctrl_sar(chan, rx_control); |
Gustavo F. Padovan | f6337c7 | 2010-05-10 18:32:04 -0300 | [diff] [blame] | 3927 | int tx_seq_offset, expected_tx_seq_offset; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3928 | int num_to_ack = (chan->tx_win/6) + 1; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3929 | int err = 0; |
| 3930 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3931 | 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] | 3932 | tx_seq, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3933 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 3934 | if (__is_ctrl_final(chan, rx_control) && |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3935 | test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3936 | __clear_monitor_timer(chan); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3937 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3938 | __set_retrans_timer(chan); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3939 | clear_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3940 | } |
| 3941 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3942 | chan->expected_ack_seq = req_seq; |
| 3943 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 9f121a5 | 2009-10-03 02:34:38 -0300 | [diff] [blame] | 3944 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3945 | 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] | 3946 | |
| 3947 | /* invalid tx_seq */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3948 | if (tx_seq_offset >= chan->tx_win) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3949 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3950 | goto drop; |
| 3951 | } |
| 3952 | |
Szymon Janc | 77f918b | 2012-01-11 10:59:48 +0100 | [diff] [blame] | 3953 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 3954 | if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) |
| 3955 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3956 | goto drop; |
Szymon Janc | 77f918b | 2012-01-11 10:59:48 +0100 | [diff] [blame] | 3957 | } |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3958 | |
Mat Martineau | 02f1b64 | 2011-06-29 14:35:19 -0700 | [diff] [blame] | 3959 | if (tx_seq == chan->expected_tx_seq) |
| 3960 | goto expected; |
| 3961 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3962 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3963 | struct srej_list *first; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3964 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3965 | first = list_first_entry(&chan->srej_l, |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3966 | struct srej_list, list); |
| 3967 | if (tx_seq == first->tx_seq) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3968 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3969 | l2cap_check_srej_gap(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3970 | |
| 3971 | list_del(&first->list); |
| 3972 | kfree(first); |
| 3973 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3974 | if (list_empty(&chan->srej_l)) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3975 | chan->buffer_seq = chan->buffer_seq_srej; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3976 | clear_bit(CONN_SREJ_SENT, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3977 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3978 | BT_DBG("chan %p, Exit SREJ_SENT", chan); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3979 | } |
| 3980 | } else { |
| 3981 | struct srej_list *l; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3982 | |
| 3983 | /* duplicated tx_seq */ |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3984 | 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] | 3985 | goto drop; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3986 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3987 | list_for_each_entry(l, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3988 | if (l->tx_seq == tx_seq) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3989 | l2cap_resend_srejframe(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3990 | return 0; |
| 3991 | } |
| 3992 | } |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3993 | |
| 3994 | err = l2cap_send_srejframe(chan, tx_seq); |
| 3995 | if (err < 0) { |
| 3996 | l2cap_send_disconn_req(chan->conn, chan, -err); |
| 3997 | return err; |
| 3998 | } |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3999 | } |
| 4000 | } else { |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4001 | expected_tx_seq_offset = __seq_offset(chan, |
| 4002 | chan->expected_tx_seq, chan->buffer_seq); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 4003 | |
| 4004 | /* duplicated tx_seq */ |
| 4005 | if (tx_seq_offset < expected_tx_seq_offset) |
| 4006 | goto drop; |
| 4007 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4008 | set_bit(CONN_SREJ_SENT, &chan->conn_state); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 4009 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 4010 | BT_DBG("chan %p, Enter SREJ", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 4011 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 4012 | INIT_LIST_HEAD(&chan->srej_l); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4013 | chan->buffer_seq_srej = chan->buffer_seq; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4014 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 4015 | __skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4016 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4017 | |
Szymon Janc | 0ef3ef0 | 2012-01-11 10:59:46 +0100 | [diff] [blame] | 4018 | /* Set P-bit only if there are some I-frames to ack. */ |
| 4019 | if (__clear_ack_timer(chan)) |
| 4020 | set_bit(CONN_SEND_PBIT, &chan->conn_state); |
Gustavo F. Padovan | ef54fd9 | 2009-08-20 22:26:04 -0300 | [diff] [blame] | 4021 | |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 4022 | err = l2cap_send_srejframe(chan, tx_seq); |
| 4023 | if (err < 0) { |
| 4024 | l2cap_send_disconn_req(chan->conn, chan, -err); |
| 4025 | return err; |
| 4026 | } |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4027 | } |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 4028 | return 0; |
| 4029 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4030 | expected: |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4031 | chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4032 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4033 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
Gustavo F. Padovan | 3b1a9f3 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 4034 | bt_cb(skb)->tx_seq = tx_seq; |
| 4035 | bt_cb(skb)->sar = sar; |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 4036 | __skb_queue_tail(&chan->srej_q, skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4037 | return 0; |
| 4038 | } |
| 4039 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4040 | err = l2cap_reassemble_sdu(chan, skb, rx_control); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4041 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 4042 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 4043 | if (err < 0) { |
| 4044 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
| 4045 | return err; |
| 4046 | } |
Gustavo F. Padovan | 2ece368 | 2010-06-16 17:21:44 -0300 | [diff] [blame] | 4047 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4048 | if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4049 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4050 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | 4ec10d9 | 2009-10-03 02:34:39 -0300 | [diff] [blame] | 4051 | } |
| 4052 | |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 4053 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4054 | chan->num_acked = (chan->num_acked + 1) % num_to_ack; |
| 4055 | if (chan->num_acked == num_to_ack - 1) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4056 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 4d611e4 | 2011-06-23 19:30:48 -0300 | [diff] [blame] | 4057 | else |
| 4058 | __set_ack_timer(chan); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 4059 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4060 | return 0; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 4061 | |
| 4062 | drop: |
| 4063 | kfree_skb(skb); |
| 4064 | return 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4065 | } |
| 4066 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4067 | 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] | 4068 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4069 | BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4070 | __get_reqseq(chan, rx_control), rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 4071 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4072 | chan->expected_ack_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4073 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4074 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4075 | if (__is_ctrl_poll(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4076 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 4077 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
| 4078 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4079 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4080 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 4081 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4082 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4083 | l2cap_send_srejtail(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 4084 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4085 | l2cap_send_i_or_rr_or_rnr(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 4086 | } |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4087 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4088 | } else if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4089 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4090 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4091 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4092 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4093 | |
| 4094 | } else { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4095 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4096 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4097 | __set_retrans_timer(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4098 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4099 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 4100 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4101 | l2cap_send_ack(chan); |
Andrei Emeltchenko | 894718a | 2010-12-01 16:58:24 +0200 | [diff] [blame] | 4102 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4103 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4104 | } |
| 4105 | } |
| 4106 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4107 | 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] | 4108 | { |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4109 | u16 tx_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4110 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4111 | 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] | 4112 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4113 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4114 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4115 | chan->expected_ack_seq = tx_seq; |
| 4116 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4117 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4118 | if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4119 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4120 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4121 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4122 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4123 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4124 | if (test_bit(CONN_WAIT_F, &chan->conn_state)) |
| 4125 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4126 | } |
| 4127 | } |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4128 | 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] | 4129 | { |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4130 | u16 tx_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4131 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4132 | 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] | 4133 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4134 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4135 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4136 | if (__is_ctrl_poll(chan, rx_control)) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4137 | chan->expected_ack_seq = tx_seq; |
| 4138 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 4139 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4140 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4141 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 4142 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4143 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 4144 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4145 | if (test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4146 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4147 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4148 | } |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4149 | } else if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4150 | if (test_bit(CONN_SREJ_ACT, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4151 | chan->srej_save_reqseq == tx_seq) |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4152 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4153 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4154 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4155 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4156 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4157 | if (test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4158 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4159 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4160 | } |
| 4161 | } |
| 4162 | } |
| 4163 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4164 | 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] | 4165 | { |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4166 | u16 tx_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4167 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4168 | 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] | 4169 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4170 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4171 | chan->expected_ack_seq = tx_seq; |
| 4172 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4173 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4174 | if (__is_ctrl_poll(chan, rx_control)) |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4175 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 4176 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4177 | if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4178 | __clear_retrans_timer(chan); |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4179 | if (__is_ctrl_poll(chan, rx_control)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4180 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4181 | return; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4182 | } |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4183 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4184 | if (__is_ctrl_poll(chan, rx_control)) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4185 | l2cap_send_srejtail(chan); |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4186 | } else { |
| 4187 | rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR); |
| 4188 | l2cap_send_sframe(chan, rx_control); |
| 4189 | } |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4190 | } |
| 4191 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4192 | 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] | 4193 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4194 | 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] | 4195 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4196 | if (__is_ctrl_final(chan, rx_control) && |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4197 | test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4198 | __clear_monitor_timer(chan); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4199 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4200 | __set_retrans_timer(chan); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4201 | clear_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 4202 | } |
| 4203 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4204 | switch (__get_ctrl_super(chan, rx_control)) { |
| 4205 | case L2CAP_SUPER_RR: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4206 | l2cap_data_channel_rrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4207 | break; |
| 4208 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4209 | case L2CAP_SUPER_REJ: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4210 | l2cap_data_channel_rejframe(chan, rx_control); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 4211 | break; |
| 4212 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4213 | case L2CAP_SUPER_SREJ: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4214 | l2cap_data_channel_srejframe(chan, rx_control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4215 | break; |
| 4216 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4217 | case L2CAP_SUPER_RNR: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4218 | l2cap_data_channel_rnrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4219 | break; |
| 4220 | } |
| 4221 | |
Gustavo F. Padovan | faaebd1 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 4222 | kfree_skb(skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4223 | return 0; |
| 4224 | } |
| 4225 | |
Szymon Janc | cad8f1d | 2012-01-23 10:06:05 +0100 | [diff] [blame] | 4226 | 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] | 4227 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4228 | u32 control; |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4229 | u16 req_seq; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4230 | int len, next_tx_seq_offset, req_seq_offset; |
| 4231 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4232 | control = __get_control(chan, skb->data); |
| 4233 | skb_pull(skb, __ctrl_size(chan)); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4234 | len = skb->len; |
| 4235 | |
| 4236 | /* |
| 4237 | * We can just drop the corrupted I-frame here. |
| 4238 | * Receiver will miss it and start proper recovery |
| 4239 | * procedures and ask retransmission. |
| 4240 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4241 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4242 | goto drop; |
| 4243 | |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 4244 | if (__is_sar_start(chan, control) && !__is_sframe(chan, control)) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4245 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4246 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4247 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4248 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4249 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4250 | if (len > chan->mps) { |
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 | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4255 | req_seq = __get_reqseq(chan, control); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4256 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4257 | req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq); |
| 4258 | |
| 4259 | next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq, |
| 4260 | chan->expected_ack_seq); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4261 | |
| 4262 | /* check for invalid req-seq */ |
| 4263 | if (req_seq_offset > next_tx_seq_offset) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4264 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4265 | goto drop; |
| 4266 | } |
| 4267 | |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 4268 | if (!__is_sframe(chan, control)) { |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4269 | if (len < 0) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4270 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4271 | goto drop; |
| 4272 | } |
| 4273 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4274 | l2cap_data_channel_iframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4275 | } else { |
| 4276 | if (len != 0) { |
| 4277 | BT_ERR("%d", len); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4278 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4279 | goto drop; |
| 4280 | } |
| 4281 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4282 | l2cap_data_channel_sframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4283 | } |
| 4284 | |
| 4285 | return 0; |
| 4286 | |
| 4287 | drop: |
| 4288 | kfree_skb(skb); |
| 4289 | return 0; |
| 4290 | } |
| 4291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) |
| 4293 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4294 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4295 | u32 control; |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 4296 | u16 tx_seq; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4297 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4299 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4300 | if (!chan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4301 | BT_DBG("unknown cid 0x%4.4x", cid); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4302 | /* Drop packet and return */ |
Dan Carpenter | 3379013 | 2012-02-28 09:52:46 +0300 | [diff] [blame] | 4303 | kfree_skb(skb); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4304 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | } |
| 4306 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4307 | l2cap_chan_lock(chan); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4308 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 4309 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4311 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | goto drop; |
| 4313 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4314 | switch (chan->mode) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4315 | case L2CAP_MODE_BASIC: |
| 4316 | /* If socket recv buffers overflows we drop data here |
| 4317 | * which is *bad* because L2CAP has to be reliable. |
| 4318 | * But we don't have any other choice. L2CAP doesn't |
| 4319 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4321 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4322 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 4324 | if (!chan->ops->recv(chan->data, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4325 | goto done; |
| 4326 | break; |
| 4327 | |
| 4328 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 5ef8cb9 | 2012-01-13 17:21:42 +0200 | [diff] [blame] | 4329 | l2cap_ertm_data_rcv(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4330 | |
Andrei Emeltchenko | fcafde2 | 2009-12-22 15:58:08 +0200 | [diff] [blame] | 4331 | goto done; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4332 | |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4333 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4334 | control = __get_control(chan, skb->data); |
| 4335 | skb_pull(skb, __ctrl_size(chan)); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4336 | len = skb->len; |
| 4337 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4338 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 2600008 | 2010-05-11 22:02:00 -0300 | [diff] [blame] | 4339 | goto drop; |
| 4340 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 4341 | if (__is_sar_start(chan, control)) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4342 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4343 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4344 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4345 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4346 | |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 4347 | if (len > chan->mps || len < 0 || __is_sframe(chan, control)) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4348 | goto drop; |
| 4349 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 4350 | tx_seq = __get_txseq(chan, control); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4351 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4352 | if (chan->expected_tx_seq != tx_seq) { |
| 4353 | /* Frame(s) missing - must discard partial SDU */ |
| 4354 | kfree_skb(chan->sdu); |
| 4355 | chan->sdu = NULL; |
| 4356 | chan->sdu_last_frag = NULL; |
| 4357 | chan->sdu_len = 0; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4358 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4359 | /* TODO: Notify userland of missing data */ |
| 4360 | } |
| 4361 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4362 | chan->expected_tx_seq = __next_seq(chan, tx_seq); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4363 | |
| 4364 | if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE) |
| 4365 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4366 | |
| 4367 | goto done; |
| 4368 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4369 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4370 | 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] | 4371 | break; |
| 4372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4373 | |
| 4374 | drop: |
| 4375 | kfree_skb(skb); |
| 4376 | |
| 4377 | done: |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4378 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4380 | return 0; |
| 4381 | } |
| 4382 | |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 4383 | 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] | 4384 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4385 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4387 | chan = l2cap_global_chan_by_psm(0, psm, conn->src); |
| 4388 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | goto drop; |
| 4390 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 4391 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4393 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4394 | goto drop; |
| 4395 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 4396 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4397 | goto drop; |
| 4398 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 4399 | if (!chan->ops->recv(chan->data, skb)) |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 4400 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | |
| 4402 | drop: |
| 4403 | kfree_skb(skb); |
| 4404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | return 0; |
| 4406 | } |
| 4407 | |
Andrei Emeltchenko | d9b8870 | 2012-03-12 12:13:08 +0200 | [diff] [blame] | 4408 | static inline int l2cap_att_channel(struct l2cap_conn *conn, u16 cid, |
| 4409 | struct sk_buff *skb) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4410 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4411 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4412 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4413 | chan = l2cap_global_chan_by_scid(0, cid, conn->src); |
| 4414 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4415 | goto drop; |
| 4416 | |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 4417 | BT_DBG("chan %p, len %d", chan, skb->len); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4418 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4419 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4420 | goto drop; |
| 4421 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 4422 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4423 | goto drop; |
| 4424 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 4425 | if (!chan->ops->recv(chan->data, skb)) |
Andrei Emeltchenko | 5b4ceda | 2012-02-24 16:35:32 +0200 | [diff] [blame] | 4426 | return 0; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4427 | |
| 4428 | drop: |
| 4429 | kfree_skb(skb); |
| 4430 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4431 | return 0; |
| 4432 | } |
| 4433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 4435 | { |
| 4436 | struct l2cap_hdr *lh = (void *) skb->data; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 4437 | u16 cid, len; |
| 4438 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | |
| 4440 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 4441 | cid = __le16_to_cpu(lh->cid); |
| 4442 | len = __le16_to_cpu(lh->len); |
| 4443 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4444 | if (len != skb->len) { |
| 4445 | kfree_skb(skb); |
| 4446 | return; |
| 4447 | } |
| 4448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 4450 | |
| 4451 | switch (cid) { |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 4452 | case L2CAP_CID_LE_SIGNALING: |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 4453 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4454 | l2cap_sig_channel(conn, skb); |
| 4455 | break; |
| 4456 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 4457 | case L2CAP_CID_CONN_LESS: |
Andrei Emeltchenko | 097db76 | 2012-03-09 14:16:17 +0200 | [diff] [blame] | 4458 | psm = get_unaligned((__le16 *) skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4459 | skb_pull(skb, 2); |
| 4460 | l2cap_conless_channel(conn, psm, skb); |
| 4461 | break; |
| 4462 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4463 | case L2CAP_CID_LE_DATA: |
| 4464 | l2cap_att_channel(conn, cid, skb); |
| 4465 | break; |
| 4466 | |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 4467 | case L2CAP_CID_SMP: |
| 4468 | if (smp_sig_channel(conn, skb)) |
| 4469 | l2cap_conn_del(conn->hcon, EACCES); |
| 4470 | break; |
| 4471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | default: |
| 4473 | l2cap_data_channel(conn, cid, skb); |
| 4474 | break; |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 4479 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4480 | int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4481 | { |
| 4482 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4483 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); |
| 4486 | |
| 4487 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4488 | read_lock(&chan_list_lock); |
| 4489 | list_for_each_entry(c, &chan_list, global_l) { |
| 4490 | struct sock *sk = c->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4491 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4492 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4493 | continue; |
| 4494 | |
| 4495 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4496 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 4497 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4498 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | exact++; |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4500 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 4501 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 4502 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4503 | lm2 |= HCI_LM_MASTER; |
| 4504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4505 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4506 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4507 | |
| 4508 | return exact ? lm1 : lm2; |
| 4509 | } |
| 4510 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4511 | int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4512 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4513 | struct l2cap_conn *conn; |
| 4514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); |
| 4516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | if (!status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4518 | conn = l2cap_conn_add(hcon, status); |
| 4519 | if (conn) |
| 4520 | l2cap_conn_ready(conn); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4521 | } else |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 4522 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | |
| 4524 | return 0; |
| 4525 | } |
| 4526 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4527 | int l2cap_disconn_ind(struct hci_conn *hcon) |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4528 | { |
| 4529 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 4530 | |
| 4531 | BT_DBG("hcon %p", hcon); |
| 4532 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4533 | if (!conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 4534 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4535 | return conn->disc_reason; |
| 4536 | } |
| 4537 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4538 | int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | { |
| 4540 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 4541 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 4542 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4543 | return 0; |
| 4544 | } |
| 4545 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4546 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4547 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 4548 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 4549 | return; |
| 4550 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4551 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4552 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4553 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4554 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 4555 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4556 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4557 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 4558 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4559 | } |
| 4560 | } |
| 4561 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4562 | int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 4564 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4565 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4566 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4567 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4568 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | BT_DBG("conn %p", conn); |
| 4571 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 4572 | if (hcon->type == LE_LINK) { |
| 4573 | smp_distribute_keys(conn, 0); |
Ulisses Furquim | 17cd3f3 | 2012-01-30 18:26:28 -0200 | [diff] [blame] | 4574 | cancel_delayed_work(&conn->security_timer); |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 4575 | } |
| 4576 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4577 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4579 | list_for_each_entry(chan, &conn->chan_l, list) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4580 | l2cap_chan_lock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4581 | |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 4582 | BT_DBG("chan->scid %d", chan->scid); |
| 4583 | |
| 4584 | if (chan->scid == L2CAP_CID_LE_DATA) { |
| 4585 | if (!status && encrypt) { |
| 4586 | chan->sec_level = hcon->sec_level; |
Andrei Emeltchenko | cf4cd00 | 2012-02-06 15:03:59 +0200 | [diff] [blame] | 4587 | l2cap_chan_ready(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 4588 | } |
| 4589 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4590 | l2cap_chan_unlock(chan); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 4591 | continue; |
| 4592 | } |
| 4593 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4594 | if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4595 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 4596 | continue; |
| 4597 | } |
| 4598 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4599 | if (!status && (chan->state == BT_CONNECTED || |
| 4600 | chan->state == BT_CONFIG)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4601 | l2cap_check_encryption(chan, encrypt); |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4602 | l2cap_chan_unlock(chan); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 4603 | continue; |
| 4604 | } |
| 4605 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4606 | if (chan->state == BT_CONNECT) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4607 | if (!status) { |
Andrei Emeltchenko | 9b27f35 | 2012-02-24 16:00:00 +0200 | [diff] [blame] | 4608 | l2cap_send_conn_req(chan); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4609 | } else { |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4610 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4611 | } |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4612 | } else if (chan->state == BT_CONNECT2) { |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4613 | struct sock *sk = chan->sk; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4614 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4615 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4616 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4617 | lock_sock(sk); |
| 4618 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4619 | if (!status) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4620 | if (bt_sk(sk)->defer_setup) { |
| 4621 | struct sock *parent = bt_sk(sk)->parent; |
| 4622 | res = L2CAP_CR_PEND; |
| 4623 | stat = L2CAP_CS_AUTHOR_PEND; |
Ilia Kolomisnky | 05e9a2f | 2011-07-15 18:30:21 +0000 | [diff] [blame] | 4624 | if (parent) |
| 4625 | parent->sk_data_ready(parent, 0); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4626 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 4627 | __l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4628 | res = L2CAP_CR_SUCCESS; |
| 4629 | stat = L2CAP_CS_NO_INFO; |
| 4630 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4631 | } else { |
Andrei Emeltchenko | 0e587be | 2012-02-21 12:54:57 +0200 | [diff] [blame] | 4632 | __l2cap_state_change(chan, BT_DISCONN); |
Marcel Holtmann | ba13ccd | 2012-03-01 14:25:33 -0800 | [diff] [blame] | 4633 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4634 | res = L2CAP_CR_SEC_BLOCK; |
| 4635 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4636 | } |
| 4637 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4638 | release_sock(sk); |
| 4639 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4640 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4641 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4642 | rsp.result = cpu_to_le16(res); |
| 4643 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4644 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 4645 | sizeof(rsp), &rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4646 | } |
| 4647 | |
Andrei Emeltchenko | 6be3655 | 2012-02-22 17:11:56 +0200 | [diff] [blame] | 4648 | l2cap_chan_unlock(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | } |
| 4650 | |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4651 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4653 | return 0; |
| 4654 | } |
| 4655 | |
Ulisses Furquim | 686ebf2 | 2011-12-21 10:11:33 -0200 | [diff] [blame] | 4656 | 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] | 4657 | { |
| 4658 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 4659 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 4660 | if (!conn) |
| 4661 | conn = l2cap_conn_add(hcon, 0); |
| 4662 | |
| 4663 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | goto drop; |
| 4665 | |
| 4666 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 4667 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 4668 | if (!(flags & ACL_CONT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4669 | struct l2cap_hdr *hdr; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4670 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4671 | u16 cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | int len; |
| 4673 | |
| 4674 | if (conn->rx_len) { |
| 4675 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 4676 | kfree_skb(conn->rx_skb); |
| 4677 | conn->rx_skb = NULL; |
| 4678 | conn->rx_len = 0; |
| 4679 | l2cap_conn_unreliable(conn, ECOMM); |
| 4680 | } |
| 4681 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 4682 | /* Start fragment always begin with Basic L2CAP header */ |
| 4683 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 4685 | l2cap_conn_unreliable(conn, ECOMM); |
| 4686 | goto drop; |
| 4687 | } |
| 4688 | |
| 4689 | hdr = (struct l2cap_hdr *) skb->data; |
| 4690 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4691 | cid = __le16_to_cpu(hdr->cid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | |
| 4693 | if (len == skb->len) { |
| 4694 | /* Complete frame received */ |
| 4695 | l2cap_recv_frame(conn, skb); |
| 4696 | return 0; |
| 4697 | } |
| 4698 | |
| 4699 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 4700 | |
| 4701 | if (skb->len > len) { |
| 4702 | BT_ERR("Frame is too long (len %d, expected len %d)", |
| 4703 | skb->len, len); |
| 4704 | l2cap_conn_unreliable(conn, ECOMM); |
| 4705 | goto drop; |
| 4706 | } |
| 4707 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4708 | chan = l2cap_get_chan_by_scid(conn, cid); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4709 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4710 | if (chan && chan->sk) { |
| 4711 | struct sock *sk = chan->sk; |
Andrei Emeltchenko | 3df91ea | 2012-02-21 12:54:55 +0200 | [diff] [blame] | 4712 | lock_sock(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4713 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4714 | if (chan->imtu < len - L2CAP_HDR_SIZE) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4715 | BT_ERR("Frame exceeding recv MTU (len %d, " |
| 4716 | "MTU %d)", len, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4717 | chan->imtu); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4718 | release_sock(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4719 | l2cap_conn_unreliable(conn, ECOMM); |
| 4720 | goto drop; |
| 4721 | } |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4722 | release_sock(sk); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4723 | } |
| 4724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | /* Allocate skb for the complete frame (with header) */ |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 4726 | conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 4727 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | goto drop; |
| 4729 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4730 | 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] | 4731 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4732 | conn->rx_len = len - skb->len; |
| 4733 | } else { |
| 4734 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 4735 | |
| 4736 | if (!conn->rx_len) { |
| 4737 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 4738 | l2cap_conn_unreliable(conn, ECOMM); |
| 4739 | goto drop; |
| 4740 | } |
| 4741 | |
| 4742 | if (skb->len > conn->rx_len) { |
| 4743 | BT_ERR("Fragment is too long (len %d, expected %d)", |
| 4744 | skb->len, conn->rx_len); |
| 4745 | kfree_skb(conn->rx_skb); |
| 4746 | conn->rx_skb = NULL; |
| 4747 | conn->rx_len = 0; |
| 4748 | l2cap_conn_unreliable(conn, ECOMM); |
| 4749 | goto drop; |
| 4750 | } |
| 4751 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4752 | 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] | 4753 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | conn->rx_len -= skb->len; |
| 4755 | |
| 4756 | if (!conn->rx_len) { |
| 4757 | /* Complete frame received */ |
| 4758 | l2cap_recv_frame(conn, conn->rx_skb); |
| 4759 | conn->rx_skb = NULL; |
| 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | drop: |
| 4764 | kfree_skb(skb); |
| 4765 | return 0; |
| 4766 | } |
| 4767 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4768 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4769 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4770 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 4772 | read_lock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4773 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4774 | list_for_each_entry(c, &chan_list, global_l) { |
| 4775 | struct sock *sk = c->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4776 | |
Gustavo F. Padovan | 903d343 | 2011-02-10 14:16:06 -0200 | [diff] [blame] | 4777 | 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] | 4778 | batostr(&bt_sk(sk)->src), |
| 4779 | batostr(&bt_sk(sk)->dst), |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4780 | c->state, __le16_to_cpu(c->psm), |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4781 | c->scid, c->dcid, c->imtu, c->omtu, |
| 4782 | c->sec_level, c->mode); |
Andrei Emeltchenko | 61e1b4b | 2012-01-19 11:19:50 +0200 | [diff] [blame] | 4783 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | |
Gustavo F. Padovan | 333055f | 2011-12-22 15:14:39 -0200 | [diff] [blame] | 4785 | read_unlock(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4786 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4787 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | } |
| 4789 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4790 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 4791 | { |
| 4792 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 4793 | } |
| 4794 | |
| 4795 | static const struct file_operations l2cap_debugfs_fops = { |
| 4796 | .open = l2cap_debugfs_open, |
| 4797 | .read = seq_read, |
| 4798 | .llseek = seq_lseek, |
| 4799 | .release = single_release, |
| 4800 | }; |
| 4801 | |
| 4802 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4803 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4804 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4805 | { |
| 4806 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4807 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4808 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | if (err < 0) |
| 4810 | return err; |
| 4811 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4812 | if (bt_debugfs) { |
| 4813 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, |
| 4814 | bt_debugfs, NULL, &l2cap_debugfs_fops); |
| 4815 | if (!l2cap_debugfs) |
| 4816 | BT_ERR("Failed to create L2CAP debug file"); |
| 4817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4820 | } |
| 4821 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4822 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4824 | debugfs_remove(l2cap_debugfs); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4825 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4826 | } |
| 4827 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4828 | module_param(disable_ertm, bool, 0644); |
| 4829 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |