blob: 6d8b988d9ef67021192adce947e5917535491f9b [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI connection handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28
29#include <linux/types.h>
30#include <linux/errno.h>
31#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
33#include <linux/poll.h>
34#include <linux/fcntl.h>
35#include <linux/init.h>
36#include <linux/skbuff.h>
37#include <linux/interrupt.h>
38#include <linux/notifier.h>
39#include <net/sock.h>
40
41#include <asm/system.h>
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/unaligned.h>
44
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
47
Ville Tervofcd89c02011-02-10 22:38:47 -030048static void hci_le_connect(struct hci_conn *conn)
49{
50 struct hci_dev *hdev = conn->hdev;
51 struct hci_cp_le_create_conn cp;
52
53 conn->state = BT_CONNECT;
54 conn->out = 1;
Vinicius Costa Gomesb92a6222011-02-10 22:38:52 -030055 conn->link_mode |= HCI_LM_MASTER;
Ville Tervofcd89c02011-02-10 22:38:47 -030056
57 memset(&cp, 0, sizeof(cp));
58 cp.scan_interval = cpu_to_le16(0x0004);
59 cp.scan_window = cpu_to_le16(0x0004);
60 bacpy(&cp.peer_addr, &conn->dst);
61 cp.conn_interval_min = cpu_to_le16(0x0008);
62 cp.conn_interval_max = cpu_to_le16(0x0100);
63 cp.supervision_timeout = cpu_to_le16(0x0064);
64 cp.min_ce_len = cpu_to_le16(0x0001);
65 cp.max_ce_len = cpu_to_le16(0x0001);
66
67 hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
68}
69
70static void hci_le_connect_cancel(struct hci_conn *conn)
71{
72 hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
73}
74
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020075void hci_acl_connect(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 struct hci_dev *hdev = conn->hdev;
78 struct inquiry_entry *ie;
79 struct hci_cp_create_conn cp;
80
81 BT_DBG("%p", conn);
82
83 conn->state = BT_CONNECT;
Marcel Holtmanna8746412008-07-14 20:13:46 +020084 conn->out = 1;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 conn->link_mode = HCI_LM_MASTER;
87
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020088 conn->attempt++;
89
Marcel Holtmanne4e8e372008-07-14 20:13:47 +020090 conn->link_policy = hdev->link_policy;
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 memset(&cp, 0, sizeof(cp));
93 bacpy(&cp.bdaddr, &conn->dst);
94 cp.pscan_rep_mode = 0x02;
95
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020096 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
97 if (ie) {
Marcel Holtmann41a96212008-07-14 20:13:48 +020098 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
99 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
100 cp.pscan_mode = ie->data.pscan_mode;
101 cp.clock_offset = ie->data.clock_offset |
102 cpu_to_le16(0x8000);
103 }
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 memcpy(conn->dev_class, ie->data.dev_class, 3);
Marcel Holtmann41a96212008-07-14 20:13:48 +0200106 conn->ssp_mode = ie->data.ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Marcel Holtmanna8746412008-07-14 20:13:46 +0200109 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200111 cp.role_switch = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 else
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200113 cp.role_switch = 0x00;
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200114
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200115 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200118static void hci_acl_connect_cancel(struct hci_conn *conn)
119{
120 struct hci_cp_create_conn_cancel cp;
121
122 BT_DBG("%p", conn);
123
124 if (conn->hdev->hci_ver < 2)
125 return;
126
127 bacpy(&cp.bdaddr, &conn->dst);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200128 hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
132{
133 struct hci_cp_disconnect cp;
134
135 BT_DBG("%p", conn);
136
137 conn->state = BT_DISCONN;
138
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700139 cp.handle = cpu_to_le16(conn->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 cp.reason = reason;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200141 hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144void hci_add_sco(struct hci_conn *conn, __u16 handle)
145{
146 struct hci_dev *hdev = conn->hdev;
147 struct hci_cp_add_sco cp;
148
149 BT_DBG("%p", conn);
150
151 conn->state = BT_CONNECT;
152 conn->out = 1;
153
Marcel Holtmannefc76882009-02-06 09:13:37 +0100154 conn->attempt++;
155
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700156 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200157 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200159 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200162void hci_setup_sync(struct hci_conn *conn, __u16 handle)
163{
164 struct hci_dev *hdev = conn->hdev;
165 struct hci_cp_setup_sync_conn cp;
166
167 BT_DBG("%p", conn);
168
169 conn->state = BT_CONNECT;
170 conn->out = 1;
171
Marcel Holtmannefc76882009-02-06 09:13:37 +0100172 conn->attempt++;
173
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200174 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200175 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200176
177 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
178 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
179 cp.max_latency = cpu_to_le16(0xffff);
180 cp.voice_setting = cpu_to_le16(hdev->voice_setting);
181 cp.retrans_effort = 0xff;
182
183 hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
184}
185
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200186void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
187 u16 latency, u16 to_multiplier)
188{
189 struct hci_cp_le_conn_update cp;
190 struct hci_dev *hdev = conn->hdev;
191
192 memset(&cp, 0, sizeof(cp));
193
194 cp.handle = cpu_to_le16(conn->handle);
195 cp.conn_interval_min = cpu_to_le16(min);
196 cp.conn_interval_max = cpu_to_le16(max);
197 cp.conn_latency = cpu_to_le16(latency);
198 cp.supervision_timeout = cpu_to_le16(to_multiplier);
199 cp.min_ce_len = cpu_to_le16(0x0001);
200 cp.max_ce_len = cpu_to_le16(0x0001);
201
202 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
203}
204EXPORT_SYMBOL(hci_le_conn_update);
205
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400206/* Device _must_ be locked */
207void hci_sco_setup(struct hci_conn *conn, __u8 status)
208{
209 struct hci_conn *sco = conn->link;
210
211 BT_DBG("%p", conn);
212
213 if (!sco)
214 return;
215
216 if (!status) {
217 if (lmp_esco_capable(conn->hdev))
218 hci_setup_sync(sco, conn->handle);
219 else
220 hci_add_sco(sco, conn->handle);
221 } else {
222 hci_proto_connect_cfm(sco, status);
223 hci_conn_del(sco);
224 }
225}
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227static void hci_conn_timeout(unsigned long arg)
228{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200229 struct hci_conn *conn = (void *) arg;
230 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100231 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 BT_DBG("conn %p state %d", conn, conn->state);
234
235 if (atomic_read(&conn->refcnt))
236 return;
237
238 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200239
240 switch (conn->state) {
241 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200242 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300243 if (conn->out) {
244 if (conn->type == ACL_LINK)
245 hci_acl_connect_cancel(conn);
246 else if (conn->type == LE_LINK)
247 hci_le_connect_cancel(conn);
248 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200249 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200250 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900251 case BT_CONNECTED:
Marcel Holtmann2950f212009-02-12 14:02:50 +0100252 reason = hci_proto_disconn_ind(conn);
253 hci_acl_disconn(conn, reason);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200254 break;
255 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200257 break;
258 }
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Marcel Holtmann04837f62006-07-03 10:02:33 +0200263static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200265 struct hci_conn *conn = (void *) arg;
266
267 BT_DBG("conn %p mode %d", conn, conn->mode);
268
269 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
273{
274 struct hci_conn *conn;
275
276 BT_DBG("%s dst %s", hdev->name, batostr(dst));
277
Marcel Holtmann04837f62006-07-03 10:02:33 +0200278 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
279 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200283 conn->hdev = hdev;
284 conn->type = type;
285 conn->mode = HCI_CM_ACTIVE;
286 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300287 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200288 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300289 conn->remote_auth = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Marcel Holtmann04837f62006-07-03 10:02:33 +0200291 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200292 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200293
Marcel Holtmanna8746412008-07-14 20:13:46 +0200294 switch (type) {
295 case ACL_LINK:
296 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
297 break;
298 case SCO_LINK:
299 if (lmp_esco_capable(hdev))
Marcel Holtmannefc76882009-02-06 09:13:37 +0100300 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
301 (hdev->esco_type & EDR_ESCO_MASK);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200302 else
303 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
304 break;
305 case ESCO_LINK:
Marcel Holtmannefc76882009-02-06 09:13:37 +0100306 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200307 break;
308 }
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200311
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800312 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
313 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 atomic_set(&conn->refcnt, 0);
316
317 hci_dev_hold(hdev);
318
319 tasklet_disable(&hdev->tx_task);
320
321 hci_conn_hash_add(hdev, conn);
322 if (hdev->notify)
323 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
324
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700325 atomic_set(&conn->devref, 0);
326
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700327 hci_conn_init_sysfs(conn);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 tasklet_enable(&hdev->tx_task);
330
331 return conn;
332}
333
334int hci_conn_del(struct hci_conn *conn)
335{
336 struct hci_dev *hdev = conn->hdev;
337
338 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
339
Marcel Holtmann04837f62006-07-03 10:02:33 +0200340 del_timer(&conn->idle_timer);
341
342 del_timer(&conn->disc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200344 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct hci_conn *sco = conn->link;
346 if (sco)
347 sco->link = NULL;
348
349 /* Unacked frames */
350 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300351 } else if (conn->type == LE_LINK) {
352 if (hdev->le_pkts)
353 hdev->le_cnt += conn->sent;
354 else
355 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200356 } else {
357 struct hci_conn *acl = conn->link;
358 if (acl) {
359 acl->link = NULL;
360 hci_conn_put(acl);
361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 hci_conn_hash_del(hdev, conn);
367 if (hdev->notify)
368 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700374 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800375
Marcel Holtmann384943e2009-05-08 18:20:43 -0700376 hci_dev_put(hdev);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379}
380
381struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
382{
383 int use_src = bacmp(src, BDADDR_ANY);
384 struct hci_dev *hdev = NULL;
385 struct list_head *p;
386
387 BT_DBG("%s -> %s", batostr(src), batostr(dst));
388
389 read_lock_bh(&hci_dev_list_lock);
390
391 list_for_each(p, &hci_dev_list) {
392 struct hci_dev *d = list_entry(p, struct hci_dev, list);
393
394 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
395 continue;
396
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900397 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * No source address - find interface with bdaddr != dst
399 * Source address - find interface with bdaddr == src
400 */
401
402 if (use_src) {
403 if (!bacmp(&d->bdaddr, src)) {
404 hdev = d; break;
405 }
406 } else {
407 if (bacmp(&d->bdaddr, dst)) {
408 hdev = d; break;
409 }
410 }
411 }
412
413 if (hdev)
414 hdev = hci_dev_hold(hdev);
415
416 read_unlock_bh(&hci_dev_list_lock);
417 return hdev;
418}
419EXPORT_SYMBOL(hci_get_route);
420
Ville Tervofcd89c02011-02-10 22:38:47 -0300421/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * Device _must_ be locked */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100423struct hci_conn *hci_connect(struct hci_dev *hdev, int type, bdaddr_t *dst, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200426 struct hci_conn *sco;
Ville Tervofcd89c02011-02-10 22:38:47 -0300427 struct hci_conn *le;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 BT_DBG("%s dst %s", hdev->name, batostr(dst));
430
Ville Tervofcd89c02011-02-10 22:38:47 -0300431 if (type == LE_LINK) {
432 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
433 if (!le)
434 le = hci_conn_add(hdev, LE_LINK, dst);
435 if (!le)
436 return NULL;
437 if (le->state == BT_OPEN)
438 hci_le_connect(le);
439
440 hci_conn_hold(le);
441
442 return le;
443 }
444
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200445 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
446 if (!acl) {
447 acl = hci_conn_add(hdev, ACL_LINK, dst);
448 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return NULL;
450 }
451
452 hci_conn_hold(acl);
453
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200454 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530455 acl->sec_level = BT_SECURITY_LOW;
456 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200457 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200461 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200463
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200464 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
465 if (!sco) {
466 sco = hci_conn_add(hdev, type, dst);
467 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200468 hci_conn_put(acl);
469 return NULL;
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200472
473 acl->link = sco;
474 sco->link = acl;
475
476 hci_conn_hold(sco);
477
478 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200479 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800480 acl->power_save = 1;
481 hci_conn_enter_active_mode(acl);
482
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400483 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
484 /* defer SCO setup until mode change completed */
485 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
486 return sco;
487 }
488
489 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200490 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200491
492 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494EXPORT_SYMBOL(hci_connect);
495
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200496/* Check link security requirement */
497int hci_conn_check_link_mode(struct hci_conn *conn)
498{
499 BT_DBG("conn %p", conn);
500
501 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
502 !(conn->link_mode & HCI_LM_ENCRYPT))
503 return 0;
504
505 return 1;
506}
507EXPORT_SYMBOL(hci_conn_check_link_mode);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100510static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 BT_DBG("conn %p", conn);
513
Johan Hedberg765c2a92011-01-19 12:06:52 +0530514 if (conn->pending_sec_level > sec_level)
515 sec_level = conn->pending_sec_level;
516
Marcel Holtmann96a31832009-02-12 16:23:03 +0100517 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530518 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100519 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return 1;
521
Johan Hedberg65cf6862011-01-19 12:06:49 +0530522 /* Make sure we preserve an existing MITM requirement*/
523 auth_type |= (conn->auth_type & 0x01);
524
Marcel Holtmann96a31832009-02-12 16:23:03 +0100525 conn->auth_type = auth_type;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
528 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700529 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200530 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
531 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return 0;
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100537/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100538int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 BT_DBG("conn %p", conn);
541
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100542 if (sec_level == BT_SECURITY_SDP)
543 return 1;
544
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700545 if (sec_level == BT_SECURITY_LOW &&
546 (!conn->ssp_mode || !conn->hdev->ssp_mode))
547 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (conn->link_mode & HCI_LM_ENCRYPT)
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100550 return hci_conn_auth(conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
553 return 0;
554
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100555 if (hci_conn_auth(conn, sec_level, auth_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct hci_cp_set_conn_encrypt cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700557 cp.handle = cpu_to_le16(conn->handle);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900558 cp.encrypt = 1;
Marcel Holtmann40be4922008-07-14 20:13:50 +0200559 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
560 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100565EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567/* Change link key */
568int hci_conn_change_link_key(struct hci_conn *conn)
569{
570 BT_DBG("conn %p", conn);
571
572 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
573 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700574 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200575 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
576 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return 0;
580}
581EXPORT_SYMBOL(hci_conn_change_link_key);
582
583/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100584int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 BT_DBG("conn %p", conn);
587
588 if (!role && conn->link_mode & HCI_LM_MASTER)
589 return 1;
590
591 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
592 struct hci_cp_switch_role cp;
593 bacpy(&cp.bdaddr, &conn->dst);
594 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200595 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 0;
599}
600EXPORT_SYMBOL(hci_conn_switch_role);
601
Marcel Holtmann04837f62006-07-03 10:02:33 +0200602/* Enter active mode */
603void hci_conn_enter_active_mode(struct hci_conn *conn)
604{
605 struct hci_dev *hdev = conn->hdev;
606
607 BT_DBG("conn %p mode %d", conn, conn->mode);
608
609 if (test_bit(HCI_RAW, &hdev->flags))
610 return;
611
612 if (conn->mode != HCI_CM_SNIFF || !conn->power_save)
613 goto timer;
614
615 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
616 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700617 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200618 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200619 }
620
621timer:
622 if (hdev->idle_timeout > 0)
623 mod_timer(&conn->idle_timer,
624 jiffies + msecs_to_jiffies(hdev->idle_timeout));
625}
626
627/* Enter sniff mode */
628void hci_conn_enter_sniff_mode(struct hci_conn *conn)
629{
630 struct hci_dev *hdev = conn->hdev;
631
632 BT_DBG("conn %p mode %d", conn, conn->mode);
633
634 if (test_bit(HCI_RAW, &hdev->flags))
635 return;
636
637 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
638 return;
639
640 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
641 return;
642
643 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
644 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700645 cp.handle = cpu_to_le16(conn->handle);
646 cp.max_latency = cpu_to_le16(0);
647 cp.min_remote_timeout = cpu_to_le16(0);
648 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200649 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200650 }
651
652 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
653 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700654 cp.handle = cpu_to_le16(conn->handle);
655 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
656 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
657 cp.attempt = cpu_to_le16(4);
658 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200659 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200660 }
661}
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663/* Drop all connection on the device */
664void hci_conn_hash_flush(struct hci_dev *hdev)
665{
666 struct hci_conn_hash *h = &hdev->conn_hash;
667 struct list_head *p;
668
669 BT_DBG("hdev %s", hdev->name);
670
671 p = h->list.next;
672 while (p != &h->list) {
673 struct hci_conn *c;
674
675 c = list_entry(p, struct hci_conn, list);
676 p = p->next;
677
678 c->state = BT_CLOSED;
679
Marcel Holtmann2950f212009-02-12 14:02:50 +0100680 hci_proto_disconn_cfm(c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 hci_conn_del(c);
682 }
683}
684
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200685/* Check pending connect attempts */
686void hci_conn_check_pending(struct hci_dev *hdev)
687{
688 struct hci_conn *conn;
689
690 BT_DBG("hdev %s", hdev->name);
691
692 hci_dev_lock(hdev);
693
694 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
695 if (conn)
696 hci_acl_connect(conn);
697
698 hci_dev_unlock(hdev);
699}
700
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700701void hci_conn_hold_device(struct hci_conn *conn)
702{
703 atomic_inc(&conn->devref);
704}
705EXPORT_SYMBOL(hci_conn_hold_device);
706
707void hci_conn_put_device(struct hci_conn *conn)
708{
709 if (atomic_dec_and_test(&conn->devref))
710 hci_conn_del_sysfs(conn);
711}
712EXPORT_SYMBOL(hci_conn_put_device);
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714int hci_get_conn_list(void __user *arg)
715{
716 struct hci_conn_list_req req, *cl;
717 struct hci_conn_info *ci;
718 struct hci_dev *hdev;
719 struct list_head *p;
720 int n = 0, size, err;
721
722 if (copy_from_user(&req, arg, sizeof(req)))
723 return -EFAULT;
724
725 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
726 return -EINVAL;
727
728 size = sizeof(req) + req.conn_num * sizeof(*ci);
729
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200730 cl = kmalloc(size, GFP_KERNEL);
731 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return -ENOMEM;
733
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200734 hdev = hci_dev_get(req.dev_id);
735 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 kfree(cl);
737 return -ENODEV;
738 }
739
740 ci = cl->conn_info;
741
742 hci_dev_lock_bh(hdev);
743 list_for_each(p, &hdev->conn_hash.list) {
744 register struct hci_conn *c;
745 c = list_entry(p, struct hci_conn, list);
746
747 bacpy(&(ci + n)->bdaddr, &c->dst);
748 (ci + n)->handle = c->handle;
749 (ci + n)->type = c->type;
750 (ci + n)->out = c->out;
751 (ci + n)->state = c->state;
752 (ci + n)->link_mode = c->link_mode;
753 if (++n >= req.conn_num)
754 break;
755 }
756 hci_dev_unlock_bh(hdev);
757
758 cl->dev_id = hdev->id;
759 cl->conn_num = n;
760 size = sizeof(req) + n * sizeof(*ci);
761
762 hci_dev_put(hdev);
763
764 err = copy_to_user(arg, cl, size);
765 kfree(cl);
766
767 return err ? -EFAULT : 0;
768}
769
770int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
771{
772 struct hci_conn_info_req req;
773 struct hci_conn_info ci;
774 struct hci_conn *conn;
775 char __user *ptr = arg + sizeof(req);
776
777 if (copy_from_user(&req, arg, sizeof(req)))
778 return -EFAULT;
779
780 hci_dev_lock_bh(hdev);
781 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
782 if (conn) {
783 bacpy(&ci.bdaddr, &conn->dst);
784 ci.handle = conn->handle;
785 ci.type = conn->type;
786 ci.out = conn->out;
787 ci.state = conn->state;
788 ci.link_mode = conn->link_mode;
789 }
790 hci_dev_unlock_bh(hdev);
791
792 if (!conn)
793 return -ENOENT;
794
795 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
796}
Marcel Holtmann40be4922008-07-14 20:13:50 +0200797
798int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
799{
800 struct hci_auth_info_req req;
801 struct hci_conn *conn;
802
803 if (copy_from_user(&req, arg, sizeof(req)))
804 return -EFAULT;
805
806 hci_dev_lock_bh(hdev);
807 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
808 if (conn)
809 req.type = conn->auth_type;
810 hci_dev_unlock_bh(hdev);
811
812 if (!conn)
813 return -ENOENT;
814
815 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
816}