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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License version 2 as |
| 11 | published by the Free Software Foundation; |
| 12 | |
| 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 16 | 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] | 17 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 18 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 19 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 21 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 22 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 23 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | SOFTWARE IS DISCLAIMED. |
| 25 | */ |
| 26 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 27 | /* Bluetooth L2CAP core. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | |
| 31 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 32 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/errno.h> |
| 34 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/sched.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/poll.h> |
| 38 | #include <linux/fcntl.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/interrupt.h> |
| 41 | #include <linux/socket.h> |
| 42 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/list.h> |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 44 | #include <linux/device.h> |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 45 | #include <linux/debugfs.h> |
| 46 | #include <linux/seq_file.h> |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 47 | #include <linux/uaccess.h> |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 48 | #include <linux/crc16.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <net/sock.h> |
| 50 | |
| 51 | #include <asm/system.h> |
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 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 59 | int 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 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 75 | static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb); |
| 76 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 77 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 78 | |
| 79 | static inline void chan_hold(struct l2cap_chan *c) |
| 80 | { |
| 81 | atomic_inc(&c->refcnt); |
| 82 | } |
| 83 | |
| 84 | static inline void chan_put(struct l2cap_chan *c) |
| 85 | { |
| 86 | if (atomic_dec_and_test(&c->refcnt)) |
| 87 | kfree(c); |
| 88 | } |
| 89 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 90 | 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] | 91 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 92 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 93 | |
| 94 | list_for_each_entry(c, &conn->chan_l, list) { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 95 | if (c->dcid == cid) |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 96 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 97 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 98 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 99 | } |
| 100 | |
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 | |
| 105 | list_for_each_entry(c, &conn->chan_l, list) { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 106 | if (c->scid == cid) |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 107 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 108 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 109 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* Find channel with given SCID. |
| 113 | * Returns locked socket */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 114 | 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] | 115 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 116 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 117 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 118 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 119 | c = __l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 120 | if (c) |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 121 | lock_sock(c->sk); |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 122 | mutex_unlock(&conn->chan_lock); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 123 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 126 | 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] | 127 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 128 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 129 | |
| 130 | list_for_each_entry(c, &conn->chan_l, list) { |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 131 | if (c->ident == ident) |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 132 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 133 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 134 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 137 | 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] | 138 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 139 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 140 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 141 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 142 | c = __l2cap_get_chan_by_ident(conn, ident); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 143 | if (c) |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 144 | lock_sock(c->sk); |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 145 | mutex_unlock(&conn->chan_lock); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 146 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 149 | 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] | 150 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 151 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 152 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 153 | list_for_each_entry(c, &chan_list, global_l) { |
| 154 | if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 155 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 156 | } |
Szymon Janc | 250938c | 2011-11-16 09:32:22 +0100 | [diff] [blame] | 157 | return NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 161 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 162 | int err; |
| 163 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 164 | write_lock_bh(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 165 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 166 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 167 | err = -EADDRINUSE; |
| 168 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 169 | } |
| 170 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 171 | if (psm) { |
| 172 | chan->psm = psm; |
| 173 | chan->sport = psm; |
| 174 | err = 0; |
| 175 | } else { |
| 176 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 177 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 178 | err = -EINVAL; |
| 179 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 180 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 181 | chan->psm = cpu_to_le16(p); |
| 182 | chan->sport = cpu_to_le16(p); |
| 183 | err = 0; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | done: |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 189 | write_unlock_bh(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 190 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 194 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 195 | write_lock_bh(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 196 | |
| 197 | chan->scid = scid; |
| 198 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 199 | write_unlock_bh(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 204 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 205 | { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 206 | u16 cid = L2CAP_CID_DYN_START; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 207 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 208 | for (; cid < L2CAP_CID_DYN_END; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 209 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 210 | return cid; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 216 | static void l2cap_set_timer(struct l2cap_chan *chan, struct delayed_work *work, long timeout) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 217 | { |
Andrei Emeltchenko | 457f485 | 2011-10-31 16:17:21 +0200 | [diff] [blame] | 218 | BT_DBG("chan %p state %d timeout %ld", chan, chan->state, timeout); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 219 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 220 | cancel_delayed_work_sync(work); |
| 221 | |
| 222 | schedule_delayed_work(work, timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 223 | } |
| 224 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 225 | static void l2cap_clear_timer(struct delayed_work *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 226 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 227 | cancel_delayed_work_sync(work); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 228 | } |
| 229 | |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 230 | static char *state_to_string(int state) |
| 231 | { |
| 232 | switch(state) { |
| 233 | case BT_CONNECTED: |
| 234 | return "BT_CONNECTED"; |
| 235 | case BT_OPEN: |
| 236 | return "BT_OPEN"; |
| 237 | case BT_BOUND: |
| 238 | return "BT_BOUND"; |
| 239 | case BT_LISTEN: |
| 240 | return "BT_LISTEN"; |
| 241 | case BT_CONNECT: |
| 242 | return "BT_CONNECT"; |
| 243 | case BT_CONNECT2: |
| 244 | return "BT_CONNECT2"; |
| 245 | case BT_CONFIG: |
| 246 | return "BT_CONFIG"; |
| 247 | case BT_DISCONN: |
| 248 | return "BT_DISCONN"; |
| 249 | case BT_CLOSED: |
| 250 | return "BT_CLOSED"; |
| 251 | } |
| 252 | |
| 253 | return "invalid state"; |
| 254 | } |
| 255 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 256 | static void l2cap_state_change(struct l2cap_chan *chan, int state) |
| 257 | { |
Gustavo F. Padovan | badaaa0 | 2011-11-23 20:11:46 -0200 | [diff] [blame] | 258 | BT_DBG("%p %s -> %s", chan, state_to_string(chan->state), |
| 259 | state_to_string(state)); |
| 260 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 261 | chan->state = state; |
| 262 | chan->ops->state_change(chan->data, state); |
| 263 | } |
| 264 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 265 | static void l2cap_chan_timeout(struct work_struct *work) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 266 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 267 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 268 | chan_timer.work); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 269 | struct sock *sk = chan->sk; |
| 270 | int reason; |
| 271 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 272 | BT_DBG("chan %p state %d", chan, chan->state); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 273 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 274 | lock_sock(sk); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 275 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 276 | if (chan->state == BT_CONNECTED || chan->state == BT_CONFIG) |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 277 | reason = ECONNREFUSED; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 278 | else if (chan->state == BT_CONNECT && |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 279 | chan->sec_level != BT_SECURITY_SDP) |
| 280 | reason = ECONNREFUSED; |
| 281 | else |
| 282 | reason = ETIMEDOUT; |
| 283 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 284 | l2cap_chan_close(chan, reason); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 285 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 286 | release_sock(sk); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 287 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 288 | chan->ops->close(chan->data); |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 289 | chan_put(chan); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 290 | } |
| 291 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 292 | struct l2cap_chan *l2cap_chan_create(struct sock *sk) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 293 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 294 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 295 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 296 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 297 | if (!chan) |
| 298 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 299 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 300 | chan->sk = sk; |
| 301 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 302 | write_lock_bh(&chan_list_lock); |
| 303 | list_add(&chan->global_l, &chan_list); |
| 304 | write_unlock_bh(&chan_list_lock); |
| 305 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 306 | INIT_DELAYED_WORK(&chan->chan_timer, l2cap_chan_timeout); |
Gustavo F. Padovan | ab07801 | 2011-05-02 18:25:01 -0300 | [diff] [blame] | 307 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 308 | chan->state = BT_OPEN; |
| 309 | |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 310 | atomic_set(&chan->refcnt, 1); |
| 311 | |
Szymon Janc | abc545b | 2011-11-03 16:05:44 +0100 | [diff] [blame] | 312 | BT_DBG("sk %p chan %p", sk, chan); |
| 313 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 314 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 315 | } |
| 316 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 317 | void l2cap_chan_destroy(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 318 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 319 | write_lock_bh(&chan_list_lock); |
| 320 | list_del(&chan->global_l); |
| 321 | write_unlock_bh(&chan_list_lock); |
| 322 | |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 323 | chan_put(chan); |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 324 | } |
| 325 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 326 | 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] | 327 | { |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 328 | BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 329 | chan->psm, chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 330 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 331 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 332 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 333 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 334 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 335 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED) { |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 336 | if (conn->hcon->type == LE_LINK) { |
| 337 | /* LE connection */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 338 | chan->omtu = L2CAP_LE_DEFAULT_MTU; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 339 | chan->scid = L2CAP_CID_LE_DATA; |
| 340 | chan->dcid = L2CAP_CID_LE_DATA; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 341 | } else { |
| 342 | /* Alloc CID for connection-oriented socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 343 | chan->scid = l2cap_alloc_cid(conn); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 344 | chan->omtu = L2CAP_DEFAULT_MTU; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 345 | } |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 346 | } else if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 347 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 348 | chan->scid = L2CAP_CID_CONN_LESS; |
| 349 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 350 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 351 | } else { |
| 352 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 353 | chan->scid = L2CAP_CID_SIGNALING; |
| 354 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 355 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Andrei Emeltchenko | 8f7975b | 2011-10-13 16:18:54 +0300 | [diff] [blame] | 358 | chan->local_id = L2CAP_BESTEFFORT_ID; |
| 359 | chan->local_stype = L2CAP_SERV_BESTEFFORT; |
| 360 | chan->local_msdu = L2CAP_DEFAULT_MAX_SDU_SIZE; |
| 361 | chan->local_sdu_itime = L2CAP_DEFAULT_SDU_ITIME; |
| 362 | chan->local_acc_lat = L2CAP_DEFAULT_ACC_LAT; |
| 363 | chan->local_flush_to = L2CAP_DEFAULT_FLUSH_TO; |
| 364 | |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 365 | chan_hold(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 366 | |
| 367 | list_add(&chan->list, &conn->chan_l); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 368 | } |
| 369 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 370 | /* Delete channel. |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 371 | * Must be called on the locked socket. */ |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 372 | static void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 373 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 374 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 375 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 376 | struct sock *parent = bt_sk(sk)->parent; |
| 377 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 378 | __clear_chan_timer(chan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 379 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 380 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 381 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 382 | if (conn) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 383 | /* Delete from channel list */ |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 384 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 385 | list_del(&chan->list); |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 386 | mutex_unlock(&conn->chan_lock); |
Gustavo F. Padovan | 71ba0e5 | 2011-05-17 14:34:52 -0300 | [diff] [blame] | 387 | chan_put(chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 388 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 389 | chan->conn = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 390 | hci_conn_put(conn->hcon); |
| 391 | } |
| 392 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 393 | l2cap_state_change(chan, BT_CLOSED); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 394 | sock_set_flag(sk, SOCK_ZAPPED); |
| 395 | |
| 396 | if (err) |
| 397 | sk->sk_err = err; |
| 398 | |
| 399 | if (parent) { |
| 400 | bt_accept_unlink(sk); |
| 401 | parent->sk_data_ready(parent, 0); |
| 402 | } else |
| 403 | sk->sk_state_change(sk); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 404 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 405 | if (!(test_bit(CONF_OUTPUT_DONE, &chan->conf_state) && |
| 406 | test_bit(CONF_INPUT_DONE, &chan->conf_state))) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 407 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 408 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 409 | skb_queue_purge(&chan->tx_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 410 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 411 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 412 | struct srej_list *l, *tmp; |
| 413 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 414 | __clear_retrans_timer(chan); |
| 415 | __clear_monitor_timer(chan); |
| 416 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 417 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 418 | skb_queue_purge(&chan->srej_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 419 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 420 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 421 | list_del(&l->list); |
| 422 | kfree(l); |
| 423 | } |
| 424 | } |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 427 | static void l2cap_chan_cleanup_listen(struct sock *parent) |
| 428 | { |
| 429 | struct sock *sk; |
| 430 | |
| 431 | BT_DBG("parent %p", parent); |
| 432 | |
| 433 | /* Close not yet accepted channels */ |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 434 | while ((sk = bt_accept_dequeue(parent, NULL))) { |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 435 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 436 | __clear_chan_timer(chan); |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 437 | lock_sock(sk); |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 438 | l2cap_chan_close(chan, ECONNRESET); |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 439 | release_sock(sk); |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 440 | chan->ops->close(chan->data); |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 441 | } |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 442 | } |
| 443 | |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 444 | void l2cap_chan_close(struct l2cap_chan *chan, int reason) |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 445 | { |
| 446 | struct l2cap_conn *conn = chan->conn; |
| 447 | struct sock *sk = chan->sk; |
| 448 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 449 | BT_DBG("chan %p state %d socket %p", chan, chan->state, sk->sk_socket); |
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: |
| 453 | l2cap_chan_cleanup_listen(sk); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 454 | |
| 455 | l2cap_state_change(chan, BT_CLOSED); |
| 456 | sock_set_flag(sk, SOCK_ZAPPED); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 457 | break; |
| 458 | |
| 459 | case BT_CONNECTED: |
| 460 | case BT_CONFIG: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 461 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 462 | conn->hcon->type == ACL_LINK) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 463 | __clear_chan_timer(chan); |
| 464 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 465 | l2cap_send_disconn_req(conn, chan, reason); |
| 466 | } else |
| 467 | l2cap_chan_del(chan, reason); |
| 468 | break; |
| 469 | |
| 470 | case BT_CONNECT2: |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 471 | if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 472 | conn->hcon->type == ACL_LINK) { |
| 473 | struct l2cap_conn_rsp rsp; |
| 474 | __u16 result; |
| 475 | |
| 476 | if (bt_sk(sk)->defer_setup) |
| 477 | result = L2CAP_CR_SEC_BLOCK; |
| 478 | else |
| 479 | result = L2CAP_CR_BAD_PSM; |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 480 | l2cap_state_change(chan, BT_DISCONN); |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 481 | |
| 482 | rsp.scid = cpu_to_le16(chan->dcid); |
| 483 | rsp.dcid = cpu_to_le16(chan->scid); |
| 484 | rsp.result = cpu_to_le16(result); |
| 485 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 486 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 487 | sizeof(rsp), &rsp); |
| 488 | } |
| 489 | |
| 490 | l2cap_chan_del(chan, reason); |
| 491 | break; |
| 492 | |
| 493 | case BT_CONNECT: |
| 494 | case BT_DISCONN: |
| 495 | l2cap_chan_del(chan, reason); |
| 496 | break; |
| 497 | |
| 498 | default: |
| 499 | sock_set_flag(sk, SOCK_ZAPPED); |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 504 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 505 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 506 | if (chan->chan_type == L2CAP_CHAN_RAW) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 507 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 508 | case BT_SECURITY_HIGH: |
| 509 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 510 | case BT_SECURITY_MEDIUM: |
| 511 | return HCI_AT_DEDICATED_BONDING; |
| 512 | default: |
| 513 | return HCI_AT_NO_BONDING; |
| 514 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 515 | } else if (chan->psm == cpu_to_le16(0x0001)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 516 | if (chan->sec_level == BT_SECURITY_LOW) |
| 517 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 518 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 519 | if (chan->sec_level == BT_SECURITY_HIGH) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 520 | return HCI_AT_NO_BONDING_MITM; |
| 521 | else |
| 522 | return HCI_AT_NO_BONDING; |
| 523 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 524 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 525 | case BT_SECURITY_HIGH: |
| 526 | return HCI_AT_GENERAL_BONDING_MITM; |
| 527 | case BT_SECURITY_MEDIUM: |
| 528 | return HCI_AT_GENERAL_BONDING; |
| 529 | default: |
| 530 | return HCI_AT_NO_BONDING; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 535 | /* Service level security */ |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 536 | int l2cap_chan_check_security(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 537 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 538 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 539 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 540 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 541 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 542 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 543 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 544 | } |
| 545 | |
Johannes Berg | b5ad8b7 | 2011-06-01 08:54:45 +0200 | [diff] [blame] | 546 | static u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 547 | { |
| 548 | u8 id; |
| 549 | |
| 550 | /* Get next available identificator. |
| 551 | * 1 - 128 are used by kernel. |
| 552 | * 129 - 199 are reserved. |
| 553 | * 200 - 254 are used by utilities like l2ping, etc. |
| 554 | */ |
| 555 | |
| 556 | spin_lock_bh(&conn->lock); |
| 557 | |
| 558 | if (++conn->tx_ident > 128) |
| 559 | conn->tx_ident = 1; |
| 560 | |
| 561 | id = conn->tx_ident; |
| 562 | |
| 563 | spin_unlock_bh(&conn->lock); |
| 564 | |
| 565 | return id; |
| 566 | } |
| 567 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 568 | 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] | 569 | { |
| 570 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 571 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 572 | |
| 573 | BT_DBG("code 0x%2.2x", code); |
| 574 | |
| 575 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 576 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 577 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 578 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 579 | flags = ACL_START_NO_FLUSH; |
| 580 | else |
| 581 | flags = ACL_START; |
| 582 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 583 | bt_cb(skb)->force_active = BT_POWER_FORCE_ACTIVE_ON; |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 584 | skb->priority = HCI_PRIO_MAX; |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 585 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 586 | hci_send_acl(conn->hchan, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 587 | } |
| 588 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 589 | static void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
| 590 | { |
| 591 | struct hci_conn *hcon = chan->conn->hcon; |
| 592 | u16 flags; |
| 593 | |
| 594 | BT_DBG("chan %p, skb %p len %d priority %u", chan, skb, skb->len, |
| 595 | skb->priority); |
| 596 | |
| 597 | if (!test_bit(FLAG_FLUSHABLE, &chan->flags) && |
| 598 | lmp_no_flush_capable(hcon->hdev)) |
| 599 | flags = ACL_START_NO_FLUSH; |
| 600 | else |
| 601 | flags = ACL_START; |
| 602 | |
| 603 | bt_cb(skb)->force_active = test_bit(FLAG_FORCE_ACTIVE, &chan->flags); |
| 604 | hci_send_acl(chan->conn->hchan, skb, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 607 | 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] | 608 | { |
| 609 | struct sk_buff *skb; |
| 610 | struct l2cap_hdr *lh; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 611 | struct l2cap_conn *conn = chan->conn; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 612 | int count, hlen; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 613 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 614 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 615 | return; |
| 616 | |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 617 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 618 | hlen = L2CAP_EXT_HDR_SIZE; |
| 619 | else |
| 620 | hlen = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 621 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 622 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 623 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 624 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 625 | BT_DBG("chan %p, control 0x%8.8x", chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 626 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 627 | count = min_t(unsigned int, conn->mtu, hlen); |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 628 | |
| 629 | control |= __set_sframe(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 630 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 631 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 632 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -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_PBIT, &chan->conn_state)) |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 635 | control |= __set_ctrl_poll(chan); |
Gustavo F. Padovan | f0946cc | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 636 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 637 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 638 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 639 | return; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 640 | |
| 641 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 642 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 643 | lh->cid = cpu_to_le16(chan->dcid); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 644 | |
| 645 | __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 646 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 647 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 648 | u16 fcs = crc16(0, (u8 *)lh, count - L2CAP_FCS_SIZE); |
| 649 | put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 650 | } |
| 651 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 652 | skb->priority = HCI_PRIO_MAX; |
| 653 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 654 | } |
| 655 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 656 | 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] | 657 | { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 658 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 659 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 660 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 661 | } else |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 662 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 663 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 664 | control |= __set_reqseq(chan, chan->buffer_seq); |
Gustavo F. Padovan | 2ab25cd | 2009-10-03 02:34:40 -0300 | [diff] [blame] | 665 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 666 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 667 | } |
| 668 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 669 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 670 | { |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 671 | return !test_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 672 | } |
| 673 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 674 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 675 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 676 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 677 | |
| 678 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 679 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 680 | return; |
| 681 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 682 | if (l2cap_chan_check_security(chan) && |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 683 | __l2cap_no_conn_pending(chan)) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 684 | struct l2cap_conn_req req; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 685 | req.scid = cpu_to_le16(chan->scid); |
| 686 | req.psm = chan->psm; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 687 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 688 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 689 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 690 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 691 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, |
| 692 | sizeof(req), &req); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 693 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 694 | } else { |
| 695 | struct l2cap_info_req req; |
| 696 | req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 697 | |
| 698 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 699 | conn->info_ident = l2cap_get_ident(conn); |
| 700 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame^] | 701 | schedule_delayed_work(&conn->info_work, |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 702 | msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); |
| 703 | |
| 704 | l2cap_send_cmd(conn, conn->info_ident, |
| 705 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 706 | } |
| 707 | } |
| 708 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 709 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 710 | { |
| 711 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 712 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 713 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 714 | |
| 715 | switch (mode) { |
| 716 | case L2CAP_MODE_ERTM: |
| 717 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 718 | case L2CAP_MODE_STREAMING: |
| 719 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 720 | default: |
| 721 | return 0x00; |
| 722 | } |
| 723 | } |
| 724 | |
Gustavo F. Padovan | 4519de9 | 2011-04-28 17:55:53 -0300 | [diff] [blame] | 725 | 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] | 726 | { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 727 | struct sock *sk; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 728 | struct l2cap_disconn_req req; |
| 729 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 730 | if (!conn) |
| 731 | return; |
| 732 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 733 | sk = chan->sk; |
| 734 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 735 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 736 | __clear_retrans_timer(chan); |
| 737 | __clear_monitor_timer(chan); |
| 738 | __clear_ack_timer(chan); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 739 | } |
| 740 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 741 | req.dcid = cpu_to_le16(chan->dcid); |
| 742 | req.scid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 743 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 744 | L2CAP_DISCONN_REQ, sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 745 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 746 | l2cap_state_change(chan, BT_DISCONN); |
Gustavo F. Padovan | 9b108fc | 2010-05-20 16:21:53 -0300 | [diff] [blame] | 747 | sk->sk_err = err; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 748 | } |
| 749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 751 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 752 | { |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 753 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 754 | |
| 755 | BT_DBG("conn %p", conn); |
| 756 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 757 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 758 | |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 759 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 760 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 761 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 762 | bh_lock_sock(sk); |
| 763 | |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 764 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 765 | bh_unlock_sock(sk); |
| 766 | continue; |
| 767 | } |
| 768 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 769 | if (chan->state == BT_CONNECT) { |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 770 | struct l2cap_conn_req req; |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 771 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 772 | if (!l2cap_chan_check_security(chan) || |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 773 | !__l2cap_no_conn_pending(chan)) { |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 774 | bh_unlock_sock(sk); |
| 775 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 776 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 777 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 778 | if (!l2cap_mode_supported(chan->mode, conn->feat_mask) |
| 779 | && test_bit(CONF_STATE2_DEVICE, |
| 780 | &chan->conf_state)) { |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 781 | /* l2cap_chan_close() calls list_del(chan) |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 782 | * so release the lock */ |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 783 | mutex_unlock(&conn->chan_lock); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 784 | l2cap_chan_close(chan, ECONNRESET); |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 785 | utex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 786 | bh_unlock_sock(sk); |
| 787 | continue; |
| 788 | } |
| 789 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 790 | req.scid = cpu_to_le16(chan->scid); |
| 791 | req.psm = chan->psm; |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 792 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 793 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 794 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 795 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 796 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, |
| 797 | sizeof(req), &req); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 798 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 799 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 800 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 801 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 802 | rsp.scid = cpu_to_le16(chan->dcid); |
| 803 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 804 | |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 805 | if (l2cap_chan_check_security(chan)) { |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 806 | if (bt_sk(sk)->defer_setup) { |
| 807 | struct sock *parent = bt_sk(sk)->parent; |
| 808 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 809 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Ilia Kolomisnky | 05e9a2f | 2011-07-15 18:30:21 +0000 | [diff] [blame] | 810 | if (parent) |
| 811 | parent->sk_data_ready(parent, 0); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 812 | |
| 813 | } else { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 814 | l2cap_state_change(chan, BT_CONFIG); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 815 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 816 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 817 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 818 | } else { |
| 819 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 820 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
| 821 | } |
| 822 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 823 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 824 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 825 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 826 | if (test_bit(CONF_REQ_SENT, &chan->conf_state) || |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 827 | rsp.result != L2CAP_CR_SUCCESS) { |
| 828 | bh_unlock_sock(sk); |
| 829 | continue; |
| 830 | } |
| 831 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 832 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 833 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 834 | l2cap_build_conf_req(chan, buf), buf); |
| 835 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | bh_unlock_sock(sk); |
| 839 | } |
| 840 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 841 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 842 | } |
| 843 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 844 | /* Find socket with cid and source bdaddr. |
| 845 | * Returns closest match, locked. |
| 846 | */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 847 | static struct l2cap_chan *l2cap_global_chan_by_scid(int state, __le16 cid, bdaddr_t *src) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 848 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 849 | struct l2cap_chan *c, *c1 = NULL; |
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 | read_lock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 852 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 853 | list_for_each_entry(c, &chan_list, global_l) { |
| 854 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 855 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 856 | if (state && c->state != state) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 857 | continue; |
| 858 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 859 | if (c->scid == cid) { |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 860 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 861 | if (!bacmp(&bt_sk(sk)->src, src)) { |
| 862 | read_unlock(&chan_list_lock); |
| 863 | return c; |
| 864 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 865 | |
| 866 | /* Closest match */ |
| 867 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 868 | c1 = c; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 869 | } |
| 870 | } |
Gustavo F. Padovan | 280f294 | 2011-04-13 19:01:22 -0300 | [diff] [blame] | 871 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 872 | read_unlock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 873 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 874 | return c1; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 878 | { |
Gustavo F. Padovan | c916fbe | 2011-04-04 16:00:55 -0300 | [diff] [blame] | 879 | struct sock *parent, *sk; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 880 | struct l2cap_chan *chan, *pchan; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 881 | |
| 882 | BT_DBG(""); |
| 883 | |
| 884 | /* Check if we have socket listening on cid */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 885 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 886 | conn->src); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 887 | if (!pchan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 888 | return; |
| 889 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 890 | parent = pchan->sk; |
| 891 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 892 | lock_sock(parent); |
Gustavo F. Padovan | 62f3a2c | 2011-04-14 18:34:34 -0300 | [diff] [blame] | 893 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 894 | /* Check for backlog size */ |
| 895 | if (sk_acceptq_is_full(parent)) { |
| 896 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
| 897 | goto clean; |
| 898 | } |
| 899 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 900 | chan = pchan->ops->new_connection(pchan->data); |
| 901 | if (!chan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 902 | goto clean; |
| 903 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 904 | sk = chan->sk; |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 905 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 906 | mutex_lock(&conn->chan_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 907 | |
| 908 | hci_conn_hold(conn->hcon); |
| 909 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 910 | bacpy(&bt_sk(sk)->src, conn->src); |
| 911 | bacpy(&bt_sk(sk)->dst, conn->dst); |
| 912 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 913 | bt_accept_enqueue(parent, sk); |
| 914 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 915 | __l2cap_chan_add(conn, chan); |
| 916 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 917 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 918 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 919 | l2cap_state_change(chan, BT_CONNECTED); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 920 | parent->sk_data_ready(parent, 0); |
| 921 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 922 | mutex_unlock(&conn->chan_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 923 | |
| 924 | clean: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 925 | release_sock(parent); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 926 | } |
| 927 | |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 928 | static void l2cap_chan_ready(struct sock *sk) |
| 929 | { |
| 930 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
| 931 | struct sock *parent = bt_sk(sk)->parent; |
| 932 | |
| 933 | BT_DBG("sk %p, parent %p", sk, parent); |
| 934 | |
| 935 | chan->conf_state = 0; |
| 936 | __clear_chan_timer(chan); |
| 937 | |
Vinicius Costa Gomes | 43f3dc4 | 2011-06-20 18:53:18 -0300 | [diff] [blame] | 938 | l2cap_state_change(chan, BT_CONNECTED); |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 939 | sk->sk_state_change(sk); |
| 940 | |
| 941 | if (parent) |
| 942 | parent->sk_data_ready(parent, 0); |
| 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 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 957 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 958 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 959 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 960 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 961 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 962 | bh_lock_sock(sk); |
| 963 | |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 964 | if (conn->hcon->type == LE_LINK) { |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 965 | if (smp_conn_security(conn, chan->sec_level)) |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 966 | l2cap_chan_ready(sk); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 967 | |
Vinicius Costa Gomes | 6312845 | 2011-06-17 22:46:26 -0300 | [diff] [blame] | 968 | } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 969 | __clear_chan_timer(chan); |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 970 | l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 971 | sk->sk_state_change(sk); |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 972 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 973 | } else if (chan->state == BT_CONNECT) |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 974 | l2cap_do_start(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 975 | |
| 976 | bh_unlock_sock(sk); |
| 977 | } |
| 978 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 979 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 980 | } |
| 981 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 982 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 983 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 984 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 985 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 986 | |
| 987 | BT_DBG("conn %p", conn); |
| 988 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 989 | mutex_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 990 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 991 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 992 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 993 | |
Andrei Emeltchenko | ecf61bd | 2011-10-11 14:04:32 +0300 | [diff] [blame] | 994 | if (test_bit(FLAG_FORCE_RELIABLE, &chan->flags)) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 995 | sk->sk_err = err; |
| 996 | } |
| 997 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -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, |
| 1004 | info_work.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; |
| 1016 | struct sock *sk; |
| 1017 | |
| 1018 | if (!conn) |
| 1019 | return; |
| 1020 | |
| 1021 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 1022 | |
| 1023 | kfree_skb(conn->rx_skb); |
| 1024 | |
| 1025 | /* Kill channels */ |
| 1026 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
| 1027 | sk = chan->sk; |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 1028 | lock_sock(sk); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1029 | l2cap_chan_del(chan, err); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 1030 | release_sock(sk); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1031 | chan->ops->close(chan->data); |
| 1032 | } |
| 1033 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1034 | hci_chan_del(conn->hchan); |
| 1035 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1036 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame^] | 1037 | cancel_delayed_work_sync(&conn->info_work); |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1038 | |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1039 | if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->pend)) { |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1040 | del_timer(&conn->security_timer); |
Vinicius Costa Gomes | 8aab475 | 2011-09-05 14:31:31 -0300 | [diff] [blame] | 1041 | smp_chan_destroy(conn); |
Vinicius Costa Gomes | d26a234 | 2011-08-19 21:06:51 -0300 | [diff] [blame] | 1042 | } |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1043 | |
| 1044 | hcon->l2cap_data = NULL; |
| 1045 | kfree(conn); |
| 1046 | } |
| 1047 | |
| 1048 | static void security_timeout(unsigned long arg) |
| 1049 | { |
| 1050 | struct l2cap_conn *conn = (void *) arg; |
| 1051 | |
| 1052 | l2cap_conn_del(conn->hcon, ETIMEDOUT); |
| 1053 | } |
| 1054 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) |
| 1056 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1057 | struct l2cap_conn *conn = hcon->l2cap_data; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1058 | struct hci_chan *hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1060 | if (conn || status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | return conn; |
| 1062 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1063 | hchan = hci_chan_create(hcon); |
| 1064 | if (!hchan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1067 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); |
| 1068 | if (!conn) { |
| 1069 | hci_chan_del(hchan); |
| 1070 | return NULL; |
| 1071 | } |
| 1072 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | hcon->l2cap_data = conn; |
| 1074 | conn->hcon = hcon; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1075 | conn->hchan = hchan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1077 | BT_DBG("hcon %p conn %p hchan %p", hcon, conn, hchan); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 1078 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1079 | if (hcon->hdev->le_mtu && hcon->type == LE_LINK) |
| 1080 | conn->mtu = hcon->hdev->le_mtu; |
| 1081 | else |
| 1082 | conn->mtu = hcon->hdev->acl_mtu; |
| 1083 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | conn->src = &hcon->hdev->bdaddr; |
| 1085 | conn->dst = &hcon->dst; |
| 1086 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 1087 | conn->feat_mask = 0; |
| 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | spin_lock_init(&conn->lock); |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 1090 | mutex_init(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1091 | |
| 1092 | INIT_LIST_HEAD(&conn->chan_l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | |
Vinicius Costa Gomes | 5d3de7d | 2011-06-14 13:37:41 -0300 | [diff] [blame] | 1094 | if (hcon->type == LE_LINK) |
| 1095 | setup_timer(&conn->security_timer, security_timeout, |
| 1096 | (unsigned long) conn); |
| 1097 | else |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame^] | 1098 | INIT_DELAYED_WORK(&conn->info_work, l2cap_info_timeout); |
Dave Young | 45054dc | 2009-10-18 20:28:30 +0000 | [diff] [blame] | 1099 | |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 1100 | conn->disc_reason = HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 1101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | return conn; |
| 1103 | } |
| 1104 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1105 | static inline void l2cap_chan_add(struct l2cap_conn *conn, struct l2cap_chan *chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | { |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 1107 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1108 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 1109 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | |
| 1114 | /* Find socket with psm and source bdaddr. |
| 1115 | * Returns closest match. |
| 1116 | */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1117 | 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] | 1118 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1119 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1121 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1122 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1123 | list_for_each_entry(c, &chan_list, global_l) { |
| 1124 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1125 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1126 | if (state && c->state != state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | continue; |
| 1128 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1129 | if (c->psm == psm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1131 | if (!bacmp(&bt_sk(sk)->src, src)) { |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 1132 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1133 | return c; |
| 1134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | |
| 1136 | /* Closest match */ |
| 1137 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1138 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | } |
| 1140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1142 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 1143 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 1144 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
Gustavo F. Padovan | 77a74c7 | 2011-04-12 18:17:14 -0300 | [diff] [blame] | 1147 | int l2cap_chan_connect(struct l2cap_chan *chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | { |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 1149 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | bdaddr_t *src = &bt_sk(sk)->src; |
| 1151 | bdaddr_t *dst = &bt_sk(sk)->dst; |
| 1152 | struct l2cap_conn *conn; |
| 1153 | struct hci_conn *hcon; |
| 1154 | struct hci_dev *hdev; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1155 | __u8 auth_type; |
Marcel Holtmann | 44d0e48 | 2009-04-20 07:09:16 +0200 | [diff] [blame] | 1156 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | |
Marcel Holtmann | f29972d | 2009-02-12 05:07:45 +0100 | [diff] [blame] | 1158 | BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst), |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1159 | chan->psm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1161 | hdev = hci_get_route(dst, src); |
| 1162 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | return -EHOSTUNREACH; |
| 1164 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1165 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1167 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1168 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1169 | if (chan->dcid == L2CAP_CID_LE_DATA) |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1170 | hcon = hci_connect(hdev, LE_LINK, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1171 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1172 | else |
| 1173 | hcon = hci_connect(hdev, ACL_LINK, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1174 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 1175 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1176 | if (IS_ERR(hcon)) { |
| 1177 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | |
| 1181 | conn = l2cap_conn_add(hcon, 0); |
| 1182 | if (!conn) { |
| 1183 | hci_conn_put(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1184 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | goto done; |
| 1186 | } |
| 1187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | /* Update source addr of the socket */ |
| 1189 | bacpy(src, conn->src); |
| 1190 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1191 | l2cap_chan_add(conn, chan); |
| 1192 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1193 | l2cap_state_change(chan, BT_CONNECT); |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1194 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | |
| 1196 | if (hcon->state == BT_CONNECTED) { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1197 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 1198 | __clear_chan_timer(chan); |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 1199 | if (l2cap_chan_check_security(chan)) |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1200 | l2cap_state_change(chan, BT_CONNECTED); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 1201 | } else |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 1202 | l2cap_do_start(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 1205 | err = 0; |
| 1206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1208 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | hci_dev_put(hdev); |
| 1210 | return err; |
| 1211 | } |
| 1212 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 1213 | int __l2cap_wait_ack(struct sock *sk) |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1214 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1215 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1216 | DECLARE_WAITQUEUE(wait, current); |
| 1217 | int err = 0; |
| 1218 | int timeo = HZ/5; |
| 1219 | |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1220 | add_wait_queue(sk_sleep(sk), &wait); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1221 | set_current_state(TASK_INTERRUPTIBLE); |
| 1222 | while (chan->unacked_frames > 0 && chan->conn) { |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1223 | if (!timeo) |
| 1224 | timeo = HZ/5; |
| 1225 | |
| 1226 | if (signal_pending(current)) { |
| 1227 | err = sock_intr_errno(timeo); |
| 1228 | break; |
| 1229 | } |
| 1230 | |
| 1231 | release_sock(sk); |
| 1232 | timeo = schedule_timeout(timeo); |
| 1233 | lock_sock(sk); |
Peter Hurley | a71a0cf | 2011-07-25 18:36:26 -0400 | [diff] [blame] | 1234 | set_current_state(TASK_INTERRUPTIBLE); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1235 | |
| 1236 | err = sock_error(sk); |
| 1237 | if (err) |
| 1238 | break; |
| 1239 | } |
| 1240 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1241 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1242 | return err; |
| 1243 | } |
| 1244 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1245 | static void l2cap_monitor_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1246 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1247 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 1248 | monitor_timer.work); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1249 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1250 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1251 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1252 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1253 | lock_sock(sk); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1254 | if (chan->retry_count >= chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1255 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1256 | release_sock(sk); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1257 | return; |
| 1258 | } |
| 1259 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1260 | chan->retry_count++; |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1261 | __set_monitor_timer(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1262 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1263 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1264 | release_sock(sk); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1265 | } |
| 1266 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1267 | static void l2cap_retrans_timeout(struct work_struct *work) |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1268 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1269 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 1270 | retrans_timer.work); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1271 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1272 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1273 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1274 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1275 | lock_sock(sk); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1276 | chan->retry_count = 1; |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1277 | __set_monitor_timer(chan); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1278 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1279 | set_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1280 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1281 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1282 | release_sock(sk); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1283 | } |
| 1284 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1285 | static void l2cap_drop_acked_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1286 | { |
| 1287 | struct sk_buff *skb; |
| 1288 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1289 | while ((skb = skb_peek(&chan->tx_q)) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1290 | chan->unacked_frames) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1291 | if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1292 | break; |
| 1293 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1294 | skb = skb_dequeue(&chan->tx_q); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1295 | kfree_skb(skb); |
| 1296 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1297 | chan->unacked_frames--; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1298 | } |
| 1299 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1300 | if (!chan->unacked_frames) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1301 | __clear_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1302 | } |
| 1303 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1304 | static void l2cap_streaming_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1305 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1306 | struct sk_buff *skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1307 | u32 control; |
| 1308 | u16 fcs; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1309 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1310 | while ((skb = skb_dequeue(&chan->tx_q))) { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1311 | control = __get_control(chan, skb->data + L2CAP_HDR_SIZE); |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1312 | control |= __set_txseq(chan, chan->next_tx_seq); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1313 | __put_control(chan, control, skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1314 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1315 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1316 | fcs = crc16(0, (u8 *)skb->data, |
| 1317 | skb->len - L2CAP_FCS_SIZE); |
| 1318 | put_unaligned_le16(fcs, |
| 1319 | skb->data + skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1320 | } |
| 1321 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1322 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1323 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1324 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1325 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1326 | } |
| 1327 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1328 | 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] | 1329 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1330 | struct sk_buff *skb, *tx_skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1331 | u16 fcs; |
| 1332 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1333 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1334 | skb = skb_peek(&chan->tx_q); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1335 | if (!skb) |
| 1336 | return; |
| 1337 | |
Szymon Janc | d1726b6 | 2011-11-16 09:32:20 +0100 | [diff] [blame] | 1338 | while (bt_cb(skb)->tx_seq != tx_seq) { |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1339 | if (skb_queue_is_last(&chan->tx_q, skb)) |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1340 | return; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1341 | |
Szymon Janc | d1726b6 | 2011-11-16 09:32:20 +0100 | [diff] [blame] | 1342 | skb = skb_queue_next(&chan->tx_q, skb); |
| 1343 | } |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1344 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1345 | if (chan->remote_max_tx && |
| 1346 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1347 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1348 | return; |
| 1349 | } |
| 1350 | |
| 1351 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1352 | bt_cb(skb)->retries++; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1353 | |
| 1354 | control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1355 | control &= __get_sar_mask(chan); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1356 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1357 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 1358 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1359 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1360 | control |= __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1361 | control |= __set_txseq(chan, tx_seq); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1362 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1363 | __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1364 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1365 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1366 | fcs = crc16(0, (u8 *)tx_skb->data, |
| 1367 | tx_skb->len - L2CAP_FCS_SIZE); |
| 1368 | put_unaligned_le16(fcs, |
| 1369 | tx_skb->data + tx_skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1370 | } |
| 1371 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1372 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1373 | } |
| 1374 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1375 | static int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1376 | { |
| 1377 | struct sk_buff *skb, *tx_skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1378 | u16 fcs; |
| 1379 | u32 control; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1380 | int nsent = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1381 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 1382 | if (chan->state != BT_CONNECTED) |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1383 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1384 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1385 | while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1386 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1387 | if (chan->remote_max_tx && |
| 1388 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1389 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1390 | break; |
| 1391 | } |
| 1392 | |
Andrei Emeltchenko | e420aba | 2009-12-23 13:07:14 +0200 | [diff] [blame] | 1393 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1394 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1395 | bt_cb(skb)->retries++; |
| 1396 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1397 | control = __get_control(chan, tx_skb->data + L2CAP_HDR_SIZE); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1398 | control &= __get_sar_mask(chan); |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1399 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1400 | if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 1401 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1402 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1403 | control |= __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 1404 | control |= __set_txseq(chan, chan->next_tx_seq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1405 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1406 | __put_control(chan, control, tx_skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1407 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1408 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1409 | fcs = crc16(0, (u8 *)skb->data, |
| 1410 | tx_skb->len - L2CAP_FCS_SIZE); |
| 1411 | put_unaligned_le16(fcs, skb->data + |
| 1412 | tx_skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1413 | } |
| 1414 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1415 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1416 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 1417 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1418 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1419 | bt_cb(skb)->tx_seq = chan->next_tx_seq; |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 1420 | |
| 1421 | chan->next_tx_seq = __next_seq(chan, chan->next_tx_seq); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1422 | |
Suraj Sumangala | 23e9fde | 2011-03-09 14:44:05 +0530 | [diff] [blame] | 1423 | if (bt_cb(skb)->retries == 1) |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1424 | chan->unacked_frames++; |
Suraj Sumangala | 23e9fde | 2011-03-09 14:44:05 +0530 | [diff] [blame] | 1425 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1426 | chan->frames_sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1427 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1428 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 1429 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1430 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1431 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1432 | |
| 1433 | nsent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1434 | } |
| 1435 | |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1436 | return nsent; |
| 1437 | } |
| 1438 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1439 | static int l2cap_retransmit_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1440 | { |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1441 | int ret; |
| 1442 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1443 | if (!skb_queue_empty(&chan->tx_q)) |
| 1444 | chan->tx_send_head = chan->tx_q.next; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1445 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1446 | chan->next_tx_seq = chan->expected_ack_seq; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1447 | ret = l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1448 | return ret; |
| 1449 | } |
| 1450 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1451 | static void l2cap_send_ack(struct l2cap_chan *chan) |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1452 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1453 | u32 control = 0; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1454 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1455 | control |= __set_reqseq(chan, chan->buffer_seq); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1456 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1457 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 1458 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1459 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1460 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1461 | return; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1462 | } |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1463 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1464 | if (l2cap_ertm_send(chan) > 0) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1465 | return; |
| 1466 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 1467 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1468 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1469 | } |
| 1470 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1471 | static void l2cap_send_srejtail(struct l2cap_chan *chan) |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1472 | { |
| 1473 | struct srej_list *tail; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1474 | u32 control; |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1475 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 1476 | control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 1477 | control |= __set_ctrl_final(chan); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1478 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 1479 | tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 1480 | control |= __set_reqseq(chan, tail->tx_seq); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1481 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1482 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1483 | } |
| 1484 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1485 | static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg, int len, int count, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1487 | struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1488 | struct sk_buff **frag; |
| 1489 | int err, sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | |
Gustavo F. Padovan | 59203a2 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1491 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1492 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
| 1494 | sent += count; |
| 1495 | len -= count; |
| 1496 | |
| 1497 | /* Continuation fragments (no L2CAP header) */ |
| 1498 | frag = &skb_shinfo(skb)->frag_list; |
| 1499 | while (len) { |
| 1500 | count = min_t(unsigned int, conn->mtu, len); |
| 1501 | |
| 1502 | *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); |
| 1503 | if (!*frag) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1504 | return err; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1505 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
| 1506 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1508 | (*frag)->priority = skb->priority; |
| 1509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | sent += count; |
| 1511 | len -= count; |
| 1512 | |
| 1513 | frag = &(*frag)->next; |
| 1514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
| 1516 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1519 | static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, |
| 1520 | struct msghdr *msg, size_t len, |
| 1521 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1522 | { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1523 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1524 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1525 | struct sk_buff *skb; |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1526 | int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1527 | struct l2cap_hdr *lh; |
| 1528 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1529 | BT_DBG("sk %p len %d priority %u", sk, (int)len, priority); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1530 | |
| 1531 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 1532 | skb = bt_skb_send_alloc(sk, count + hlen, |
| 1533 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1534 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1535 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1536 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1537 | skb->priority = priority; |
| 1538 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1539 | /* Create L2CAP header */ |
| 1540 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1541 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1542 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1543 | put_unaligned_le16(chan->psm, skb_put(skb, 2)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1544 | |
| 1545 | err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); |
| 1546 | if (unlikely(err < 0)) { |
| 1547 | kfree_skb(skb); |
| 1548 | return ERR_PTR(err); |
| 1549 | } |
| 1550 | return skb; |
| 1551 | } |
| 1552 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1553 | static struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, |
| 1554 | struct msghdr *msg, size_t len, |
| 1555 | u32 priority) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1556 | { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1557 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1558 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1559 | struct sk_buff *skb; |
| 1560 | int err, count, hlen = L2CAP_HDR_SIZE; |
| 1561 | struct l2cap_hdr *lh; |
| 1562 | |
| 1563 | BT_DBG("sk %p len %d", sk, (int)len); |
| 1564 | |
| 1565 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 1566 | skb = bt_skb_send_alloc(sk, count + hlen, |
| 1567 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1568 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1569 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1570 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1571 | skb->priority = priority; |
| 1572 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1573 | /* Create L2CAP header */ |
| 1574 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1575 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1576 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 1577 | |
| 1578 | err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); |
| 1579 | if (unlikely(err < 0)) { |
| 1580 | kfree_skb(skb); |
| 1581 | return ERR_PTR(err); |
| 1582 | } |
| 1583 | return skb; |
| 1584 | } |
| 1585 | |
Luiz Augusto von Dentz | ab0ff76 | 2011-09-12 20:00:50 +0300 | [diff] [blame] | 1586 | static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, |
| 1587 | struct msghdr *msg, size_t len, |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1588 | u32 control, u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1589 | { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1590 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1591 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1592 | struct sk_buff *skb; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 1593 | int err, count, hlen; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1594 | struct l2cap_hdr *lh; |
| 1595 | |
| 1596 | BT_DBG("sk %p len %d", sk, (int)len); |
| 1597 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 1598 | if (!conn) |
| 1599 | return ERR_PTR(-ENOTCONN); |
| 1600 | |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 1601 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 1602 | hlen = L2CAP_EXT_HDR_SIZE; |
| 1603 | else |
| 1604 | hlen = L2CAP_ENH_HDR_SIZE; |
| 1605 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1606 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1607 | hlen += L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1608 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1609 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1610 | hlen += L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1611 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1612 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 1613 | skb = bt_skb_send_alloc(sk, count + hlen, |
| 1614 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 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 | |
| 1618 | /* Create L2CAP header */ |
| 1619 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1620 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1621 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1622 | |
| 1623 | __put_control(chan, control, skb_put(skb, __ctrl_size(chan))); |
| 1624 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1625 | if (sdulen) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1626 | put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1627 | |
| 1628 | err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); |
| 1629 | if (unlikely(err < 0)) { |
| 1630 | kfree_skb(skb); |
| 1631 | return ERR_PTR(err); |
| 1632 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1633 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1634 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 1635 | put_unaligned_le16(0, skb_put(skb, L2CAP_FCS_SIZE)); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1636 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1637 | bt_cb(skb)->retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1638 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
Szymon Janc | 67c9e84 | 2011-07-28 16:24:33 +0200 | [diff] [blame] | 1641 | 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] | 1642 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1643 | struct sk_buff *skb; |
| 1644 | struct sk_buff_head sar_queue; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1645 | u32 control; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1646 | size_t size = 0; |
| 1647 | |
Gustavo F. Padovan | ff12fd6 | 2010-05-05 22:09:15 -0300 | [diff] [blame] | 1648 | skb_queue_head_init(&sar_queue); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1649 | control = __set_ctrl_sar(chan, L2CAP_SAR_START); |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1650 | 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] | 1651 | if (IS_ERR(skb)) |
| 1652 | return PTR_ERR(skb); |
| 1653 | |
| 1654 | __skb_queue_tail(&sar_queue, skb); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1655 | len -= chan->remote_mps; |
| 1656 | size += chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1657 | |
| 1658 | while (len > 0) { |
| 1659 | size_t buflen; |
| 1660 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1661 | if (len > chan->remote_mps) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1662 | control = __set_ctrl_sar(chan, L2CAP_SAR_CONTINUE); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1663 | buflen = chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1664 | } else { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1665 | control = __set_ctrl_sar(chan, L2CAP_SAR_END); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1666 | buflen = len; |
| 1667 | } |
| 1668 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1669 | skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1670 | if (IS_ERR(skb)) { |
| 1671 | skb_queue_purge(&sar_queue); |
| 1672 | return PTR_ERR(skb); |
| 1673 | } |
| 1674 | |
| 1675 | __skb_queue_tail(&sar_queue, skb); |
| 1676 | len -= buflen; |
| 1677 | size += buflen; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1678 | } |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1679 | skb_queue_splice_tail(&sar_queue, &chan->tx_q); |
| 1680 | if (chan->tx_send_head == NULL) |
| 1681 | chan->tx_send_head = sar_queue.next; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1682 | |
| 1683 | return size; |
| 1684 | } |
| 1685 | |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1686 | int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len, |
| 1687 | u32 priority) |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1688 | { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1689 | struct sk_buff *skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 1690 | u32 control; |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1691 | int err; |
| 1692 | |
| 1693 | /* Connectionless channel */ |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1694 | if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1695 | skb = l2cap_create_connless_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1696 | if (IS_ERR(skb)) |
| 1697 | return PTR_ERR(skb); |
| 1698 | |
| 1699 | l2cap_do_send(chan, skb); |
| 1700 | return len; |
| 1701 | } |
| 1702 | |
| 1703 | switch (chan->mode) { |
| 1704 | case L2CAP_MODE_BASIC: |
| 1705 | /* Check outgoing MTU */ |
| 1706 | if (len > chan->omtu) |
| 1707 | return -EMSGSIZE; |
| 1708 | |
| 1709 | /* Create a basic PDU */ |
Luiz Augusto von Dentz | 5e59b79 | 2011-11-01 10:58:57 +0200 | [diff] [blame] | 1710 | skb = l2cap_create_basic_pdu(chan, msg, len, priority); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1711 | if (IS_ERR(skb)) |
| 1712 | return PTR_ERR(skb); |
| 1713 | |
| 1714 | l2cap_do_send(chan, skb); |
| 1715 | err = len; |
| 1716 | break; |
| 1717 | |
| 1718 | case L2CAP_MODE_ERTM: |
| 1719 | case L2CAP_MODE_STREAMING: |
| 1720 | /* Entire SDU fits into one PDU */ |
| 1721 | if (len <= chan->remote_mps) { |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 1722 | control = __set_ctrl_sar(chan, L2CAP_SAR_UNSEGMENTED); |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1723 | skb = l2cap_create_iframe_pdu(chan, msg, len, control, |
| 1724 | 0); |
| 1725 | if (IS_ERR(skb)) |
| 1726 | return PTR_ERR(skb); |
| 1727 | |
| 1728 | __skb_queue_tail(&chan->tx_q, skb); |
| 1729 | |
| 1730 | if (chan->tx_send_head == NULL) |
| 1731 | chan->tx_send_head = skb; |
| 1732 | |
| 1733 | } else { |
| 1734 | /* Segment SDU into multiples PDUs */ |
| 1735 | err = l2cap_sar_segment_sdu(chan, msg, len); |
| 1736 | if (err < 0) |
| 1737 | return err; |
| 1738 | } |
| 1739 | |
| 1740 | if (chan->mode == L2CAP_MODE_STREAMING) { |
| 1741 | l2cap_streaming_send(chan); |
| 1742 | err = len; |
| 1743 | break; |
| 1744 | } |
| 1745 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 1746 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
| 1747 | test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 9a91a04 | 2011-04-28 18:50:17 -0300 | [diff] [blame] | 1748 | err = len; |
| 1749 | break; |
| 1750 | } |
| 1751 | |
| 1752 | err = l2cap_ertm_send(chan); |
| 1753 | if (err >= 0) |
| 1754 | err = len; |
| 1755 | |
| 1756 | break; |
| 1757 | |
| 1758 | default: |
| 1759 | BT_DBG("bad state %1.1x", chan->mode); |
| 1760 | err = -EBADFD; |
| 1761 | } |
| 1762 | |
| 1763 | return err; |
| 1764 | } |
| 1765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | /* Copy frame to all raw sockets on that connection */ |
| 1767 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1768 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1770 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
| 1772 | BT_DBG("conn %p", conn); |
| 1773 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 1774 | mutex_lock(&conn->chan_lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1775 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1776 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 1777 | if (chan->chan_type != L2CAP_CHAN_RAW) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | continue; |
| 1779 | |
| 1780 | /* Don't send frame to the socket it came from */ |
| 1781 | if (skb->sk == sk) |
| 1782 | continue; |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1783 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 1784 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | continue; |
| 1786 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 1787 | if (chan->ops->recv(chan->data, nskb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | kfree_skb(nskb); |
| 1789 | } |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 1790 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | } |
| 1792 | |
| 1793 | /* ---- L2CAP signalling commands ---- */ |
| 1794 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
| 1795 | u8 code, u8 ident, u16 dlen, void *data) |
| 1796 | { |
| 1797 | struct sk_buff *skb, **frag; |
| 1798 | struct l2cap_cmd_hdr *cmd; |
| 1799 | struct l2cap_hdr *lh; |
| 1800 | int len, count; |
| 1801 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1802 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", |
| 1803 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | |
| 1805 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 1806 | count = min_t(unsigned int, conn->mtu, len); |
| 1807 | |
| 1808 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 1809 | if (!skb) |
| 1810 | return NULL; |
| 1811 | |
| 1812 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1813 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 1814 | |
| 1815 | if (conn->hcon->type == LE_LINK) |
| 1816 | lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
| 1817 | else |
| 1818 | lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | |
| 1820 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 1821 | cmd->code = code; |
| 1822 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1823 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | |
| 1825 | if (dlen) { |
| 1826 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 1827 | memcpy(skb_put(skb, count), data, count); |
| 1828 | data += count; |
| 1829 | } |
| 1830 | |
| 1831 | len -= skb->len; |
| 1832 | |
| 1833 | /* Continuation fragments (no L2CAP header) */ |
| 1834 | frag = &skb_shinfo(skb)->frag_list; |
| 1835 | while (len) { |
| 1836 | count = min_t(unsigned int, conn->mtu, len); |
| 1837 | |
| 1838 | *frag = bt_skb_alloc(count, GFP_ATOMIC); |
| 1839 | if (!*frag) |
| 1840 | goto fail; |
| 1841 | |
| 1842 | memcpy(skb_put(*frag, count), data, count); |
| 1843 | |
| 1844 | len -= count; |
| 1845 | data += count; |
| 1846 | |
| 1847 | frag = &(*frag)->next; |
| 1848 | } |
| 1849 | |
| 1850 | return skb; |
| 1851 | |
| 1852 | fail: |
| 1853 | kfree_skb(skb); |
| 1854 | return NULL; |
| 1855 | } |
| 1856 | |
| 1857 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) |
| 1858 | { |
| 1859 | struct l2cap_conf_opt *opt = *ptr; |
| 1860 | int len; |
| 1861 | |
| 1862 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 1863 | *ptr += len; |
| 1864 | |
| 1865 | *type = opt->type; |
| 1866 | *olen = opt->len; |
| 1867 | |
| 1868 | switch (opt->len) { |
| 1869 | case 1: |
| 1870 | *val = *((u8 *) opt->val); |
| 1871 | break; |
| 1872 | |
| 1873 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1874 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | break; |
| 1876 | |
| 1877 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1878 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | break; |
| 1880 | |
| 1881 | default: |
| 1882 | *val = (unsigned long) opt->val; |
| 1883 | break; |
| 1884 | } |
| 1885 | |
| 1886 | BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); |
| 1887 | return len; |
| 1888 | } |
| 1889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 1891 | { |
| 1892 | struct l2cap_conf_opt *opt = *ptr; |
| 1893 | |
| 1894 | BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); |
| 1895 | |
| 1896 | opt->type = type; |
| 1897 | opt->len = len; |
| 1898 | |
| 1899 | switch (len) { |
| 1900 | case 1: |
| 1901 | *((u8 *) opt->val) = val; |
| 1902 | break; |
| 1903 | |
| 1904 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1905 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | break; |
| 1907 | |
| 1908 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1909 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | break; |
| 1911 | |
| 1912 | default: |
| 1913 | memcpy(opt->val, (void *) val, len); |
| 1914 | break; |
| 1915 | } |
| 1916 | |
| 1917 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 1918 | } |
| 1919 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 1920 | static void l2cap_add_opt_efs(void **ptr, struct l2cap_chan *chan) |
| 1921 | { |
| 1922 | struct l2cap_conf_efs efs; |
| 1923 | |
Szymon Janc | 1ec918c | 2011-11-16 09:32:21 +0100 | [diff] [blame] | 1924 | switch (chan->mode) { |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 1925 | case L2CAP_MODE_ERTM: |
| 1926 | efs.id = chan->local_id; |
| 1927 | efs.stype = chan->local_stype; |
| 1928 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 1929 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 1930 | efs.acc_lat = cpu_to_le32(L2CAP_DEFAULT_ACC_LAT); |
| 1931 | efs.flush_to = cpu_to_le32(L2CAP_DEFAULT_FLUSH_TO); |
| 1932 | break; |
| 1933 | |
| 1934 | case L2CAP_MODE_STREAMING: |
| 1935 | efs.id = 1; |
| 1936 | efs.stype = L2CAP_SERV_BESTEFFORT; |
| 1937 | efs.msdu = cpu_to_le16(chan->local_msdu); |
| 1938 | efs.sdu_itime = cpu_to_le32(chan->local_sdu_itime); |
| 1939 | efs.acc_lat = 0; |
| 1940 | efs.flush_to = 0; |
| 1941 | break; |
| 1942 | |
| 1943 | default: |
| 1944 | return; |
| 1945 | } |
| 1946 | |
| 1947 | l2cap_add_conf_opt(ptr, L2CAP_CONF_EFS, sizeof(efs), |
| 1948 | (unsigned long) &efs); |
| 1949 | } |
| 1950 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1951 | static void l2cap_ack_timeout(struct work_struct *work) |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 1952 | { |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1953 | struct l2cap_chan *chan = container_of(work, struct l2cap_chan, |
| 1954 | ack_timer.work); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 1955 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1956 | lock_sock(chan->sk); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1957 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1958 | release_sock(chan->sk); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 1959 | } |
| 1960 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1961 | static inline void l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1962 | { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1963 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1964 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1965 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1966 | chan->num_acked = 0; |
| 1967 | chan->frames_sent = 0; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1968 | |
Gustavo F. Padovan | 721c418 | 2011-06-23 19:29:58 -0300 | [diff] [blame] | 1969 | INIT_DELAYED_WORK(&chan->retrans_timer, l2cap_retrans_timeout); |
| 1970 | INIT_DELAYED_WORK(&chan->monitor_timer, l2cap_monitor_timeout); |
| 1971 | INIT_DELAYED_WORK(&chan->ack_timer, l2cap_ack_timeout); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1972 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 1973 | skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1974 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 1975 | INIT_LIST_HEAD(&chan->srej_l); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1976 | } |
| 1977 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1978 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 1979 | { |
| 1980 | switch (mode) { |
| 1981 | case L2CAP_MODE_STREAMING: |
| 1982 | case L2CAP_MODE_ERTM: |
| 1983 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 1984 | return mode; |
| 1985 | /* fall through */ |
| 1986 | default: |
| 1987 | return L2CAP_MODE_BASIC; |
| 1988 | } |
| 1989 | } |
| 1990 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 1991 | static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) |
| 1992 | { |
| 1993 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; |
| 1994 | } |
| 1995 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 1996 | static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) |
| 1997 | { |
| 1998 | return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; |
| 1999 | } |
| 2000 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2001 | static inline void l2cap_txwin_setup(struct l2cap_chan *chan) |
| 2002 | { |
| 2003 | if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2004 | __l2cap_ews_supported(chan)) { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2005 | /* use extended control field */ |
| 2006 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2007 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
| 2008 | } else { |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2009 | chan->tx_win = min_t(u16, chan->tx_win, |
| 2010 | L2CAP_DEFAULT_TX_WINDOW); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2011 | chan->tx_win_max = L2CAP_DEFAULT_TX_WINDOW; |
| 2012 | } |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2013 | } |
| 2014 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2015 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2017 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2018 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | void *ptr = req->data; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2020 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 2022 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2024 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2025 | goto done; |
| 2026 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2027 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2028 | case L2CAP_MODE_STREAMING: |
| 2029 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2030 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2031 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2032 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2033 | if (__l2cap_efs_supported(chan)) |
| 2034 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 2035 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 2036 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2037 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2038 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2039 | break; |
| 2040 | } |
| 2041 | |
| 2042 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2043 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 2044 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681c | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 2045 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2046 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2047 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2048 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
| 2049 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2050 | break; |
| 2051 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 2052 | rfc.mode = L2CAP_MODE_BASIC; |
| 2053 | rfc.txwin_size = 0; |
| 2054 | rfc.max_transmit = 0; |
| 2055 | rfc.retrans_timeout = 0; |
| 2056 | rfc.monitor_timeout = 0; |
| 2057 | rfc.max_pdu_size = 0; |
| 2058 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2059 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 2060 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2061 | break; |
| 2062 | |
| 2063 | case L2CAP_MODE_ERTM: |
| 2064 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2065 | rfc.max_transmit = chan->max_tx; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2066 | rfc.retrans_timeout = 0; |
| 2067 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2068 | |
| 2069 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
| 2070 | L2CAP_EXT_HDR_SIZE - |
| 2071 | L2CAP_SDULEN_SIZE - |
| 2072 | L2CAP_FCS_SIZE); |
| 2073 | rfc.max_pdu_size = cpu_to_le16(size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2074 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2075 | l2cap_txwin_setup(chan); |
| 2076 | |
| 2077 | rfc.txwin_size = min_t(u16, chan->tx_win, |
| 2078 | L2CAP_DEFAULT_TX_WINDOW); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2079 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2080 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 2081 | (unsigned long) &rfc); |
| 2082 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2083 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 2084 | l2cap_add_opt_efs(&ptr, chan); |
| 2085 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2086 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2087 | break; |
| 2088 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2089 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2090 | test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2091 | chan->fcs = L2CAP_FCS_NONE; |
| 2092 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2093 | } |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2094 | |
| 2095 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 2096 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
| 2097 | chan->tx_win); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2098 | break; |
| 2099 | |
| 2100 | case L2CAP_MODE_STREAMING: |
| 2101 | rfc.mode = L2CAP_MODE_STREAMING; |
| 2102 | rfc.txwin_size = 0; |
| 2103 | rfc.max_transmit = 0; |
| 2104 | rfc.retrans_timeout = 0; |
| 2105 | rfc.monitor_timeout = 0; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2106 | |
| 2107 | size = min_t(u16, L2CAP_DEFAULT_MAX_PDU_SIZE, chan->conn->mtu - |
| 2108 | L2CAP_EXT_HDR_SIZE - |
| 2109 | L2CAP_SDULEN_SIZE - |
| 2110 | L2CAP_FCS_SIZE); |
| 2111 | rfc.max_pdu_size = cpu_to_le16(size); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2112 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 2113 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 2114 | (unsigned long) &rfc); |
| 2115 | |
Andrei Emeltchenko | f89cef0 | 2011-10-13 16:18:55 +0300 | [diff] [blame] | 2116 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) |
| 2117 | l2cap_add_opt_efs(&ptr, chan); |
| 2118 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2119 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2120 | break; |
| 2121 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2122 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2123 | test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2124 | chan->fcs = L2CAP_FCS_NONE; |
| 2125 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2126 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 2127 | break; |
| 2128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2130 | req->dcid = cpu_to_le16(chan->dcid); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2131 | req->flags = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | |
| 2133 | return ptr - data; |
| 2134 | } |
| 2135 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2136 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2138 | struct l2cap_conf_rsp *rsp = data; |
| 2139 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2140 | void *req = chan->conf_req; |
| 2141 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2142 | int type, hint, olen; |
| 2143 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2144 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2145 | struct l2cap_conf_efs efs; |
| 2146 | u8 remote_efs = 0; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 2147 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2148 | u16 result = L2CAP_CONF_SUCCESS; |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2149 | u16 size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2151 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 2152 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2153 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2154 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 2156 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 2157 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2158 | |
| 2159 | switch (type) { |
| 2160 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 2161 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2162 | break; |
| 2163 | |
| 2164 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2165 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2166 | break; |
| 2167 | |
| 2168 | case L2CAP_CONF_QOS: |
| 2169 | break; |
| 2170 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2171 | case L2CAP_CONF_RFC: |
| 2172 | if (olen == sizeof(rfc)) |
| 2173 | memcpy(&rfc, (void *) val, olen); |
| 2174 | break; |
| 2175 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2176 | case L2CAP_CONF_FCS: |
| 2177 | if (val == L2CAP_FCS_NONE) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2178 | set_bit(CONF_NO_FCS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2179 | break; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2180 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2181 | case L2CAP_CONF_EFS: |
| 2182 | remote_efs = 1; |
| 2183 | if (olen == sizeof(efs)) |
| 2184 | memcpy(&efs, (void *) val, olen); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2185 | break; |
| 2186 | |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2187 | case L2CAP_CONF_EWS: |
| 2188 | if (!enable_hs) |
| 2189 | return -ECONNREFUSED; |
| 2190 | |
| 2191 | set_bit(FLAG_EXT_CTRL, &chan->flags); |
| 2192 | set_bit(CONF_EWS_RECV, &chan->conf_state); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 2193 | chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2194 | chan->remote_tx_win = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2195 | break; |
| 2196 | |
| 2197 | default: |
| 2198 | if (hint) |
| 2199 | break; |
| 2200 | |
| 2201 | result = L2CAP_CONF_UNKNOWN; |
| 2202 | *((u8 *) ptr++) = type; |
| 2203 | break; |
| 2204 | } |
| 2205 | } |
| 2206 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2207 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2208 | goto done; |
| 2209 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2210 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2211 | case L2CAP_MODE_STREAMING: |
| 2212 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2213 | if (!test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2214 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2215 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 2216 | break; |
| 2217 | } |
| 2218 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2219 | if (remote_efs) { |
| 2220 | if (__l2cap_efs_supported(chan)) |
| 2221 | set_bit(FLAG_EFS_ENABLE, &chan->flags); |
| 2222 | else |
| 2223 | return -ECONNREFUSED; |
| 2224 | } |
| 2225 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2226 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2227 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 2228 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2229 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2233 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2234 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2235 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2236 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2237 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2238 | return -ECONNREFUSED; |
| 2239 | |
| 2240 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2241 | sizeof(rfc), (unsigned long) &rfc); |
| 2242 | } |
| 2243 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2244 | if (result == L2CAP_CONF_SUCCESS) { |
| 2245 | /* Configure output options and let the other side know |
| 2246 | * which ones we don't like. */ |
| 2247 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2248 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 2249 | result = L2CAP_CONF_UNACCEPT; |
| 2250 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2251 | chan->omtu = mtu; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2252 | set_bit(CONF_MTU_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2253 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2254 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2255 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2256 | if (remote_efs) { |
| 2257 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
| 2258 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 2259 | efs.stype != chan->local_stype) { |
| 2260 | |
| 2261 | result = L2CAP_CONF_UNACCEPT; |
| 2262 | |
| 2263 | if (chan->num_conf_req >= 1) |
| 2264 | return -ECONNREFUSED; |
| 2265 | |
| 2266 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2267 | sizeof(efs), |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2268 | (unsigned long) &efs); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2269 | } else { |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2270 | /* Send PENDING Conf Rsp */ |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2271 | result = L2CAP_CONF_PENDING; |
| 2272 | set_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2273 | } |
| 2274 | } |
| 2275 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2276 | switch (rfc.mode) { |
| 2277 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2278 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2279 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2280 | break; |
| 2281 | |
| 2282 | case L2CAP_MODE_ERTM: |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2283 | if (!test_bit(CONF_EWS_RECV, &chan->conf_state)) |
| 2284 | chan->remote_tx_win = rfc.txwin_size; |
| 2285 | else |
| 2286 | rfc.txwin_size = L2CAP_DEFAULT_TX_WINDOW; |
| 2287 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 2288 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 2289 | |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2290 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
| 2291 | chan->conn->mtu - |
| 2292 | L2CAP_EXT_HDR_SIZE - |
| 2293 | L2CAP_SDULEN_SIZE - |
| 2294 | L2CAP_FCS_SIZE); |
| 2295 | rfc.max_pdu_size = cpu_to_le16(size); |
| 2296 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2297 | |
Gustavo F. Padovan | 10467e9 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 2298 | rfc.retrans_timeout = |
| 2299 | le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); |
| 2300 | rfc.monitor_timeout = |
| 2301 | le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2302 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2303 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 2304 | |
| 2305 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2306 | sizeof(rfc), (unsigned long) &rfc); |
| 2307 | |
Andrei Emeltchenko | 42dceae | 2011-10-17 14:35:30 +0300 | [diff] [blame] | 2308 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 2309 | chan->remote_id = efs.id; |
| 2310 | chan->remote_stype = efs.stype; |
| 2311 | chan->remote_msdu = le16_to_cpu(efs.msdu); |
| 2312 | chan->remote_flush_to = |
| 2313 | le32_to_cpu(efs.flush_to); |
| 2314 | chan->remote_acc_lat = |
| 2315 | le32_to_cpu(efs.acc_lat); |
| 2316 | chan->remote_sdu_itime = |
| 2317 | le32_to_cpu(efs.sdu_itime); |
| 2318 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
| 2319 | sizeof(efs), (unsigned long) &efs); |
| 2320 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2321 | break; |
| 2322 | |
| 2323 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | c8f7916 | 2011-10-17 12:19:59 +0300 | [diff] [blame] | 2324 | size = min_t(u16, le16_to_cpu(rfc.max_pdu_size), |
| 2325 | chan->conn->mtu - |
| 2326 | L2CAP_EXT_HDR_SIZE - |
| 2327 | L2CAP_SDULEN_SIZE - |
| 2328 | L2CAP_FCS_SIZE); |
| 2329 | rfc.max_pdu_size = cpu_to_le16(size); |
| 2330 | chan->remote_mps = size; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2331 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2332 | set_bit(CONF_MODE_DONE, &chan->conf_state); |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 2333 | |
| 2334 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2335 | sizeof(rfc), (unsigned long) &rfc); |
| 2336 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2337 | break; |
| 2338 | |
| 2339 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2340 | result = L2CAP_CONF_UNACCEPT; |
| 2341 | |
| 2342 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2343 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 2344 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2345 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2346 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2347 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2348 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2349 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2350 | rsp->result = cpu_to_le16(result); |
| 2351 | rsp->flags = cpu_to_le16(0x0000); |
| 2352 | |
| 2353 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2356 | 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] | 2357 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2358 | struct l2cap_conf_req *req = data; |
| 2359 | void *ptr = req->data; |
| 2360 | int type, olen; |
| 2361 | unsigned long val; |
| 2362 | struct l2cap_conf_rfc rfc; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2363 | struct l2cap_conf_efs efs; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2364 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2365 | 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] | 2366 | |
| 2367 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2368 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 2369 | |
| 2370 | switch (type) { |
| 2371 | case L2CAP_CONF_MTU: |
| 2372 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 2373 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2374 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2375 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2376 | chan->imtu = val; |
| 2377 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2378 | break; |
| 2379 | |
| 2380 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2381 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2382 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2383 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2384 | break; |
| 2385 | |
| 2386 | case L2CAP_CONF_RFC: |
| 2387 | if (olen == sizeof(rfc)) |
| 2388 | memcpy(&rfc, (void *)val, olen); |
| 2389 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2390 | if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state) && |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2391 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2392 | return -ECONNREFUSED; |
| 2393 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2394 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2395 | |
| 2396 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 2397 | sizeof(rfc), (unsigned long) &rfc); |
| 2398 | break; |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2399 | |
| 2400 | case L2CAP_CONF_EWS: |
| 2401 | chan->tx_win = min_t(u16, val, |
| 2402 | L2CAP_DEFAULT_EXT_WINDOW); |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2403 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EWS, 2, |
| 2404 | chan->tx_win); |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 2405 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2406 | |
| 2407 | case L2CAP_CONF_EFS: |
| 2408 | if (olen == sizeof(efs)) |
| 2409 | memcpy(&efs, (void *)val, olen); |
| 2410 | |
| 2411 | if (chan->local_stype != L2CAP_SERV_NOTRAFIC && |
| 2412 | efs.stype != L2CAP_SERV_NOTRAFIC && |
| 2413 | efs.stype != chan->local_stype) |
| 2414 | return -ECONNREFUSED; |
| 2415 | |
| 2416 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_EFS, |
| 2417 | sizeof(efs), (unsigned long) &efs); |
| 2418 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2419 | } |
| 2420 | } |
| 2421 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2422 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 2423 | return -ECONNREFUSED; |
| 2424 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2425 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 2426 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2427 | if (*result == L2CAP_CONF_SUCCESS || *result == L2CAP_CONF_PENDING) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2428 | switch (rfc.mode) { |
| 2429 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2430 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 2431 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 2432 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2433 | |
| 2434 | if (test_bit(FLAG_EFS_ENABLE, &chan->flags)) { |
| 2435 | chan->local_msdu = le16_to_cpu(efs.msdu); |
| 2436 | chan->local_sdu_itime = |
| 2437 | le32_to_cpu(efs.sdu_itime); |
| 2438 | chan->local_acc_lat = le32_to_cpu(efs.acc_lat); |
| 2439 | chan->local_flush_to = |
| 2440 | le32_to_cpu(efs.flush_to); |
| 2441 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2442 | break; |
Andrei Emeltchenko | 66af7aa | 2011-11-07 14:20:33 +0200 | [diff] [blame] | 2443 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2444 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2445 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2446 | } |
| 2447 | } |
| 2448 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2449 | req->dcid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2450 | req->flags = cpu_to_le16(0x0000); |
| 2451 | |
| 2452 | return ptr - data; |
| 2453 | } |
| 2454 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2455 | 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] | 2456 | { |
| 2457 | struct l2cap_conf_rsp *rsp = data; |
| 2458 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2460 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2462 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2463 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2464 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | |
| 2466 | return ptr - data; |
| 2467 | } |
| 2468 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2469 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2470 | { |
| 2471 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2472 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2473 | u8 buf[128]; |
| 2474 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2475 | rsp.scid = cpu_to_le16(chan->dcid); |
| 2476 | rsp.dcid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2477 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 2478 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 2479 | l2cap_send_cmd(conn, chan->ident, |
| 2480 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 2481 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2482 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2483 | return; |
| 2484 | |
Gustavo F. Padovan | 710f9b0a | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2485 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
| 2486 | l2cap_build_conf_req(chan, buf), buf); |
| 2487 | chan->num_conf_req++; |
| 2488 | } |
| 2489 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2490 | 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] | 2491 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2492 | int type, olen; |
| 2493 | unsigned long val; |
| 2494 | struct l2cap_conf_rfc rfc; |
| 2495 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2496 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2497 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2498 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2499 | return; |
| 2500 | |
| 2501 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2502 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 2503 | |
| 2504 | switch (type) { |
| 2505 | case L2CAP_CONF_RFC: |
| 2506 | if (olen == sizeof(rfc)) |
| 2507 | memcpy(&rfc, (void *)val, olen); |
| 2508 | goto done; |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | done: |
| 2513 | switch (rfc.mode) { |
| 2514 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2515 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 2516 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 2517 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2518 | break; |
| 2519 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2520 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2521 | } |
| 2522 | } |
| 2523 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2524 | static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2525 | { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2526 | struct l2cap_cmd_rej_unk *rej = (struct l2cap_cmd_rej_unk *) data; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2527 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2528 | if (rej->reason != L2CAP_REJ_NOT_UNDERSTOOD) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2529 | return 0; |
| 2530 | |
| 2531 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
| 2532 | cmd->ident == conn->info_ident) { |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame^] | 2533 | cancel_delayed_work_sync(&conn->info_work); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2534 | |
| 2535 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2536 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2537 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2538 | l2cap_conn_start(conn); |
| 2539 | } |
| 2540 | |
| 2541 | return 0; |
| 2542 | } |
| 2543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2545 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2546 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 2547 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2548 | struct l2cap_chan *chan = NULL, *pchan; |
Nathan Holstein | d793fe8 | 2010-10-15 11:54:02 -0400 | [diff] [blame] | 2549 | struct sock *parent, *sk = NULL; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2550 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | |
| 2552 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2553 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | |
| 2555 | BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); |
| 2556 | |
| 2557 | /* Check if we have socket listening on psm */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2558 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); |
| 2559 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | result = L2CAP_CR_BAD_PSM; |
| 2561 | goto sendresp; |
| 2562 | } |
| 2563 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2564 | parent = pchan->sk; |
| 2565 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2566 | lock_sock(parent); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 2567 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2568 | /* Check if the ACL is secure enough (if not SDP) */ |
| 2569 | if (psm != cpu_to_le16(0x0001) && |
| 2570 | !hci_conn_check_link_mode(conn->hcon)) { |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 2571 | conn->disc_reason = HCI_ERROR_AUTH_FAILURE; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2572 | result = L2CAP_CR_SEC_BLOCK; |
| 2573 | goto response; |
| 2574 | } |
| 2575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2576 | result = L2CAP_CR_NO_MEM; |
| 2577 | |
| 2578 | /* Check for backlog size */ |
| 2579 | if (sk_acceptq_is_full(parent)) { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2580 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | goto response; |
| 2582 | } |
| 2583 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 2584 | chan = pchan->ops->new_connection(pchan->data); |
| 2585 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2586 | goto response; |
| 2587 | |
Gustavo F. Padovan | 80808e4 | 2011-05-16 17:24:37 -0300 | [diff] [blame] | 2588 | sk = chan->sk; |
| 2589 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 2590 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2591 | |
| 2592 | /* Check if we already have channel with that dcid */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2593 | if (__l2cap_get_chan_by_dcid(conn, scid)) { |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 2594 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | sock_set_flag(sk, SOCK_ZAPPED); |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 2596 | chan->ops->close(chan->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | goto response; |
| 2598 | } |
| 2599 | |
| 2600 | hci_conn_hold(conn->hcon); |
| 2601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | bacpy(&bt_sk(sk)->src, conn->src); |
| 2603 | bacpy(&bt_sk(sk)->dst, conn->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2604 | chan->psm = psm; |
| 2605 | chan->dcid = scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 2607 | bt_accept_enqueue(parent, sk); |
| 2608 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2609 | __l2cap_chan_add(conn, chan); |
| 2610 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2611 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 2613 | __set_chan_timer(chan, sk->sk_sndtimeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2615 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2617 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | d45fc42 | 2011-11-05 19:54:24 -0200 | [diff] [blame] | 2618 | if (l2cap_chan_check_security(chan)) { |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2619 | if (bt_sk(sk)->defer_setup) { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2620 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2621 | result = L2CAP_CR_PEND; |
| 2622 | status = L2CAP_CS_AUTHOR_PEND; |
| 2623 | parent->sk_data_ready(parent, 0); |
| 2624 | } else { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2625 | l2cap_state_change(chan, BT_CONFIG); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2626 | result = L2CAP_CR_SUCCESS; |
| 2627 | status = L2CAP_CS_NO_INFO; |
| 2628 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2629 | } else { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2630 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2631 | result = L2CAP_CR_PEND; |
| 2632 | status = L2CAP_CS_AUTHEN_PEND; |
| 2633 | } |
| 2634 | } else { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2635 | l2cap_state_change(chan, BT_CONNECT2); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2636 | result = L2CAP_CR_PEND; |
| 2637 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 2640 | mutex_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | |
| 2642 | response: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2643 | release_sock(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | |
| 2645 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2646 | rsp.scid = cpu_to_le16(scid); |
| 2647 | rsp.dcid = cpu_to_le16(dcid); |
| 2648 | rsp.result = cpu_to_le16(result); |
| 2649 | rsp.status = cpu_to_le16(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2651 | |
| 2652 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 2653 | struct l2cap_info_req info; |
| 2654 | info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 2655 | |
| 2656 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 2657 | conn->info_ident = l2cap_get_ident(conn); |
| 2658 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame^] | 2659 | schedule_delayed_work(&conn->info_work, |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2660 | msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); |
| 2661 | |
| 2662 | l2cap_send_cmd(conn, conn->info_ident, |
| 2663 | L2CAP_INFO_REQ, sizeof(info), &info); |
| 2664 | } |
| 2665 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2666 | if (chan && !test_bit(CONF_REQ_SENT, &chan->conf_state) && |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2667 | result == L2CAP_CR_SUCCESS) { |
| 2668 | u8 buf[128]; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2669 | set_bit(CONF_REQ_SENT, &chan->conf_state); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2670 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2671 | l2cap_build_conf_req(chan, buf), buf); |
| 2672 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2673 | } |
| 2674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | return 0; |
| 2676 | } |
| 2677 | |
| 2678 | static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2679 | { |
| 2680 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 2681 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2682 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | struct sock *sk; |
| 2684 | u8 req[128]; |
| 2685 | |
| 2686 | scid = __le16_to_cpu(rsp->scid); |
| 2687 | dcid = __le16_to_cpu(rsp->dcid); |
| 2688 | result = __le16_to_cpu(rsp->result); |
| 2689 | status = __le16_to_cpu(rsp->status); |
| 2690 | |
| 2691 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); |
| 2692 | |
| 2693 | if (scid) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2694 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2695 | if (!chan) |
João Paulo Rechi Vita | 57d3b22 | 2010-06-22 13:56:26 -0300 | [diff] [blame] | 2696 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | } else { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2698 | chan = l2cap_get_chan_by_ident(conn, cmd->ident); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2699 | if (!chan) |
João Paulo Rechi Vita | 57d3b22 | 2010-06-22 13:56:26 -0300 | [diff] [blame] | 2700 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2703 | sk = chan->sk; |
| 2704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | switch (result) { |
| 2706 | case L2CAP_CR_SUCCESS: |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2707 | l2cap_state_change(chan, BT_CONFIG); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2708 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2709 | chan->dcid = dcid; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2710 | clear_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 2711 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2712 | if (test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2713 | break; |
| 2714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2716 | l2cap_build_conf_req(chan, req), req); |
| 2717 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2718 | break; |
| 2719 | |
| 2720 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2721 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | break; |
| 2723 | |
| 2724 | default: |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2725 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | break; |
| 2727 | } |
| 2728 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2729 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | return 0; |
| 2731 | } |
| 2732 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2733 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2734 | { |
| 2735 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 2736 | * sides request it. |
| 2737 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2738 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2739 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2740 | else if (!test_bit(CONF_NO_FCS_RECV, &chan->conf_state)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2741 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2742 | } |
| 2743 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2744 | 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] | 2745 | { |
| 2746 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 2747 | u16 dcid, flags; |
| 2748 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2749 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | struct sock *sk; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2751 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | |
| 2753 | dcid = __le16_to_cpu(req->dcid); |
| 2754 | flags = __le16_to_cpu(req->flags); |
| 2755 | |
| 2756 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 2757 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2758 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2759 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | return -ENOENT; |
| 2761 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2762 | sk = chan->sk; |
| 2763 | |
David S. Miller | 033b114 | 2011-07-21 13:38:42 -0700 | [diff] [blame] | 2764 | if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2765 | struct l2cap_cmd_rej_cid rej; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2766 | |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 2767 | rej.reason = cpu_to_le16(L2CAP_REJ_INVALID_CID); |
| 2768 | rej.scid = cpu_to_le16(chan->scid); |
| 2769 | rej.dcid = cpu_to_le16(chan->dcid); |
| 2770 | |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2771 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 2772 | sizeof(rej), &rej); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2773 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2774 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2775 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2776 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2777 | len = cmd_len - sizeof(*req); |
Dan Rosenberg | 7ac2881 | 2011-06-24 08:38:05 -0400 | [diff] [blame] | 2778 | if (len < 0 || chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2779 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2780 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2781 | L2CAP_CONF_REJECT, flags), rsp); |
| 2782 | goto unlock; |
| 2783 | } |
| 2784 | |
| 2785 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2786 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 2787 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | |
| 2789 | if (flags & 0x0001) { |
| 2790 | /* Incomplete config. Send empty response. */ |
| 2791 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2792 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2793 | L2CAP_CONF_SUCCESS, 0x0001), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | goto unlock; |
| 2795 | } |
| 2796 | |
| 2797 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2798 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2799 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2800 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2802 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2804 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2805 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2806 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2807 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2808 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2809 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2810 | if (!test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2811 | goto unlock; |
| 2812 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2813 | if (test_bit(CONF_INPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2814 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2815 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2816 | l2cap_state_change(chan, BT_CONNECTED); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2817 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2818 | chan->next_tx_seq = 0; |
| 2819 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2820 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2821 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2822 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | l2cap_chan_ready(sk); |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2825 | goto unlock; |
| 2826 | } |
| 2827 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2828 | if (!test_and_set_bit(CONF_REQ_SENT, &chan->conf_state)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2829 | u8 buf[64]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2831 | l2cap_build_conf_req(chan, buf), buf); |
| 2832 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | } |
| 2834 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2835 | /* Got Conf Rsp PENDING from remote side and asume we sent |
| 2836 | Conf Rsp PENDING in the code above */ |
| 2837 | if (test_bit(CONF_REM_CONF_PEND, &chan->conf_state) && |
| 2838 | test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 2839 | |
| 2840 | /* check compatibility */ |
| 2841 | |
| 2842 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 2843 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 2844 | |
| 2845 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2846 | l2cap_build_conf_rsp(chan, rsp, |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2847 | L2CAP_CONF_SUCCESS, 0x0000), rsp); |
| 2848 | } |
| 2849 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | unlock: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2851 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | return 0; |
| 2853 | } |
| 2854 | |
| 2855 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2856 | { |
| 2857 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 2858 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2859 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | struct sock *sk; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2861 | int len = cmd->len - sizeof(*rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | |
| 2863 | scid = __le16_to_cpu(rsp->scid); |
| 2864 | flags = __le16_to_cpu(rsp->flags); |
| 2865 | result = __le16_to_cpu(rsp->result); |
| 2866 | |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2867 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", |
| 2868 | scid, flags, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2870 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2871 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2872 | return 0; |
| 2873 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2874 | sk = chan->sk; |
| 2875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2876 | switch (result) { |
| 2877 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2878 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2879 | clear_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2880 | break; |
| 2881 | |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2882 | case L2CAP_CONF_PENDING: |
| 2883 | set_bit(CONF_REM_CONF_PEND, &chan->conf_state); |
| 2884 | |
| 2885 | if (test_bit(CONF_LOC_CONF_PEND, &chan->conf_state)) { |
| 2886 | char buf[64]; |
| 2887 | |
| 2888 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
| 2889 | buf, &result); |
| 2890 | if (len < 0) { |
| 2891 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
| 2892 | goto done; |
| 2893 | } |
| 2894 | |
| 2895 | /* check compatibility */ |
| 2896 | |
| 2897 | clear_bit(CONF_LOC_CONF_PEND, &chan->conf_state); |
| 2898 | set_bit(CONF_OUTPUT_DONE, &chan->conf_state); |
| 2899 | |
| 2900 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | 3e6b3b9 | 2011-11-01 14:06:23 -0200 | [diff] [blame] | 2901 | l2cap_build_conf_rsp(chan, buf, |
Andrei Emeltchenko | 0e8b207 | 2011-10-17 14:35:32 +0300 | [diff] [blame] | 2902 | L2CAP_CONF_SUCCESS, 0x0000), buf); |
| 2903 | } |
| 2904 | goto done; |
| 2905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2907 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2908 | char req[64]; |
| 2909 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 2910 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2911 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 2912 | goto done; |
| 2913 | } |
| 2914 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2915 | /* throw out any old stored conf requests */ |
| 2916 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2917 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
| 2918 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2919 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2920 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2921 | goto done; |
| 2922 | } |
| 2923 | |
| 2924 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 2925 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2926 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2927 | if (result != L2CAP_CONF_SUCCESS) |
| 2928 | goto done; |
| 2929 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | } |
| 2931 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2932 | default: |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 2933 | sk->sk_err = ECONNRESET; |
Andrzej Kaczmarek | f3f668b | 2011-11-07 17:19:04 -0200 | [diff] [blame] | 2934 | __set_chan_timer(chan, L2CAP_DISC_REJ_TIMEOUT); |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2935 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | goto done; |
| 2937 | } |
| 2938 | |
| 2939 | if (flags & 0x01) |
| 2940 | goto done; |
| 2941 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2942 | set_bit(CONF_INPUT_DONE, &chan->conf_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 2944 | if (test_bit(CONF_OUTPUT_DONE, &chan->conf_state)) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2945 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2946 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 2947 | l2cap_state_change(chan, BT_CONNECTED); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2948 | chan->next_tx_seq = 0; |
| 2949 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2950 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2951 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2952 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | l2cap_chan_ready(sk); |
| 2955 | } |
| 2956 | |
| 2957 | done: |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2958 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | return 0; |
| 2960 | } |
| 2961 | |
| 2962 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2963 | { |
| 2964 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 2965 | struct l2cap_disconn_rsp rsp; |
| 2966 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2967 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | struct sock *sk; |
| 2969 | |
| 2970 | scid = __le16_to_cpu(req->scid); |
| 2971 | dcid = __le16_to_cpu(req->dcid); |
| 2972 | |
| 2973 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 2974 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2975 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2976 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | return 0; |
| 2978 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2979 | sk = chan->sk; |
| 2980 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2981 | rsp.dcid = cpu_to_le16(chan->scid); |
| 2982 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 2984 | |
| 2985 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 2986 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2987 | l2cap_chan_del(chan, ECONNRESET); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 2988 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 2990 | chan->ops->close(chan->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2991 | return 0; |
| 2992 | } |
| 2993 | |
| 2994 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2995 | { |
| 2996 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 2997 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2998 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | struct sock *sk; |
| 3000 | |
| 3001 | scid = __le16_to_cpu(rsp->scid); |
| 3002 | dcid = __le16_to_cpu(rsp->dcid); |
| 3003 | |
| 3004 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 3005 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 3006 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3007 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | return 0; |
| 3009 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3010 | sk = chan->sk; |
| 3011 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3012 | l2cap_chan_del(chan, 0); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 3013 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | |
Gustavo F. Padovan | ba3bd0e | 2011-05-16 18:23:24 -0300 | [diff] [blame] | 3015 | chan->ops->close(chan->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | return 0; |
| 3017 | } |
| 3018 | |
| 3019 | static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3020 | { |
| 3021 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | u16 type; |
| 3023 | |
| 3024 | type = __le16_to_cpu(req->type); |
| 3025 | |
| 3026 | BT_DBG("type 0x%4.4x", type); |
| 3027 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3028 | if (type == L2CAP_IT_FEAT_MASK) { |
| 3029 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 3030 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3031 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
| 3032 | rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 3033 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 3034 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3035 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
| 3036 | | L2CAP_FEAT_FCS; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 3037 | if (enable_hs) |
Andrei Emeltchenko | 6327eb9 | 2011-10-11 13:37:42 +0300 | [diff] [blame] | 3038 | feat_mask |= L2CAP_FEAT_EXT_FLOW |
| 3039 | | L2CAP_FEAT_EXT_WINDOW; |
Andrei Emeltchenko | a5fd6f3 | 2011-09-16 16:26:32 +0300 | [diff] [blame] | 3040 | |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 3041 | put_unaligned_le32(feat_mask, rsp->data); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3042 | l2cap_send_cmd(conn, cmd->ident, |
| 3043 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3044 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 3045 | u8 buf[12]; |
| 3046 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
Mat Martineau | 50a147c | 2011-11-02 16:18:34 -0700 | [diff] [blame] | 3047 | |
| 3048 | if (enable_hs) |
| 3049 | l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; |
| 3050 | else |
| 3051 | l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; |
| 3052 | |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3053 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 3054 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Andrei Emeltchenko | c6337ea | 2011-10-20 17:02:44 +0300 | [diff] [blame] | 3055 | memcpy(rsp->data, l2cap_fixed_chan, sizeof(l2cap_fixed_chan)); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3056 | l2cap_send_cmd(conn, cmd->ident, |
| 3057 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 3058 | } else { |
| 3059 | struct l2cap_info_rsp rsp; |
| 3060 | rsp.type = cpu_to_le16(type); |
| 3061 | rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); |
| 3062 | l2cap_send_cmd(conn, cmd->ident, |
| 3063 | L2CAP_INFO_RSP, sizeof(rsp), &rsp); |
| 3064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3065 | |
| 3066 | return 0; |
| 3067 | } |
| 3068 | |
| 3069 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3070 | { |
| 3071 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 3072 | u16 type, result; |
| 3073 | |
| 3074 | type = __le16_to_cpu(rsp->type); |
| 3075 | result = __le16_to_cpu(rsp->result); |
| 3076 | |
| 3077 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 3078 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 3079 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 3080 | if (cmd->ident != conn->info_ident || |
| 3081 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
| 3082 | return 0; |
| 3083 | |
Gustavo F. Padovan | f878fca | 2011-12-15 01:16:14 -0200 | [diff] [blame^] | 3084 | cancel_delayed_work_sync(&conn->info_work); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3085 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 3086 | if (result != L2CAP_IR_SUCCESS) { |
| 3087 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 3088 | conn->info_ident = 0; |
| 3089 | |
| 3090 | l2cap_conn_start(conn); |
| 3091 | |
| 3092 | return 0; |
| 3093 | } |
| 3094 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3095 | if (type == L2CAP_IT_FEAT_MASK) { |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 3096 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3097 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 3098 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3099 | struct l2cap_info_req req; |
| 3100 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 3101 | |
| 3102 | conn->info_ident = l2cap_get_ident(conn); |
| 3103 | |
| 3104 | l2cap_send_cmd(conn, conn->info_ident, |
| 3105 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 3106 | } else { |
| 3107 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 3108 | conn->info_ident = 0; |
| 3109 | |
| 3110 | l2cap_conn_start(conn); |
| 3111 | } |
| 3112 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3113 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 3114 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 3115 | |
| 3116 | l2cap_conn_start(conn); |
| 3117 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 3118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | return 0; |
| 3120 | } |
| 3121 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 3122 | static inline int l2cap_create_channel_req(struct l2cap_conn *conn, |
| 3123 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, |
| 3124 | void *data) |
| 3125 | { |
| 3126 | struct l2cap_create_chan_req *req = data; |
| 3127 | struct l2cap_create_chan_rsp rsp; |
| 3128 | u16 psm, scid; |
| 3129 | |
| 3130 | if (cmd_len != sizeof(*req)) |
| 3131 | return -EPROTO; |
| 3132 | |
| 3133 | if (!enable_hs) |
| 3134 | return -EINVAL; |
| 3135 | |
| 3136 | psm = le16_to_cpu(req->psm); |
| 3137 | scid = le16_to_cpu(req->scid); |
| 3138 | |
| 3139 | BT_DBG("psm %d, scid %d, amp_id %d", psm, scid, req->amp_id); |
| 3140 | |
| 3141 | /* Placeholder: Always reject */ |
| 3142 | rsp.dcid = 0; |
| 3143 | rsp.scid = cpu_to_le16(scid); |
| 3144 | rsp.result = L2CAP_CR_NO_MEM; |
| 3145 | rsp.status = L2CAP_CS_NO_INFO; |
| 3146 | |
| 3147 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, |
| 3148 | sizeof(rsp), &rsp); |
| 3149 | |
| 3150 | return 0; |
| 3151 | } |
| 3152 | |
| 3153 | static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn, |
| 3154 | struct l2cap_cmd_hdr *cmd, void *data) |
| 3155 | { |
| 3156 | BT_DBG("conn %p", conn); |
| 3157 | |
| 3158 | return l2cap_connect_rsp(conn, cmd, data); |
| 3159 | } |
| 3160 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 3161 | static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident, |
| 3162 | u16 icid, u16 result) |
| 3163 | { |
| 3164 | struct l2cap_move_chan_rsp rsp; |
| 3165 | |
| 3166 | BT_DBG("icid %d, result %d", icid, result); |
| 3167 | |
| 3168 | rsp.icid = cpu_to_le16(icid); |
| 3169 | rsp.result = cpu_to_le16(result); |
| 3170 | |
| 3171 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp); |
| 3172 | } |
| 3173 | |
| 3174 | static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn, |
| 3175 | struct l2cap_chan *chan, u16 icid, u16 result) |
| 3176 | { |
| 3177 | struct l2cap_move_chan_cfm cfm; |
| 3178 | u8 ident; |
| 3179 | |
| 3180 | BT_DBG("icid %d, result %d", icid, result); |
| 3181 | |
| 3182 | ident = l2cap_get_ident(conn); |
| 3183 | if (chan) |
| 3184 | chan->ident = ident; |
| 3185 | |
| 3186 | cfm.icid = cpu_to_le16(icid); |
| 3187 | cfm.result = cpu_to_le16(result); |
| 3188 | |
| 3189 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm); |
| 3190 | } |
| 3191 | |
| 3192 | static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident, |
| 3193 | u16 icid) |
| 3194 | { |
| 3195 | struct l2cap_move_chan_cfm_rsp rsp; |
| 3196 | |
| 3197 | BT_DBG("icid %d", icid); |
| 3198 | |
| 3199 | rsp.icid = cpu_to_le16(icid); |
| 3200 | l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp); |
| 3201 | } |
| 3202 | |
| 3203 | static inline int l2cap_move_channel_req(struct l2cap_conn *conn, |
| 3204 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3205 | { |
| 3206 | struct l2cap_move_chan_req *req = data; |
| 3207 | u16 icid = 0; |
| 3208 | u16 result = L2CAP_MR_NOT_ALLOWED; |
| 3209 | |
| 3210 | if (cmd_len != sizeof(*req)) |
| 3211 | return -EPROTO; |
| 3212 | |
| 3213 | icid = le16_to_cpu(req->icid); |
| 3214 | |
| 3215 | BT_DBG("icid %d, dest_amp_id %d", icid, req->dest_amp_id); |
| 3216 | |
| 3217 | if (!enable_hs) |
| 3218 | return -EINVAL; |
| 3219 | |
| 3220 | /* Placeholder: Always refuse */ |
| 3221 | l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result); |
| 3222 | |
| 3223 | return 0; |
| 3224 | } |
| 3225 | |
| 3226 | static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn, |
| 3227 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3228 | { |
| 3229 | struct l2cap_move_chan_rsp *rsp = data; |
| 3230 | u16 icid, result; |
| 3231 | |
| 3232 | if (cmd_len != sizeof(*rsp)) |
| 3233 | return -EPROTO; |
| 3234 | |
| 3235 | icid = le16_to_cpu(rsp->icid); |
| 3236 | result = le16_to_cpu(rsp->result); |
| 3237 | |
| 3238 | BT_DBG("icid %d, result %d", icid, result); |
| 3239 | |
| 3240 | /* Placeholder: Always unconfirmed */ |
| 3241 | l2cap_send_move_chan_cfm(conn, NULL, icid, L2CAP_MC_UNCONFIRMED); |
| 3242 | |
| 3243 | return 0; |
| 3244 | } |
| 3245 | |
| 3246 | static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn, |
| 3247 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3248 | { |
| 3249 | struct l2cap_move_chan_cfm *cfm = data; |
| 3250 | u16 icid, result; |
| 3251 | |
| 3252 | if (cmd_len != sizeof(*cfm)) |
| 3253 | return -EPROTO; |
| 3254 | |
| 3255 | icid = le16_to_cpu(cfm->icid); |
| 3256 | result = le16_to_cpu(cfm->result); |
| 3257 | |
| 3258 | BT_DBG("icid %d, result %d", icid, result); |
| 3259 | |
| 3260 | l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid); |
| 3261 | |
| 3262 | return 0; |
| 3263 | } |
| 3264 | |
| 3265 | static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn, |
| 3266 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, void *data) |
| 3267 | { |
| 3268 | struct l2cap_move_chan_cfm_rsp *rsp = data; |
| 3269 | u16 icid; |
| 3270 | |
| 3271 | if (cmd_len != sizeof(*rsp)) |
| 3272 | return -EPROTO; |
| 3273 | |
| 3274 | icid = le16_to_cpu(rsp->icid); |
| 3275 | |
| 3276 | BT_DBG("icid %d", icid); |
| 3277 | |
| 3278 | return 0; |
| 3279 | } |
| 3280 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 3281 | 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] | 3282 | u16 to_multiplier) |
| 3283 | { |
| 3284 | u16 max_latency; |
| 3285 | |
| 3286 | if (min > max || min < 6 || max > 3200) |
| 3287 | return -EINVAL; |
| 3288 | |
| 3289 | if (to_multiplier < 10 || to_multiplier > 3200) |
| 3290 | return -EINVAL; |
| 3291 | |
| 3292 | if (max >= to_multiplier * 8) |
| 3293 | return -EINVAL; |
| 3294 | |
| 3295 | max_latency = (to_multiplier * 8 / max) - 1; |
| 3296 | if (latency > 499 || latency > max_latency) |
| 3297 | return -EINVAL; |
| 3298 | |
| 3299 | return 0; |
| 3300 | } |
| 3301 | |
| 3302 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
| 3303 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3304 | { |
| 3305 | struct hci_conn *hcon = conn->hcon; |
| 3306 | struct l2cap_conn_param_update_req *req; |
| 3307 | struct l2cap_conn_param_update_rsp rsp; |
| 3308 | u16 min, max, latency, to_multiplier, cmd_len; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 3309 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3310 | |
| 3311 | if (!(hcon->link_mode & HCI_LM_MASTER)) |
| 3312 | return -EINVAL; |
| 3313 | |
| 3314 | cmd_len = __le16_to_cpu(cmd->len); |
| 3315 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 3316 | return -EPROTO; |
| 3317 | |
| 3318 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 3319 | min = __le16_to_cpu(req->min); |
| 3320 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3321 | latency = __le16_to_cpu(req->latency); |
| 3322 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 3323 | |
| 3324 | BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", |
| 3325 | min, max, latency, to_multiplier); |
| 3326 | |
| 3327 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 3328 | |
| 3329 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); |
| 3330 | if (err) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3331 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
| 3332 | else |
| 3333 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
| 3334 | |
| 3335 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
| 3336 | sizeof(rsp), &rsp); |
| 3337 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 3338 | if (!err) |
| 3339 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); |
| 3340 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3341 | return 0; |
| 3342 | } |
| 3343 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3344 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
| 3345 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
| 3346 | { |
| 3347 | int err = 0; |
| 3348 | |
| 3349 | switch (cmd->code) { |
| 3350 | case L2CAP_COMMAND_REJ: |
| 3351 | l2cap_command_rej(conn, cmd, data); |
| 3352 | break; |
| 3353 | |
| 3354 | case L2CAP_CONN_REQ: |
| 3355 | err = l2cap_connect_req(conn, cmd, data); |
| 3356 | break; |
| 3357 | |
| 3358 | case L2CAP_CONN_RSP: |
| 3359 | err = l2cap_connect_rsp(conn, cmd, data); |
| 3360 | break; |
| 3361 | |
| 3362 | case L2CAP_CONF_REQ: |
| 3363 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 3364 | break; |
| 3365 | |
| 3366 | case L2CAP_CONF_RSP: |
| 3367 | err = l2cap_config_rsp(conn, cmd, data); |
| 3368 | break; |
| 3369 | |
| 3370 | case L2CAP_DISCONN_REQ: |
| 3371 | err = l2cap_disconnect_req(conn, cmd, data); |
| 3372 | break; |
| 3373 | |
| 3374 | case L2CAP_DISCONN_RSP: |
| 3375 | err = l2cap_disconnect_rsp(conn, cmd, data); |
| 3376 | break; |
| 3377 | |
| 3378 | case L2CAP_ECHO_REQ: |
| 3379 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 3380 | break; |
| 3381 | |
| 3382 | case L2CAP_ECHO_RSP: |
| 3383 | break; |
| 3384 | |
| 3385 | case L2CAP_INFO_REQ: |
| 3386 | err = l2cap_information_req(conn, cmd, data); |
| 3387 | break; |
| 3388 | |
| 3389 | case L2CAP_INFO_RSP: |
| 3390 | err = l2cap_information_rsp(conn, cmd, data); |
| 3391 | break; |
| 3392 | |
Mat Martineau | f94ff6f | 2011-11-02 16:18:32 -0700 | [diff] [blame] | 3393 | case L2CAP_CREATE_CHAN_REQ: |
| 3394 | err = l2cap_create_channel_req(conn, cmd, cmd_len, data); |
| 3395 | break; |
| 3396 | |
| 3397 | case L2CAP_CREATE_CHAN_RSP: |
| 3398 | err = l2cap_create_channel_rsp(conn, cmd, data); |
| 3399 | break; |
| 3400 | |
Mat Martineau | 8d5a04a | 2011-11-02 16:18:35 -0700 | [diff] [blame] | 3401 | case L2CAP_MOVE_CHAN_REQ: |
| 3402 | err = l2cap_move_channel_req(conn, cmd, cmd_len, data); |
| 3403 | break; |
| 3404 | |
| 3405 | case L2CAP_MOVE_CHAN_RSP: |
| 3406 | err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); |
| 3407 | break; |
| 3408 | |
| 3409 | case L2CAP_MOVE_CHAN_CFM: |
| 3410 | err = l2cap_move_channel_confirm(conn, cmd, cmd_len, data); |
| 3411 | break; |
| 3412 | |
| 3413 | case L2CAP_MOVE_CHAN_CFM_RSP: |
| 3414 | err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); |
| 3415 | break; |
| 3416 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3417 | default: |
| 3418 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 3419 | err = -EINVAL; |
| 3420 | break; |
| 3421 | } |
| 3422 | |
| 3423 | return err; |
| 3424 | } |
| 3425 | |
| 3426 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
| 3427 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 3428 | { |
| 3429 | switch (cmd->code) { |
| 3430 | case L2CAP_COMMAND_REJ: |
| 3431 | return 0; |
| 3432 | |
| 3433 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 3434 | return l2cap_conn_param_update_req(conn, cmd, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3435 | |
| 3436 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
| 3437 | return 0; |
| 3438 | |
| 3439 | default: |
| 3440 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
| 3441 | return -EINVAL; |
| 3442 | } |
| 3443 | } |
| 3444 | |
| 3445 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
| 3446 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | { |
| 3448 | u8 *data = skb->data; |
| 3449 | int len = skb->len; |
| 3450 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3451 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | |
| 3453 | l2cap_raw_recv(conn, skb); |
| 3454 | |
| 3455 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3456 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 3458 | data += L2CAP_CMD_HDR_SIZE; |
| 3459 | len -= L2CAP_CMD_HDR_SIZE; |
| 3460 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3461 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3463 | 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] | 3464 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3465 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | BT_DBG("corrupted command"); |
| 3467 | break; |
| 3468 | } |
| 3469 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3470 | if (conn->hcon->type == LE_LINK) |
| 3471 | err = l2cap_le_sig_cmd(conn, &cmd, data); |
| 3472 | else |
| 3473 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | |
| 3475 | if (err) { |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3476 | struct l2cap_cmd_rej_unk rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 3477 | |
| 3478 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3479 | |
| 3480 | /* FIXME: Map err to a valid reason */ |
Ilia Kolomisnky | e2fd318 | 2011-07-10 08:47:44 +0300 | [diff] [blame] | 3481 | rej.reason = cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3482 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); |
| 3483 | } |
| 3484 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 3485 | data += cmd_len; |
| 3486 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | } |
| 3488 | |
| 3489 | kfree_skb(skb); |
| 3490 | } |
| 3491 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3492 | 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] | 3493 | { |
| 3494 | u16 our_fcs, rcv_fcs; |
Andrei Emeltchenko | e4ca6d9 | 2011-10-11 13:37:52 +0300 | [diff] [blame] | 3495 | int hdr_size; |
| 3496 | |
| 3497 | if (test_bit(FLAG_EXT_CTRL, &chan->flags)) |
| 3498 | hdr_size = L2CAP_EXT_HDR_SIZE; |
| 3499 | else |
| 3500 | hdr_size = L2CAP_ENH_HDR_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3501 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3502 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 3503 | skb_trim(skb, skb->len - L2CAP_FCS_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3504 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 3505 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 3506 | |
| 3507 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 3508 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3509 | } |
| 3510 | return 0; |
| 3511 | } |
| 3512 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3513 | 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] | 3514 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3515 | u32 control = 0; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3516 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3517 | chan->frames_sent = 0; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3518 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3519 | control |= __set_reqseq(chan, chan->buffer_seq); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3520 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3521 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3522 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3523 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3524 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3525 | } |
| 3526 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3527 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3528 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3529 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3530 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3531 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3532 | if (!test_bit(CONN_LOCAL_BUSY, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3533 | chan->frames_sent == 0) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3534 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3535 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 3536 | } |
| 3537 | } |
| 3538 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3539 | 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] | 3540 | { |
| 3541 | struct sk_buff *next_skb; |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3542 | int tx_seq_offset, next_tx_seq_offset; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3543 | |
| 3544 | bt_cb(skb)->tx_seq = tx_seq; |
| 3545 | bt_cb(skb)->sar = sar; |
| 3546 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3547 | next_skb = skb_peek(&chan->srej_q); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3548 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3549 | 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] | 3550 | |
Szymon Janc | 039d957 | 2011-11-16 09:32:19 +0100 | [diff] [blame] | 3551 | while (next_skb) { |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3552 | if (bt_cb(next_skb)->tx_seq == tx_seq) |
| 3553 | return -EINVAL; |
| 3554 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3555 | next_tx_seq_offset = __seq_offset(chan, |
| 3556 | bt_cb(next_skb)->tx_seq, chan->buffer_seq); |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 3557 | |
| 3558 | if (next_tx_seq_offset > tx_seq_offset) { |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3559 | __skb_queue_before(&chan->srej_q, next_skb, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3560 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3561 | } |
| 3562 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3563 | if (skb_queue_is_last(&chan->srej_q, next_skb)) |
Szymon Janc | 039d957 | 2011-11-16 09:32:19 +0100 | [diff] [blame] | 3564 | next_skb = NULL; |
| 3565 | else |
| 3566 | next_skb = skb_queue_next(&chan->srej_q, next_skb); |
| 3567 | } |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3568 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3569 | __skb_queue_tail(&chan->srej_q, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3570 | |
| 3571 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3572 | } |
| 3573 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3574 | static void append_skb_frag(struct sk_buff *skb, |
| 3575 | struct sk_buff *new_frag, struct sk_buff **last_frag) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3576 | { |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3577 | /* skb->len reflects data in skb as well as all fragments |
| 3578 | * skb->data_len reflects only data in fragments |
| 3579 | */ |
| 3580 | if (!skb_has_frag_list(skb)) |
| 3581 | skb_shinfo(skb)->frag_list = new_frag; |
| 3582 | |
| 3583 | new_frag->next = NULL; |
| 3584 | |
| 3585 | (*last_frag)->next = new_frag; |
| 3586 | *last_frag = new_frag; |
| 3587 | |
| 3588 | skb->len += new_frag->len; |
| 3589 | skb->data_len += new_frag->len; |
| 3590 | skb->truesize += new_frag->truesize; |
| 3591 | } |
| 3592 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3593 | 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] | 3594 | { |
| 3595 | int err = -EINVAL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3596 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3597 | switch (__get_ctrl_sar(chan, control)) { |
| 3598 | case L2CAP_SAR_UNSEGMENTED: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3599 | if (chan->sdu) |
| 3600 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3601 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3602 | err = chan->ops->recv(chan->data, skb); |
| 3603 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3604 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3605 | case L2CAP_SAR_START: |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3606 | if (chan->sdu) |
| 3607 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3608 | |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3609 | chan->sdu_len = get_unaligned_le16(skb->data); |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 3610 | skb_pull(skb, L2CAP_SDULEN_SIZE); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3611 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3612 | if (chan->sdu_len > chan->imtu) { |
| 3613 | err = -EMSGSIZE; |
| 3614 | break; |
| 3615 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3616 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3617 | if (skb->len >= chan->sdu_len) |
| 3618 | break; |
| 3619 | |
| 3620 | chan->sdu = skb; |
| 3621 | chan->sdu_last_frag = skb; |
| 3622 | |
| 3623 | skb = NULL; |
| 3624 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3625 | break; |
| 3626 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3627 | case L2CAP_SAR_CONTINUE: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3628 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3629 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3630 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3631 | append_skb_frag(chan->sdu, skb, |
| 3632 | &chan->sdu_last_frag); |
| 3633 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3634 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3635 | if (chan->sdu->len >= chan->sdu_len) |
| 3636 | break; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3637 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3638 | err = 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3639 | break; |
| 3640 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3641 | case L2CAP_SAR_END: |
Gustavo F. Padovan | 6f61fd47 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3642 | if (!chan->sdu) |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3643 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3644 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3645 | append_skb_frag(chan->sdu, skb, |
| 3646 | &chan->sdu_last_frag); |
| 3647 | skb = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3648 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3649 | if (chan->sdu->len != chan->sdu_len) |
| 3650 | break; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3651 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3652 | err = chan->ops->recv(chan->data, chan->sdu); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3653 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3654 | if (!err) { |
| 3655 | /* Reassembly complete */ |
| 3656 | chan->sdu = NULL; |
| 3657 | chan->sdu_last_frag = NULL; |
| 3658 | chan->sdu_len = 0; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3659 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3660 | break; |
| 3661 | } |
| 3662 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3663 | if (err) { |
| 3664 | kfree_skb(skb); |
| 3665 | kfree_skb(chan->sdu); |
| 3666 | chan->sdu = NULL; |
| 3667 | chan->sdu_last_frag = NULL; |
| 3668 | chan->sdu_len = 0; |
| 3669 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3670 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3671 | return err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3672 | } |
| 3673 | |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3674 | static void l2cap_ertm_enter_local_busy(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3675 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3676 | u32 control; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3677 | |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3678 | BT_DBG("chan %p, Enter local busy", chan); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3679 | |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3680 | set_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 3681 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3682 | control = __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3683 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RNR); |
Mat Martineau | 26f880d | 2011-07-07 09:39:01 -0700 | [diff] [blame] | 3684 | l2cap_send_sframe(chan, control); |
| 3685 | |
| 3686 | set_bit(CONN_RNR_SENT, &chan->conn_state); |
| 3687 | |
| 3688 | __clear_ack_timer(chan); |
| 3689 | } |
| 3690 | |
| 3691 | static void l2cap_ertm_exit_local_busy(struct l2cap_chan *chan) |
| 3692 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3693 | u32 control; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3694 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3695 | if (!test_bit(CONN_RNR_SENT, &chan->conn_state)) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3696 | goto done; |
| 3697 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3698 | control = __set_reqseq(chan, chan->buffer_seq); |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 3699 | control |= __set_ctrl_poll(chan); |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3700 | control |= __set_ctrl_super(chan, L2CAP_SUPER_RR); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3701 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3702 | chan->retry_count = 1; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3703 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3704 | __clear_retrans_timer(chan); |
| 3705 | __set_monitor_timer(chan); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3706 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3707 | set_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3708 | |
| 3709 | done: |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3710 | clear_bit(CONN_LOCAL_BUSY, &chan->conn_state); |
| 3711 | clear_bit(CONN_RNR_SENT, &chan->conn_state); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3712 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3713 | BT_DBG("chan %p, Exit local busy", chan); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3714 | } |
| 3715 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3716 | void l2cap_chan_busy(struct l2cap_chan *chan, int busy) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3717 | { |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3718 | if (chan->mode == L2CAP_MODE_ERTM) { |
| 3719 | if (busy) |
| 3720 | l2cap_ertm_enter_local_busy(chan); |
| 3721 | else |
| 3722 | l2cap_ertm_exit_local_busy(chan); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3723 | } |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3724 | } |
| 3725 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3726 | 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] | 3727 | { |
| 3728 | struct sk_buff *skb; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3729 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3730 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3731 | while ((skb = skb_peek(&chan->srej_q)) && |
| 3732 | !test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { |
| 3733 | int err; |
| 3734 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3735 | if (bt_cb(skb)->tx_seq != tx_seq) |
| 3736 | break; |
| 3737 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3738 | skb = skb_dequeue(&chan->srej_q); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3739 | control = __set_ctrl_sar(chan, bt_cb(skb)->sar); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3740 | err = l2cap_reassemble_sdu(chan, skb, control); |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3741 | |
| 3742 | if (err < 0) { |
| 3743 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
| 3744 | break; |
| 3745 | } |
| 3746 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3747 | chan->buffer_seq_srej = __next_seq(chan, chan->buffer_seq_srej); |
| 3748 | tx_seq = __next_seq(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3749 | } |
| 3750 | } |
| 3751 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3752 | 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] | 3753 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3754 | struct srej_list *l, *tmp; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3755 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3756 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3757 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3758 | if (l->tx_seq == tx_seq) { |
| 3759 | list_del(&l->list); |
| 3760 | kfree(l); |
| 3761 | return; |
| 3762 | } |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3763 | control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3764 | control |= __set_reqseq(chan, l->tx_seq); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3765 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3766 | list_del(&l->list); |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3767 | list_add_tail(&l->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3771 | 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] | 3772 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3773 | struct srej_list *new; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3774 | u32 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3775 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3776 | while (tx_seq != chan->expected_tx_seq) { |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 3777 | control = __set_ctrl_super(chan, L2CAP_SUPER_SREJ); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3778 | control |= __set_reqseq(chan, chan->expected_tx_seq); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3779 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3780 | |
| 3781 | new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3782 | if (!new) |
| 3783 | return -ENOMEM; |
| 3784 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3785 | new->tx_seq = chan->expected_tx_seq; |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3786 | |
| 3787 | chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); |
| 3788 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3789 | list_add_tail(&new->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3790 | } |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3791 | |
| 3792 | chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3793 | |
| 3794 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3795 | } |
| 3796 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3797 | 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] | 3798 | { |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 3799 | u16 tx_seq = __get_txseq(chan, rx_control); |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3800 | u16 req_seq = __get_reqseq(chan, rx_control); |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 3801 | u8 sar = __get_ctrl_sar(chan, rx_control); |
Gustavo F. Padovan | f6337c7 | 2010-05-10 18:32:04 -0300 | [diff] [blame] | 3802 | int tx_seq_offset, expected_tx_seq_offset; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3803 | int num_to_ack = (chan->tx_win/6) + 1; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3804 | int err = 0; |
| 3805 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3806 | 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] | 3807 | tx_seq, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3808 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 3809 | if (__is_ctrl_final(chan, rx_control) && |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3810 | test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3811 | __clear_monitor_timer(chan); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3812 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3813 | __set_retrans_timer(chan); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3814 | clear_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3815 | } |
| 3816 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3817 | chan->expected_ack_seq = req_seq; |
| 3818 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 9f121a5 | 2009-10-03 02:34:38 -0300 | [diff] [blame] | 3819 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3820 | 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] | 3821 | |
| 3822 | /* invalid tx_seq */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3823 | if (tx_seq_offset >= chan->tx_win) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3824 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3825 | goto drop; |
| 3826 | } |
| 3827 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3828 | if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3829 | goto drop; |
| 3830 | |
Mat Martineau | 02f1b64 | 2011-06-29 14:35:19 -0700 | [diff] [blame] | 3831 | if (tx_seq == chan->expected_tx_seq) |
| 3832 | goto expected; |
| 3833 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3834 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3835 | struct srej_list *first; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3836 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3837 | first = list_first_entry(&chan->srej_l, |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3838 | struct srej_list, list); |
| 3839 | if (tx_seq == first->tx_seq) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3840 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3841 | l2cap_check_srej_gap(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3842 | |
| 3843 | list_del(&first->list); |
| 3844 | kfree(first); |
| 3845 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3846 | if (list_empty(&chan->srej_l)) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3847 | chan->buffer_seq = chan->buffer_seq_srej; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3848 | clear_bit(CONN_SREJ_SENT, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3849 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3850 | BT_DBG("chan %p, Exit SREJ_SENT", chan); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3851 | } |
| 3852 | } else { |
| 3853 | struct srej_list *l; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3854 | |
| 3855 | /* duplicated tx_seq */ |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3856 | 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] | 3857 | goto drop; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3858 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3859 | list_for_each_entry(l, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3860 | if (l->tx_seq == tx_seq) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3861 | l2cap_resend_srejframe(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3862 | return 0; |
| 3863 | } |
| 3864 | } |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3865 | |
| 3866 | err = l2cap_send_srejframe(chan, tx_seq); |
| 3867 | if (err < 0) { |
| 3868 | l2cap_send_disconn_req(chan->conn, chan, -err); |
| 3869 | return err; |
| 3870 | } |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3871 | } |
| 3872 | } else { |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3873 | expected_tx_seq_offset = __seq_offset(chan, |
| 3874 | chan->expected_tx_seq, chan->buffer_seq); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3875 | |
| 3876 | /* duplicated tx_seq */ |
| 3877 | if (tx_seq_offset < expected_tx_seq_offset) |
| 3878 | goto drop; |
| 3879 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3880 | set_bit(CONN_SREJ_SENT, &chan->conn_state); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3881 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3882 | BT_DBG("chan %p, Enter SREJ", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3883 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3884 | INIT_LIST_HEAD(&chan->srej_l); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3885 | chan->buffer_seq_srej = chan->buffer_seq; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3886 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3887 | __skb_queue_head_init(&chan->srej_q); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3888 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3889 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3890 | set_bit(CONN_SEND_PBIT, &chan->conn_state); |
Gustavo F. Padovan | ef54fd9 | 2009-08-20 22:26:04 -0300 | [diff] [blame] | 3891 | |
Szymon Janc | aef89f2 | 2011-11-16 09:32:18 +0100 | [diff] [blame] | 3892 | err = l2cap_send_srejframe(chan, tx_seq); |
| 3893 | if (err < 0) { |
| 3894 | l2cap_send_disconn_req(chan->conn, chan, -err); |
| 3895 | return err; |
| 3896 | } |
Gustavo F. Padovan | 7fe9b29 | 2010-05-12 18:32:04 -0300 | [diff] [blame] | 3897 | |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3898 | __clear_ack_timer(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3899 | } |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3900 | return 0; |
| 3901 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3902 | expected: |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3903 | chan->expected_tx_seq = __next_seq(chan, chan->expected_tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3904 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3905 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
Gustavo F. Padovan | 3b1a9f3 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 3906 | bt_cb(skb)->tx_seq = tx_seq; |
| 3907 | bt_cb(skb)->sar = sar; |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3908 | __skb_queue_tail(&chan->srej_q, skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3909 | return 0; |
| 3910 | } |
| 3911 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 3912 | err = l2cap_reassemble_sdu(chan, skb, rx_control); |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 3913 | chan->buffer_seq = __next_seq(chan, chan->buffer_seq); |
| 3914 | |
Mat Martineau | e328140 | 2011-07-07 09:39:02 -0700 | [diff] [blame] | 3915 | if (err < 0) { |
| 3916 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
| 3917 | return err; |
| 3918 | } |
Gustavo F. Padovan | 2ece368 | 2010-06-16 17:21:44 -0300 | [diff] [blame] | 3919 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 3920 | if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3921 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3922 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | 4ec10d9 | 2009-10-03 02:34:39 -0300 | [diff] [blame] | 3923 | } |
| 3924 | |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3925 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3926 | chan->num_acked = (chan->num_acked + 1) % num_to_ack; |
| 3927 | if (chan->num_acked == num_to_ack - 1) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3928 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 4d611e4 | 2011-06-23 19:30:48 -0300 | [diff] [blame] | 3929 | else |
| 3930 | __set_ack_timer(chan); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3931 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3932 | return 0; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3933 | |
| 3934 | drop: |
| 3935 | kfree_skb(skb); |
| 3936 | return 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3937 | } |
| 3938 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3939 | 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] | 3940 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3941 | BT_DBG("chan %p, req_seq %d ctrl 0x%8.8x", chan, |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3942 | __get_reqseq(chan, rx_control), rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3943 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3944 | chan->expected_ack_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3945 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3946 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 3947 | if (__is_ctrl_poll(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3948 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
| 3949 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
| 3950 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3951 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3952 | __set_retrans_timer(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3953 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3954 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3955 | l2cap_send_srejtail(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3956 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3957 | l2cap_send_i_or_rr_or_rnr(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3958 | } |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3959 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 3960 | } else if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3961 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3962 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3963 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3964 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3965 | |
| 3966 | } else { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3967 | if (test_bit(CONN_REMOTE_BUSY, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3968 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 3969 | __set_retrans_timer(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3970 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3971 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
| 3972 | if (test_bit(CONN_SREJ_SENT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3973 | l2cap_send_ack(chan); |
Andrei Emeltchenko | 894718a | 2010-12-01 16:58:24 +0200 | [diff] [blame] | 3974 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3975 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3976 | } |
| 3977 | } |
| 3978 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3979 | 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] | 3980 | { |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 3981 | u16 tx_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3982 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 3983 | 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] | 3984 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3985 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3986 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3987 | chan->expected_ack_seq = tx_seq; |
| 3988 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3989 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 3990 | if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3991 | if (!test_and_clear_bit(CONN_REJ_ACT, &chan->conn_state)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3992 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3993 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3994 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3995 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 3996 | if (test_bit(CONN_WAIT_F, &chan->conn_state)) |
| 3997 | set_bit(CONN_REJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3998 | } |
| 3999 | } |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4000 | 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] | 4001 | { |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4002 | u16 tx_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4003 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4004 | 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] | 4005 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4006 | clear_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4007 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4008 | if (__is_ctrl_poll(chan, rx_control)) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4009 | chan->expected_ack_seq = tx_seq; |
| 4010 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 4011 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4012 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4013 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 4014 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4015 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 4016 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4017 | if (test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4018 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4019 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4020 | } |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4021 | } else if (__is_ctrl_final(chan, rx_control)) { |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4022 | if (test_bit(CONN_SREJ_ACT, &chan->conn_state) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4023 | chan->srej_save_reqseq == tx_seq) |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4024 | clear_bit(CONN_SREJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4025 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4026 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4027 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4028 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4029 | if (test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4030 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4031 | set_bit(CONN_SREJ_ACT, &chan->conn_state); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4032 | } |
| 4033 | } |
| 4034 | } |
| 4035 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4036 | 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] | 4037 | { |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4038 | u16 tx_seq = __get_reqseq(chan, rx_control); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4039 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4040 | 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] | 4041 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4042 | set_bit(CONN_REMOTE_BUSY, &chan->conn_state); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 4043 | chan->expected_ack_seq = tx_seq; |
| 4044 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4045 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4046 | if (__is_ctrl_poll(chan, rx_control)) |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4047 | set_bit(CONN_SEND_FBIT, &chan->conn_state); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 4048 | |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4049 | if (!test_bit(CONN_SREJ_SENT, &chan->conn_state)) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4050 | __clear_retrans_timer(chan); |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4051 | if (__is_ctrl_poll(chan, rx_control)) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4052 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4053 | return; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4054 | } |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4055 | |
Andrei Emeltchenko | e378173 | 2011-10-11 13:37:50 +0300 | [diff] [blame] | 4056 | if (__is_ctrl_poll(chan, rx_control)) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4057 | l2cap_send_srejtail(chan); |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4058 | } else { |
| 4059 | rx_control = __set_ctrl_super(chan, L2CAP_SUPER_RR); |
| 4060 | l2cap_send_sframe(chan, rx_control); |
| 4061 | } |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 4062 | } |
| 4063 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4064 | 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] | 4065 | { |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4066 | 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] | 4067 | |
Andrei Emeltchenko | 03f6715 | 2011-10-11 13:37:49 +0300 | [diff] [blame] | 4068 | if (__is_ctrl_final(chan, rx_control) && |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4069 | test_bit(CONN_WAIT_F, &chan->conn_state)) { |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4070 | __clear_monitor_timer(chan); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 4071 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1a09bcb | 2011-05-17 15:13:19 -0300 | [diff] [blame] | 4072 | __set_retrans_timer(chan); |
Gustavo F. Padovan | e2ab435 | 2011-06-10 21:28:49 -0300 | [diff] [blame] | 4073 | clear_bit(CONN_WAIT_F, &chan->conn_state); |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 4074 | } |
| 4075 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4076 | switch (__get_ctrl_super(chan, rx_control)) { |
| 4077 | case L2CAP_SUPER_RR: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4078 | l2cap_data_channel_rrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4079 | break; |
| 4080 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4081 | case L2CAP_SUPER_REJ: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4082 | l2cap_data_channel_rejframe(chan, rx_control); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 4083 | break; |
| 4084 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4085 | case L2CAP_SUPER_SREJ: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4086 | l2cap_data_channel_srejframe(chan, rx_control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 4087 | break; |
| 4088 | |
Andrei Emeltchenko | ab784b7 | 2011-10-11 13:37:44 +0300 | [diff] [blame] | 4089 | case L2CAP_SUPER_RNR: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4090 | l2cap_data_channel_rnrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4091 | break; |
| 4092 | } |
| 4093 | |
Gustavo F. Padovan | faaebd1 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 4094 | kfree_skb(skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4095 | return 0; |
| 4096 | } |
| 4097 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4098 | static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) |
| 4099 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4100 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4101 | u32 control; |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4102 | u16 req_seq; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4103 | int len, next_tx_seq_offset, req_seq_offset; |
| 4104 | |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4105 | control = __get_control(chan, skb->data); |
| 4106 | skb_pull(skb, __ctrl_size(chan)); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4107 | len = skb->len; |
| 4108 | |
| 4109 | /* |
| 4110 | * We can just drop the corrupted I-frame here. |
| 4111 | * Receiver will miss it and start proper recovery |
| 4112 | * procedures and ask retransmission. |
| 4113 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4114 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4115 | goto drop; |
| 4116 | |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 4117 | if (__is_sar_start(chan, control) && !__is_sframe(chan, control)) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4118 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4119 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4120 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4121 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4122 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4123 | if (len > chan->mps) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4124 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4125 | goto drop; |
| 4126 | } |
| 4127 | |
Andrei Emeltchenko | 0b209fa | 2011-10-11 13:37:46 +0300 | [diff] [blame] | 4128 | req_seq = __get_reqseq(chan, control); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4129 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4130 | req_seq_offset = __seq_offset(chan, req_seq, chan->expected_ack_seq); |
| 4131 | |
| 4132 | next_tx_seq_offset = __seq_offset(chan, chan->next_tx_seq, |
| 4133 | chan->expected_ack_seq); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4134 | |
| 4135 | /* check for invalid req-seq */ |
| 4136 | if (req_seq_offset > next_tx_seq_offset) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4137 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4138 | goto drop; |
| 4139 | } |
| 4140 | |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 4141 | if (!__is_sframe(chan, control)) { |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4142 | if (len < 0) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4143 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4144 | goto drop; |
| 4145 | } |
| 4146 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4147 | l2cap_data_channel_iframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4148 | } else { |
| 4149 | if (len != 0) { |
| 4150 | BT_ERR("%d", len); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 4151 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4152 | goto drop; |
| 4153 | } |
| 4154 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 4155 | l2cap_data_channel_sframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | return 0; |
| 4159 | |
| 4160 | drop: |
| 4161 | kfree_skb(skb); |
| 4162 | return 0; |
| 4163 | } |
| 4164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) |
| 4166 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4167 | struct l2cap_chan *chan; |
David S. Miller | bf73484 | 2011-04-25 13:03:02 -0700 | [diff] [blame] | 4168 | struct sock *sk = NULL; |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4169 | u32 control; |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 4170 | u16 tx_seq; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 4171 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4172 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4173 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4174 | if (!chan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4175 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 4176 | goto drop; |
| 4177 | } |
| 4178 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4179 | sk = chan->sk; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4180 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 4181 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4182 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4183 | if (chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | goto drop; |
| 4185 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4186 | switch (chan->mode) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4187 | case L2CAP_MODE_BASIC: |
| 4188 | /* If socket recv buffers overflows we drop data here |
| 4189 | * which is *bad* because L2CAP has to be reliable. |
| 4190 | * But we don't have any other choice. L2CAP doesn't |
| 4191 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4193 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4194 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 4196 | if (!chan->ops->recv(chan->data, skb)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4197 | goto done; |
| 4198 | break; |
| 4199 | |
| 4200 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | eb403a1 | 2011-06-24 01:54:50 -0300 | [diff] [blame] | 4201 | l2cap_ertm_data_rcv(sk, skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4202 | |
Andrei Emeltchenko | fcafde2 | 2009-12-22 15:58:08 +0200 | [diff] [blame] | 4203 | goto done; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4204 | |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4205 | case L2CAP_MODE_STREAMING: |
Andrei Emeltchenko | 88843ab | 2011-10-17 12:19:56 +0300 | [diff] [blame] | 4206 | control = __get_control(chan, skb->data); |
| 4207 | skb_pull(skb, __ctrl_size(chan)); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4208 | len = skb->len; |
| 4209 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4210 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 2600008 | 2010-05-11 22:02:00 -0300 | [diff] [blame] | 4211 | goto drop; |
| 4212 | |
Andrei Emeltchenko | 7e0ef6e | 2011-10-11 13:37:45 +0300 | [diff] [blame] | 4213 | if (__is_sar_start(chan, control)) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4214 | len -= L2CAP_SDULEN_SIZE; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4215 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 4216 | if (chan->fcs == L2CAP_FCS_CRC16) |
Andrei Emeltchenko | 03a5121 | 2011-10-17 12:19:58 +0300 | [diff] [blame] | 4217 | len -= L2CAP_FCS_SIZE; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 4218 | |
Andrei Emeltchenko | 793c2f1 | 2011-10-11 13:37:48 +0300 | [diff] [blame] | 4219 | if (len > chan->mps || len < 0 || __is_sframe(chan, control)) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4220 | goto drop; |
| 4221 | |
Andrei Emeltchenko | fb45de7 | 2011-10-11 13:37:47 +0300 | [diff] [blame] | 4222 | tx_seq = __get_txseq(chan, control); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4223 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4224 | if (chan->expected_tx_seq != tx_seq) { |
| 4225 | /* Frame(s) missing - must discard partial SDU */ |
| 4226 | kfree_skb(chan->sdu); |
| 4227 | chan->sdu = NULL; |
| 4228 | chan->sdu_last_frag = NULL; |
| 4229 | chan->sdu_len = 0; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4230 | |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4231 | /* TODO: Notify userland of missing data */ |
| 4232 | } |
| 4233 | |
Andrei Emeltchenko | 836be93 | 2011-10-17 12:19:57 +0300 | [diff] [blame] | 4234 | chan->expected_tx_seq = __next_seq(chan, tx_seq); |
Mat Martineau | 84084a3 | 2011-07-22 14:54:00 -0700 | [diff] [blame] | 4235 | |
| 4236 | if (l2cap_reassemble_sdu(chan, skb, control) == -EMSGSIZE) |
| 4237 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 4238 | |
| 4239 | goto done; |
| 4240 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4241 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4242 | 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] | 4243 | break; |
| 4244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | |
| 4246 | drop: |
| 4247 | kfree_skb(skb); |
| 4248 | |
| 4249 | done: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4250 | if (sk) |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4251 | release_sock(sk); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4253 | return 0; |
| 4254 | } |
| 4255 | |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 4256 | 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] | 4257 | { |
David S. Miller | 6dcae1e | 2011-05-16 23:09:26 -0400 | [diff] [blame] | 4258 | struct sock *sk = NULL; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4259 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4261 | chan = l2cap_global_chan_by_psm(0, psm, conn->src); |
| 4262 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | goto drop; |
| 4264 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4265 | sk = chan->sk; |
| 4266 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4267 | lock_sock(sk); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 4268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | BT_DBG("sk %p, len %d", sk, skb->len); |
| 4270 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4271 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4272 | goto drop; |
| 4273 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 4274 | if (chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | goto drop; |
| 4276 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 4277 | if (!chan->ops->recv(chan->data, skb)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | goto done; |
| 4279 | |
| 4280 | drop: |
| 4281 | kfree_skb(skb); |
| 4282 | |
| 4283 | done: |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 4284 | if (sk) |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4285 | release_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4286 | return 0; |
| 4287 | } |
| 4288 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4289 | static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) |
| 4290 | { |
David S. Miller | 6dcae1e | 2011-05-16 23:09:26 -0400 | [diff] [blame] | 4291 | struct sock *sk = NULL; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4292 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4293 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4294 | chan = l2cap_global_chan_by_scid(0, cid, conn->src); |
| 4295 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4296 | goto drop; |
| 4297 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4298 | sk = chan->sk; |
| 4299 | |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4300 | lock_sock(sk); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4301 | |
| 4302 | BT_DBG("sk %p, len %d", sk, skb->len); |
| 4303 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4304 | if (chan->state != BT_BOUND && chan->state != BT_CONNECTED) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4305 | goto drop; |
| 4306 | |
Vinicius Costa Gomes | e13e21d | 2011-06-17 22:46:27 -0300 | [diff] [blame] | 4307 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4308 | goto drop; |
| 4309 | |
Gustavo F. Padovan | 2307049 | 2011-05-16 17:57:22 -0300 | [diff] [blame] | 4310 | if (!chan->ops->recv(chan->data, skb)) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4311 | goto done; |
| 4312 | |
| 4313 | drop: |
| 4314 | kfree_skb(skb); |
| 4315 | |
| 4316 | done: |
| 4317 | if (sk) |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4318 | release_sock(sk); |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4319 | return 0; |
| 4320 | } |
| 4321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 4323 | { |
| 4324 | struct l2cap_hdr *lh = (void *) skb->data; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 4325 | u16 cid, len; |
| 4326 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | |
| 4328 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 4329 | cid = __le16_to_cpu(lh->cid); |
| 4330 | len = __le16_to_cpu(lh->len); |
| 4331 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 4332 | if (len != skb->len) { |
| 4333 | kfree_skb(skb); |
| 4334 | return; |
| 4335 | } |
| 4336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4337 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 4338 | |
| 4339 | switch (cid) { |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 4340 | case L2CAP_CID_LE_SIGNALING: |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 4341 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4342 | l2cap_sig_channel(conn, skb); |
| 4343 | break; |
| 4344 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 4345 | case L2CAP_CID_CONN_LESS: |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 4346 | psm = get_unaligned_le16(skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | skb_pull(skb, 2); |
| 4348 | l2cap_conless_channel(conn, psm, skb); |
| 4349 | break; |
| 4350 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 4351 | case L2CAP_CID_LE_DATA: |
| 4352 | l2cap_att_channel(conn, cid, skb); |
| 4353 | break; |
| 4354 | |
Anderson Briglia | b501d6a | 2011-06-07 18:46:31 -0300 | [diff] [blame] | 4355 | case L2CAP_CID_SMP: |
| 4356 | if (smp_sig_channel(conn, skb)) |
| 4357 | l2cap_conn_del(conn->hcon, EACCES); |
| 4358 | break; |
| 4359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | default: |
| 4361 | l2cap_data_channel(conn, cid, skb); |
| 4362 | break; |
| 4363 | } |
| 4364 | } |
| 4365 | |
| 4366 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 4367 | |
| 4368 | static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
| 4369 | { |
| 4370 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4371 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4372 | |
| 4373 | if (type != ACL_LINK) |
João Paulo Rechi Vita | 963cf68 | 2010-06-22 13:56:28 -0300 | [diff] [blame] | 4374 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4375 | |
| 4376 | BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); |
| 4377 | |
| 4378 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4379 | read_lock(&chan_list_lock); |
| 4380 | list_for_each_entry(c, &chan_list, global_l) { |
| 4381 | struct sock *sk = c->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4382 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4383 | if (c->state != BT_LISTEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4384 | continue; |
| 4385 | |
| 4386 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4387 | lm1 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 4388 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4389 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | exact++; |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4391 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 4392 | lm2 |= HCI_LM_ACCEPT; |
Andrei Emeltchenko | 43bd0f3 | 2011-10-11 14:04:34 +0300 | [diff] [blame] | 4393 | if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 4394 | lm2 |= HCI_LM_MASTER; |
| 4395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4397 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | |
| 4399 | return exact ? lm1 : lm2; |
| 4400 | } |
| 4401 | |
| 4402 | static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
| 4403 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4404 | struct l2cap_conn *conn; |
| 4405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4406 | BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); |
| 4407 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 4408 | if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) |
João Paulo Rechi Vita | 963cf68 | 2010-06-22 13:56:28 -0300 | [diff] [blame] | 4409 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | |
| 4411 | if (!status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4412 | conn = l2cap_conn_add(hcon, status); |
| 4413 | if (conn) |
| 4414 | l2cap_conn_ready(conn); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4415 | } else |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 4416 | l2cap_conn_del(hcon, bt_to_errno(status)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | |
| 4418 | return 0; |
| 4419 | } |
| 4420 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4421 | static int l2cap_disconn_ind(struct hci_conn *hcon) |
| 4422 | { |
| 4423 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 4424 | |
| 4425 | BT_DBG("hcon %p", hcon); |
| 4426 | |
Gustavo F. Padovan | b569450 | 2011-06-08 19:09:13 -0300 | [diff] [blame] | 4427 | if ((hcon->type != ACL_LINK && hcon->type != LE_LINK) || !conn) |
Andrei Emeltchenko | 9f5a0d7 | 2011-11-07 14:20:25 +0200 | [diff] [blame] | 4428 | return HCI_ERROR_REMOTE_USER_TERM; |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4429 | |
| 4430 | return conn->disc_reason; |
| 4431 | } |
| 4432 | |
| 4433 | static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | { |
| 4435 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 4436 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 4437 | if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) |
João Paulo Rechi Vita | 963cf68 | 2010-06-22 13:56:28 -0300 | [diff] [blame] | 4438 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | |
Joe Perches | e175072 | 2011-06-29 18:18:29 -0700 | [diff] [blame] | 4440 | l2cap_conn_del(hcon, bt_to_errno(reason)); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4442 | return 0; |
| 4443 | } |
| 4444 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4445 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4446 | { |
Gustavo F. Padovan | 715ec00 | 2011-05-02 17:13:55 -0300 | [diff] [blame] | 4447 | if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 4448 | return; |
| 4449 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4450 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4451 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 4452 | __clear_chan_timer(chan); |
Andrzej Kaczmarek | f3f668b | 2011-11-07 17:19:04 -0200 | [diff] [blame] | 4453 | __set_chan_timer(chan, L2CAP_ENC_TIMEOUT); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4454 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
Gustavo F. Padovan | 0f85272 | 2011-05-04 19:42:50 -0300 | [diff] [blame] | 4455 | l2cap_chan_close(chan, ECONNREFUSED); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4456 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4457 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 4458 | __clear_chan_timer(chan); |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 4459 | } |
| 4460 | } |
| 4461 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 4462 | static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4463 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 4464 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4465 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4467 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4468 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 4469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4470 | BT_DBG("conn %p", conn); |
| 4471 | |
Vinicius Costa Gomes | 160dc6a | 2011-08-19 21:06:55 -0300 | [diff] [blame] | 4472 | if (hcon->type == LE_LINK) { |
| 4473 | smp_distribute_keys(conn, 0); |
| 4474 | del_timer(&conn->security_timer); |
| 4475 | } |
| 4476 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 4477 | mutex_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4478 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4479 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4480 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | bh_lock_sock(sk); |
| 4483 | |
Vinicius Costa Gomes | f1cb9af | 2011-01-26 21:42:57 -0300 | [diff] [blame] | 4484 | BT_DBG("chan->scid %d", chan->scid); |
| 4485 | |
| 4486 | if (chan->scid == L2CAP_CID_LE_DATA) { |
| 4487 | if (!status && encrypt) { |
| 4488 | chan->sec_level = hcon->sec_level; |
| 4489 | l2cap_chan_ready(sk); |
| 4490 | } |
| 4491 | |
| 4492 | bh_unlock_sock(sk); |
| 4493 | continue; |
| 4494 | } |
| 4495 | |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4496 | if (test_bit(CONF_CONNECT_PEND, &chan->conf_state)) { |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 4497 | bh_unlock_sock(sk); |
| 4498 | continue; |
| 4499 | } |
| 4500 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4501 | if (!status && (chan->state == BT_CONNECTED || |
| 4502 | chan->state == BT_CONFIG)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 4503 | l2cap_check_encryption(chan, encrypt); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 4504 | bh_unlock_sock(sk); |
| 4505 | continue; |
| 4506 | } |
| 4507 | |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4508 | if (chan->state == BT_CONNECT) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4509 | if (!status) { |
| 4510 | struct l2cap_conn_req req; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4511 | req.scid = cpu_to_le16(chan->scid); |
| 4512 | req.psm = chan->psm; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4513 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4514 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | c1360a1 | 2011-06-10 17:02:12 -0300 | [diff] [blame] | 4515 | set_bit(CONF_CONNECT_PEND, &chan->conf_state); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4516 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4517 | l2cap_send_cmd(conn, chan->ident, |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4518 | L2CAP_CONN_REQ, sizeof(req), &req); |
| 4519 | } else { |
Gustavo F. Padovan | c9b6667 | 2011-05-17 14:59:01 -0300 | [diff] [blame] | 4520 | __clear_chan_timer(chan); |
Andrzej Kaczmarek | f3f668b | 2011-11-07 17:19:04 -0200 | [diff] [blame] | 4521 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4522 | } |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4523 | } else if (chan->state == BT_CONNECT2) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4524 | struct l2cap_conn_rsp rsp; |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4525 | __u16 res, stat; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4526 | |
| 4527 | if (!status) { |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4528 | if (bt_sk(sk)->defer_setup) { |
| 4529 | struct sock *parent = bt_sk(sk)->parent; |
| 4530 | res = L2CAP_CR_PEND; |
| 4531 | stat = L2CAP_CS_AUTHOR_PEND; |
Ilia Kolomisnky | 05e9a2f | 2011-07-15 18:30:21 +0000 | [diff] [blame] | 4532 | if (parent) |
| 4533 | parent->sk_data_ready(parent, 0); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4534 | } else { |
Gustavo F. Padovan | 0555891 | 2011-06-21 14:52:56 -0300 | [diff] [blame] | 4535 | l2cap_state_change(chan, BT_CONFIG); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4536 | res = L2CAP_CR_SUCCESS; |
| 4537 | stat = L2CAP_CS_NO_INFO; |
| 4538 | } |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4539 | } else { |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4540 | l2cap_state_change(chan, BT_DISCONN); |
Andrzej Kaczmarek | f3f668b | 2011-11-07 17:19:04 -0200 | [diff] [blame] | 4541 | __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4542 | res = L2CAP_CR_SEC_BLOCK; |
| 4543 | stat = L2CAP_CS_NO_INFO; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4544 | } |
| 4545 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4546 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4547 | rsp.dcid = cpu_to_le16(chan->scid); |
Johan Hedberg | df3c393 | 2011-06-14 12:48:19 +0300 | [diff] [blame] | 4548 | rsp.result = cpu_to_le16(res); |
| 4549 | rsp.status = cpu_to_le16(stat); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4550 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 4551 | sizeof(rsp), &rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4552 | } |
| 4553 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4554 | bh_unlock_sock(sk); |
| 4555 | } |
| 4556 | |
Gustavo F. Padovan | d01b2ff | 2011-12-09 04:45:12 -0200 | [diff] [blame] | 4557 | mutex_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4559 | return 0; |
| 4560 | } |
| 4561 | |
| 4562 | static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) |
| 4563 | { |
| 4564 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 4565 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 4566 | if (!conn) |
| 4567 | conn = l2cap_conn_add(hcon, 0); |
| 4568 | |
| 4569 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | goto drop; |
| 4571 | |
| 4572 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 4573 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 4574 | if (!(flags & ACL_CONT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | struct l2cap_hdr *hdr; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4576 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4577 | u16 cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | int len; |
| 4579 | |
| 4580 | if (conn->rx_len) { |
| 4581 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 4582 | kfree_skb(conn->rx_skb); |
| 4583 | conn->rx_skb = NULL; |
| 4584 | conn->rx_len = 0; |
| 4585 | l2cap_conn_unreliable(conn, ECOMM); |
| 4586 | } |
| 4587 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 4588 | /* Start fragment always begin with Basic L2CAP header */ |
| 4589 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 4591 | l2cap_conn_unreliable(conn, ECOMM); |
| 4592 | goto drop; |
| 4593 | } |
| 4594 | |
| 4595 | hdr = (struct l2cap_hdr *) skb->data; |
| 4596 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4597 | cid = __le16_to_cpu(hdr->cid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4598 | |
| 4599 | if (len == skb->len) { |
| 4600 | /* Complete frame received */ |
| 4601 | l2cap_recv_frame(conn, skb); |
| 4602 | return 0; |
| 4603 | } |
| 4604 | |
| 4605 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 4606 | |
| 4607 | if (skb->len > len) { |
| 4608 | BT_ERR("Frame is too long (len %d, expected len %d)", |
| 4609 | skb->len, len); |
| 4610 | l2cap_conn_unreliable(conn, ECOMM); |
| 4611 | goto drop; |
| 4612 | } |
| 4613 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4614 | chan = l2cap_get_chan_by_scid(conn, cid); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4615 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4616 | if (chan && chan->sk) { |
| 4617 | struct sock *sk = chan->sk; |
| 4618 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4619 | if (chan->imtu < len - L2CAP_HDR_SIZE) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4620 | BT_ERR("Frame exceeding recv MTU (len %d, " |
| 4621 | "MTU %d)", len, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4622 | chan->imtu); |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4623 | release_sock(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4624 | l2cap_conn_unreliable(conn, ECOMM); |
| 4625 | goto drop; |
| 4626 | } |
Gustavo F. Padovan | aa2ac88 | 2011-06-24 01:53:01 -0300 | [diff] [blame] | 4627 | release_sock(sk); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4628 | } |
| 4629 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | /* Allocate skb for the complete frame (with header) */ |
Gustavo F. Padovan | af05b30b | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 4631 | conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 4632 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | goto drop; |
| 4634 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4635 | 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] | 4636 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | conn->rx_len = len - skb->len; |
| 4638 | } else { |
| 4639 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 4640 | |
| 4641 | if (!conn->rx_len) { |
| 4642 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 4643 | l2cap_conn_unreliable(conn, ECOMM); |
| 4644 | goto drop; |
| 4645 | } |
| 4646 | |
| 4647 | if (skb->len > conn->rx_len) { |
| 4648 | BT_ERR("Fragment is too long (len %d, expected %d)", |
| 4649 | skb->len, conn->rx_len); |
| 4650 | kfree_skb(conn->rx_skb); |
| 4651 | conn->rx_skb = NULL; |
| 4652 | conn->rx_len = 0; |
| 4653 | l2cap_conn_unreliable(conn, ECOMM); |
| 4654 | goto drop; |
| 4655 | } |
| 4656 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4657 | 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] | 4658 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | conn->rx_len -= skb->len; |
| 4660 | |
| 4661 | if (!conn->rx_len) { |
| 4662 | /* Complete frame received */ |
| 4663 | l2cap_recv_frame(conn, conn->rx_skb); |
| 4664 | conn->rx_skb = NULL; |
| 4665 | } |
| 4666 | } |
| 4667 | |
| 4668 | drop: |
| 4669 | kfree_skb(skb); |
| 4670 | return 0; |
| 4671 | } |
| 4672 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4673 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4674 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4675 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4676 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4677 | read_lock_bh(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4678 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4679 | list_for_each_entry(c, &chan_list, global_l) { |
| 4680 | struct sock *sk = c->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | |
Gustavo F. Padovan | 903d343 | 2011-02-10 14:16:06 -0200 | [diff] [blame] | 4682 | 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] | 4683 | batostr(&bt_sk(sk)->src), |
| 4684 | batostr(&bt_sk(sk)->dst), |
Gustavo F. Padovan | 89bc500 | 2011-06-03 00:19:47 -0300 | [diff] [blame] | 4685 | c->state, __le16_to_cpu(c->psm), |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4686 | c->scid, c->dcid, c->imtu, c->omtu, |
| 4687 | c->sec_level, c->mode); |
Gustavo F. Padovan | 0555891 | 2011-06-21 14:52:56 -0300 | [diff] [blame] | 4688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4689 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4690 | read_unlock_bh(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4691 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4692 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4693 | } |
| 4694 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4695 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 4696 | { |
| 4697 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 4698 | } |
| 4699 | |
| 4700 | static const struct file_operations l2cap_debugfs_fops = { |
| 4701 | .open = l2cap_debugfs_open, |
| 4702 | .read = seq_read, |
| 4703 | .llseek = seq_lseek, |
| 4704 | .release = single_release, |
| 4705 | }; |
| 4706 | |
| 4707 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4708 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4709 | static struct hci_proto l2cap_hci_proto = { |
| 4710 | .name = "L2CAP", |
| 4711 | .id = HCI_PROTO_L2CAP, |
| 4712 | .connect_ind = l2cap_connect_ind, |
| 4713 | .connect_cfm = l2cap_connect_cfm, |
| 4714 | .disconn_ind = l2cap_disconn_ind, |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4715 | .disconn_cfm = l2cap_disconn_cfm, |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 4716 | .security_cfm = l2cap_security_cfm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4717 | .recv_acldata = l2cap_recv_acldata |
| 4718 | }; |
| 4719 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4720 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4721 | { |
| 4722 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4723 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4724 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | if (err < 0) |
| 4726 | return err; |
| 4727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | err = hci_register_proto(&l2cap_hci_proto); |
| 4729 | if (err < 0) { |
| 4730 | BT_ERR("L2CAP protocol registration failed"); |
| 4731 | bt_sock_unregister(BTPROTO_L2CAP); |
| 4732 | goto error; |
| 4733 | } |
| 4734 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4735 | if (bt_debugfs) { |
| 4736 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, |
| 4737 | bt_debugfs, NULL, &l2cap_debugfs_fops); |
| 4738 | if (!l2cap_debugfs) |
| 4739 | BT_ERR("Failed to create L2CAP debug file"); |
| 4740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | return 0; |
| 4743 | |
| 4744 | error: |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4745 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | return err; |
| 4747 | } |
| 4748 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4749 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4751 | debugfs_remove(l2cap_debugfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | if (hci_unregister_proto(&l2cap_hci_proto) < 0) |
| 4754 | BT_ERR("L2CAP protocol unregistration failed"); |
| 4755 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4756 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | } |
| 4758 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4759 | module_param(disable_ertm, bool, 0644); |
| 4760 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |