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 | af05b30 | 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> |
| 57 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 58 | int disable_ertm; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 59 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 60 | static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 61 | static u8 l2cap_fixed_chan[8] = { 0x02, }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 63 | static struct workqueue_struct *_busy_wq; |
| 64 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 65 | LIST_HEAD(chan_list); |
| 66 | DEFINE_RWLOCK(chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 68 | static void l2cap_busy_work(struct work_struct *work); |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
| 71 | u8 code, u8 ident, u16 dlen, void *data); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 72 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 74 | static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb); |
| 75 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 76 | /* ---- L2CAP channels ---- */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 77 | static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 78 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 79 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 80 | |
| 81 | list_for_each_entry(c, &conn->chan_l, list) { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 82 | if (c->dcid == cid) |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 83 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 84 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 85 | return NULL; |
| 86 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 89 | static struct l2cap_chan *__l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 90 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 91 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 92 | |
| 93 | list_for_each_entry(c, &conn->chan_l, list) { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 94 | if (c->scid == cid) |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 95 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 96 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 97 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* Find channel with given SCID. |
| 101 | * Returns locked socket */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 102 | static struct l2cap_chan *l2cap_get_chan_by_scid(struct l2cap_conn *conn, u16 cid) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 103 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 104 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 105 | |
| 106 | read_lock(&conn->chan_lock); |
| 107 | c = __l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 108 | if (c) |
| 109 | bh_lock_sock(c->sk); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 110 | read_unlock(&conn->chan_lock); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 111 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 112 | } |
| 113 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 114 | 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] | 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 | |
| 118 | list_for_each_entry(c, &conn->chan_l, list) { |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 119 | if (c->ident == ident) |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 120 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 121 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 122 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 125 | 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] | 126 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 127 | struct l2cap_chan *c; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 128 | |
| 129 | read_lock(&conn->chan_lock); |
| 130 | c = __l2cap_get_chan_by_ident(conn, ident); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 131 | if (c) |
| 132 | bh_lock_sock(c->sk); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 133 | read_unlock(&conn->chan_lock); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 134 | return c; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 137 | 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] | 138 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 139 | struct l2cap_chan *c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 140 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 141 | list_for_each_entry(c, &chan_list, global_l) { |
| 142 | if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 143 | goto found; |
| 144 | } |
| 145 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 146 | c = NULL; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 147 | found: |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 148 | return c; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | int l2cap_add_psm(struct l2cap_chan *chan, bdaddr_t *src, __le16 psm) |
| 152 | { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 153 | int err; |
| 154 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 155 | write_lock_bh(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 156 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 157 | if (psm && __l2cap_global_chan_by_addr(psm, src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 158 | err = -EADDRINUSE; |
| 159 | goto done; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 160 | } |
| 161 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 162 | if (psm) { |
| 163 | chan->psm = psm; |
| 164 | chan->sport = psm; |
| 165 | err = 0; |
| 166 | } else { |
| 167 | u16 p; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 168 | |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 169 | err = -EINVAL; |
| 170 | for (p = 0x1001; p < 0x1100; p += 2) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 171 | if (!__l2cap_global_chan_by_addr(cpu_to_le16(p), src)) { |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 172 | chan->psm = cpu_to_le16(p); |
| 173 | chan->sport = cpu_to_le16(p); |
| 174 | err = 0; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | done: |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 180 | write_unlock_bh(&chan_list_lock); |
Gustavo F. Padovan | 73b2ec1 | 2011-04-18 19:36:44 -0300 | [diff] [blame] | 181 | return err; |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | int l2cap_add_scid(struct l2cap_chan *chan, __u16 scid) |
| 185 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 186 | write_lock_bh(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 187 | |
| 188 | chan->scid = scid; |
| 189 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 190 | write_unlock_bh(&chan_list_lock); |
Gustavo F. Padovan | 9e4425f | 2011-04-18 18:38:43 -0300 | [diff] [blame] | 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 195 | static u16 l2cap_alloc_cid(struct l2cap_conn *conn) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 196 | { |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 197 | u16 cid = L2CAP_CID_DYN_START; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 198 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 199 | for (; cid < L2CAP_CID_DYN_END; cid++) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 200 | if (!__l2cap_get_chan_by_scid(conn, cid)) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 201 | return cid; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 207 | struct l2cap_chan *l2cap_chan_create(struct sock *sk) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 208 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 209 | struct l2cap_chan *chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 210 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 211 | chan = kzalloc(sizeof(*chan), GFP_ATOMIC); |
| 212 | if (!chan) |
| 213 | return NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 214 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 215 | chan->sk = sk; |
| 216 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 217 | write_lock_bh(&chan_list_lock); |
| 218 | list_add(&chan->global_l, &chan_list); |
| 219 | write_unlock_bh(&chan_list_lock); |
| 220 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 221 | return chan; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 224 | void l2cap_chan_destroy(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 225 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 226 | write_lock_bh(&chan_list_lock); |
| 227 | list_del(&chan->global_l); |
| 228 | write_unlock_bh(&chan_list_lock); |
| 229 | |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 230 | kfree(chan); |
| 231 | } |
| 232 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 233 | 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] | 234 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 235 | struct sock *sk = chan->sk; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 236 | |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 237 | 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] | 238 | chan->psm, chan->dcid); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 239 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 240 | conn->disc_reason = 0x13; |
| 241 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 242 | chan->conn = conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 243 | |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 244 | if (sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM) { |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 245 | if (conn->hcon->type == LE_LINK) { |
| 246 | /* LE connection */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 247 | chan->omtu = L2CAP_LE_DEFAULT_MTU; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 248 | chan->scid = L2CAP_CID_LE_DATA; |
| 249 | chan->dcid = L2CAP_CID_LE_DATA; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 250 | } else { |
| 251 | /* Alloc CID for connection-oriented socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 252 | chan->scid = l2cap_alloc_cid(conn); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 253 | chan->omtu = L2CAP_DEFAULT_MTU; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 254 | } |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 255 | } else if (sk->sk_type == SOCK_DGRAM) { |
| 256 | /* Connectionless socket */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 257 | chan->scid = L2CAP_CID_CONN_LESS; |
| 258 | chan->dcid = L2CAP_CID_CONN_LESS; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 259 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 260 | } else { |
| 261 | /* Raw socket can send/recv signalling messages only */ |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 262 | chan->scid = L2CAP_CID_SIGNALING; |
| 263 | chan->dcid = L2CAP_CID_SIGNALING; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 264 | chan->omtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 267 | sock_hold(sk); |
| 268 | |
| 269 | list_add(&chan->list, &conn->chan_l); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 270 | } |
| 271 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 272 | /* Delete channel. |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 273 | * Must be called on the locked socket. */ |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 274 | void l2cap_chan_del(struct l2cap_chan *chan, int err) |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 275 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 276 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 277 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 278 | struct sock *parent = bt_sk(sk)->parent; |
| 279 | |
| 280 | l2cap_sock_clear_timer(sk); |
| 281 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 282 | BT_DBG("chan %p, conn %p, err %d", chan, conn, err); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 283 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 284 | if (conn) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 285 | /* Delete from channel list */ |
| 286 | write_lock_bh(&conn->chan_lock); |
| 287 | list_del(&chan->list); |
| 288 | write_unlock_bh(&conn->chan_lock); |
| 289 | __sock_put(sk); |
| 290 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 291 | chan->conn = NULL; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 292 | hci_conn_put(conn->hcon); |
| 293 | } |
| 294 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 295 | sk->sk_state = BT_CLOSED; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 296 | sock_set_flag(sk, SOCK_ZAPPED); |
| 297 | |
| 298 | if (err) |
| 299 | sk->sk_err = err; |
| 300 | |
| 301 | if (parent) { |
| 302 | bt_accept_unlink(sk); |
| 303 | parent->sk_data_ready(parent, 0); |
| 304 | } else |
| 305 | sk->sk_state_change(sk); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 306 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 307 | if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE && |
| 308 | chan->conf_state & L2CAP_CONF_INPUT_DONE)) |
Gustavo F. Padovan | 6ff5abb | 2011-04-25 15:10:41 -0300 | [diff] [blame] | 309 | return; |
Gustavo F. Padovan | 2ead70b | 2011-04-01 15:13:36 -0300 | [diff] [blame] | 310 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 311 | skb_queue_purge(&chan->tx_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 312 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 313 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 314 | struct srej_list *l, *tmp; |
| 315 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 316 | del_timer(&chan->retrans_timer); |
| 317 | del_timer(&chan->monitor_timer); |
| 318 | del_timer(&chan->ack_timer); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 319 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 320 | skb_queue_purge(&chan->srej_q); |
| 321 | skb_queue_purge(&chan->busy_q); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 322 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 323 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 324 | list_del(&l->list); |
| 325 | kfree(l); |
| 326 | } |
| 327 | } |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 330 | static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 331 | { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 332 | struct sock *sk = chan->sk; |
| 333 | |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 334 | if (sk->sk_type == SOCK_RAW) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 335 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 336 | case BT_SECURITY_HIGH: |
| 337 | return HCI_AT_DEDICATED_BONDING_MITM; |
| 338 | case BT_SECURITY_MEDIUM: |
| 339 | return HCI_AT_DEDICATED_BONDING; |
| 340 | default: |
| 341 | return HCI_AT_NO_BONDING; |
| 342 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 343 | } else if (chan->psm == cpu_to_le16(0x0001)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 344 | if (chan->sec_level == BT_SECURITY_LOW) |
| 345 | chan->sec_level = BT_SECURITY_SDP; |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 346 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 347 | if (chan->sec_level == BT_SECURITY_HIGH) |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 348 | return HCI_AT_NO_BONDING_MITM; |
| 349 | else |
| 350 | return HCI_AT_NO_BONDING; |
| 351 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 352 | switch (chan->sec_level) { |
Johan Hedberg | 8556edd3 | 2011-01-19 12:06:50 +0530 | [diff] [blame] | 353 | case BT_SECURITY_HIGH: |
| 354 | return HCI_AT_GENERAL_BONDING_MITM; |
| 355 | case BT_SECURITY_MEDIUM: |
| 356 | return HCI_AT_GENERAL_BONDING; |
| 357 | default: |
| 358 | return HCI_AT_NO_BONDING; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 363 | /* Service level security */ |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 364 | static inline int l2cap_check_security(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 365 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 366 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 367 | __u8 auth_type; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 368 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 369 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 370 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 371 | return hci_conn_security(conn->hcon, chan->sec_level, auth_type); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 374 | u8 l2cap_get_ident(struct l2cap_conn *conn) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 375 | { |
| 376 | u8 id; |
| 377 | |
| 378 | /* Get next available identificator. |
| 379 | * 1 - 128 are used by kernel. |
| 380 | * 129 - 199 are reserved. |
| 381 | * 200 - 254 are used by utilities like l2ping, etc. |
| 382 | */ |
| 383 | |
| 384 | spin_lock_bh(&conn->lock); |
| 385 | |
| 386 | if (++conn->tx_ident > 128) |
| 387 | conn->tx_ident = 1; |
| 388 | |
| 389 | id = conn->tx_ident; |
| 390 | |
| 391 | spin_unlock_bh(&conn->lock); |
| 392 | |
| 393 | return id; |
| 394 | } |
| 395 | |
Gustavo F. Padovan | 6898325 | 2011-02-04 03:02:31 -0200 | [diff] [blame] | 396 | 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] | 397 | { |
| 398 | struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data); |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 399 | u8 flags; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 400 | |
| 401 | BT_DBG("code 0x%2.2x", code); |
| 402 | |
| 403 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 404 | return; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 405 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 406 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 407 | flags = ACL_START_NO_FLUSH; |
| 408 | else |
| 409 | flags = ACL_START; |
| 410 | |
| 411 | hci_send_acl(conn->hcon, skb, flags); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 412 | } |
| 413 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 414 | static inline void l2cap_send_sframe(struct l2cap_chan *chan, u16 control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 415 | { |
| 416 | struct sk_buff *skb; |
| 417 | struct l2cap_hdr *lh; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 418 | struct l2cap_pinfo *pi = l2cap_pi(chan->sk); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 419 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 420 | struct sock *sk = (struct sock *)pi; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 421 | int count, hlen = L2CAP_HDR_SIZE + 2; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 422 | u8 flags; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 423 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 424 | if (sk->sk_state != BT_CONNECTED) |
| 425 | return; |
| 426 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 427 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 428 | hlen += 2; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 429 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 430 | BT_DBG("chan %p, control 0x%2.2x", chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 431 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 432 | count = min_t(unsigned int, conn->mtu, hlen); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 433 | control |= L2CAP_CTRL_FRAME_TYPE; |
| 434 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 435 | if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 436 | control |= L2CAP_CTRL_FINAL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 437 | chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 438 | } |
| 439 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 440 | if (chan->conn_state & L2CAP_CONN_SEND_PBIT) { |
Gustavo F. Padovan | f0946cc | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 441 | control |= L2CAP_CTRL_POLL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 442 | chan->conn_state &= ~L2CAP_CONN_SEND_PBIT; |
Gustavo F. Padovan | f0946cc | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 443 | } |
| 444 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 445 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 446 | if (!skb) |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 447 | return; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 448 | |
| 449 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 450 | lh->len = cpu_to_le16(hlen - L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 451 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 452 | put_unaligned_le16(control, skb_put(skb, 2)); |
| 453 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 454 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 455 | u16 fcs = crc16(0, (u8 *)lh, count - 2); |
| 456 | put_unaligned_le16(fcs, skb_put(skb, 2)); |
| 457 | } |
| 458 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 459 | if (lmp_no_flush_capable(conn->hcon->hdev)) |
| 460 | flags = ACL_START_NO_FLUSH; |
| 461 | else |
| 462 | flags = ACL_START; |
| 463 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 464 | hci_send_acl(chan->conn->hcon, skb, flags); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 465 | } |
| 466 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 467 | static inline void l2cap_send_rr_or_rnr(struct l2cap_chan *chan, u16 control) |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 468 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 469 | if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 470 | control |= L2CAP_SUPER_RCV_NOT_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 471 | chan->conn_state |= L2CAP_CONN_RNR_SENT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 472 | } else |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 473 | control |= L2CAP_SUPER_RCV_READY; |
| 474 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 475 | control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 2ab25cd | 2009-10-03 02:34:40 -0300 | [diff] [blame] | 476 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 477 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 7e74309 | 2009-08-26 04:04:03 -0300 | [diff] [blame] | 478 | } |
| 479 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 480 | static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan) |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 481 | { |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 482 | return !(chan->conf_state & L2CAP_CONF_CONNECT_PEND); |
Andrei Emeltchenko | e501d05 | 2010-07-08 12:14:41 +0300 | [diff] [blame] | 483 | } |
| 484 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 485 | static void l2cap_do_start(struct l2cap_chan *chan) |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 486 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 487 | struct l2cap_conn *conn = chan->conn; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 488 | |
| 489 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 490 | if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) |
| 491 | return; |
| 492 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 493 | if (l2cap_check_security(chan) && |
| 494 | __l2cap_no_conn_pending(chan)) { |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 495 | struct l2cap_conn_req req; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 496 | req.scid = cpu_to_le16(chan->scid); |
| 497 | req.psm = chan->psm; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 498 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 499 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 500 | chan->conf_state |= L2CAP_CONF_CONNECT_PEND; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 501 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 502 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, |
| 503 | sizeof(req), &req); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 504 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 505 | } else { |
| 506 | struct l2cap_info_req req; |
| 507 | req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 508 | |
| 509 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 510 | conn->info_ident = l2cap_get_ident(conn); |
| 511 | |
| 512 | mod_timer(&conn->info_timer, jiffies + |
| 513 | msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); |
| 514 | |
| 515 | l2cap_send_cmd(conn, conn->info_ident, |
| 516 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 517 | } |
| 518 | } |
| 519 | |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 520 | static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask) |
| 521 | { |
| 522 | u32 local_feat_mask = l2cap_feat_mask; |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 523 | if (!disable_ertm) |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 524 | local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING; |
| 525 | |
| 526 | switch (mode) { |
| 527 | case L2CAP_MODE_ERTM: |
| 528 | return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask; |
| 529 | case L2CAP_MODE_STREAMING: |
| 530 | return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask; |
| 531 | default: |
| 532 | return 0x00; |
| 533 | } |
| 534 | } |
| 535 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 536 | 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] | 537 | { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 538 | struct sock *sk; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 539 | struct l2cap_disconn_req req; |
| 540 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 541 | if (!conn) |
| 542 | return; |
| 543 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 544 | sk = chan->sk; |
| 545 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 546 | if (chan->mode == L2CAP_MODE_ERTM) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 547 | del_timer(&chan->retrans_timer); |
| 548 | del_timer(&chan->monitor_timer); |
| 549 | del_timer(&chan->ack_timer); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 550 | } |
| 551 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 552 | req.dcid = cpu_to_le16(chan->dcid); |
| 553 | req.scid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 554 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 555 | L2CAP_DISCONN_REQ, sizeof(req), &req); |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 556 | |
| 557 | sk->sk_state = BT_DISCONN; |
Gustavo F. Padovan | 9b108fc | 2010-05-20 16:21:53 -0300 | [diff] [blame] | 558 | sk->sk_err = err; |
Gustavo F. Padovan | 22121fc | 2009-07-23 10:27:23 -0300 | [diff] [blame] | 559 | } |
| 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | /* ---- L2CAP connections ---- */ |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 562 | static void l2cap_conn_start(struct l2cap_conn *conn) |
| 563 | { |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 564 | struct l2cap_chan *chan, *tmp; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 565 | |
| 566 | BT_DBG("conn %p", conn); |
| 567 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 568 | read_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 569 | |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 570 | list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 571 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 572 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 573 | bh_lock_sock(sk); |
| 574 | |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 575 | if (sk->sk_type != SOCK_SEQPACKET && |
| 576 | sk->sk_type != SOCK_STREAM) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 577 | bh_unlock_sock(sk); |
| 578 | continue; |
| 579 | } |
| 580 | |
| 581 | if (sk->sk_state == BT_CONNECT) { |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 582 | struct l2cap_conn_req req; |
Gustavo F. Padovan | cf6c2c0 | 2010-06-07 20:54:45 -0300 | [diff] [blame] | 583 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 584 | if (!l2cap_check_security(chan) || |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 585 | !__l2cap_no_conn_pending(chan)) { |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 586 | bh_unlock_sock(sk); |
| 587 | continue; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 588 | } |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 589 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 590 | if (!l2cap_mode_supported(chan->mode, |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 591 | conn->feat_mask) |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 592 | && chan->conf_state & |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 593 | L2CAP_CONF_STATE2_DEVICE) { |
Gustavo F. Padovan | 820ffdb | 2011-04-01 00:35:21 -0300 | [diff] [blame] | 594 | /* __l2cap_sock_close() calls list_del(chan) |
| 595 | * so release the lock */ |
| 596 | read_unlock_bh(&conn->chan_lock); |
| 597 | __l2cap_sock_close(sk, ECONNRESET); |
| 598 | read_lock_bh(&conn->chan_lock); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 599 | bh_unlock_sock(sk); |
| 600 | continue; |
| 601 | } |
| 602 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 603 | req.scid = cpu_to_le16(chan->scid); |
| 604 | req.psm = chan->psm; |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 605 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 606 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 607 | chan->conf_state |= L2CAP_CONF_CONNECT_PEND; |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 608 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 609 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_REQ, |
| 610 | sizeof(req), &req); |
Gustavo F. Padovan | 47731de | 2010-07-09 16:38:35 -0300 | [diff] [blame] | 611 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 612 | } else if (sk->sk_state == BT_CONNECT2) { |
| 613 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 614 | char buf[128]; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 615 | rsp.scid = cpu_to_le16(chan->dcid); |
| 616 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 617 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 618 | if (l2cap_check_security(chan)) { |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 619 | if (bt_sk(sk)->defer_setup) { |
| 620 | struct sock *parent = bt_sk(sk)->parent; |
| 621 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 622 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND); |
Nick Pelly | f86e4b0 | 2010-04-08 16:23:32 -0700 | [diff] [blame^] | 623 | if (parent) |
| 624 | parent->sk_data_ready(parent, 0); |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 625 | |
| 626 | } else { |
| 627 | sk->sk_state = BT_CONFIG; |
| 628 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 629 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 630 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 631 | } else { |
| 632 | rsp.result = cpu_to_le16(L2CAP_CR_PEND); |
| 633 | rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND); |
| 634 | } |
| 635 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 636 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 637 | sizeof(rsp), &rsp); |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 638 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 639 | if (chan->conf_state & L2CAP_CONF_REQ_SENT || |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 640 | rsp.result != L2CAP_CR_SUCCESS) { |
| 641 | bh_unlock_sock(sk); |
| 642 | continue; |
| 643 | } |
| 644 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 645 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 646 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 647 | l2cap_build_conf_req(chan, buf), buf); |
| 648 | chan->num_conf_req++; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | bh_unlock_sock(sk); |
| 652 | } |
| 653 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 654 | read_unlock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 655 | } |
| 656 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 657 | /* Find socket with cid and source bdaddr. |
| 658 | * Returns closest match, locked. |
| 659 | */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 660 | 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] | 661 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 662 | struct l2cap_chan *c, *c1 = NULL; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 663 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 664 | read_lock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 665 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 666 | list_for_each_entry(c, &chan_list, global_l) { |
| 667 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 668 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 669 | if (state && sk->sk_state != state) |
| 670 | continue; |
| 671 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 672 | if (c->scid == cid) { |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 673 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 674 | if (!bacmp(&bt_sk(sk)->src, src)) { |
| 675 | read_unlock(&chan_list_lock); |
| 676 | return c; |
| 677 | } |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 678 | |
| 679 | /* Closest match */ |
| 680 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 681 | c1 = c; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 682 | } |
| 683 | } |
Gustavo F. Padovan | 280f294 | 2011-04-13 19:01:22 -0300 | [diff] [blame] | 684 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 685 | read_unlock(&chan_list_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 686 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 687 | return c1; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static void l2cap_le_conn_ready(struct l2cap_conn *conn) |
| 691 | { |
Gustavo F. Padovan | c916fbe | 2011-04-04 16:00:55 -0300 | [diff] [blame] | 692 | struct sock *parent, *sk; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 693 | struct l2cap_chan *chan, *pchan; |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 694 | |
| 695 | BT_DBG(""); |
| 696 | |
| 697 | /* Check if we have socket listening on cid */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 698 | pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA, |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 699 | conn->src); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 700 | if (!pchan) |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 701 | return; |
| 702 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 703 | parent = pchan->sk; |
| 704 | |
Gustavo F. Padovan | 62f3a2c | 2011-04-14 18:34:34 -0300 | [diff] [blame] | 705 | bh_lock_sock(parent); |
| 706 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 707 | /* Check for backlog size */ |
| 708 | if (sk_acceptq_is_full(parent)) { |
| 709 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
| 710 | goto clean; |
| 711 | } |
| 712 | |
| 713 | sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); |
| 714 | if (!sk) |
| 715 | goto clean; |
| 716 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 717 | chan = l2cap_chan_create(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 718 | if (!chan) { |
| 719 | l2cap_sock_kill(sk); |
| 720 | goto clean; |
| 721 | } |
| 722 | |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 723 | l2cap_pi(sk)->chan = chan; |
| 724 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 725 | write_lock_bh(&conn->chan_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 726 | |
| 727 | hci_conn_hold(conn->hcon); |
| 728 | |
| 729 | l2cap_sock_init(sk, parent); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 730 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 731 | bacpy(&bt_sk(sk)->src, conn->src); |
| 732 | bacpy(&bt_sk(sk)->dst, conn->dst); |
| 733 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 734 | bt_accept_enqueue(parent, sk); |
| 735 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 736 | __l2cap_chan_add(conn, chan); |
| 737 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 738 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
| 739 | |
| 740 | sk->sk_state = BT_CONNECTED; |
| 741 | parent->sk_data_ready(parent, 0); |
| 742 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 743 | write_unlock_bh(&conn->chan_lock); |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 744 | |
| 745 | clean: |
| 746 | bh_unlock_sock(parent); |
| 747 | } |
| 748 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 749 | static void l2cap_conn_ready(struct l2cap_conn *conn) |
| 750 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 751 | struct l2cap_chan *chan; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 752 | |
| 753 | BT_DBG("conn %p", conn); |
| 754 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 755 | if (!conn->hcon->out && conn->hcon->type == LE_LINK) |
| 756 | l2cap_le_conn_ready(conn); |
| 757 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 758 | read_lock(&conn->chan_lock); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 759 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 760 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 761 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 762 | |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 763 | bh_lock_sock(sk); |
| 764 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 765 | if (conn->hcon->type == LE_LINK) { |
| 766 | l2cap_sock_clear_timer(sk); |
| 767 | sk->sk_state = BT_CONNECTED; |
| 768 | sk->sk_state_change(sk); |
| 769 | } |
| 770 | |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 771 | if (sk->sk_type != SOCK_SEQPACKET && |
| 772 | sk->sk_type != SOCK_STREAM) { |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 773 | l2cap_sock_clear_timer(sk); |
| 774 | sk->sk_state = BT_CONNECTED; |
| 775 | sk->sk_state_change(sk); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 776 | } else if (sk->sk_state == BT_CONNECT) |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 777 | l2cap_do_start(chan); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 778 | |
| 779 | bh_unlock_sock(sk); |
| 780 | } |
| 781 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 782 | read_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 783 | } |
| 784 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 785 | /* Notify sockets that we cannot guaranty reliability anymore */ |
| 786 | static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err) |
| 787 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 788 | struct l2cap_chan *chan; |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 789 | |
| 790 | BT_DBG("conn %p", conn); |
| 791 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 792 | read_lock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 793 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 794 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 795 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 796 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 797 | if (chan->force_reliable) |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 798 | sk->sk_err = err; |
| 799 | } |
| 800 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 801 | read_unlock(&conn->chan_lock); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | static void l2cap_info_timeout(unsigned long arg) |
| 805 | { |
| 806 | struct l2cap_conn *conn = (void *) arg; |
| 807 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 808 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 809 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 810 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 811 | l2cap_conn_start(conn); |
| 812 | } |
| 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status) |
| 815 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 816 | struct l2cap_conn *conn = hcon->l2cap_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 818 | if (conn || status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | return conn; |
| 820 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 821 | conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC); |
| 822 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | hcon->l2cap_data = conn; |
| 826 | conn->hcon = hcon; |
| 827 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 828 | BT_DBG("hcon %p conn %p", hcon, conn); |
| 829 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 830 | if (hcon->hdev->le_mtu && hcon->type == LE_LINK) |
| 831 | conn->mtu = hcon->hdev->le_mtu; |
| 832 | else |
| 833 | conn->mtu = hcon->hdev->acl_mtu; |
| 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | conn->src = &hcon->hdev->bdaddr; |
| 836 | conn->dst = &hcon->dst; |
| 837 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 838 | conn->feat_mask = 0; |
| 839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | spin_lock_init(&conn->lock); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 841 | rwlock_init(&conn->chan_lock); |
| 842 | |
| 843 | INIT_LIST_HEAD(&conn->chan_l); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
Ville Tervo | b62f328 | 2011-02-10 22:38:50 -0300 | [diff] [blame] | 845 | if (hcon->type != LE_LINK) |
| 846 | setup_timer(&conn->info_timer, l2cap_info_timeout, |
Dave Young | 45054dc | 2009-10-18 20:28:30 +0000 | [diff] [blame] | 847 | (unsigned long) conn); |
| 848 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 849 | conn->disc_reason = 0x13; |
| 850 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | return conn; |
| 852 | } |
| 853 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 854 | static void l2cap_conn_del(struct hci_conn *hcon, int err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 856 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 857 | struct l2cap_chan *chan, *l; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | struct sock *sk; |
| 859 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 860 | if (!conn) |
| 861 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
| 863 | BT_DBG("hcon %p conn %p, err %d", hcon, conn, err); |
| 864 | |
Wei Yongjun | 7585b97 | 2009-02-25 18:29:52 +0800 | [diff] [blame] | 865 | kfree_skb(conn->rx_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
| 867 | /* Kill channels */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 868 | list_for_each_entry_safe(chan, l, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 869 | sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | bh_lock_sock(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 871 | l2cap_chan_del(chan, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | bh_unlock_sock(sk); |
| 873 | l2cap_sock_kill(sk); |
| 874 | } |
| 875 | |
Dave Young | 8e8440f | 2008-03-03 12:18:55 -0800 | [diff] [blame] | 876 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) |
| 877 | del_timer_sync(&conn->info_timer); |
Thomas Gleixner | 3ab2273 | 2008-02-26 17:42:56 -0800 | [diff] [blame] | 878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | hcon->l2cap_data = NULL; |
| 880 | kfree(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 883 | 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] | 884 | { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 885 | write_lock_bh(&conn->chan_lock); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 886 | __l2cap_chan_add(conn, chan); |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 887 | write_unlock_bh(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | } |
| 889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | /* ---- Socket interface ---- */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
| 892 | /* Find socket with psm and source bdaddr. |
| 893 | * Returns closest match. |
| 894 | */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 895 | 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] | 896 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 897 | struct l2cap_chan *c, *c1 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 899 | read_lock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 900 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 901 | list_for_each_entry(c, &chan_list, global_l) { |
| 902 | struct sock *sk = c->sk; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 903 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | if (state && sk->sk_state != state) |
| 905 | continue; |
| 906 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 907 | if (c->psm == psm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | /* Exact match. */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 909 | if (!bacmp(&bt_sk(sk)->src, src)) { |
Johannes Berg | a7567b2 | 2011-06-01 08:29:54 +0200 | [diff] [blame] | 910 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 911 | return c; |
| 912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
| 914 | /* Closest match */ |
| 915 | if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 916 | c1 = c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | } |
| 918 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 920 | read_unlock(&chan_list_lock); |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 921 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 922 | return c1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Gustavo F. Padovan | 77a74c7 | 2011-04-12 18:17:14 -0300 | [diff] [blame] | 925 | int l2cap_chan_connect(struct l2cap_chan *chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | { |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 927 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | bdaddr_t *src = &bt_sk(sk)->src; |
| 929 | bdaddr_t *dst = &bt_sk(sk)->dst; |
| 930 | struct l2cap_conn *conn; |
| 931 | struct hci_conn *hcon; |
| 932 | struct hci_dev *hdev; |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 933 | __u8 auth_type; |
Marcel Holtmann | 44d0e48 | 2009-04-20 07:09:16 +0200 | [diff] [blame] | 934 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | |
Marcel Holtmann | f29972d | 2009-02-12 05:07:45 +0100 | [diff] [blame] | 936 | 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] | 937 | chan->psm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 939 | hdev = hci_get_route(dst, src); |
| 940 | if (!hdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | return -EHOSTUNREACH; |
| 942 | |
| 943 | hci_dev_lock_bh(hdev); |
| 944 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 945 | auth_type = l2cap_get_auth_type(chan); |
Marcel Holtmann | 09ab6f4 | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 946 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 947 | if (chan->dcid == L2CAP_CID_LE_DATA) |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 948 | hcon = hci_connect(hdev, LE_LINK, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 949 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 950 | else |
| 951 | hcon = hci_connect(hdev, ACL_LINK, dst, |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 952 | chan->sec_level, auth_type); |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 953 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 954 | if (IS_ERR(hcon)) { |
| 955 | err = PTR_ERR(hcon); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | goto done; |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 957 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | |
| 959 | conn = l2cap_conn_add(hcon, 0); |
| 960 | if (!conn) { |
| 961 | hci_conn_put(hcon); |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 962 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | goto done; |
| 964 | } |
| 965 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | /* Update source addr of the socket */ |
| 967 | bacpy(src, conn->src); |
| 968 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 969 | l2cap_chan_add(conn, chan); |
| 970 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | sk->sk_state = BT_CONNECT; |
| 972 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
| 973 | |
| 974 | if (hcon->state == BT_CONNECTED) { |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 975 | if (sk->sk_type != SOCK_SEQPACKET && |
| 976 | sk->sk_type != SOCK_STREAM) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | l2cap_sock_clear_timer(sk); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 978 | if (l2cap_check_security(chan)) |
Johan Hedberg | d00ef24 | 2011-01-19 12:06:51 +0530 | [diff] [blame] | 979 | sk->sk_state = BT_CONNECTED; |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 980 | } else |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 981 | l2cap_do_start(chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 984 | err = 0; |
| 985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | done: |
| 987 | hci_dev_unlock_bh(hdev); |
| 988 | hci_dev_put(hdev); |
| 989 | return err; |
| 990 | } |
| 991 | |
Gustavo F. Padovan | dcba0db | 2011-02-04 03:08:36 -0200 | [diff] [blame] | 992 | int __l2cap_wait_ack(struct sock *sk) |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 993 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 994 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 995 | DECLARE_WAITQUEUE(wait, current); |
| 996 | int err = 0; |
| 997 | int timeo = HZ/5; |
| 998 | |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 999 | add_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1000 | while ((chan->unacked_frames > 0 && chan->conn)) { |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1001 | set_current_state(TASK_INTERRUPTIBLE); |
| 1002 | |
| 1003 | if (!timeo) |
| 1004 | timeo = HZ/5; |
| 1005 | |
| 1006 | if (signal_pending(current)) { |
| 1007 | err = sock_intr_errno(timeo); |
| 1008 | break; |
| 1009 | } |
| 1010 | |
| 1011 | release_sock(sk); |
| 1012 | timeo = schedule_timeout(timeo); |
| 1013 | lock_sock(sk); |
| 1014 | |
| 1015 | err = sock_error(sk); |
| 1016 | if (err) |
| 1017 | break; |
| 1018 | } |
| 1019 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 1020 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 6161c03 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1021 | return err; |
| 1022 | } |
| 1023 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1024 | static void l2cap_monitor_timeout(unsigned long arg) |
| 1025 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1026 | struct l2cap_chan *chan = (void *) arg; |
| 1027 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1028 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1029 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1030 | |
Gustavo F. Padovan | e686219 | 2009-08-24 00:45:19 -0300 | [diff] [blame] | 1031 | bh_lock_sock(sk); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1032 | if (chan->retry_count >= chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1033 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Andrei Emeltchenko | b13f586 | 2009-12-15 11:38:04 +0200 | [diff] [blame] | 1034 | bh_unlock_sock(sk); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1035 | return; |
| 1036 | } |
| 1037 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1038 | chan->retry_count++; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1039 | __mod_monitor_timer(); |
| 1040 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1041 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Gustavo F. Padovan | e686219 | 2009-08-24 00:45:19 -0300 | [diff] [blame] | 1042 | bh_unlock_sock(sk); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static void l2cap_retrans_timeout(unsigned long arg) |
| 1046 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1047 | struct l2cap_chan *chan = (void *) arg; |
| 1048 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1049 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1050 | BT_DBG("chan %p", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 1051 | |
Gustavo F. Padovan | e686219 | 2009-08-24 00:45:19 -0300 | [diff] [blame] | 1052 | bh_lock_sock(sk); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1053 | chan->retry_count = 1; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1054 | __mod_monitor_timer(); |
| 1055 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1056 | chan->conn_state |= L2CAP_CONN_WAIT_F; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1057 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1058 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_POLL); |
Gustavo F. Padovan | e686219 | 2009-08-24 00:45:19 -0300 | [diff] [blame] | 1059 | bh_unlock_sock(sk); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1060 | } |
| 1061 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1062 | static void l2cap_drop_acked_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1063 | { |
| 1064 | struct sk_buff *skb; |
| 1065 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1066 | while ((skb = skb_peek(&chan->tx_q)) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1067 | chan->unacked_frames) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1068 | if (bt_cb(skb)->tx_seq == chan->expected_ack_seq) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1069 | break; |
| 1070 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1071 | skb = skb_dequeue(&chan->tx_q); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1072 | kfree_skb(skb); |
| 1073 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1074 | chan->unacked_frames--; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1075 | } |
| 1076 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1077 | if (!chan->unacked_frames) |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 1078 | del_timer(&chan->retrans_timer); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1079 | } |
| 1080 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1081 | void l2cap_do_send(struct l2cap_chan *chan, struct sk_buff *skb) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1082 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1083 | struct hci_conn *hcon = chan->conn->hcon; |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 1084 | u16 flags; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1085 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1086 | BT_DBG("chan %p, skb %p len %d", chan, skb, skb->len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1087 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1088 | if (!chan->flushable && lmp_no_flush_capable(hcon->hdev)) |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 1089 | flags = ACL_START_NO_FLUSH; |
| 1090 | else |
| 1091 | flags = ACL_START; |
| 1092 | |
| 1093 | hci_send_acl(hcon, skb, flags); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1094 | } |
| 1095 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1096 | void l2cap_streaming_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1097 | { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1098 | struct sk_buff *skb; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1099 | u16 control, fcs; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1100 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1101 | while ((skb = skb_dequeue(&chan->tx_q))) { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1102 | control = get_unaligned_le16(skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1103 | control |= chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT; |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1104 | put_unaligned_le16(control, skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1105 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1106 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | ccbb84a | 2010-08-30 18:44:44 -0300 | [diff] [blame] | 1107 | fcs = crc16(0, (u8 *)skb->data, skb->len - 2); |
| 1108 | put_unaligned_le16(fcs, skb->data + skb->len - 2); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1109 | } |
| 1110 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1111 | l2cap_do_send(chan, skb); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1112 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1113 | chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1114 | } |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 1115 | } |
| 1116 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1117 | static void l2cap_retransmit_one_frame(struct l2cap_chan *chan, u8 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1118 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1119 | struct sk_buff *skb, *tx_skb; |
| 1120 | u16 control, fcs; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1121 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1122 | skb = skb_peek(&chan->tx_q); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1123 | if (!skb) |
| 1124 | return; |
| 1125 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1126 | do { |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1127 | if (bt_cb(skb)->tx_seq == tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1128 | break; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1129 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1130 | if (skb_queue_is_last(&chan->tx_q, skb)) |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1131 | return; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1132 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1133 | } while ((skb = skb_queue_next(&chan->tx_q, skb))); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1134 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1135 | if (chan->remote_max_tx && |
| 1136 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1137 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1142 | bt_cb(skb)->retries++; |
| 1143 | control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); |
Ruiyi Zhang | a429b51 | 2011-04-18 11:04:30 +0800 | [diff] [blame] | 1144 | control &= L2CAP_CTRL_SAR; |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1145 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1146 | if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1147 | control |= L2CAP_CTRL_FINAL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1148 | chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1149 | } |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1150 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1151 | control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1152 | | (tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 1153 | |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1154 | put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); |
| 1155 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1156 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | f11d676 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1157 | fcs = crc16(0, (u8 *)tx_skb->data, tx_skb->len - 2); |
| 1158 | put_unaligned_le16(fcs, tx_skb->data + tx_skb->len - 2); |
| 1159 | } |
| 1160 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1161 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 1162 | } |
| 1163 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1164 | int l2cap_ertm_send(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1165 | { |
| 1166 | struct sk_buff *skb, *tx_skb; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1167 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1168 | u16 control, fcs; |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1169 | int nsent = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1170 | |
Gustavo F. Padovan | c13ffa6 | 2010-05-13 20:50:12 -0300 | [diff] [blame] | 1171 | if (sk->sk_state != BT_CONNECTED) |
| 1172 | return -ENOTCONN; |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1173 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1174 | while ((skb = chan->tx_send_head) && (!l2cap_tx_window_full(chan))) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1175 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1176 | if (chan->remote_max_tx && |
| 1177 | bt_cb(skb)->retries == chan->remote_max_tx) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1178 | l2cap_send_disconn_req(chan->conn, chan, ECONNABORTED); |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1179 | break; |
| 1180 | } |
| 1181 | |
Andrei Emeltchenko | e420aba | 2009-12-23 13:07:14 +0200 | [diff] [blame] | 1182 | tx_skb = skb_clone(skb, GFP_ATOMIC); |
| 1183 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1184 | bt_cb(skb)->retries++; |
| 1185 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1186 | control = get_unaligned_le16(tx_skb->data + L2CAP_HDR_SIZE); |
Gustavo F. Padovan | 95ffa97 | 2010-06-18 20:37:33 -0300 | [diff] [blame] | 1187 | control &= L2CAP_CTRL_SAR; |
| 1188 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1189 | if (chan->conn_state & L2CAP_CONN_SEND_FBIT) { |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 1190 | control |= L2CAP_CTRL_FINAL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1191 | chan->conn_state &= ~L2CAP_CONN_SEND_FBIT; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 1192 | } |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1193 | control |= (chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT) |
| 1194 | | (chan->next_tx_seq << L2CAP_CTRL_TXSEQ_SHIFT); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1195 | put_unaligned_le16(control, tx_skb->data + L2CAP_HDR_SIZE); |
| 1196 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1197 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1198 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1199 | fcs = crc16(0, (u8 *)skb->data, tx_skb->len - 2); |
| 1200 | put_unaligned_le16(fcs, skb->data + tx_skb->len - 2); |
| 1201 | } |
| 1202 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 1203 | l2cap_do_send(chan, tx_skb); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1204 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1205 | __mod_retrans_timer(); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1206 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1207 | bt_cb(skb)->tx_seq = chan->next_tx_seq; |
| 1208 | chan->next_tx_seq = (chan->next_tx_seq + 1) % 64; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1209 | |
Suraj Sumangala | 23e9fde | 2011-03-09 14:44:05 +0530 | [diff] [blame] | 1210 | if (bt_cb(skb)->retries == 1) |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1211 | chan->unacked_frames++; |
Suraj Sumangala | 23e9fde | 2011-03-09 14:44:05 +0530 | [diff] [blame] | 1212 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1213 | chan->frames_sent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1214 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1215 | if (skb_queue_is_last(&chan->tx_q, skb)) |
| 1216 | chan->tx_send_head = NULL; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1217 | else |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1218 | chan->tx_send_head = skb_queue_next(&chan->tx_q, skb); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1219 | |
| 1220 | nsent++; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1221 | } |
| 1222 | |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1223 | return nsent; |
| 1224 | } |
| 1225 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1226 | static int l2cap_retransmit_frames(struct l2cap_chan *chan) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1227 | { |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1228 | int ret; |
| 1229 | |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1230 | if (!skb_queue_empty(&chan->tx_q)) |
| 1231 | chan->tx_send_head = chan->tx_q.next; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1232 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1233 | chan->next_tx_seq = chan->expected_ack_seq; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1234 | ret = l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1235 | return ret; |
| 1236 | } |
| 1237 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1238 | static void l2cap_send_ack(struct l2cap_chan *chan) |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1239 | { |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1240 | u16 control = 0; |
| 1241 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1242 | control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1243 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1244 | if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1245 | control |= L2CAP_SUPER_RCV_NOT_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1246 | chan->conn_state |= L2CAP_CONN_RNR_SENT; |
| 1247 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 9a9c6a3 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1248 | return; |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 1249 | } |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1250 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1251 | if (l2cap_ertm_send(chan) > 0) |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 1252 | return; |
| 1253 | |
| 1254 | control |= L2CAP_SUPER_RCV_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1255 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1256 | } |
| 1257 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1258 | static void l2cap_send_srejtail(struct l2cap_chan *chan) |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1259 | { |
| 1260 | struct srej_list *tail; |
| 1261 | u16 control; |
| 1262 | |
| 1263 | control = L2CAP_SUPER_SELECT_REJECT; |
| 1264 | control |= L2CAP_CTRL_FINAL; |
| 1265 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 1266 | tail = list_entry((&chan->srej_l)->prev, struct srej_list, list); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1267 | control |= tail->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
| 1268 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1269 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 1270 | } |
| 1271 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1272 | 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] | 1273 | { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1274 | struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1275 | struct sk_buff **frag; |
| 1276 | int err, sent = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | |
Gustavo F. Padovan | 59203a2 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1278 | if (memcpy_fromiovec(skb_put(skb, count), msg->msg_iov, count)) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1279 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
| 1281 | sent += count; |
| 1282 | len -= count; |
| 1283 | |
| 1284 | /* Continuation fragments (no L2CAP header) */ |
| 1285 | frag = &skb_shinfo(skb)->frag_list; |
| 1286 | while (len) { |
| 1287 | count = min_t(unsigned int, conn->mtu, len); |
| 1288 | |
| 1289 | *frag = bt_skb_send_alloc(sk, count, msg->msg_flags & MSG_DONTWAIT, &err); |
| 1290 | if (!*frag) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1291 | return err; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1292 | if (memcpy_fromiovec(skb_put(*frag, count), msg->msg_iov, count)) |
| 1293 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | |
| 1295 | sent += count; |
| 1296 | len -= count; |
| 1297 | |
| 1298 | frag = &(*frag)->next; |
| 1299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
| 1301 | return sent; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1304 | struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1305 | { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1306 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1307 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1308 | struct sk_buff *skb; |
| 1309 | int err, count, hlen = L2CAP_HDR_SIZE + 2; |
| 1310 | struct l2cap_hdr *lh; |
| 1311 | |
| 1312 | BT_DBG("sk %p len %d", sk, (int)len); |
| 1313 | |
| 1314 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 1315 | skb = bt_skb_send_alloc(sk, count + hlen, |
| 1316 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1317 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1318 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1319 | |
| 1320 | /* Create L2CAP header */ |
| 1321 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1322 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1323 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1324 | put_unaligned_le16(chan->psm, skb_put(skb, 2)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1325 | |
| 1326 | err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); |
| 1327 | if (unlikely(err < 0)) { |
| 1328 | kfree_skb(skb); |
| 1329 | return ERR_PTR(err); |
| 1330 | } |
| 1331 | return skb; |
| 1332 | } |
| 1333 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1334 | struct sk_buff *l2cap_create_basic_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1335 | { |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1336 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1337 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1338 | struct sk_buff *skb; |
| 1339 | int err, count, hlen = L2CAP_HDR_SIZE; |
| 1340 | struct l2cap_hdr *lh; |
| 1341 | |
| 1342 | BT_DBG("sk %p len %d", sk, (int)len); |
| 1343 | |
| 1344 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 1345 | skb = bt_skb_send_alloc(sk, count + hlen, |
| 1346 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1347 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1348 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1349 | |
| 1350 | /* Create L2CAP header */ |
| 1351 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1352 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1353 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 1354 | |
| 1355 | err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); |
| 1356 | if (unlikely(err < 0)) { |
| 1357 | kfree_skb(skb); |
| 1358 | return ERR_PTR(err); |
| 1359 | } |
| 1360 | return skb; |
| 1361 | } |
| 1362 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1363 | struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan, struct msghdr *msg, size_t len, u16 control, u16 sdulen) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1364 | { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1365 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1366 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1367 | struct sk_buff *skb; |
| 1368 | int err, count, hlen = L2CAP_HDR_SIZE + 2; |
| 1369 | struct l2cap_hdr *lh; |
| 1370 | |
| 1371 | BT_DBG("sk %p len %d", sk, (int)len); |
| 1372 | |
Gustavo F. Padovan | 0ee0d20 | 2010-05-01 16:15:41 -0300 | [diff] [blame] | 1373 | if (!conn) |
| 1374 | return ERR_PTR(-ENOTCONN); |
| 1375 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1376 | if (sdulen) |
| 1377 | hlen += 2; |
| 1378 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1379 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1380 | hlen += 2; |
| 1381 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1382 | count = min_t(unsigned int, (conn->mtu - hlen), len); |
| 1383 | skb = bt_skb_send_alloc(sk, count + hlen, |
| 1384 | msg->msg_flags & MSG_DONTWAIT, &err); |
| 1385 | if (!skb) |
Gustavo F. Padovan | 0175d62 | 2010-09-24 20:30:57 -0300 | [diff] [blame] | 1386 | return ERR_PTR(err); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1387 | |
| 1388 | /* Create L2CAP header */ |
| 1389 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1390 | lh->cid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1391 | lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE)); |
| 1392 | put_unaligned_le16(control, skb_put(skb, 2)); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1393 | if (sdulen) |
| 1394 | put_unaligned_le16(sdulen, skb_put(skb, 2)); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1395 | |
| 1396 | err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb); |
| 1397 | if (unlikely(err < 0)) { |
| 1398 | kfree_skb(skb); |
| 1399 | return ERR_PTR(err); |
| 1400 | } |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1401 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1402 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1403 | put_unaligned_le16(0, skb_put(skb, 2)); |
| 1404 | |
Gustavo F. Padovan | e90bac0 | 2009-08-20 22:26:00 -0300 | [diff] [blame] | 1405 | bt_cb(skb)->retries = 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 1406 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1409 | 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] | 1410 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1411 | struct sk_buff *skb; |
| 1412 | struct sk_buff_head sar_queue; |
| 1413 | u16 control; |
| 1414 | size_t size = 0; |
| 1415 | |
Gustavo F. Padovan | ff12fd6 | 2010-05-05 22:09:15 -0300 | [diff] [blame] | 1416 | skb_queue_head_init(&sar_queue); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1417 | control = L2CAP_SDU_START; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1418 | 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] | 1419 | if (IS_ERR(skb)) |
| 1420 | return PTR_ERR(skb); |
| 1421 | |
| 1422 | __skb_queue_tail(&sar_queue, skb); |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1423 | len -= chan->remote_mps; |
| 1424 | size += chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1425 | |
| 1426 | while (len > 0) { |
| 1427 | size_t buflen; |
| 1428 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1429 | if (len > chan->remote_mps) { |
Gustavo F. Padovan | 44651b8 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1430 | control = L2CAP_SDU_CONTINUE; |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1431 | buflen = chan->remote_mps; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1432 | } else { |
Gustavo F. Padovan | 44651b8 | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 1433 | control = L2CAP_SDU_END; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1434 | buflen = len; |
| 1435 | } |
| 1436 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1437 | skb = l2cap_create_iframe_pdu(chan, msg, buflen, control, 0); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1438 | if (IS_ERR(skb)) { |
| 1439 | skb_queue_purge(&sar_queue); |
| 1440 | return PTR_ERR(skb); |
| 1441 | } |
| 1442 | |
| 1443 | __skb_queue_tail(&sar_queue, skb); |
| 1444 | len -= buflen; |
| 1445 | size += buflen; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1446 | } |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 1447 | skb_queue_splice_tail(&sar_queue, &chan->tx_q); |
| 1448 | if (chan->tx_send_head == NULL) |
| 1449 | chan->tx_send_head = sar_queue.next; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1450 | |
| 1451 | return size; |
| 1452 | } |
| 1453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | static void l2cap_chan_ready(struct sock *sk) |
| 1455 | { |
| 1456 | struct sock *parent = bt_sk(sk)->parent; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1457 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
| 1459 | BT_DBG("sk %p, parent %p", sk, parent); |
| 1460 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1461 | chan->conf_state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | l2cap_sock_clear_timer(sk); |
| 1463 | |
| 1464 | if (!parent) { |
| 1465 | /* Outgoing channel. |
| 1466 | * Wake up socket sleeping on connect. |
| 1467 | */ |
| 1468 | sk->sk_state = BT_CONNECTED; |
| 1469 | sk->sk_state_change(sk); |
| 1470 | } else { |
| 1471 | /* Incoming channel. |
| 1472 | * Wake up socket sleeping on accept. |
| 1473 | */ |
| 1474 | parent->sk_data_ready(parent, 0); |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | /* Copy frame to all raw sockets on that connection */ |
| 1479 | static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb) |
| 1480 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | struct sk_buff *nskb; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1482 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | |
| 1484 | BT_DBG("conn %p", conn); |
| 1485 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1486 | read_lock(&conn->chan_lock); |
| 1487 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 1488 | struct sock *sk = chan->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | if (sk->sk_type != SOCK_RAW) |
| 1490 | continue; |
| 1491 | |
| 1492 | /* Don't send frame to the socket it came from */ |
| 1493 | if (skb->sk == sk) |
| 1494 | continue; |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1495 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 1496 | if (!nskb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | continue; |
| 1498 | |
| 1499 | if (sock_queue_rcv_skb(sk, nskb)) |
| 1500 | kfree_skb(nskb); |
| 1501 | } |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 1502 | read_unlock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /* ---- L2CAP signalling commands ---- */ |
| 1506 | static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn, |
| 1507 | u8 code, u8 ident, u16 dlen, void *data) |
| 1508 | { |
| 1509 | struct sk_buff *skb, **frag; |
| 1510 | struct l2cap_cmd_hdr *cmd; |
| 1511 | struct l2cap_hdr *lh; |
| 1512 | int len, count; |
| 1513 | |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 1514 | BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d", |
| 1515 | conn, code, ident, dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | |
| 1517 | len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen; |
| 1518 | count = min_t(unsigned int, conn->mtu, len); |
| 1519 | |
| 1520 | skb = bt_skb_alloc(count, GFP_ATOMIC); |
| 1521 | if (!skb) |
| 1522 | return NULL; |
| 1523 | |
| 1524 | lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1525 | lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 1526 | |
| 1527 | if (conn->hcon->type == LE_LINK) |
| 1528 | lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING); |
| 1529 | else |
| 1530 | lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | |
| 1532 | cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE); |
| 1533 | cmd->code = code; |
| 1534 | cmd->ident = ident; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1535 | cmd->len = cpu_to_le16(dlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | |
| 1537 | if (dlen) { |
| 1538 | count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE; |
| 1539 | memcpy(skb_put(skb, count), data, count); |
| 1540 | data += count; |
| 1541 | } |
| 1542 | |
| 1543 | len -= skb->len; |
| 1544 | |
| 1545 | /* Continuation fragments (no L2CAP header) */ |
| 1546 | frag = &skb_shinfo(skb)->frag_list; |
| 1547 | while (len) { |
| 1548 | count = min_t(unsigned int, conn->mtu, len); |
| 1549 | |
| 1550 | *frag = bt_skb_alloc(count, GFP_ATOMIC); |
| 1551 | if (!*frag) |
| 1552 | goto fail; |
| 1553 | |
| 1554 | memcpy(skb_put(*frag, count), data, count); |
| 1555 | |
| 1556 | len -= count; |
| 1557 | data += count; |
| 1558 | |
| 1559 | frag = &(*frag)->next; |
| 1560 | } |
| 1561 | |
| 1562 | return skb; |
| 1563 | |
| 1564 | fail: |
| 1565 | kfree_skb(skb); |
| 1566 | return NULL; |
| 1567 | } |
| 1568 | |
| 1569 | static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val) |
| 1570 | { |
| 1571 | struct l2cap_conf_opt *opt = *ptr; |
| 1572 | int len; |
| 1573 | |
| 1574 | len = L2CAP_CONF_OPT_SIZE + opt->len; |
| 1575 | *ptr += len; |
| 1576 | |
| 1577 | *type = opt->type; |
| 1578 | *olen = opt->len; |
| 1579 | |
| 1580 | switch (opt->len) { |
| 1581 | case 1: |
| 1582 | *val = *((u8 *) opt->val); |
| 1583 | break; |
| 1584 | |
| 1585 | case 2: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1586 | *val = get_unaligned_le16(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | break; |
| 1588 | |
| 1589 | case 4: |
steven miao | bfaaeb3 | 2010-10-16 18:29:47 -0400 | [diff] [blame] | 1590 | *val = get_unaligned_le32(opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | break; |
| 1592 | |
| 1593 | default: |
| 1594 | *val = (unsigned long) opt->val; |
| 1595 | break; |
| 1596 | } |
| 1597 | |
| 1598 | BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val); |
| 1599 | return len; |
| 1600 | } |
| 1601 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val) |
| 1603 | { |
| 1604 | struct l2cap_conf_opt *opt = *ptr; |
| 1605 | |
| 1606 | BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val); |
| 1607 | |
| 1608 | opt->type = type; |
| 1609 | opt->len = len; |
| 1610 | |
| 1611 | switch (len) { |
| 1612 | case 1: |
| 1613 | *((u8 *) opt->val) = val; |
| 1614 | break; |
| 1615 | |
| 1616 | case 2: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1617 | put_unaligned_le16(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | break; |
| 1619 | |
| 1620 | case 4: |
Gustavo F. Padovan | 4f8b691 | 2010-10-18 14:25:53 -0200 | [diff] [blame] | 1621 | put_unaligned_le32(val, opt->val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | break; |
| 1623 | |
| 1624 | default: |
| 1625 | memcpy(opt->val, (void *) val, len); |
| 1626 | break; |
| 1627 | } |
| 1628 | |
| 1629 | *ptr += L2CAP_CONF_OPT_SIZE + len; |
| 1630 | } |
| 1631 | |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 1632 | static void l2cap_ack_timeout(unsigned long arg) |
| 1633 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1634 | struct l2cap_chan *chan = (void *) arg; |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 1635 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1636 | bh_lock_sock(chan->sk); |
| 1637 | l2cap_send_ack(chan); |
| 1638 | bh_unlock_sock(chan->sk); |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 1639 | } |
| 1640 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1641 | static inline void l2cap_ertm_init(struct l2cap_chan *chan) |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1642 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 1643 | struct sock *sk = chan->sk; |
| 1644 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1645 | chan->expected_ack_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1646 | chan->unacked_frames = 0; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 1647 | chan->buffer_seq = 0; |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 1648 | chan->num_acked = 0; |
| 1649 | chan->frames_sent = 0; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1650 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 1651 | setup_timer(&chan->retrans_timer, l2cap_retrans_timeout, |
| 1652 | (unsigned long) chan); |
| 1653 | setup_timer(&chan->monitor_timer, l2cap_monitor_timeout, |
| 1654 | (unsigned long) chan); |
| 1655 | setup_timer(&chan->ack_timer, l2cap_ack_timeout, (unsigned long) chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1656 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 1657 | skb_queue_head_init(&chan->srej_q); |
| 1658 | skb_queue_head_init(&chan->busy_q); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 1659 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 1660 | INIT_LIST_HEAD(&chan->srej_l); |
| 1661 | |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 1662 | INIT_WORK(&chan->busy_work, l2cap_busy_work); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 1663 | |
| 1664 | sk->sk_backlog_rcv = l2cap_ertm_data_rcv; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 1665 | } |
| 1666 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1667 | static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask) |
| 1668 | { |
| 1669 | switch (mode) { |
| 1670 | case L2CAP_MODE_STREAMING: |
| 1671 | case L2CAP_MODE_ERTM: |
| 1672 | if (l2cap_mode_supported(mode, remote_feat_mask)) |
| 1673 | return mode; |
| 1674 | /* fall through */ |
| 1675 | default: |
| 1676 | return L2CAP_MODE_BASIC; |
| 1677 | } |
| 1678 | } |
| 1679 | |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 1680 | static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | struct l2cap_conf_req *req = data; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1683 | struct l2cap_conf_rfc rfc = { .mode = chan->mode }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | void *ptr = req->data; |
| 1685 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 1686 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1688 | if (chan->num_conf_req || chan->num_conf_rsp) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1689 | goto done; |
| 1690 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1691 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1692 | case L2CAP_MODE_STREAMING: |
| 1693 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1694 | if (chan->conf_state & L2CAP_CONF_STATE2_DEVICE) |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 1695 | break; |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 1696 | |
Gustavo F. Padovan | 2ba13ed | 2010-06-09 16:39:05 -0300 | [diff] [blame] | 1697 | /* fall through */ |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1698 | default: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1699 | chan->mode = l2cap_select_mode(rfc.mode, chan->conn->feat_mask); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1700 | break; |
| 1701 | } |
| 1702 | |
| 1703 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1704 | if (chan->imtu != L2CAP_DEFAULT_MTU) |
| 1705 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | 7990681 | 2011-01-24 16:01:43 -0200 | [diff] [blame] | 1706 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1707 | switch (chan->mode) { |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 1708 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1709 | if (!(chan->conn->feat_mask & L2CAP_FEAT_ERTM) && |
| 1710 | !(chan->conn->feat_mask & L2CAP_FEAT_STREAMING)) |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 1711 | break; |
| 1712 | |
Gustavo F. Padovan | 6254775 | 2010-06-08 20:05:31 -0300 | [diff] [blame] | 1713 | rfc.mode = L2CAP_MODE_BASIC; |
| 1714 | rfc.txwin_size = 0; |
| 1715 | rfc.max_transmit = 0; |
| 1716 | rfc.retrans_timeout = 0; |
| 1717 | rfc.monitor_timeout = 0; |
| 1718 | rfc.max_pdu_size = 0; |
| 1719 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 1720 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 1721 | (unsigned long) &rfc); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 1722 | break; |
| 1723 | |
| 1724 | case L2CAP_MODE_ERTM: |
| 1725 | rfc.mode = L2CAP_MODE_ERTM; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1726 | rfc.txwin_size = chan->tx_win; |
| 1727 | rfc.max_transmit = chan->max_tx; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1728 | rfc.retrans_timeout = 0; |
| 1729 | rfc.monitor_timeout = 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1730 | rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1731 | if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) |
| 1732 | rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1733 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 1734 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 1735 | (unsigned long) &rfc); |
| 1736 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1737 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1738 | break; |
| 1739 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1740 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1741 | chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1742 | chan->fcs = L2CAP_FCS_NONE; |
| 1743 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1744 | } |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1745 | break; |
| 1746 | |
| 1747 | case L2CAP_MODE_STREAMING: |
| 1748 | rfc.mode = L2CAP_MODE_STREAMING; |
| 1749 | rfc.txwin_size = 0; |
| 1750 | rfc.max_transmit = 0; |
| 1751 | rfc.retrans_timeout = 0; |
| 1752 | rfc.monitor_timeout = 0; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 1753 | rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1754 | if (L2CAP_DEFAULT_MAX_PDU_SIZE > chan->conn->mtu - 10) |
| 1755 | rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 1756 | |
Gustavo F. Padovan | 6340650 | 2010-08-03 23:49:29 -0300 | [diff] [blame] | 1757 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc), |
| 1758 | (unsigned long) &rfc); |
| 1759 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1760 | if (!(chan->conn->feat_mask & L2CAP_FEAT_FCS)) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1761 | break; |
| 1762 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1763 | if (chan->fcs == L2CAP_FCS_NONE || |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1764 | chan->conf_state & L2CAP_CONF_NO_FCS_RECV) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1765 | chan->fcs = L2CAP_FCS_NONE; |
| 1766 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, chan->fcs); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1767 | } |
Marcel Holtmann | 65c7c49 | 2009-05-02 23:07:53 -0700 | [diff] [blame] | 1768 | break; |
| 1769 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1771 | req->dcid = cpu_to_le16(chan->dcid); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1772 | req->flags = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | |
| 1774 | return ptr - data; |
| 1775 | } |
| 1776 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1777 | static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1779 | struct l2cap_conf_rsp *rsp = data; |
| 1780 | void *ptr = rsp->data; |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1781 | void *req = chan->conf_req; |
| 1782 | int len = chan->conf_len; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1783 | int type, hint, olen; |
| 1784 | unsigned long val; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 1785 | struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC }; |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 1786 | u16 mtu = L2CAP_DEFAULT_MTU; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1787 | u16 result = L2CAP_CONF_SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1789 | BT_DBG("chan %p", chan); |
Marcel Holtmann | 820ae1b | 2006-11-18 22:15:00 +0100 | [diff] [blame] | 1790 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1791 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 1792 | len -= l2cap_get_conf_opt(&req, &type, &olen, &val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | |
Gustavo F. Padovan | 589d274 | 2009-04-20 01:31:07 -0300 | [diff] [blame] | 1794 | hint = type & L2CAP_CONF_HINT; |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 1795 | type &= L2CAP_CONF_MASK; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1796 | |
| 1797 | switch (type) { |
| 1798 | case L2CAP_CONF_MTU: |
Marcel Holtmann | 861d688 | 2007-10-20 13:37:06 +0200 | [diff] [blame] | 1799 | mtu = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1800 | break; |
| 1801 | |
| 1802 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1803 | chan->flush_to = val; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1804 | break; |
| 1805 | |
| 1806 | case L2CAP_CONF_QOS: |
| 1807 | break; |
| 1808 | |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 1809 | case L2CAP_CONF_RFC: |
| 1810 | if (olen == sizeof(rfc)) |
| 1811 | memcpy(&rfc, (void *) val, olen); |
| 1812 | break; |
| 1813 | |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1814 | case L2CAP_CONF_FCS: |
| 1815 | if (val == L2CAP_FCS_NONE) |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1816 | chan->conf_state |= L2CAP_CONF_NO_FCS_RECV; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 1817 | |
| 1818 | break; |
| 1819 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1820 | default: |
| 1821 | if (hint) |
| 1822 | break; |
| 1823 | |
| 1824 | result = L2CAP_CONF_UNKNOWN; |
| 1825 | *((u8 *) ptr++) = type; |
| 1826 | break; |
| 1827 | } |
| 1828 | } |
| 1829 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1830 | if (chan->num_conf_rsp || chan->num_conf_req > 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1831 | goto done; |
| 1832 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1833 | switch (chan->mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1834 | case L2CAP_MODE_STREAMING: |
| 1835 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1836 | if (!(chan->conf_state & L2CAP_CONF_STATE2_DEVICE)) { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1837 | chan->mode = l2cap_select_mode(rfc.mode, |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1838 | chan->conn->feat_mask); |
Gustavo F. Padovan | 85eb53c | 2010-06-03 18:43:28 -0300 | [diff] [blame] | 1839 | break; |
| 1840 | } |
| 1841 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1842 | if (chan->mode != rfc.mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1843 | return -ECONNREFUSED; |
Gustavo F. Padovan | 742e519 | 2010-06-08 19:09:48 -0300 | [diff] [blame] | 1844 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1845 | break; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | done: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1849 | if (chan->mode != rfc.mode) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1850 | result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1851 | rfc.mode = chan->mode; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1852 | |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 1853 | if (chan->num_conf_rsp == 1) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1854 | return -ECONNREFUSED; |
| 1855 | |
| 1856 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 1857 | sizeof(rfc), (unsigned long) &rfc); |
| 1858 | } |
| 1859 | |
| 1860 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1861 | if (result == L2CAP_CONF_SUCCESS) { |
| 1862 | /* Configure output options and let the other side know |
| 1863 | * which ones we don't like. */ |
| 1864 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1865 | if (mtu < L2CAP_DEFAULT_MIN_MTU) |
| 1866 | result = L2CAP_CONF_UNACCEPT; |
| 1867 | else { |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1868 | chan->omtu = mtu; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1869 | chan->conf_state |= L2CAP_CONF_MTU_DONE; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1870 | } |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1871 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->omtu); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1872 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1873 | switch (rfc.mode) { |
| 1874 | case L2CAP_MODE_BASIC: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1875 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1876 | chan->conf_state |= L2CAP_CONF_MODE_DONE; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1877 | break; |
| 1878 | |
| 1879 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1880 | chan->remote_tx_win = rfc.txwin_size; |
| 1881 | chan->remote_max_tx = rfc.max_transmit; |
Mat Martineau | 86b1b26 | 2010-08-05 15:54:22 -0700 | [diff] [blame] | 1882 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1883 | if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) |
| 1884 | rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); |
Gustavo F. Padovan | 1c76215 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 1885 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1886 | chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1887 | |
Gustavo F. Padovan | 10467e9 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 1888 | rfc.retrans_timeout = |
| 1889 | le16_to_cpu(L2CAP_DEFAULT_RETRANS_TO); |
| 1890 | rfc.monitor_timeout = |
| 1891 | le16_to_cpu(L2CAP_DEFAULT_MONITOR_TO); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1892 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1893 | chan->conf_state |= L2CAP_CONF_MODE_DONE; |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 1894 | |
| 1895 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 1896 | sizeof(rfc), (unsigned long) &rfc); |
| 1897 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1898 | break; |
| 1899 | |
| 1900 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 1901 | if (le16_to_cpu(rfc.max_pdu_size) > chan->conn->mtu - 10) |
| 1902 | rfc.max_pdu_size = cpu_to_le16(chan->conn->mtu - 10); |
Gustavo F. Padovan | 1c76215 | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 1903 | |
Gustavo F. Padovan | 2c03a7a | 2011-03-25 20:15:28 -0300 | [diff] [blame] | 1904 | chan->remote_mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1905 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1906 | chan->conf_state |= L2CAP_CONF_MODE_DONE; |
Gustavo F. Padovan | 68ae663 | 2009-10-17 21:41:01 -0300 | [diff] [blame] | 1907 | |
| 1908 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 1909 | sizeof(rfc), (unsigned long) &rfc); |
| 1910 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1911 | break; |
| 1912 | |
| 1913 | default: |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 1914 | result = L2CAP_CONF_UNACCEPT; |
| 1915 | |
| 1916 | memset(&rfc, 0, sizeof(rfc)); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1917 | rfc.mode = chan->mode; |
Marcel Holtmann | 6464f35 | 2007-10-20 13:39:51 +0200 | [diff] [blame] | 1918 | } |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1919 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1920 | if (result == L2CAP_CONF_SUCCESS) |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1921 | chan->conf_state |= L2CAP_CONF_OUTPUT_DONE; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1922 | } |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1923 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 1924 | rsp->result = cpu_to_le16(result); |
| 1925 | rsp->flags = cpu_to_le16(0x0000); |
| 1926 | |
| 1927 | return ptr - data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | } |
| 1929 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1930 | 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] | 1931 | { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1932 | struct l2cap_conf_req *req = data; |
| 1933 | void *ptr = req->data; |
| 1934 | int type, olen; |
| 1935 | unsigned long val; |
| 1936 | struct l2cap_conf_rfc rfc; |
| 1937 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1938 | 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] | 1939 | |
| 1940 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 1941 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 1942 | |
| 1943 | switch (type) { |
| 1944 | case L2CAP_CONF_MTU: |
| 1945 | if (val < L2CAP_DEFAULT_MIN_MTU) { |
| 1946 | *result = L2CAP_CONF_UNACCEPT; |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1947 | chan->imtu = L2CAP_DEFAULT_MIN_MTU; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1948 | } else |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1949 | chan->imtu = val; |
| 1950 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, chan->imtu); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1951 | break; |
| 1952 | |
| 1953 | case L2CAP_CONF_FLUSH_TO: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1954 | chan->flush_to = val; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1955 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1956 | 2, chan->flush_to); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1957 | break; |
| 1958 | |
| 1959 | case L2CAP_CONF_RFC: |
| 1960 | if (olen == sizeof(rfc)) |
| 1961 | memcpy(&rfc, (void *)val, olen); |
| 1962 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 1963 | if ((chan->conf_state & L2CAP_CONF_STATE2_DEVICE) && |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1964 | rfc.mode != chan->mode) |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1965 | return -ECONNREFUSED; |
| 1966 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1967 | chan->fcs = 0; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1968 | |
| 1969 | l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, |
| 1970 | sizeof(rfc), (unsigned long) &rfc); |
| 1971 | break; |
| 1972 | } |
| 1973 | } |
| 1974 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1975 | if (chan->mode == L2CAP_MODE_BASIC && chan->mode != rfc.mode) |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 1976 | return -ECONNREFUSED; |
| 1977 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 1978 | chan->mode = rfc.mode; |
Gustavo F. Padovan | 6c2ea7a | 2010-06-08 20:08:49 -0300 | [diff] [blame] | 1979 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1980 | if (*result == L2CAP_CONF_SUCCESS) { |
| 1981 | switch (rfc.mode) { |
| 1982 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1983 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 1984 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 1985 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1986 | break; |
| 1987 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 1988 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1989 | } |
| 1990 | } |
| 1991 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1992 | req->dcid = cpu_to_le16(chan->dcid); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 1993 | req->flags = cpu_to_le16(0x0000); |
| 1994 | |
| 1995 | return ptr - data; |
| 1996 | } |
| 1997 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 1998 | 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] | 1999 | { |
| 2000 | struct l2cap_conf_rsp *rsp = data; |
| 2001 | void *ptr = rsp->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2003 | BT_DBG("chan %p", chan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2005 | rsp->scid = cpu_to_le16(chan->dcid); |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2006 | rsp->result = cpu_to_le16(result); |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2007 | rsp->flags = cpu_to_le16(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
| 2009 | return ptr - data; |
| 2010 | } |
| 2011 | |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2012 | void __l2cap_connect_rsp_defer(struct l2cap_chan *chan) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2013 | { |
| 2014 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 2015 | struct l2cap_conn *conn = chan->conn; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2016 | u8 buf[128]; |
| 2017 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2018 | rsp.scid = cpu_to_le16(chan->dcid); |
| 2019 | rsp.dcid = cpu_to_le16(chan->scid); |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2020 | rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS); |
| 2021 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
| 2022 | l2cap_send_cmd(conn, chan->ident, |
| 2023 | L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
| 2024 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2025 | if (chan->conf_state & L2CAP_CONF_REQ_SENT) |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2026 | return; |
| 2027 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2028 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Gustavo F. Padovan | 710f9b0 | 2011-03-25 14:30:37 -0300 | [diff] [blame] | 2029 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
| 2030 | l2cap_build_conf_req(chan, buf), buf); |
| 2031 | chan->num_conf_req++; |
| 2032 | } |
| 2033 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2034 | 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] | 2035 | { |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2036 | int type, olen; |
| 2037 | unsigned long val; |
| 2038 | struct l2cap_conf_rfc rfc; |
| 2039 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2040 | BT_DBG("chan %p, rsp %p, len %d", chan, rsp, len); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2041 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2042 | if ((chan->mode != L2CAP_MODE_ERTM) && (chan->mode != L2CAP_MODE_STREAMING)) |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2043 | return; |
| 2044 | |
| 2045 | while (len >= L2CAP_CONF_OPT_SIZE) { |
| 2046 | len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val); |
| 2047 | |
| 2048 | switch (type) { |
| 2049 | case L2CAP_CONF_RFC: |
| 2050 | if (olen == sizeof(rfc)) |
| 2051 | memcpy(&rfc, (void *)val, olen); |
| 2052 | goto done; |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | done: |
| 2057 | switch (rfc.mode) { |
| 2058 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2059 | chan->retrans_timeout = le16_to_cpu(rfc.retrans_timeout); |
| 2060 | chan->monitor_timeout = le16_to_cpu(rfc.monitor_timeout); |
| 2061 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2062 | break; |
| 2063 | case L2CAP_MODE_STREAMING: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2064 | chan->mps = le16_to_cpu(rfc.max_pdu_size); |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2065 | } |
| 2066 | } |
| 2067 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2068 | static inline int l2cap_command_rej(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2069 | { |
| 2070 | struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data; |
| 2071 | |
| 2072 | if (rej->reason != 0x0000) |
| 2073 | return 0; |
| 2074 | |
| 2075 | if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) && |
| 2076 | cmd->ident == conn->info_ident) { |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2077 | del_timer(&conn->info_timer); |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2078 | |
| 2079 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2080 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2081 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2082 | l2cap_conn_start(conn); |
| 2083 | } |
| 2084 | |
| 2085 | return 0; |
| 2086 | } |
| 2087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | static inline int l2cap_connect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2089 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; |
| 2091 | struct l2cap_conn_rsp rsp; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2092 | struct l2cap_chan *chan = NULL, *pchan; |
Nathan Holstein | d793fe8 | 2010-10-15 11:54:02 -0400 | [diff] [blame] | 2093 | struct sock *parent, *sk = NULL; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2094 | int result, status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | |
| 2096 | u16 dcid = 0, scid = __le16_to_cpu(req->scid); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2097 | __le16 psm = req->psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | |
| 2099 | BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid); |
| 2100 | |
| 2101 | /* Check if we have socket listening on psm */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2102 | pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src); |
| 2103 | if (!pchan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | result = L2CAP_CR_BAD_PSM; |
| 2105 | goto sendresp; |
| 2106 | } |
| 2107 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2108 | parent = pchan->sk; |
| 2109 | |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 2110 | bh_lock_sock(parent); |
| 2111 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2112 | /* Check if the ACL is secure enough (if not SDP) */ |
| 2113 | if (psm != cpu_to_le16(0x0001) && |
| 2114 | !hci_conn_check_link_mode(conn->hcon)) { |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 2115 | conn->disc_reason = 0x05; |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 2116 | result = L2CAP_CR_SEC_BLOCK; |
| 2117 | goto response; |
| 2118 | } |
| 2119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | result = L2CAP_CR_NO_MEM; |
| 2121 | |
| 2122 | /* Check for backlog size */ |
| 2123 | if (sk_acceptq_is_full(parent)) { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2124 | BT_DBG("backlog full %d", parent->sk_ack_backlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | goto response; |
| 2126 | } |
| 2127 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2128 | sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | if (!sk) |
| 2130 | goto response; |
| 2131 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 2132 | chan = l2cap_chan_create(sk); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2133 | if (!chan) { |
| 2134 | l2cap_sock_kill(sk); |
| 2135 | goto response; |
| 2136 | } |
| 2137 | |
Gustavo F. Padovan | 5d41ce1 | 2011-04-08 15:40:02 -0300 | [diff] [blame] | 2138 | l2cap_pi(sk)->chan = chan; |
| 2139 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2140 | write_lock_bh(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | |
| 2142 | /* Check if we already have channel with that dcid */ |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2143 | if (__l2cap_get_chan_by_dcid(conn, scid)) { |
| 2144 | write_unlock_bh(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2145 | sock_set_flag(sk, SOCK_ZAPPED); |
| 2146 | l2cap_sock_kill(sk); |
| 2147 | goto response; |
| 2148 | } |
| 2149 | |
| 2150 | hci_conn_hold(conn->hcon); |
| 2151 | |
| 2152 | l2cap_sock_init(sk, parent); |
| 2153 | bacpy(&bt_sk(sk)->src, conn->src); |
| 2154 | bacpy(&bt_sk(sk)->dst, conn->dst); |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2155 | chan->psm = psm; |
| 2156 | chan->dcid = scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
Gustavo F. Padovan | d101024 | 2011-03-25 00:39:48 -0300 | [diff] [blame] | 2158 | bt_accept_enqueue(parent, sk); |
| 2159 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2160 | __l2cap_chan_add(conn, chan); |
| 2161 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2162 | dcid = chan->scid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | |
| 2164 | l2cap_sock_set_timer(sk, sk->sk_sndtimeo); |
| 2165 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2166 | chan->ident = cmd->ident; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2168 | if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 2169 | if (l2cap_check_security(chan)) { |
Marcel Holtmann | f66dc81 | 2009-01-15 21:57:00 +0100 | [diff] [blame] | 2170 | if (bt_sk(sk)->defer_setup) { |
| 2171 | sk->sk_state = BT_CONNECT2; |
| 2172 | result = L2CAP_CR_PEND; |
| 2173 | status = L2CAP_CS_AUTHOR_PEND; |
| 2174 | parent->sk_data_ready(parent, 0); |
| 2175 | } else { |
| 2176 | sk->sk_state = BT_CONFIG; |
| 2177 | result = L2CAP_CR_SUCCESS; |
| 2178 | status = L2CAP_CS_NO_INFO; |
| 2179 | } |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2180 | } else { |
| 2181 | sk->sk_state = BT_CONNECT2; |
| 2182 | result = L2CAP_CR_PEND; |
| 2183 | status = L2CAP_CS_AUTHEN_PEND; |
| 2184 | } |
| 2185 | } else { |
| 2186 | sk->sk_state = BT_CONNECT2; |
| 2187 | result = L2CAP_CR_PEND; |
| 2188 | status = L2CAP_CS_NO_INFO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2191 | write_unlock_bh(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | |
| 2193 | response: |
| 2194 | bh_unlock_sock(parent); |
| 2195 | |
| 2196 | sendresp: |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2197 | rsp.scid = cpu_to_le16(scid); |
| 2198 | rsp.dcid = cpu_to_le16(dcid); |
| 2199 | rsp.result = cpu_to_le16(result); |
| 2200 | rsp.status = cpu_to_le16(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_RSP, sizeof(rsp), &rsp); |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2202 | |
| 2203 | if (result == L2CAP_CR_PEND && status == L2CAP_CS_NO_INFO) { |
| 2204 | struct l2cap_info_req info; |
| 2205 | info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 2206 | |
| 2207 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT; |
| 2208 | conn->info_ident = l2cap_get_ident(conn); |
| 2209 | |
| 2210 | mod_timer(&conn->info_timer, jiffies + |
| 2211 | msecs_to_jiffies(L2CAP_INFO_TIMEOUT)); |
| 2212 | |
| 2213 | l2cap_send_cmd(conn, conn->info_ident, |
| 2214 | L2CAP_INFO_REQ, sizeof(info), &info); |
| 2215 | } |
| 2216 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2217 | if (chan && !(chan->conf_state & L2CAP_CONF_REQ_SENT) && |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2218 | result == L2CAP_CR_SUCCESS) { |
| 2219 | u8 buf[128]; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2220 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2221 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2222 | l2cap_build_conf_req(chan, buf), buf); |
| 2223 | chan->num_conf_req++; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2224 | } |
| 2225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | return 0; |
| 2227 | } |
| 2228 | |
| 2229 | static inline int l2cap_connect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2230 | { |
| 2231 | struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data; |
| 2232 | u16 scid, dcid, result, status; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2233 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | struct sock *sk; |
| 2235 | u8 req[128]; |
| 2236 | |
| 2237 | scid = __le16_to_cpu(rsp->scid); |
| 2238 | dcid = __le16_to_cpu(rsp->dcid); |
| 2239 | result = __le16_to_cpu(rsp->result); |
| 2240 | status = __le16_to_cpu(rsp->status); |
| 2241 | |
| 2242 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status); |
| 2243 | |
| 2244 | if (scid) { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2245 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2246 | if (!chan) |
João Paulo Rechi Vita | 57d3b22 | 2010-06-22 13:56:26 -0300 | [diff] [blame] | 2247 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | } else { |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2249 | chan = l2cap_get_chan_by_ident(conn, cmd->ident); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2250 | if (!chan) |
João Paulo Rechi Vita | 57d3b22 | 2010-06-22 13:56:26 -0300 | [diff] [blame] | 2251 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | } |
| 2253 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2254 | sk = chan->sk; |
| 2255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | switch (result) { |
| 2257 | case L2CAP_CR_SUCCESS: |
| 2258 | sk->sk_state = BT_CONFIG; |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 2259 | chan->ident = 0; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2260 | chan->dcid = dcid; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2261 | chan->conf_state &= ~L2CAP_CONF_CONNECT_PEND; |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 2262 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2263 | if (chan->conf_state & L2CAP_CONF_REQ_SENT) |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2264 | break; |
| 2265 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2266 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Gustavo F. Padovan | e9aeb2d | 2010-07-08 20:08:18 -0300 | [diff] [blame] | 2267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2268 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2269 | l2cap_build_conf_req(chan, req), req); |
| 2270 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | break; |
| 2272 | |
| 2273 | case L2CAP_CR_PEND: |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2274 | chan->conf_state |= L2CAP_CONF_CONNECT_PEND; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | break; |
| 2276 | |
| 2277 | default: |
Andrei Emeltchenko | a49184c | 2010-11-03 12:32:44 +0200 | [diff] [blame] | 2278 | /* don't delete l2cap channel if sk is owned by user */ |
| 2279 | if (sock_owned_by_user(sk)) { |
| 2280 | sk->sk_state = BT_DISCONN; |
| 2281 | l2cap_sock_clear_timer(sk); |
| 2282 | l2cap_sock_set_timer(sk, HZ / 5); |
| 2283 | break; |
| 2284 | } |
| 2285 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2286 | l2cap_chan_del(chan, ECONNREFUSED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | break; |
| 2288 | } |
| 2289 | |
| 2290 | bh_unlock_sock(sk); |
| 2291 | return 0; |
| 2292 | } |
| 2293 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2294 | static inline void set_default_fcs(struct l2cap_chan *chan) |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2295 | { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2296 | struct l2cap_pinfo *pi = l2cap_pi(chan->sk); |
| 2297 | |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2298 | /* FCS is enabled only in ERTM or streaming mode, if one or both |
| 2299 | * sides request it. |
| 2300 | */ |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2301 | if (chan->mode != L2CAP_MODE_ERTM && chan->mode != L2CAP_MODE_STREAMING) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2302 | chan->fcs = L2CAP_FCS_NONE; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2303 | else if (!(pi->chan->conf_state & L2CAP_CONF_NO_FCS_RECV)) |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2304 | chan->fcs = L2CAP_FCS_CRC16; |
Mat Martineau | 8c462b6 | 2010-08-24 15:35:42 -0700 | [diff] [blame] | 2305 | } |
| 2306 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2307 | 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] | 2308 | { |
| 2309 | struct l2cap_conf_req *req = (struct l2cap_conf_req *) data; |
| 2310 | u16 dcid, flags; |
| 2311 | u8 rsp[64]; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2312 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | struct sock *sk; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2314 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | |
| 2316 | dcid = __le16_to_cpu(req->dcid); |
| 2317 | flags = __le16_to_cpu(req->flags); |
| 2318 | |
| 2319 | BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); |
| 2320 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2321 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2322 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | return -ENOENT; |
| 2324 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2325 | sk = chan->sk; |
| 2326 | |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2327 | if (sk->sk_state != BT_CONFIG) { |
| 2328 | struct l2cap_cmd_rej rej; |
| 2329 | |
| 2330 | rej.reason = cpu_to_le16(0x0002); |
| 2331 | l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ, |
| 2332 | sizeof(rej), &rej); |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2333 | goto unlock; |
Gustavo F. Padovan | df6bd74 | 2010-06-14 02:26:15 -0300 | [diff] [blame] | 2334 | } |
Marcel Holtmann | 354f60a | 2006-11-18 22:15:20 +0100 | [diff] [blame] | 2335 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2336 | /* Reject if config buffer is too small. */ |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2337 | len = cmd_len - sizeof(*req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2338 | if (chan->conf_len + len > sizeof(chan->conf_req)) { |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2339 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2340 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2341 | L2CAP_CONF_REJECT, flags), rsp); |
| 2342 | goto unlock; |
| 2343 | } |
| 2344 | |
| 2345 | /* Store config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2346 | memcpy(chan->conf_req + chan->conf_len, req->data, len); |
| 2347 | chan->conf_len += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | |
| 2349 | if (flags & 0x0001) { |
| 2350 | /* Incomplete config. Send empty response. */ |
| 2351 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2352 | l2cap_build_conf_rsp(chan, rsp, |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2353 | L2CAP_CONF_SUCCESS, 0x0001), rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2354 | goto unlock; |
| 2355 | } |
| 2356 | |
| 2357 | /* Complete config. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2358 | len = l2cap_parse_conf_req(chan, rsp); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2359 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2360 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | goto unlock; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2364 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rsp); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2365 | chan->num_conf_rsp++; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2366 | |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2367 | /* Reset config buffer. */ |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2368 | chan->conf_len = 0; |
Marcel Holtmann | 5dee9e7 | 2007-05-24 14:27:19 +0200 | [diff] [blame] | 2369 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2370 | if (!(chan->conf_state & L2CAP_CONF_OUTPUT_DONE)) |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2371 | goto unlock; |
| 2372 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2373 | if (chan->conf_state & L2CAP_CONF_INPUT_DONE) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2374 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | sk->sk_state = BT_CONNECTED; |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2377 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2378 | chan->next_tx_seq = 0; |
| 2379 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2380 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2381 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2382 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | l2cap_chan_ready(sk); |
Marcel Holtmann | 876d948 | 2007-10-20 13:35:42 +0200 | [diff] [blame] | 2385 | goto unlock; |
| 2386 | } |
| 2387 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2388 | if (!(chan->conf_state & L2CAP_CONF_REQ_SENT)) { |
Marcel Holtmann | 79d554a | 2008-07-14 20:13:44 +0200 | [diff] [blame] | 2389 | u8 buf[64]; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2390 | chan->conf_state |= L2CAP_CONF_REQ_SENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ, |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2392 | l2cap_build_conf_req(chan, buf), buf); |
| 2393 | chan->num_conf_req++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | unlock: |
| 2397 | bh_unlock_sock(sk); |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | static inline int l2cap_config_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2402 | { |
| 2403 | struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data; |
| 2404 | u16 scid, flags, result; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2405 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | struct sock *sk; |
Gustavo F. Padovan | 7b1c004 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 2407 | int len = cmd->len - sizeof(*rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | |
| 2409 | scid = __le16_to_cpu(rsp->scid); |
| 2410 | flags = __le16_to_cpu(rsp->flags); |
| 2411 | result = __le16_to_cpu(rsp->result); |
| 2412 | |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 2413 | BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x", |
| 2414 | scid, flags, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2416 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2417 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | return 0; |
| 2419 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2420 | sk = chan->sk; |
| 2421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2422 | switch (result) { |
| 2423 | case L2CAP_CONF_SUCCESS: |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2424 | l2cap_conf_rfc_get(chan, rsp->data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2425 | break; |
| 2426 | |
| 2427 | case L2CAP_CONF_UNACCEPT: |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2428 | if (chan->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) { |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2429 | char req[64]; |
| 2430 | |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 2431 | if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2432 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Andrei Emeltchenko | c2c77ec | 2010-03-19 10:26:28 +0200 | [diff] [blame] | 2433 | goto done; |
| 2434 | } |
| 2435 | |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2436 | /* throw out any old stored conf requests */ |
| 2437 | result = L2CAP_CONF_SUCCESS; |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2438 | len = l2cap_parse_conf_rsp(chan, rsp->data, len, |
| 2439 | req, &result); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2440 | if (len < 0) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2441 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2442 | goto done; |
| 2443 | } |
| 2444 | |
| 2445 | l2cap_send_cmd(conn, l2cap_get_ident(conn), |
| 2446 | L2CAP_CONF_REQ, len, req); |
Gustavo F. Padovan | 73ffa90 | 2011-03-25 14:16:54 -0300 | [diff] [blame] | 2447 | chan->num_conf_req++; |
Gustavo F. Padovan | f2fcfcd | 2009-07-04 15:06:24 -0300 | [diff] [blame] | 2448 | if (result != L2CAP_CONF_SUCCESS) |
| 2449 | goto done; |
| 2450 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | } |
| 2452 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 2453 | default: |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 2454 | sk->sk_err = ECONNRESET; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | l2cap_sock_set_timer(sk, HZ * 5); |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 2456 | l2cap_send_disconn_req(conn, chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | goto done; |
| 2458 | } |
| 2459 | |
| 2460 | if (flags & 0x01) |
| 2461 | goto done; |
| 2462 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2463 | chan->conf_state |= L2CAP_CONF_INPUT_DONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 2465 | if (chan->conf_state & L2CAP_CONF_OUTPUT_DONE) { |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2466 | set_default_fcs(chan); |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | sk->sk_state = BT_CONNECTED; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2469 | chan->next_tx_seq = 0; |
| 2470 | chan->expected_tx_seq = 0; |
Gustavo F. Padovan | 58d35f8 | 2011-04-04 16:16:44 -0300 | [diff] [blame] | 2471 | skb_queue_head_init(&chan->tx_q); |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2472 | if (chan->mode == L2CAP_MODE_ERTM) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2473 | l2cap_ertm_init(chan); |
Gustavo F. Padovan | 0565c1c | 2009-10-03 02:34:36 -0300 | [diff] [blame] | 2474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | l2cap_chan_ready(sk); |
| 2476 | } |
| 2477 | |
| 2478 | done: |
| 2479 | bh_unlock_sock(sk); |
| 2480 | return 0; |
| 2481 | } |
| 2482 | |
| 2483 | static inline int l2cap_disconnect_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2484 | { |
| 2485 | struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data; |
| 2486 | struct l2cap_disconn_rsp rsp; |
| 2487 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2488 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | struct sock *sk; |
| 2490 | |
| 2491 | scid = __le16_to_cpu(req->scid); |
| 2492 | dcid = __le16_to_cpu(req->dcid); |
| 2493 | |
| 2494 | BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid); |
| 2495 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2496 | chan = l2cap_get_chan_by_scid(conn, dcid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2497 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | return 0; |
| 2499 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2500 | sk = chan->sk; |
| 2501 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 2502 | rsp.dcid = cpu_to_le16(chan->scid); |
| 2503 | rsp.scid = cpu_to_le16(chan->dcid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); |
| 2505 | |
| 2506 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 2507 | |
Andrei Emeltchenko | a49184c | 2010-11-03 12:32:44 +0200 | [diff] [blame] | 2508 | /* don't delete l2cap channel if sk is owned by user */ |
| 2509 | if (sock_owned_by_user(sk)) { |
| 2510 | sk->sk_state = BT_DISCONN; |
| 2511 | l2cap_sock_clear_timer(sk); |
| 2512 | l2cap_sock_set_timer(sk, HZ / 5); |
| 2513 | bh_unlock_sock(sk); |
| 2514 | return 0; |
| 2515 | } |
| 2516 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2517 | l2cap_chan_del(chan, ECONNRESET); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | bh_unlock_sock(sk); |
| 2519 | |
| 2520 | l2cap_sock_kill(sk); |
| 2521 | return 0; |
| 2522 | } |
| 2523 | |
| 2524 | static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2525 | { |
| 2526 | struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data; |
| 2527 | u16 dcid, scid; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2528 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | struct sock *sk; |
| 2530 | |
| 2531 | scid = __le16_to_cpu(rsp->scid); |
| 2532 | dcid = __le16_to_cpu(rsp->dcid); |
| 2533 | |
| 2534 | BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid); |
| 2535 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 2536 | chan = l2cap_get_chan_by_scid(conn, scid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2537 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 | return 0; |
| 2539 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2540 | sk = chan->sk; |
| 2541 | |
Andrei Emeltchenko | a49184c | 2010-11-03 12:32:44 +0200 | [diff] [blame] | 2542 | /* don't delete l2cap channel if sk is owned by user */ |
| 2543 | if (sock_owned_by_user(sk)) { |
| 2544 | sk->sk_state = BT_DISCONN; |
| 2545 | l2cap_sock_clear_timer(sk); |
| 2546 | l2cap_sock_set_timer(sk, HZ / 5); |
| 2547 | bh_unlock_sock(sk); |
| 2548 | return 0; |
| 2549 | } |
| 2550 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 2551 | l2cap_chan_del(chan, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2552 | bh_unlock_sock(sk); |
| 2553 | |
| 2554 | l2cap_sock_kill(sk); |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
| 2558 | static inline int l2cap_information_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2559 | { |
| 2560 | struct l2cap_info_req *req = (struct l2cap_info_req *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | u16 type; |
| 2562 | |
| 2563 | type = __le16_to_cpu(req->type); |
| 2564 | |
| 2565 | BT_DBG("type 0x%4.4x", type); |
| 2566 | |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 2567 | if (type == L2CAP_IT_FEAT_MASK) { |
| 2568 | u8 buf[8]; |
Marcel Holtmann | 44dd46d | 2009-05-02 19:09:01 -0700 | [diff] [blame] | 2569 | u32 feat_mask = l2cap_feat_mask; |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 2570 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
| 2571 | rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK); |
| 2572 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 2573 | if (!disable_ertm) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2574 | feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING |
| 2575 | | L2CAP_FEAT_FCS; |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 2576 | put_unaligned_le32(feat_mask, rsp->data); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 2577 | l2cap_send_cmd(conn, cmd->ident, |
| 2578 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2579 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
| 2580 | u8 buf[12]; |
| 2581 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; |
| 2582 | rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 2583 | rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS); |
| 2584 | memcpy(buf + 4, l2cap_fixed_chan, 8); |
| 2585 | l2cap_send_cmd(conn, cmd->ident, |
| 2586 | L2CAP_INFO_RSP, sizeof(buf), buf); |
Marcel Holtmann | f0709e0 | 2007-10-20 13:38:51 +0200 | [diff] [blame] | 2587 | } else { |
| 2588 | struct l2cap_info_rsp rsp; |
| 2589 | rsp.type = cpu_to_le16(type); |
| 2590 | rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP); |
| 2591 | l2cap_send_cmd(conn, cmd->ident, |
| 2592 | L2CAP_INFO_RSP, sizeof(rsp), &rsp); |
| 2593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | |
| 2595 | return 0; |
| 2596 | } |
| 2597 | |
| 2598 | static inline int l2cap_information_rsp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2599 | { |
| 2600 | struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data; |
| 2601 | u16 type, result; |
| 2602 | |
| 2603 | type = __le16_to_cpu(rsp->type); |
| 2604 | result = __le16_to_cpu(rsp->result); |
| 2605 | |
| 2606 | BT_DBG("type 0x%4.4x result 0x%2.2x", type, result); |
| 2607 | |
Andrei Emeltchenko | e90165b | 2011-03-25 11:31:41 +0200 | [diff] [blame] | 2608 | /* L2CAP Info req/rsp are unbound to channels, add extra checks */ |
| 2609 | if (cmd->ident != conn->info_ident || |
| 2610 | conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) |
| 2611 | return 0; |
| 2612 | |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2613 | del_timer(&conn->info_timer); |
| 2614 | |
Ville Tervo | adb08ed | 2010-08-04 09:43:33 +0300 | [diff] [blame] | 2615 | if (result != L2CAP_IR_SUCCESS) { |
| 2616 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 2617 | conn->info_ident = 0; |
| 2618 | |
| 2619 | l2cap_conn_start(conn); |
| 2620 | |
| 2621 | return 0; |
| 2622 | } |
| 2623 | |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2624 | if (type == L2CAP_IT_FEAT_MASK) { |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 2625 | conn->feat_mask = get_unaligned_le32(rsp->data); |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2626 | |
Marcel Holtmann | 47ec1dcd | 2009-05-02 18:57:55 -0700 | [diff] [blame] | 2627 | if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) { |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2628 | struct l2cap_info_req req; |
| 2629 | req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN); |
| 2630 | |
| 2631 | conn->info_ident = l2cap_get_ident(conn); |
| 2632 | |
| 2633 | l2cap_send_cmd(conn, conn->info_ident, |
| 2634 | L2CAP_INFO_REQ, sizeof(req), &req); |
| 2635 | } else { |
| 2636 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
| 2637 | conn->info_ident = 0; |
| 2638 | |
| 2639 | l2cap_conn_start(conn); |
| 2640 | } |
| 2641 | } else if (type == L2CAP_IT_FIXED_CHAN) { |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2642 | conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE; |
Marcel Holtmann | e1027a7 | 2009-02-09 09:18:02 +0100 | [diff] [blame] | 2643 | conn->info_ident = 0; |
Marcel Holtmann | 984947d | 2009-02-06 23:35:19 +0100 | [diff] [blame] | 2644 | |
| 2645 | l2cap_conn_start(conn); |
| 2646 | } |
Marcel Holtmann | 4e8402a | 2007-10-20 13:37:56 +0200 | [diff] [blame] | 2647 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | return 0; |
| 2649 | } |
| 2650 | |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 2651 | 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] | 2652 | u16 to_multiplier) |
| 2653 | { |
| 2654 | u16 max_latency; |
| 2655 | |
| 2656 | if (min > max || min < 6 || max > 3200) |
| 2657 | return -EINVAL; |
| 2658 | |
| 2659 | if (to_multiplier < 10 || to_multiplier > 3200) |
| 2660 | return -EINVAL; |
| 2661 | |
| 2662 | if (max >= to_multiplier * 8) |
| 2663 | return -EINVAL; |
| 2664 | |
| 2665 | max_latency = (to_multiplier * 8 / max) - 1; |
| 2666 | if (latency > 499 || latency > max_latency) |
| 2667 | return -EINVAL; |
| 2668 | |
| 2669 | return 0; |
| 2670 | } |
| 2671 | |
| 2672 | static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn, |
| 2673 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2674 | { |
| 2675 | struct hci_conn *hcon = conn->hcon; |
| 2676 | struct l2cap_conn_param_update_req *req; |
| 2677 | struct l2cap_conn_param_update_rsp rsp; |
| 2678 | u16 min, max, latency, to_multiplier, cmd_len; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 2679 | int err; |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2680 | |
| 2681 | if (!(hcon->link_mode & HCI_LM_MASTER)) |
| 2682 | return -EINVAL; |
| 2683 | |
| 2684 | cmd_len = __le16_to_cpu(cmd->len); |
| 2685 | if (cmd_len != sizeof(struct l2cap_conn_param_update_req)) |
| 2686 | return -EPROTO; |
| 2687 | |
| 2688 | req = (struct l2cap_conn_param_update_req *) data; |
Gustavo F. Padovan | e2174ca | 2011-02-17 19:16:55 -0300 | [diff] [blame] | 2689 | min = __le16_to_cpu(req->min); |
| 2690 | max = __le16_to_cpu(req->max); |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2691 | latency = __le16_to_cpu(req->latency); |
| 2692 | to_multiplier = __le16_to_cpu(req->to_multiplier); |
| 2693 | |
| 2694 | BT_DBG("min 0x%4.4x max 0x%4.4x latency: 0x%4.4x Timeout: 0x%4.4x", |
| 2695 | min, max, latency, to_multiplier); |
| 2696 | |
| 2697 | memset(&rsp, 0, sizeof(rsp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 2698 | |
| 2699 | err = l2cap_check_conn_param(min, max, latency, to_multiplier); |
| 2700 | if (err) |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2701 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED); |
| 2702 | else |
| 2703 | rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED); |
| 2704 | |
| 2705 | l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP, |
| 2706 | sizeof(rsp), &rsp); |
| 2707 | |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 2708 | if (!err) |
| 2709 | hci_le_conn_update(hcon, min, max, latency, to_multiplier); |
| 2710 | |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2711 | return 0; |
| 2712 | } |
| 2713 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2714 | static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn, |
| 2715 | struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data) |
| 2716 | { |
| 2717 | int err = 0; |
| 2718 | |
| 2719 | switch (cmd->code) { |
| 2720 | case L2CAP_COMMAND_REJ: |
| 2721 | l2cap_command_rej(conn, cmd, data); |
| 2722 | break; |
| 2723 | |
| 2724 | case L2CAP_CONN_REQ: |
| 2725 | err = l2cap_connect_req(conn, cmd, data); |
| 2726 | break; |
| 2727 | |
| 2728 | case L2CAP_CONN_RSP: |
| 2729 | err = l2cap_connect_rsp(conn, cmd, data); |
| 2730 | break; |
| 2731 | |
| 2732 | case L2CAP_CONF_REQ: |
| 2733 | err = l2cap_config_req(conn, cmd, cmd_len, data); |
| 2734 | break; |
| 2735 | |
| 2736 | case L2CAP_CONF_RSP: |
| 2737 | err = l2cap_config_rsp(conn, cmd, data); |
| 2738 | break; |
| 2739 | |
| 2740 | case L2CAP_DISCONN_REQ: |
| 2741 | err = l2cap_disconnect_req(conn, cmd, data); |
| 2742 | break; |
| 2743 | |
| 2744 | case L2CAP_DISCONN_RSP: |
| 2745 | err = l2cap_disconnect_rsp(conn, cmd, data); |
| 2746 | break; |
| 2747 | |
| 2748 | case L2CAP_ECHO_REQ: |
| 2749 | l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data); |
| 2750 | break; |
| 2751 | |
| 2752 | case L2CAP_ECHO_RSP: |
| 2753 | break; |
| 2754 | |
| 2755 | case L2CAP_INFO_REQ: |
| 2756 | err = l2cap_information_req(conn, cmd, data); |
| 2757 | break; |
| 2758 | |
| 2759 | case L2CAP_INFO_RSP: |
| 2760 | err = l2cap_information_rsp(conn, cmd, data); |
| 2761 | break; |
| 2762 | |
| 2763 | default: |
| 2764 | BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code); |
| 2765 | err = -EINVAL; |
| 2766 | break; |
| 2767 | } |
| 2768 | |
| 2769 | return err; |
| 2770 | } |
| 2771 | |
| 2772 | static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn, |
| 2773 | struct l2cap_cmd_hdr *cmd, u8 *data) |
| 2774 | { |
| 2775 | switch (cmd->code) { |
| 2776 | case L2CAP_COMMAND_REJ: |
| 2777 | return 0; |
| 2778 | |
| 2779 | case L2CAP_CONN_PARAM_UPDATE_REQ: |
Claudio Takahasi | de73115 | 2011-02-11 19:28:55 -0200 | [diff] [blame] | 2780 | return l2cap_conn_param_update_req(conn, cmd, data); |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2781 | |
| 2782 | case L2CAP_CONN_PARAM_UPDATE_RSP: |
| 2783 | return 0; |
| 2784 | |
| 2785 | default: |
| 2786 | BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code); |
| 2787 | return -EINVAL; |
| 2788 | } |
| 2789 | } |
| 2790 | |
| 2791 | static inline void l2cap_sig_channel(struct l2cap_conn *conn, |
| 2792 | struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | { |
| 2794 | u8 *data = skb->data; |
| 2795 | int len = skb->len; |
| 2796 | struct l2cap_cmd_hdr cmd; |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2797 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | |
| 2799 | l2cap_raw_recv(conn, skb); |
| 2800 | |
| 2801 | while (len >= L2CAP_CMD_HDR_SIZE) { |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2802 | u16 cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); |
| 2804 | data += L2CAP_CMD_HDR_SIZE; |
| 2805 | len -= L2CAP_CMD_HDR_SIZE; |
| 2806 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2807 | cmd_len = le16_to_cpu(cmd.len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2808 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2809 | 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] | 2810 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2811 | if (cmd_len > len || !cmd.ident) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | BT_DBG("corrupted command"); |
| 2813 | break; |
| 2814 | } |
| 2815 | |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 2816 | if (conn->hcon->type == LE_LINK) |
| 2817 | err = l2cap_le_sig_cmd(conn, &cmd, data); |
| 2818 | else |
| 2819 | err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | |
| 2821 | if (err) { |
| 2822 | struct l2cap_cmd_rej rej; |
Gustavo F. Padovan | 2c6d1a2 | 2011-03-23 14:38:32 -0300 | [diff] [blame] | 2823 | |
| 2824 | BT_ERR("Wrong link type (%d)", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | |
| 2826 | /* FIXME: Map err to a valid reason */ |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 2827 | rej.reason = cpu_to_le16(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej); |
| 2829 | } |
| 2830 | |
Al Viro | 88219a0 | 2007-07-29 00:17:25 -0700 | [diff] [blame] | 2831 | data += cmd_len; |
| 2832 | len -= cmd_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | } |
| 2834 | |
| 2835 | kfree_skb(skb); |
| 2836 | } |
| 2837 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2838 | 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] | 2839 | { |
| 2840 | u16 our_fcs, rcv_fcs; |
| 2841 | int hdr_size = L2CAP_HDR_SIZE + 2; |
| 2842 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 2843 | if (chan->fcs == L2CAP_FCS_CRC16) { |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2844 | skb_trim(skb, skb->len - 2); |
| 2845 | rcv_fcs = get_unaligned_le16(skb->data + skb->len); |
| 2846 | our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size); |
| 2847 | |
| 2848 | if (our_fcs != rcv_fcs) |
João Paulo Rechi Vita | 7a560e5 | 2010-06-22 13:56:27 -0300 | [diff] [blame] | 2849 | return -EBADMSG; |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 2850 | } |
| 2851 | return 0; |
| 2852 | } |
| 2853 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2854 | 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] | 2855 | { |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 2856 | u16 control = 0; |
| 2857 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2858 | chan->frames_sent = 0; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 2859 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2860 | control |= chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 2861 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2862 | if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { |
Gustavo F. Padovan | 6498886 | 2010-05-10 14:54:14 -0300 | [diff] [blame] | 2863 | control |= L2CAP_SUPER_RCV_NOT_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2864 | l2cap_send_sframe(chan, control); |
| 2865 | chan->conn_state |= L2CAP_CONN_RNR_SENT; |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 2866 | } |
| 2867 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2868 | if (chan->conn_state & L2CAP_CONN_REMOTE_BUSY) |
| 2869 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 2870 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2871 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 2872 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2873 | if (!(chan->conn_state & L2CAP_CONN_LOCAL_BUSY) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 2874 | chan->frames_sent == 0) { |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 2875 | control |= L2CAP_SUPER_RCV_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2876 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | d5392c8 | 2010-05-01 16:15:36 -0300 | [diff] [blame] | 2877 | } |
| 2878 | } |
| 2879 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2880 | static int l2cap_add_to_srej_queue(struct l2cap_chan *chan, struct sk_buff *skb, u8 tx_seq, u8 sar) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 2881 | { |
| 2882 | struct sk_buff *next_skb; |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 2883 | int tx_seq_offset, next_tx_seq_offset; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 2884 | |
| 2885 | bt_cb(skb)->tx_seq = tx_seq; |
| 2886 | bt_cb(skb)->sar = sar; |
| 2887 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 2888 | next_skb = skb_peek(&chan->srej_q); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 2889 | if (!next_skb) { |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 2890 | __skb_queue_tail(&chan->srej_q, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2891 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 2892 | } |
| 2893 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2894 | tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 2895 | if (tx_seq_offset < 0) |
| 2896 | tx_seq_offset += 64; |
| 2897 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 2898 | do { |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2899 | if (bt_cb(next_skb)->tx_seq == tx_seq) |
| 2900 | return -EINVAL; |
| 2901 | |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 2902 | next_tx_seq_offset = (bt_cb(next_skb)->tx_seq - |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 2903 | chan->buffer_seq) % 64; |
João Paulo Rechi Vita | bfbacc1 | 2010-05-31 18:35:44 -0300 | [diff] [blame] | 2904 | if (next_tx_seq_offset < 0) |
| 2905 | next_tx_seq_offset += 64; |
| 2906 | |
| 2907 | if (next_tx_seq_offset > tx_seq_offset) { |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 2908 | __skb_queue_before(&chan->srej_q, next_skb, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2909 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 2910 | } |
| 2911 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 2912 | if (skb_queue_is_last(&chan->srej_q, next_skb)) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 2913 | break; |
| 2914 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 2915 | } while ((next_skb = skb_queue_next(&chan->srej_q, next_skb))); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 2916 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 2917 | __skb_queue_tail(&chan->srej_q, skb); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2918 | |
| 2919 | return 0; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 2920 | } |
| 2921 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2922 | static int l2cap_ertm_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2923 | { |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2924 | struct sk_buff *_skb; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2925 | int err; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2926 | |
| 2927 | switch (control & L2CAP_CTRL_SAR) { |
| 2928 | case L2CAP_SDU_UNSEGMENTED: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2929 | if (chan->conn_state & L2CAP_CONN_SAR_SDU) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2930 | goto drop; |
| 2931 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2932 | err = sock_queue_rcv_skb(chan->sk, skb); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2933 | if (!err) |
| 2934 | return err; |
| 2935 | |
| 2936 | break; |
| 2937 | |
| 2938 | case L2CAP_SDU_START: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2939 | if (chan->conn_state & L2CAP_CONN_SAR_SDU) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2940 | goto drop; |
| 2941 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2942 | chan->sdu_len = get_unaligned_le16(skb->data); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2943 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2944 | if (chan->sdu_len > chan->imtu) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2945 | goto disconnect; |
| 2946 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2947 | chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); |
| 2948 | if (!chan->sdu) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2949 | return -ENOMEM; |
| 2950 | |
| 2951 | /* pull sdu_len bytes only after alloc, because of Local Busy |
| 2952 | * condition we have to be sure that this will be executed |
| 2953 | * only once, i.e., when alloc does not fail */ |
| 2954 | skb_pull(skb, 2); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2955 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2956 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2957 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2958 | chan->conn_state |= L2CAP_CONN_SAR_SDU; |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2959 | chan->partial_sdu_len = skb->len; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2960 | break; |
| 2961 | |
| 2962 | case L2CAP_SDU_CONTINUE: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2963 | if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2964 | goto disconnect; |
| 2965 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2966 | if (!chan->sdu) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2967 | goto disconnect; |
| 2968 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2969 | chan->partial_sdu_len += skb->len; |
| 2970 | if (chan->partial_sdu_len > chan->sdu_len) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2971 | goto drop; |
| 2972 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2973 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 2974 | |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2975 | break; |
| 2976 | |
| 2977 | case L2CAP_SDU_END: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2978 | if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2979 | goto disconnect; |
| 2980 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2981 | if (!chan->sdu) |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2982 | goto disconnect; |
| 2983 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2984 | if (!(chan->conn_state & L2CAP_CONN_SAR_RETRY)) { |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2985 | chan->partial_sdu_len += skb->len; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2986 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 2987 | if (chan->partial_sdu_len > chan->imtu) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2988 | goto drop; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2989 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2990 | if (chan->partial_sdu_len != chan->sdu_len) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2991 | goto drop; |
Gustavo F. Padovan | 4178ba4 | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 2992 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2993 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2994 | } |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2995 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 2996 | _skb = skb_clone(chan->sdu, GFP_ATOMIC); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2997 | if (!_skb) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 2998 | chan->conn_state |= L2CAP_CONN_SAR_RETRY; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 2999 | return -ENOMEM; |
| 3000 | } |
| 3001 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3002 | err = sock_queue_rcv_skb(chan->sk, _skb); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3003 | if (err < 0) { |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3004 | kfree_skb(_skb); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3005 | chan->conn_state |= L2CAP_CONN_SAR_RETRY; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3006 | return err; |
| 3007 | } |
| 3008 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3009 | chan->conn_state &= ~L2CAP_CONN_SAR_RETRY; |
| 3010 | chan->conn_state &= ~L2CAP_CONN_SAR_SDU; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3011 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3012 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3013 | break; |
| 3014 | } |
| 3015 | |
| 3016 | kfree_skb(skb); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3017 | return 0; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3018 | |
| 3019 | drop: |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3020 | kfree_skb(chan->sdu); |
| 3021 | chan->sdu = NULL; |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3022 | |
| 3023 | disconnect: |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3024 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3025 | kfree_skb(skb); |
| 3026 | return 0; |
| 3027 | } |
| 3028 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3029 | static int l2cap_try_push_rx_skb(struct l2cap_chan *chan) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3030 | { |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3031 | struct sk_buff *skb; |
| 3032 | u16 control; |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3033 | int err; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3034 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3035 | while ((skb = skb_dequeue(&chan->busy_q))) { |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3036 | control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3037 | err = l2cap_ertm_reassembly_sdu(chan, skb, control); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3038 | if (err < 0) { |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3039 | skb_queue_head(&chan->busy_q, skb); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3040 | return -EBUSY; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3041 | } |
| 3042 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3043 | chan->buffer_seq = (chan->buffer_seq + 1) % 64; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3044 | } |
| 3045 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3046 | if (!(chan->conn_state & L2CAP_CONN_RNR_SENT)) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3047 | goto done; |
| 3048 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3049 | control = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3050 | control |= L2CAP_SUPER_RCV_READY | L2CAP_CTRL_POLL; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3051 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3052 | chan->retry_count = 1; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3053 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3054 | del_timer(&chan->retrans_timer); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3055 | __mod_monitor_timer(); |
| 3056 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3057 | chan->conn_state |= L2CAP_CONN_WAIT_F; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3058 | |
| 3059 | done: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3060 | chan->conn_state &= ~L2CAP_CONN_LOCAL_BUSY; |
| 3061 | chan->conn_state &= ~L2CAP_CONN_RNR_SENT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3062 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3063 | BT_DBG("chan %p, Exit local busy", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3064 | |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3065 | return 0; |
| 3066 | } |
| 3067 | |
| 3068 | static void l2cap_busy_work(struct work_struct *work) |
| 3069 | { |
| 3070 | DECLARE_WAITQUEUE(wait, current); |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3071 | struct l2cap_chan *chan = |
| 3072 | container_of(work, struct l2cap_chan, busy_work); |
| 3073 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3074 | int n_tries = 0, timeo = HZ/5, err; |
| 3075 | struct sk_buff *skb; |
| 3076 | |
| 3077 | lock_sock(sk); |
| 3078 | |
| 3079 | add_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3080 | while ((skb = skb_peek(&chan->busy_q))) { |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3081 | set_current_state(TASK_INTERRUPTIBLE); |
| 3082 | |
| 3083 | if (n_tries++ > L2CAP_LOCAL_BUSY_TRIES) { |
| 3084 | err = -EBUSY; |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3085 | l2cap_send_disconn_req(chan->conn, chan, EBUSY); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3086 | break; |
| 3087 | } |
| 3088 | |
| 3089 | if (!timeo) |
| 3090 | timeo = HZ/5; |
| 3091 | |
| 3092 | if (signal_pending(current)) { |
| 3093 | err = sock_intr_errno(timeo); |
| 3094 | break; |
| 3095 | } |
| 3096 | |
| 3097 | release_sock(sk); |
| 3098 | timeo = schedule_timeout(timeo); |
| 3099 | lock_sock(sk); |
| 3100 | |
| 3101 | err = sock_error(sk); |
| 3102 | if (err) |
| 3103 | break; |
| 3104 | |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3105 | if (l2cap_try_push_rx_skb(chan) == 0) |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3106 | break; |
| 3107 | } |
| 3108 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3109 | set_current_state(TASK_RUNNING); |
Marcel Holtmann | 2b0b05d | 2010-05-10 11:33:10 +0200 | [diff] [blame] | 3110 | remove_wait_queue(sk_sleep(sk), &wait); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3111 | |
| 3112 | release_sock(sk); |
| 3113 | } |
| 3114 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3115 | static int l2cap_push_rx_skb(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3116 | { |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3117 | int sctrl, err; |
| 3118 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3119 | if (chan->conn_state & L2CAP_CONN_LOCAL_BUSY) { |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3120 | bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3121 | __skb_queue_tail(&chan->busy_q, skb); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3122 | return l2cap_try_push_rx_skb(chan); |
Gustavo F. Padovan | 712132e | 2010-06-21 19:39:50 -0300 | [diff] [blame] | 3123 | |
| 3124 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3125 | } |
| 3126 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3127 | err = l2cap_ertm_reassembly_sdu(chan, skb, control); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3128 | if (err >= 0) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3129 | chan->buffer_seq = (chan->buffer_seq + 1) % 64; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3130 | return err; |
| 3131 | } |
| 3132 | |
| 3133 | /* Busy Condition */ |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3134 | BT_DBG("chan %p, Enter local busy", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3135 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3136 | chan->conn_state |= L2CAP_CONN_LOCAL_BUSY; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3137 | bt_cb(skb)->sar = control >> L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3138 | __skb_queue_tail(&chan->busy_q, skb); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3139 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3140 | sctrl = chan->buffer_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3141 | sctrl |= L2CAP_SUPER_RCV_NOT_READY; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3142 | l2cap_send_sframe(chan, sctrl); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3143 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3144 | chan->conn_state |= L2CAP_CONN_RNR_SENT; |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3145 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3146 | del_timer(&chan->ack_timer); |
Gustavo F. Padovan | 7fe9b29 | 2010-05-12 18:32:04 -0300 | [diff] [blame] | 3147 | |
Gustavo F. Padovan | 311bb89 | 2011-03-25 20:41:00 -0300 | [diff] [blame] | 3148 | queue_work(_busy_wq, &chan->busy_work); |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3149 | |
| 3150 | return err; |
| 3151 | } |
| 3152 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3153 | static int l2cap_streaming_reassembly_sdu(struct l2cap_chan *chan, struct sk_buff *skb, u16 control) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3154 | { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3155 | struct sk_buff *_skb; |
| 3156 | int err = -EINVAL; |
| 3157 | |
Gustavo F. Padovan | 18778a6 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3158 | /* |
| 3159 | * TODO: We have to notify the userland if some data is lost with the |
| 3160 | * Streaming Mode. |
| 3161 | */ |
| 3162 | |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3163 | switch (control & L2CAP_CTRL_SAR) { |
| 3164 | case L2CAP_SDU_UNSEGMENTED: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3165 | if (chan->conn_state & L2CAP_CONN_SAR_SDU) { |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3166 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3167 | break; |
| 3168 | } |
| 3169 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3170 | err = sock_queue_rcv_skb(chan->sk, skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3171 | if (!err) |
| 3172 | return 0; |
| 3173 | |
| 3174 | break; |
| 3175 | |
| 3176 | case L2CAP_SDU_START: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3177 | if (chan->conn_state & L2CAP_CONN_SAR_SDU) { |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3178 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3179 | break; |
| 3180 | } |
| 3181 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3182 | chan->sdu_len = get_unaligned_le16(skb->data); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3183 | skb_pull(skb, 2); |
| 3184 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3185 | if (chan->sdu_len > chan->imtu) { |
Gustavo F. Padovan | 052897c | 2010-05-01 16:15:40 -0300 | [diff] [blame] | 3186 | err = -EMSGSIZE; |
| 3187 | break; |
| 3188 | } |
| 3189 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3190 | chan->sdu = bt_skb_alloc(chan->sdu_len, GFP_ATOMIC); |
| 3191 | if (!chan->sdu) { |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3192 | err = -ENOMEM; |
| 3193 | break; |
| 3194 | } |
| 3195 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3196 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3197 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3198 | chan->conn_state |= L2CAP_CONN_SAR_SDU; |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3199 | chan->partial_sdu_len = skb->len; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3200 | err = 0; |
| 3201 | break; |
| 3202 | |
| 3203 | case L2CAP_SDU_CONTINUE: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3204 | if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3205 | break; |
| 3206 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3207 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3208 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3209 | chan->partial_sdu_len += skb->len; |
| 3210 | if (chan->partial_sdu_len > chan->sdu_len) |
| 3211 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3212 | else |
| 3213 | err = 0; |
| 3214 | |
| 3215 | break; |
| 3216 | |
| 3217 | case L2CAP_SDU_END: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3218 | if (!(chan->conn_state & L2CAP_CONN_SAR_SDU)) |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3219 | break; |
| 3220 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3221 | memcpy(skb_put(chan->sdu, skb->len), skb->data, skb->len); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3222 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3223 | chan->conn_state &= ~L2CAP_CONN_SAR_SDU; |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3224 | chan->partial_sdu_len += skb->len; |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3225 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3226 | if (chan->partial_sdu_len > chan->imtu) |
Gustavo F. Padovan | 36f2fd5 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3227 | goto drop; |
| 3228 | |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3229 | if (chan->partial_sdu_len == chan->sdu_len) { |
| 3230 | _skb = skb_clone(chan->sdu, GFP_ATOMIC); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3231 | err = sock_queue_rcv_skb(chan->sk, _skb); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3232 | if (err < 0) |
| 3233 | kfree_skb(_skb); |
| 3234 | } |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3235 | err = 0; |
| 3236 | |
Gustavo F. Padovan | 36f2fd5 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3237 | drop: |
Gustavo F. Padovan | 6f61fd4 | 2011-03-25 20:09:37 -0300 | [diff] [blame] | 3238 | kfree_skb(chan->sdu); |
Gustavo F. Padovan | c74e560 | 2009-08-20 22:25:58 -0300 | [diff] [blame] | 3239 | break; |
| 3240 | } |
| 3241 | |
| 3242 | kfree_skb(skb); |
| 3243 | return err; |
| 3244 | } |
| 3245 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3246 | static void l2cap_check_srej_gap(struct l2cap_chan *chan, u8 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3247 | { |
| 3248 | struct sk_buff *skb; |
Gustavo F. Padovan | afefdbc | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 3249 | u16 control; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3250 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3251 | while ((skb = skb_peek(&chan->srej_q))) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3252 | if (bt_cb(skb)->tx_seq != tx_seq) |
| 3253 | break; |
| 3254 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3255 | skb = skb_dequeue(&chan->srej_q); |
Gustavo F. Padovan | afefdbc | 2010-05-01 16:15:43 -0300 | [diff] [blame] | 3256 | control = bt_cb(skb)->sar << L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3257 | l2cap_ertm_reassembly_sdu(chan, skb, control); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3258 | chan->buffer_seq_srej = |
| 3259 | (chan->buffer_seq_srej + 1) % 64; |
Gustavo F. Padovan | 8ff50ec | 2010-05-10 19:34:11 -0300 | [diff] [blame] | 3260 | tx_seq = (tx_seq + 1) % 64; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3261 | } |
| 3262 | } |
| 3263 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3264 | static void l2cap_resend_srejframe(struct l2cap_chan *chan, u8 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3265 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3266 | struct srej_list *l, *tmp; |
| 3267 | u16 control; |
| 3268 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3269 | list_for_each_entry_safe(l, tmp, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3270 | if (l->tx_seq == tx_seq) { |
| 3271 | list_del(&l->list); |
| 3272 | kfree(l); |
| 3273 | return; |
| 3274 | } |
| 3275 | control = L2CAP_SUPER_SELECT_REJECT; |
| 3276 | control |= l->tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3277 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3278 | list_del(&l->list); |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3279 | list_add_tail(&l->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3280 | } |
| 3281 | } |
| 3282 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3283 | static void l2cap_send_srejframe(struct l2cap_chan *chan, u8 tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3284 | { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3285 | struct srej_list *new; |
| 3286 | u16 control; |
| 3287 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3288 | while (tx_seq != chan->expected_tx_seq) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3289 | control = L2CAP_SUPER_SELECT_REJECT; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3290 | control |= chan->expected_tx_seq << L2CAP_CTRL_REQSEQ_SHIFT; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3291 | l2cap_send_sframe(chan, control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3292 | |
| 3293 | new = kzalloc(sizeof(struct srej_list), GFP_ATOMIC); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3294 | new->tx_seq = chan->expected_tx_seq; |
| 3295 | chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3296 | list_add_tail(&new->list, &chan->srej_l); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3297 | } |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3298 | chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3299 | } |
| 3300 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3301 | static inline int l2cap_data_channel_iframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3302 | { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3303 | u8 tx_seq = __get_txseq(rx_control); |
Gustavo F. Padovan | 9f121a5 | 2009-10-03 02:34:38 -0300 | [diff] [blame] | 3304 | u8 req_seq = __get_reqseq(rx_control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3305 | u8 sar = rx_control >> L2CAP_CTRL_SAR_SHIFT; |
Gustavo F. Padovan | f6337c7 | 2010-05-10 18:32:04 -0300 | [diff] [blame] | 3306 | int tx_seq_offset, expected_tx_seq_offset; |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3307 | int num_to_ack = (chan->tx_win/6) + 1; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3308 | int err = 0; |
| 3309 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3310 | BT_DBG("chan %p len %d tx_seq %d rx_control 0x%4.4x", chan, skb->len, |
| 3311 | tx_seq, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3312 | |
Gustavo F. Padovan | 9b16dc6 | 2010-05-05 20:05:57 -0300 | [diff] [blame] | 3313 | if (L2CAP_CTRL_FINAL & rx_control && |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3314 | chan->conn_state & L2CAP_CONN_WAIT_F) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3315 | del_timer(&chan->monitor_timer); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3316 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3317 | __mod_retrans_timer(); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3318 | chan->conn_state &= ~L2CAP_CONN_WAIT_F; |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3319 | } |
| 3320 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3321 | chan->expected_ack_seq = req_seq; |
| 3322 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 9f121a5 | 2009-10-03 02:34:38 -0300 | [diff] [blame] | 3323 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3324 | if (tx_seq == chan->expected_tx_seq) |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3325 | goto expected; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3326 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3327 | tx_seq_offset = (tx_seq - chan->buffer_seq) % 64; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3328 | if (tx_seq_offset < 0) |
| 3329 | tx_seq_offset += 64; |
| 3330 | |
| 3331 | /* invalid tx_seq */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3332 | if (tx_seq_offset >= chan->tx_win) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3333 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3334 | goto drop; |
| 3335 | } |
| 3336 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3337 | if (chan->conn_state == L2CAP_CONN_LOCAL_BUSY) |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3338 | goto drop; |
| 3339 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3340 | if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3341 | struct srej_list *first; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3342 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3343 | first = list_first_entry(&chan->srej_l, |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3344 | struct srej_list, list); |
| 3345 | if (tx_seq == first->tx_seq) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3346 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3347 | l2cap_check_srej_gap(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3348 | |
| 3349 | list_del(&first->list); |
| 3350 | kfree(first); |
| 3351 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3352 | if (list_empty(&chan->srej_l)) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3353 | chan->buffer_seq = chan->buffer_seq_srej; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3354 | chan->conn_state &= ~L2CAP_CONN_SREJ_SENT; |
| 3355 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3356 | BT_DBG("chan %p, Exit SREJ_SENT", chan); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3357 | } |
| 3358 | } else { |
| 3359 | struct srej_list *l; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3360 | |
| 3361 | /* duplicated tx_seq */ |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3362 | 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] | 3363 | goto drop; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3364 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3365 | list_for_each_entry(l, &chan->srej_l, list) { |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3366 | if (l->tx_seq == tx_seq) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3367 | l2cap_resend_srejframe(chan, tx_seq); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3368 | return 0; |
| 3369 | } |
| 3370 | } |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3371 | l2cap_send_srejframe(chan, tx_seq); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3372 | } |
| 3373 | } else { |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3374 | expected_tx_seq_offset = |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3375 | (chan->expected_tx_seq - chan->buffer_seq) % 64; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3376 | if (expected_tx_seq_offset < 0) |
| 3377 | expected_tx_seq_offset += 64; |
| 3378 | |
| 3379 | /* duplicated tx_seq */ |
| 3380 | if (tx_seq_offset < expected_tx_seq_offset) |
| 3381 | goto drop; |
| 3382 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3383 | chan->conn_state |= L2CAP_CONN_SREJ_SENT; |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3384 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3385 | BT_DBG("chan %p, Enter SREJ", chan); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3386 | |
Gustavo F. Padovan | 39d5a3e | 2011-04-04 15:40:12 -0300 | [diff] [blame] | 3387 | INIT_LIST_HEAD(&chan->srej_l); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3388 | chan->buffer_seq_srej = chan->buffer_seq; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3389 | |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3390 | __skb_queue_head_init(&chan->srej_q); |
| 3391 | __skb_queue_head_init(&chan->busy_q); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3392 | l2cap_add_to_srej_queue(chan, skb, tx_seq, sar); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3393 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3394 | chan->conn_state |= L2CAP_CONN_SEND_PBIT; |
Gustavo F. Padovan | ef54fd9 | 2009-08-20 22:26:04 -0300 | [diff] [blame] | 3395 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3396 | l2cap_send_srejframe(chan, tx_seq); |
Gustavo F. Padovan | 7fe9b29 | 2010-05-12 18:32:04 -0300 | [diff] [blame] | 3397 | |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3398 | del_timer(&chan->ack_timer); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3399 | } |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3400 | return 0; |
| 3401 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3402 | expected: |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3403 | chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3404 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3405 | if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { |
Gustavo F. Padovan | 3b1a9f3 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 3406 | bt_cb(skb)->tx_seq = tx_seq; |
| 3407 | bt_cb(skb)->sar = sar; |
Gustavo F. Padovan | f1c6775 | 2011-03-25 20:36:10 -0300 | [diff] [blame] | 3408 | __skb_queue_tail(&chan->srej_q, skb); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3409 | return 0; |
| 3410 | } |
| 3411 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3412 | err = l2cap_push_rx_skb(chan, skb, rx_control); |
Gustavo F. Padovan | 2ece368 | 2010-06-16 17:21:44 -0300 | [diff] [blame] | 3413 | if (err < 0) |
| 3414 | return 0; |
| 3415 | |
Gustavo F. Padovan | 4ec10d9 | 2009-10-03 02:34:39 -0300 | [diff] [blame] | 3416 | if (rx_control & L2CAP_CTRL_FINAL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3417 | if (chan->conn_state & L2CAP_CONN_REJ_ACT) |
| 3418 | chan->conn_state &= ~L2CAP_CONN_REJ_ACT; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3419 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3420 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | 4ec10d9 | 2009-10-03 02:34:39 -0300 | [diff] [blame] | 3421 | } |
| 3422 | |
Gustavo F. Padovan | c1b4f43 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3423 | __mod_ack_timer(); |
| 3424 | |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3425 | chan->num_acked = (chan->num_acked + 1) % num_to_ack; |
| 3426 | if (chan->num_acked == num_to_ack - 1) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3427 | l2cap_send_ack(chan); |
Gustavo F. Padovan | 9e917af | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3428 | |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3429 | return 0; |
João Paulo Rechi Vita | 9b53350 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 3430 | |
| 3431 | drop: |
| 3432 | kfree_skb(skb); |
| 3433 | return 0; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3434 | } |
| 3435 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3436 | static inline void l2cap_data_channel_rrframe(struct l2cap_chan *chan, u16 rx_control) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3437 | { |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3438 | BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, __get_reqseq(rx_control), |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3439 | rx_control); |
| 3440 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3441 | chan->expected_ack_seq = __get_reqseq(rx_control); |
| 3442 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3443 | |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3444 | if (rx_control & L2CAP_CTRL_POLL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3445 | chan->conn_state |= L2CAP_CONN_SEND_FBIT; |
| 3446 | if (chan->conn_state & L2CAP_CONN_SREJ_SENT) { |
| 3447 | if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3448 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3449 | __mod_retrans_timer(); |
| 3450 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3451 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
| 3452 | l2cap_send_srejtail(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3453 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3454 | l2cap_send_i_or_rr_or_rnr(chan); |
Gustavo F. Padovan | 05fbd89 | 2010-05-01 16:15:39 -0300 | [diff] [blame] | 3455 | } |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3456 | |
| 3457 | } else if (rx_control & L2CAP_CTRL_FINAL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3458 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3459 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3460 | if (chan->conn_state & L2CAP_CONN_REJ_ACT) |
| 3461 | chan->conn_state &= ~L2CAP_CONN_REJ_ACT; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3462 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3463 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3464 | |
| 3465 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3466 | if ((chan->conn_state & L2CAP_CONN_REMOTE_BUSY) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3467 | (chan->unacked_frames > 0)) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3468 | __mod_retrans_timer(); |
| 3469 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3470 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
| 3471 | if (chan->conn_state & L2CAP_CONN_SREJ_SENT) |
| 3472 | l2cap_send_ack(chan); |
Andrei Emeltchenko | 894718a | 2010-12-01 16:58:24 +0200 | [diff] [blame] | 3473 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3474 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3475 | } |
| 3476 | } |
| 3477 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3478 | static inline void l2cap_data_channel_rejframe(struct l2cap_chan *chan, u16 rx_control) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3479 | { |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3480 | u8 tx_seq = __get_reqseq(rx_control); |
| 3481 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3482 | BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3483 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3484 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3485 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3486 | chan->expected_ack_seq = tx_seq; |
| 3487 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3488 | |
| 3489 | if (rx_control & L2CAP_CTRL_FINAL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3490 | if (chan->conn_state & L2CAP_CONN_REJ_ACT) |
| 3491 | chan->conn_state &= ~L2CAP_CONN_REJ_ACT; |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3492 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3493 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3494 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3495 | l2cap_retransmit_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3496 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3497 | if (chan->conn_state & L2CAP_CONN_WAIT_F) |
| 3498 | chan->conn_state |= L2CAP_CONN_REJ_ACT; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3499 | } |
| 3500 | } |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3501 | static inline void l2cap_data_channel_srejframe(struct l2cap_chan *chan, u16 rx_control) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3502 | { |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3503 | u8 tx_seq = __get_reqseq(rx_control); |
| 3504 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3505 | BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3506 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3507 | chan->conn_state &= ~L2CAP_CONN_REMOTE_BUSY; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3508 | |
| 3509 | if (rx_control & L2CAP_CTRL_POLL) { |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3510 | chan->expected_ack_seq = tx_seq; |
| 3511 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 3512 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3513 | chan->conn_state |= L2CAP_CONN_SEND_FBIT; |
| 3514 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3515 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3516 | l2cap_ertm_send(chan); |
Gustavo F. Padovan | dfc909b | 2010-05-01 16:15:45 -0300 | [diff] [blame] | 3517 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3518 | if (chan->conn_state & L2CAP_CONN_WAIT_F) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3519 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3520 | chan->conn_state |= L2CAP_CONN_SREJ_ACT; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3521 | } |
| 3522 | } else if (rx_control & L2CAP_CTRL_FINAL) { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3523 | if ((chan->conn_state & L2CAP_CONN_SREJ_ACT) && |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3524 | chan->srej_save_reqseq == tx_seq) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3525 | chan->conn_state &= ~L2CAP_CONN_SREJ_ACT; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3526 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3527 | l2cap_retransmit_one_frame(chan, tx_seq); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3528 | } else { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3529 | l2cap_retransmit_one_frame(chan, tx_seq); |
| 3530 | if (chan->conn_state & L2CAP_CONN_WAIT_F) { |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3531 | chan->srej_save_reqseq = tx_seq; |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3532 | chan->conn_state |= L2CAP_CONN_SREJ_ACT; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3533 | } |
| 3534 | } |
| 3535 | } |
| 3536 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3537 | static inline void l2cap_data_channel_rnrframe(struct l2cap_chan *chan, u16 rx_control) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3538 | { |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3539 | u8 tx_seq = __get_reqseq(rx_control); |
| 3540 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3541 | BT_DBG("chan %p, req_seq %d ctrl 0x%4.4x", chan, tx_seq, rx_control); |
Gustavo F. Padovan | 0e98958 | 2010-04-19 14:45:38 -0300 | [diff] [blame] | 3542 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3543 | chan->conn_state |= L2CAP_CONN_REMOTE_BUSY; |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3544 | chan->expected_ack_seq = tx_seq; |
| 3545 | l2cap_drop_acked_frames(chan); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3546 | |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 3547 | if (rx_control & L2CAP_CTRL_POLL) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3548 | chan->conn_state |= L2CAP_CONN_SEND_FBIT; |
Gustavo F. Padovan | 3cb123d | 2010-05-29 02:24:35 -0300 | [diff] [blame] | 3549 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3550 | if (!(chan->conn_state & L2CAP_CONN_SREJ_SENT)) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3551 | del_timer(&chan->retrans_timer); |
Gustavo F. Padovan | a2e12a2 | 2010-05-05 19:58:27 -0300 | [diff] [blame] | 3552 | if (rx_control & L2CAP_CTRL_POLL) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3553 | l2cap_send_rr_or_rnr(chan, L2CAP_CTRL_FINAL); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3554 | return; |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3555 | } |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3556 | |
| 3557 | if (rx_control & L2CAP_CTRL_POLL) |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3558 | l2cap_send_srejtail(chan); |
Gustavo F. Padovan | 99b0d4b | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3559 | else |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3560 | l2cap_send_sframe(chan, L2CAP_SUPER_RCV_READY); |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3561 | } |
| 3562 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3563 | static inline int l2cap_data_channel_sframe(struct l2cap_chan *chan, u16 rx_control, struct sk_buff *skb) |
Gustavo F. Padovan | e072745 | 2010-05-01 16:15:38 -0300 | [diff] [blame] | 3564 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3565 | BT_DBG("chan %p rx_control 0x%4.4x len %d", chan, rx_control, skb->len); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3566 | |
Gustavo F. Padovan | 9b16dc6 | 2010-05-05 20:05:57 -0300 | [diff] [blame] | 3567 | if (L2CAP_CTRL_FINAL & rx_control && |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3568 | chan->conn_state & L2CAP_CONN_WAIT_F) { |
Gustavo F. Padovan | e92c8e7 | 2011-04-01 00:53:45 -0300 | [diff] [blame] | 3569 | del_timer(&chan->monitor_timer); |
Gustavo F. Padovan | 6a02661 | 2011-04-01 00:38:50 -0300 | [diff] [blame] | 3570 | if (chan->unacked_frames > 0) |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3571 | __mod_retrans_timer(); |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3572 | chan->conn_state &= ~L2CAP_CONN_WAIT_F; |
Gustavo F. Padovan | 1d8f5d1 | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3573 | } |
| 3574 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3575 | switch (rx_control & L2CAP_CTRL_SUPERVISE) { |
| 3576 | case L2CAP_SUPER_RCV_READY: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3577 | l2cap_data_channel_rrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3578 | break; |
| 3579 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3580 | case L2CAP_SUPER_REJECT: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3581 | l2cap_data_channel_rejframe(chan, rx_control); |
Gustavo F. Padovan | 30afb5b | 2009-08-20 22:25:59 -0300 | [diff] [blame] | 3582 | break; |
| 3583 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3584 | case L2CAP_SUPER_SELECT_REJECT: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3585 | l2cap_data_channel_srejframe(chan, rx_control); |
Gustavo F. Padovan | 8f17154 | 2009-08-20 22:26:03 -0300 | [diff] [blame] | 3586 | break; |
| 3587 | |
| 3588 | case L2CAP_SUPER_RCV_NOT_READY: |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3589 | l2cap_data_channel_rnrframe(chan, rx_control); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3590 | break; |
| 3591 | } |
| 3592 | |
Gustavo F. Padovan | faaebd1 | 2010-05-01 16:15:35 -0300 | [diff] [blame] | 3593 | kfree_skb(skb); |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3594 | return 0; |
| 3595 | } |
| 3596 | |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3597 | static int l2cap_ertm_data_rcv(struct sock *sk, struct sk_buff *skb) |
| 3598 | { |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3599 | struct l2cap_chan *chan = l2cap_pi(sk)->chan; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3600 | u16 control; |
| 3601 | u8 req_seq; |
| 3602 | int len, next_tx_seq_offset, req_seq_offset; |
| 3603 | |
| 3604 | control = get_unaligned_le16(skb->data); |
| 3605 | skb_pull(skb, 2); |
| 3606 | len = skb->len; |
| 3607 | |
| 3608 | /* |
| 3609 | * We can just drop the corrupted I-frame here. |
| 3610 | * Receiver will miss it and start proper recovery |
| 3611 | * procedures and ask retransmission. |
| 3612 | */ |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3613 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3614 | goto drop; |
| 3615 | |
| 3616 | if (__is_sar_start(control) && __is_iframe(control)) |
| 3617 | len -= 2; |
| 3618 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3619 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3620 | len -= 2; |
| 3621 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3622 | if (len > chan->mps) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3623 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3624 | goto drop; |
| 3625 | } |
| 3626 | |
| 3627 | req_seq = __get_reqseq(control); |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3628 | req_seq_offset = (req_seq - chan->expected_ack_seq) % 64; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3629 | if (req_seq_offset < 0) |
| 3630 | req_seq_offset += 64; |
| 3631 | |
| 3632 | next_tx_seq_offset = |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3633 | (chan->next_tx_seq - chan->expected_ack_seq) % 64; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3634 | if (next_tx_seq_offset < 0) |
| 3635 | next_tx_seq_offset += 64; |
| 3636 | |
| 3637 | /* check for invalid req-seq */ |
| 3638 | if (req_seq_offset > next_tx_seq_offset) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3639 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3640 | goto drop; |
| 3641 | } |
| 3642 | |
| 3643 | if (__is_iframe(control)) { |
| 3644 | if (len < 0) { |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3645 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3646 | goto drop; |
| 3647 | } |
| 3648 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3649 | l2cap_data_channel_iframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3650 | } else { |
| 3651 | if (len != 0) { |
| 3652 | BT_ERR("%d", len); |
Gustavo F. Padovan | 8c1d787 | 2011-04-13 20:23:55 -0300 | [diff] [blame] | 3653 | l2cap_send_disconn_req(chan->conn, chan, ECONNRESET); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3654 | goto drop; |
| 3655 | } |
| 3656 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3657 | l2cap_data_channel_sframe(chan, control, skb); |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | return 0; |
| 3661 | |
| 3662 | drop: |
| 3663 | kfree_skb(skb); |
| 3664 | return 0; |
| 3665 | } |
| 3666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | static inline int l2cap_data_channel(struct l2cap_conn *conn, u16 cid, struct sk_buff *skb) |
| 3668 | { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3669 | struct l2cap_chan *chan; |
David S. Miller | bf73484 | 2011-04-25 13:03:02 -0700 | [diff] [blame] | 3670 | struct sock *sk = NULL; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3671 | struct l2cap_pinfo *pi; |
Nathan Holstein | 51893f8 | 2010-06-09 15:46:25 -0400 | [diff] [blame] | 3672 | u16 control; |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3673 | u8 tx_seq; |
| 3674 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 3676 | chan = l2cap_get_chan_by_scid(conn, cid); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3677 | if (!chan) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3678 | BT_DBG("unknown cid 0x%4.4x", cid); |
| 3679 | goto drop; |
| 3680 | } |
| 3681 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3682 | sk = chan->sk; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3683 | pi = l2cap_pi(sk); |
| 3684 | |
Gustavo F. Padovan | 49208c9 | 2011-04-04 15:59:54 -0300 | [diff] [blame] | 3685 | BT_DBG("chan %p, len %d", chan, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | |
| 3687 | if (sk->sk_state != BT_CONNECTED) |
| 3688 | goto drop; |
| 3689 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3690 | switch (chan->mode) { |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3691 | case L2CAP_MODE_BASIC: |
| 3692 | /* If socket recv buffers overflows we drop data here |
| 3693 | * which is *bad* because L2CAP has to be reliable. |
| 3694 | * But we don't have any other choice. L2CAP doesn't |
| 3695 | * provide flow control mechanism. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3697 | if (chan->imtu < skb->len) |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3698 | goto drop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3700 | if (!sock_queue_rcv_skb(sk, skb)) |
| 3701 | goto done; |
| 3702 | break; |
| 3703 | |
| 3704 | case L2CAP_MODE_ERTM: |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3705 | if (!sock_owned_by_user(sk)) { |
| 3706 | l2cap_ertm_data_rcv(sk, skb); |
Gustavo F. Padovan | 277ffbe | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3707 | } else { |
Gustavo F. Padovan | 218bb9d | 2010-06-21 18:53:22 -0300 | [diff] [blame] | 3708 | if (sk_add_backlog(sk, skb)) |
Gustavo F. Padovan | 277ffbe | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3709 | goto drop; |
Gustavo F. Padovan | 277ffbe | 2010-05-01 16:15:37 -0300 | [diff] [blame] | 3710 | } |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3711 | |
Andrei Emeltchenko | fcafde2 | 2009-12-22 15:58:08 +0200 | [diff] [blame] | 3712 | goto done; |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3713 | |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3714 | case L2CAP_MODE_STREAMING: |
| 3715 | control = get_unaligned_le16(skb->data); |
| 3716 | skb_pull(skb, 2); |
| 3717 | len = skb->len; |
| 3718 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3719 | if (l2cap_check_fcs(chan, skb)) |
Gustavo F. Padovan | 2600008 | 2010-05-11 22:02:00 -0300 | [diff] [blame] | 3720 | goto drop; |
| 3721 | |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3722 | if (__is_sar_start(control)) |
| 3723 | len -= 2; |
| 3724 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3725 | if (chan->fcs == L2CAP_FCS_CRC16) |
Gustavo F. Padovan | fcc203c | 2009-08-20 22:26:02 -0300 | [diff] [blame] | 3726 | len -= 2; |
| 3727 | |
Gustavo F. Padovan | 47d1ec6 | 2011-04-13 15:57:03 -0300 | [diff] [blame] | 3728 | if (len > chan->mps || len < 0 || __is_sframe(control)) |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3729 | goto drop; |
| 3730 | |
| 3731 | tx_seq = __get_txseq(control); |
| 3732 | |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3733 | if (chan->expected_tx_seq == tx_seq) |
| 3734 | chan->expected_tx_seq = (chan->expected_tx_seq + 1) % 64; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3735 | else |
Gustavo F. Padovan | 42e5c80 | 2011-03-25 19:58:34 -0300 | [diff] [blame] | 3736 | chan->expected_tx_seq = (tx_seq + 1) % 64; |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3737 | |
Gustavo F. Padovan | 525cd18 | 2011-03-25 19:43:39 -0300 | [diff] [blame] | 3738 | l2cap_streaming_reassembly_sdu(chan, skb, control); |
Gustavo F. Padovan | 6840ed0 | 2009-08-20 22:26:01 -0300 | [diff] [blame] | 3739 | |
| 3740 | goto done; |
| 3741 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3742 | default: |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3743 | 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] | 3744 | break; |
| 3745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | |
| 3747 | drop: |
| 3748 | kfree_skb(skb); |
| 3749 | |
| 3750 | done: |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 3751 | if (sk) |
| 3752 | bh_unlock_sock(sk); |
| 3753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3754 | return 0; |
| 3755 | } |
| 3756 | |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 3757 | 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] | 3758 | { |
David S. Miller | 6dcae1e | 2011-05-16 23:09:26 -0400 | [diff] [blame] | 3759 | struct sock *sk = NULL; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3760 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3761 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3762 | chan = l2cap_global_chan_by_psm(0, psm, conn->src); |
| 3763 | if (!chan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | goto drop; |
| 3765 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3766 | sk = chan->sk; |
| 3767 | |
Gustavo F. Padovan | e0f0cb5 | 2010-11-01 18:43:53 +0000 | [diff] [blame] | 3768 | bh_lock_sock(sk); |
| 3769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3770 | BT_DBG("sk %p, len %d", sk, skb->len); |
| 3771 | |
| 3772 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) |
| 3773 | goto drop; |
| 3774 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3775 | if (l2cap_pi(sk)->chan->imtu < skb->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | goto drop; |
| 3777 | |
| 3778 | if (!sock_queue_rcv_skb(sk, skb)) |
| 3779 | goto done; |
| 3780 | |
| 3781 | drop: |
| 3782 | kfree_skb(skb); |
| 3783 | |
| 3784 | done: |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 3785 | if (sk) |
| 3786 | bh_unlock_sock(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3787 | return 0; |
| 3788 | } |
| 3789 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3790 | static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid, struct sk_buff *skb) |
| 3791 | { |
David S. Miller | 6dcae1e | 2011-05-16 23:09:26 -0400 | [diff] [blame] | 3792 | struct sock *sk = NULL; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3793 | struct l2cap_chan *chan; |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3794 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3795 | chan = l2cap_global_chan_by_scid(0, cid, conn->src); |
| 3796 | if (!chan) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3797 | goto drop; |
| 3798 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3799 | sk = chan->sk; |
| 3800 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3801 | bh_lock_sock(sk); |
| 3802 | |
| 3803 | BT_DBG("sk %p, len %d", sk, skb->len); |
| 3804 | |
| 3805 | if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) |
| 3806 | goto drop; |
| 3807 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 3808 | if (l2cap_pi(sk)->chan->imtu < skb->len) |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3809 | goto drop; |
| 3810 | |
| 3811 | if (!sock_queue_rcv_skb(sk, skb)) |
| 3812 | goto done; |
| 3813 | |
| 3814 | drop: |
| 3815 | kfree_skb(skb); |
| 3816 | |
| 3817 | done: |
| 3818 | if (sk) |
| 3819 | bh_unlock_sock(sk); |
| 3820 | return 0; |
| 3821 | } |
| 3822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3823 | static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb) |
| 3824 | { |
| 3825 | struct l2cap_hdr *lh = (void *) skb->data; |
Al Viro | 8e036fc | 2007-07-29 00:16:36 -0700 | [diff] [blame] | 3826 | u16 cid, len; |
| 3827 | __le16 psm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | |
| 3829 | skb_pull(skb, L2CAP_HDR_SIZE); |
| 3830 | cid = __le16_to_cpu(lh->cid); |
| 3831 | len = __le16_to_cpu(lh->len); |
| 3832 | |
Gustavo F. Padovan | 1c2acff | 2009-08-20 22:25:57 -0300 | [diff] [blame] | 3833 | if (len != skb->len) { |
| 3834 | kfree_skb(skb); |
| 3835 | return; |
| 3836 | } |
| 3837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | BT_DBG("len %d, cid 0x%4.4x", len, cid); |
| 3839 | |
| 3840 | switch (cid) { |
Claudio Takahasi | 3300d9a | 2011-02-11 19:28:54 -0200 | [diff] [blame] | 3841 | case L2CAP_CID_LE_SIGNALING: |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 3842 | case L2CAP_CID_SIGNALING: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | l2cap_sig_channel(conn, skb); |
| 3844 | break; |
| 3845 | |
Gustavo F. Padovan | 8db4dc4 | 2009-04-20 01:31:05 -0300 | [diff] [blame] | 3846 | case L2CAP_CID_CONN_LESS: |
Gustavo F. Padovan | 1b7bf4e | 2009-08-24 00:45:20 -0300 | [diff] [blame] | 3847 | psm = get_unaligned_le16(skb->data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | skb_pull(skb, 2); |
| 3849 | l2cap_conless_channel(conn, psm, skb); |
| 3850 | break; |
| 3851 | |
Gustavo F. Padovan | 9f69bda | 2011-04-07 16:40:25 -0300 | [diff] [blame] | 3852 | case L2CAP_CID_LE_DATA: |
| 3853 | l2cap_att_channel(conn, cid, skb); |
| 3854 | break; |
| 3855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3856 | default: |
| 3857 | l2cap_data_channel(conn, cid, skb); |
| 3858 | break; |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | /* ---- L2CAP interface with lower layer (HCI) ---- */ |
| 3863 | |
| 3864 | static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
| 3865 | { |
| 3866 | int exact = 0, lm1 = 0, lm2 = 0; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3867 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | |
| 3869 | if (type != ACL_LINK) |
João Paulo Rechi Vita | 963cf68 | 2010-06-22 13:56:28 -0300 | [diff] [blame] | 3870 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | |
| 3872 | BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr)); |
| 3873 | |
| 3874 | /* Find listening sockets and check their link_mode */ |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3875 | read_lock(&chan_list_lock); |
| 3876 | list_for_each_entry(c, &chan_list, global_l) { |
| 3877 | struct sock *sk = c->sk; |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 3878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | if (sk->sk_state != BT_LISTEN) |
| 3880 | continue; |
| 3881 | |
| 3882 | if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 3883 | lm1 |= HCI_LM_ACCEPT; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3884 | if (c->role_switch) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 3885 | lm1 |= HCI_LM_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3886 | exact++; |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 3887 | } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { |
| 3888 | lm2 |= HCI_LM_ACCEPT; |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3889 | if (c->role_switch) |
Marcel Holtmann | 2af6b9d | 2009-01-15 21:58:38 +0100 | [diff] [blame] | 3890 | lm2 |= HCI_LM_MASTER; |
| 3891 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3892 | } |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 3893 | read_unlock(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3894 | |
| 3895 | return exact ? lm1 : lm2; |
| 3896 | } |
| 3897 | |
| 3898 | static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status) |
| 3899 | { |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 3900 | struct l2cap_conn *conn; |
| 3901 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3902 | BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status); |
| 3903 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 3904 | if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) |
João Paulo Rechi Vita | 963cf68 | 2010-06-22 13:56:28 -0300 | [diff] [blame] | 3905 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | |
| 3907 | if (!status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3908 | conn = l2cap_conn_add(hcon, status); |
| 3909 | if (conn) |
| 3910 | l2cap_conn_ready(conn); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 3911 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3912 | l2cap_conn_del(hcon, bt_err(status)); |
| 3913 | |
| 3914 | return 0; |
| 3915 | } |
| 3916 | |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 3917 | static int l2cap_disconn_ind(struct hci_conn *hcon) |
| 3918 | { |
| 3919 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 3920 | |
| 3921 | BT_DBG("hcon %p", hcon); |
| 3922 | |
| 3923 | if (hcon->type != ACL_LINK || !conn) |
| 3924 | return 0x13; |
| 3925 | |
| 3926 | return conn->disc_reason; |
| 3927 | } |
| 3928 | |
| 3929 | static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | { |
| 3931 | BT_DBG("hcon %p reason %d", hcon, reason); |
| 3932 | |
Ville Tervo | acd7d37 | 2011-02-10 22:38:49 -0300 | [diff] [blame] | 3933 | if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK)) |
João Paulo Rechi Vita | 963cf68 | 2010-06-22 13:56:28 -0300 | [diff] [blame] | 3934 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3935 | |
| 3936 | l2cap_conn_del(hcon, bt_err(reason)); |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 3937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3938 | return 0; |
| 3939 | } |
| 3940 | |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 3941 | static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 3942 | { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 3943 | struct sock *sk = chan->sk; |
| 3944 | |
Gustavo F. Padovan | bd3c9e2 | 2010-05-01 16:15:42 -0300 | [diff] [blame] | 3945 | if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM) |
Marcel Holtmann | 255c760 | 2009-02-04 21:07:19 +0100 | [diff] [blame] | 3946 | return; |
| 3947 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 3948 | if (encrypt == 0x00) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 3949 | if (chan->sec_level == BT_SECURITY_MEDIUM) { |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 3950 | l2cap_sock_clear_timer(sk); |
| 3951 | l2cap_sock_set_timer(sk, HZ * 5); |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 3952 | } else if (chan->sec_level == BT_SECURITY_HIGH) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 3953 | __l2cap_sock_close(sk, ECONNREFUSED); |
| 3954 | } else { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 3955 | if (chan->sec_level == BT_SECURITY_MEDIUM) |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 3956 | l2cap_sock_clear_timer(sk); |
| 3957 | } |
| 3958 | } |
| 3959 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 3960 | 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] | 3961 | { |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 3962 | struct l2cap_conn *conn = hcon->l2cap_data; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3963 | struct l2cap_chan *chan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 3965 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | return 0; |
Marcel Holtmann | 0139418 | 2006-07-03 10:02:46 +0200 | [diff] [blame] | 3967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | BT_DBG("conn %p", conn); |
| 3969 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 3970 | read_lock(&conn->chan_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 3972 | list_for_each_entry(chan, &conn->chan_l, list) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 3973 | struct sock *sk = chan->sk; |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 3974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | bh_lock_sock(sk); |
| 3976 | |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 3977 | if (chan->conf_state & L2CAP_CONF_CONNECT_PEND) { |
Marcel Holtmann | 6a8d301 | 2009-02-06 23:56:36 +0100 | [diff] [blame] | 3978 | bh_unlock_sock(sk); |
| 3979 | continue; |
| 3980 | } |
| 3981 | |
Marcel Holtmann | f62e432 | 2009-01-15 21:58:44 +0100 | [diff] [blame] | 3982 | if (!status && (sk->sk_state == BT_CONNECTED || |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 3983 | sk->sk_state == BT_CONFIG)) { |
Gustavo F. Padovan | 4343478 | 2011-04-12 18:31:57 -0300 | [diff] [blame] | 3984 | l2cap_check_encryption(chan, encrypt); |
Marcel Holtmann | 9719f8a | 2008-07-14 20:13:45 +0200 | [diff] [blame] | 3985 | bh_unlock_sock(sk); |
| 3986 | continue; |
| 3987 | } |
| 3988 | |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 3989 | if (sk->sk_state == BT_CONNECT) { |
| 3990 | if (!status) { |
| 3991 | struct l2cap_conn_req req; |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 3992 | req.scid = cpu_to_le16(chan->scid); |
| 3993 | req.psm = chan->psm; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 3994 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3995 | chan->ident = l2cap_get_ident(conn); |
Gustavo F. Padovan | b445003 | 2011-04-12 18:15:09 -0300 | [diff] [blame] | 3996 | chan->conf_state |= L2CAP_CONF_CONNECT_PEND; |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 3997 | |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 3998 | l2cap_send_cmd(conn, chan->ident, |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 3999 | L2CAP_CONN_REQ, sizeof(req), &req); |
| 4000 | } else { |
| 4001 | l2cap_sock_clear_timer(sk); |
| 4002 | l2cap_sock_set_timer(sk, HZ / 10); |
| 4003 | } |
| 4004 | } else if (sk->sk_state == BT_CONNECT2) { |
| 4005 | struct l2cap_conn_rsp rsp; |
| 4006 | __u16 result; |
| 4007 | |
| 4008 | if (!status) { |
| 4009 | sk->sk_state = BT_CONFIG; |
| 4010 | result = L2CAP_CR_SUCCESS; |
| 4011 | } else { |
| 4012 | sk->sk_state = BT_DISCONN; |
| 4013 | l2cap_sock_set_timer(sk, HZ / 10); |
| 4014 | result = L2CAP_CR_SEC_BLOCK; |
| 4015 | } |
| 4016 | |
Gustavo F. Padovan | fe4128e | 2011-04-13 19:50:45 -0300 | [diff] [blame] | 4017 | rsp.scid = cpu_to_le16(chan->dcid); |
| 4018 | rsp.dcid = cpu_to_le16(chan->scid); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4019 | rsp.result = cpu_to_le16(result); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 4020 | rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO); |
Gustavo F. Padovan | fc7f8a7 | 2011-03-25 13:59:37 -0300 | [diff] [blame] | 4021 | l2cap_send_cmd(conn, chan->ident, L2CAP_CONN_RSP, |
| 4022 | sizeof(rsp), &rsp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | } |
| 4024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4025 | bh_unlock_sock(sk); |
| 4026 | } |
| 4027 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4028 | read_unlock(&conn->chan_lock); |
Marcel Holtmann | b1235d7 | 2008-07-14 20:13:54 +0200 | [diff] [blame] | 4029 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | return 0; |
| 4031 | } |
| 4032 | |
| 4033 | static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) |
| 4034 | { |
| 4035 | struct l2cap_conn *conn = hcon->l2cap_data; |
| 4036 | |
Andrei Emeltchenko | 5a08ecc | 2011-01-11 17:20:20 +0200 | [diff] [blame] | 4037 | if (!conn) |
| 4038 | conn = l2cap_conn_add(hcon, 0); |
| 4039 | |
| 4040 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4041 | goto drop; |
| 4042 | |
| 4043 | BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags); |
| 4044 | |
Andrei Emeltchenko | e702112 | 2011-01-03 11:14:36 +0200 | [diff] [blame] | 4045 | if (!(flags & ACL_CONT)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4046 | struct l2cap_hdr *hdr; |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4047 | struct l2cap_chan *chan; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4048 | u16 cid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | int len; |
| 4050 | |
| 4051 | if (conn->rx_len) { |
| 4052 | BT_ERR("Unexpected start frame (len %d)", skb->len); |
| 4053 | kfree_skb(conn->rx_skb); |
| 4054 | conn->rx_skb = NULL; |
| 4055 | conn->rx_len = 0; |
| 4056 | l2cap_conn_unreliable(conn, ECOMM); |
| 4057 | } |
| 4058 | |
Andrei Emeltchenko | aae7fe2 | 2010-09-15 14:28:43 +0300 | [diff] [blame] | 4059 | /* Start fragment always begin with Basic L2CAP header */ |
| 4060 | if (skb->len < L2CAP_HDR_SIZE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4061 | BT_ERR("Frame is too short (len %d)", skb->len); |
| 4062 | l2cap_conn_unreliable(conn, ECOMM); |
| 4063 | goto drop; |
| 4064 | } |
| 4065 | |
| 4066 | hdr = (struct l2cap_hdr *) skb->data; |
| 4067 | len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE; |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4068 | cid = __le16_to_cpu(hdr->cid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | |
| 4070 | if (len == skb->len) { |
| 4071 | /* Complete frame received */ |
| 4072 | l2cap_recv_frame(conn, skb); |
| 4073 | return 0; |
| 4074 | } |
| 4075 | |
| 4076 | BT_DBG("Start: total len %d, frag len %d", len, skb->len); |
| 4077 | |
| 4078 | if (skb->len > len) { |
| 4079 | BT_ERR("Frame is too long (len %d, expected len %d)", |
| 4080 | skb->len, len); |
| 4081 | l2cap_conn_unreliable(conn, ECOMM); |
| 4082 | goto drop; |
| 4083 | } |
| 4084 | |
Gustavo F. Padovan | baa7e1f | 2011-03-31 16:17:41 -0300 | [diff] [blame] | 4085 | chan = l2cap_get_chan_by_scid(conn, cid); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4086 | |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4087 | if (chan && chan->sk) { |
| 4088 | struct sock *sk = chan->sk; |
| 4089 | |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4090 | if (chan->imtu < len - L2CAP_HDR_SIZE) { |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4091 | BT_ERR("Frame exceeding recv MTU (len %d, " |
| 4092 | "MTU %d)", len, |
Gustavo F. Padovan | 0c1bc5c | 2011-04-13 17:20:49 -0300 | [diff] [blame] | 4093 | chan->imtu); |
Gustavo F. Padovan | 4845407 | 2011-03-25 00:22:30 -0300 | [diff] [blame] | 4094 | bh_unlock_sock(sk); |
| 4095 | l2cap_conn_unreliable(conn, ECOMM); |
| 4096 | goto drop; |
| 4097 | } |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4098 | bh_unlock_sock(sk); |
Andrei Emeltchenko | 8979481 | 2010-09-15 14:28:44 +0300 | [diff] [blame] | 4099 | } |
| 4100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4101 | /* Allocate skb for the complete frame (with header) */ |
Gustavo F. Padovan | af05b30 | 2009-04-20 01:31:08 -0300 | [diff] [blame] | 4102 | conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 4103 | if (!conn->rx_skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | goto drop; |
| 4105 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4106 | 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] | 4107 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | conn->rx_len = len - skb->len; |
| 4109 | } else { |
| 4110 | BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len); |
| 4111 | |
| 4112 | if (!conn->rx_len) { |
| 4113 | BT_ERR("Unexpected continuation frame (len %d)", skb->len); |
| 4114 | l2cap_conn_unreliable(conn, ECOMM); |
| 4115 | goto drop; |
| 4116 | } |
| 4117 | |
| 4118 | if (skb->len > conn->rx_len) { |
| 4119 | BT_ERR("Fragment is too long (len %d, expected %d)", |
| 4120 | skb->len, conn->rx_len); |
| 4121 | kfree_skb(conn->rx_skb); |
| 4122 | conn->rx_skb = NULL; |
| 4123 | conn->rx_len = 0; |
| 4124 | l2cap_conn_unreliable(conn, ECOMM); |
| 4125 | goto drop; |
| 4126 | } |
| 4127 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 4128 | 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] | 4129 | skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4130 | conn->rx_len -= skb->len; |
| 4131 | |
| 4132 | if (!conn->rx_len) { |
| 4133 | /* Complete frame received */ |
| 4134 | l2cap_recv_frame(conn, conn->rx_skb); |
| 4135 | conn->rx_skb = NULL; |
| 4136 | } |
| 4137 | } |
| 4138 | |
| 4139 | drop: |
| 4140 | kfree_skb(skb); |
| 4141 | return 0; |
| 4142 | } |
| 4143 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4144 | static int l2cap_debugfs_show(struct seq_file *f, void *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4145 | { |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4146 | struct l2cap_chan *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4147 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4148 | read_lock_bh(&chan_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4150 | list_for_each_entry(c, &chan_list, global_l) { |
| 4151 | struct sock *sk = c->sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | |
Gustavo F. Padovan | 903d343 | 2011-02-10 14:16:06 -0200 | [diff] [blame] | 4153 | 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] | 4154 | batostr(&bt_sk(sk)->src), |
| 4155 | batostr(&bt_sk(sk)->dst), |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4156 | sk->sk_state, __le16_to_cpu(c->psm), |
| 4157 | c->scid, c->dcid, c->imtu, c->omtu, |
| 4158 | c->sec_level, c->mode); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | |
Gustavo F. Padovan | 23691d7 | 2011-04-27 18:26:32 -0300 | [diff] [blame] | 4161 | read_unlock_bh(&chan_list_lock); |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4162 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4163 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4164 | } |
| 4165 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4166 | static int l2cap_debugfs_open(struct inode *inode, struct file *file) |
| 4167 | { |
| 4168 | return single_open(file, l2cap_debugfs_show, inode->i_private); |
| 4169 | } |
| 4170 | |
| 4171 | static const struct file_operations l2cap_debugfs_fops = { |
| 4172 | .open = l2cap_debugfs_open, |
| 4173 | .read = seq_read, |
| 4174 | .llseek = seq_lseek, |
| 4175 | .release = single_release, |
| 4176 | }; |
| 4177 | |
| 4178 | static struct dentry *l2cap_debugfs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | static struct hci_proto l2cap_hci_proto = { |
| 4181 | .name = "L2CAP", |
| 4182 | .id = HCI_PROTO_L2CAP, |
| 4183 | .connect_ind = l2cap_connect_ind, |
| 4184 | .connect_cfm = l2cap_connect_cfm, |
| 4185 | .disconn_ind = l2cap_disconn_ind, |
Marcel Holtmann | 2950f21 | 2009-02-12 14:02:50 +0100 | [diff] [blame] | 4186 | .disconn_cfm = l2cap_disconn_cfm, |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 4187 | .security_cfm = l2cap_security_cfm, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | .recv_acldata = l2cap_recv_acldata |
| 4189 | }; |
| 4190 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4191 | int __init l2cap_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | { |
| 4193 | int err; |
Marcel Holtmann | be9d122 | 2005-11-08 09:57:38 -0800 | [diff] [blame] | 4194 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4195 | err = l2cap_init_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4196 | if (err < 0) |
| 4197 | return err; |
| 4198 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 4199 | _busy_wq = create_singlethread_workqueue("l2cap"); |
Anderson Lizardo | b78d7b4f | 2010-11-29 12:15:50 -0400 | [diff] [blame] | 4200 | if (!_busy_wq) { |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4201 | err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4202 | goto error; |
| 4203 | } |
| 4204 | |
| 4205 | err = hci_register_proto(&l2cap_hci_proto); |
| 4206 | if (err < 0) { |
| 4207 | BT_ERR("L2CAP protocol registration failed"); |
| 4208 | bt_sock_unregister(BTPROTO_L2CAP); |
| 4209 | goto error; |
| 4210 | } |
| 4211 | |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4212 | if (bt_debugfs) { |
| 4213 | l2cap_debugfs = debugfs_create_file("l2cap", 0444, |
| 4214 | bt_debugfs, NULL, &l2cap_debugfs_fops); |
| 4215 | if (!l2cap_debugfs) |
| 4216 | BT_ERR("Failed to create L2CAP debug file"); |
| 4217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4219 | return 0; |
| 4220 | |
| 4221 | error: |
Anderson Lizardo | b78d7b4f | 2010-11-29 12:15:50 -0400 | [diff] [blame] | 4222 | destroy_workqueue(_busy_wq); |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4223 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | return err; |
| 4225 | } |
| 4226 | |
Gustavo F. Padovan | 6427451 | 2011-02-07 20:08:52 -0200 | [diff] [blame] | 4227 | void l2cap_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4228 | { |
Marcel Holtmann | aef7d97 | 2010-03-21 05:27:45 +0100 | [diff] [blame] | 4229 | debugfs_remove(l2cap_debugfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | |
Gustavo F. Padovan | 1890d36 | 2010-05-01 16:15:44 -0300 | [diff] [blame] | 4231 | flush_workqueue(_busy_wq); |
| 4232 | destroy_workqueue(_busy_wq); |
| 4233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | if (hci_unregister_proto(&l2cap_hci_proto) < 0) |
| 4235 | BT_ERR("L2CAP protocol unregistration failed"); |
| 4236 | |
Gustavo F. Padovan | bb58f74 | 2011-02-03 20:50:35 -0200 | [diff] [blame] | 4237 | l2cap_cleanup_sockets(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | } |
| 4239 | |
Gustavo F. Padovan | d1c4a17 | 2010-07-18 16:25:54 -0300 | [diff] [blame] | 4240 | module_param(disable_ertm, bool, 0644); |
| 4241 | MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode"); |