blob: efcd2b508f5d996764b87e81cb7c133b1364171b [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
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400186/* Device _must_ be locked */
187void hci_sco_setup(struct hci_conn *conn, __u8 status)
188{
189 struct hci_conn *sco = conn->link;
190
191 BT_DBG("%p", conn);
192
193 if (!sco)
194 return;
195
196 if (!status) {
197 if (lmp_esco_capable(conn->hdev))
198 hci_setup_sync(sco, conn->handle);
199 else
200 hci_add_sco(sco, conn->handle);
201 } else {
202 hci_proto_connect_cfm(sco, status);
203 hci_conn_del(sco);
204 }
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static void hci_conn_timeout(unsigned long arg)
208{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200209 struct hci_conn *conn = (void *) arg;
210 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100211 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 BT_DBG("conn %p state %d", conn, conn->state);
214
215 if (atomic_read(&conn->refcnt))
216 return;
217
218 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200219
220 switch (conn->state) {
221 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200222 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300223 if (conn->out) {
224 if (conn->type == ACL_LINK)
225 hci_acl_connect_cancel(conn);
226 else if (conn->type == LE_LINK)
227 hci_le_connect_cancel(conn);
228 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200229 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200230 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900231 case BT_CONNECTED:
Marcel Holtmann2950f212009-02-12 14:02:50 +0100232 reason = hci_proto_disconn_ind(conn);
233 hci_acl_disconn(conn, reason);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200234 break;
235 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200237 break;
238 }
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Marcel Holtmann04837f62006-07-03 10:02:33 +0200243static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200245 struct hci_conn *conn = (void *) arg;
246
247 BT_DBG("conn %p mode %d", conn, conn->mode);
248
249 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
253{
254 struct hci_conn *conn;
255
256 BT_DBG("%s dst %s", hdev->name, batostr(dst));
257
Marcel Holtmann04837f62006-07-03 10:02:33 +0200258 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
259 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200263 conn->hdev = hdev;
264 conn->type = type;
265 conn->mode = HCI_CM_ACTIVE;
266 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300267 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200268 conn->io_capability = hdev->io_capability;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Marcel Holtmann04837f62006-07-03 10:02:33 +0200270 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200271 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200272
Marcel Holtmanna8746412008-07-14 20:13:46 +0200273 switch (type) {
274 case ACL_LINK:
275 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
276 break;
277 case SCO_LINK:
278 if (lmp_esco_capable(hdev))
Marcel Holtmannefc76882009-02-06 09:13:37 +0100279 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
280 (hdev->esco_type & EDR_ESCO_MASK);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200281 else
282 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
283 break;
284 case ESCO_LINK:
Marcel Holtmannefc76882009-02-06 09:13:37 +0100285 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200286 break;
287 }
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200290
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800291 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
292 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 atomic_set(&conn->refcnt, 0);
295
296 hci_dev_hold(hdev);
297
298 tasklet_disable(&hdev->tx_task);
299
300 hci_conn_hash_add(hdev, conn);
301 if (hdev->notify)
302 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
303
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700304 atomic_set(&conn->devref, 0);
305
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700306 hci_conn_init_sysfs(conn);
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 tasklet_enable(&hdev->tx_task);
309
310 return conn;
311}
312
313int hci_conn_del(struct hci_conn *conn)
314{
315 struct hci_dev *hdev = conn->hdev;
316
317 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
318
Marcel Holtmann04837f62006-07-03 10:02:33 +0200319 del_timer(&conn->idle_timer);
320
321 del_timer(&conn->disc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200323 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct hci_conn *sco = conn->link;
325 if (sco)
326 sco->link = NULL;
327
328 /* Unacked frames */
329 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300330 } else if (conn->type == LE_LINK) {
331 if (hdev->le_pkts)
332 hdev->le_cnt += conn->sent;
333 else
334 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200335 } else {
336 struct hci_conn *acl = conn->link;
337 if (acl) {
338 acl->link = NULL;
339 hci_conn_put(acl);
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 hci_conn_hash_del(hdev, conn);
346 if (hdev->notify)
347 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700353 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800354
Marcel Holtmann384943e2009-05-08 18:20:43 -0700355 hci_dev_put(hdev);
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359
360struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
361{
362 int use_src = bacmp(src, BDADDR_ANY);
363 struct hci_dev *hdev = NULL;
364 struct list_head *p;
365
366 BT_DBG("%s -> %s", batostr(src), batostr(dst));
367
368 read_lock_bh(&hci_dev_list_lock);
369
370 list_for_each(p, &hci_dev_list) {
371 struct hci_dev *d = list_entry(p, struct hci_dev, list);
372
373 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
374 continue;
375
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900376 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 * No source address - find interface with bdaddr != dst
378 * Source address - find interface with bdaddr == src
379 */
380
381 if (use_src) {
382 if (!bacmp(&d->bdaddr, src)) {
383 hdev = d; break;
384 }
385 } else {
386 if (bacmp(&d->bdaddr, dst)) {
387 hdev = d; break;
388 }
389 }
390 }
391
392 if (hdev)
393 hdev = hci_dev_hold(hdev);
394
395 read_unlock_bh(&hci_dev_list_lock);
396 return hdev;
397}
398EXPORT_SYMBOL(hci_get_route);
399
Ville Tervofcd89c02011-02-10 22:38:47 -0300400/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * Device _must_ be locked */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100402struct 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 -0700403{
404 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200405 struct hci_conn *sco;
Ville Tervofcd89c02011-02-10 22:38:47 -0300406 struct hci_conn *le;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 BT_DBG("%s dst %s", hdev->name, batostr(dst));
409
Ville Tervofcd89c02011-02-10 22:38:47 -0300410 if (type == LE_LINK) {
411 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
412 if (!le)
413 le = hci_conn_add(hdev, LE_LINK, dst);
414 if (!le)
415 return NULL;
416 if (le->state == BT_OPEN)
417 hci_le_connect(le);
418
419 hci_conn_hold(le);
420
421 return le;
422 }
423
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200424 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
425 if (!acl) {
426 acl = hci_conn_add(hdev, ACL_LINK, dst);
427 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return NULL;
429 }
430
431 hci_conn_hold(acl);
432
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200433 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530434 acl->sec_level = BT_SECURITY_LOW;
435 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200436 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200440 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200442
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200443 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
444 if (!sco) {
445 sco = hci_conn_add(hdev, type, dst);
446 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200447 hci_conn_put(acl);
448 return NULL;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200451
452 acl->link = sco;
453 sco->link = acl;
454
455 hci_conn_hold(sco);
456
457 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200458 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800459 acl->power_save = 1;
460 hci_conn_enter_active_mode(acl);
461
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400462 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
463 /* defer SCO setup until mode change completed */
464 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
465 return sco;
466 }
467
468 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200469 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200470
471 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473EXPORT_SYMBOL(hci_connect);
474
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200475/* Check link security requirement */
476int hci_conn_check_link_mode(struct hci_conn *conn)
477{
478 BT_DBG("conn %p", conn);
479
480 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
481 !(conn->link_mode & HCI_LM_ENCRYPT))
482 return 0;
483
484 return 1;
485}
486EXPORT_SYMBOL(hci_conn_check_link_mode);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100489static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 BT_DBG("conn %p", conn);
492
Johan Hedberg765c2a92011-01-19 12:06:52 +0530493 if (conn->pending_sec_level > sec_level)
494 sec_level = conn->pending_sec_level;
495
Marcel Holtmann96a31832009-02-12 16:23:03 +0100496 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530497 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100498 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 1;
500
Johan Hedberg65cf6862011-01-19 12:06:49 +0530501 /* Make sure we preserve an existing MITM requirement*/
502 auth_type |= (conn->auth_type & 0x01);
503
Marcel Holtmann96a31832009-02-12 16:23:03 +0100504 conn->auth_type = auth_type;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
507 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700508 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200509 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
510 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
514}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100516/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100517int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 BT_DBG("conn %p", conn);
520
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100521 if (sec_level == BT_SECURITY_SDP)
522 return 1;
523
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700524 if (sec_level == BT_SECURITY_LOW &&
525 (!conn->ssp_mode || !conn->hdev->ssp_mode))
526 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (conn->link_mode & HCI_LM_ENCRYPT)
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100529 return hci_conn_auth(conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
532 return 0;
533
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100534 if (hci_conn_auth(conn, sec_level, auth_type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct hci_cp_set_conn_encrypt cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700536 cp.handle = cpu_to_le16(conn->handle);
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900537 cp.encrypt = 1;
Marcel Holtmann40be4922008-07-14 20:13:50 +0200538 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
539 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
543}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100544EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546/* Change link key */
547int hci_conn_change_link_key(struct hci_conn *conn)
548{
549 BT_DBG("conn %p", conn);
550
551 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
552 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700553 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200554 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
555 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
559}
560EXPORT_SYMBOL(hci_conn_change_link_key);
561
562/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100563int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
565 BT_DBG("conn %p", conn);
566
567 if (!role && conn->link_mode & HCI_LM_MASTER)
568 return 1;
569
570 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
571 struct hci_cp_switch_role cp;
572 bacpy(&cp.bdaddr, &conn->dst);
573 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200574 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578}
579EXPORT_SYMBOL(hci_conn_switch_role);
580
Marcel Holtmann04837f62006-07-03 10:02:33 +0200581/* Enter active mode */
582void hci_conn_enter_active_mode(struct hci_conn *conn)
583{
584 struct hci_dev *hdev = conn->hdev;
585
586 BT_DBG("conn %p mode %d", conn, conn->mode);
587
588 if (test_bit(HCI_RAW, &hdev->flags))
589 return;
590
591 if (conn->mode != HCI_CM_SNIFF || !conn->power_save)
592 goto timer;
593
594 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
595 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700596 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200597 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200598 }
599
600timer:
601 if (hdev->idle_timeout > 0)
602 mod_timer(&conn->idle_timer,
603 jiffies + msecs_to_jiffies(hdev->idle_timeout));
604}
605
606/* Enter sniff mode */
607void hci_conn_enter_sniff_mode(struct hci_conn *conn)
608{
609 struct hci_dev *hdev = conn->hdev;
610
611 BT_DBG("conn %p mode %d", conn, conn->mode);
612
613 if (test_bit(HCI_RAW, &hdev->flags))
614 return;
615
616 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
617 return;
618
619 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
620 return;
621
622 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
623 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700624 cp.handle = cpu_to_le16(conn->handle);
625 cp.max_latency = cpu_to_le16(0);
626 cp.min_remote_timeout = cpu_to_le16(0);
627 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200628 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200629 }
630
631 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
632 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700633 cp.handle = cpu_to_le16(conn->handle);
634 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
635 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
636 cp.attempt = cpu_to_le16(4);
637 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200638 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200639 }
640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/* Drop all connection on the device */
643void hci_conn_hash_flush(struct hci_dev *hdev)
644{
645 struct hci_conn_hash *h = &hdev->conn_hash;
646 struct list_head *p;
647
648 BT_DBG("hdev %s", hdev->name);
649
650 p = h->list.next;
651 while (p != &h->list) {
652 struct hci_conn *c;
653
654 c = list_entry(p, struct hci_conn, list);
655 p = p->next;
656
657 c->state = BT_CLOSED;
658
Marcel Holtmann2950f212009-02-12 14:02:50 +0100659 hci_proto_disconn_cfm(c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 hci_conn_del(c);
661 }
662}
663
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200664/* Check pending connect attempts */
665void hci_conn_check_pending(struct hci_dev *hdev)
666{
667 struct hci_conn *conn;
668
669 BT_DBG("hdev %s", hdev->name);
670
671 hci_dev_lock(hdev);
672
673 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
674 if (conn)
675 hci_acl_connect(conn);
676
677 hci_dev_unlock(hdev);
678}
679
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700680void hci_conn_hold_device(struct hci_conn *conn)
681{
682 atomic_inc(&conn->devref);
683}
684EXPORT_SYMBOL(hci_conn_hold_device);
685
686void hci_conn_put_device(struct hci_conn *conn)
687{
688 if (atomic_dec_and_test(&conn->devref))
689 hci_conn_del_sysfs(conn);
690}
691EXPORT_SYMBOL(hci_conn_put_device);
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693int hci_get_conn_list(void __user *arg)
694{
695 struct hci_conn_list_req req, *cl;
696 struct hci_conn_info *ci;
697 struct hci_dev *hdev;
698 struct list_head *p;
699 int n = 0, size, err;
700
701 if (copy_from_user(&req, arg, sizeof(req)))
702 return -EFAULT;
703
704 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
705 return -EINVAL;
706
707 size = sizeof(req) + req.conn_num * sizeof(*ci);
708
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200709 cl = kmalloc(size, GFP_KERNEL);
710 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -ENOMEM;
712
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200713 hdev = hci_dev_get(req.dev_id);
714 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 kfree(cl);
716 return -ENODEV;
717 }
718
719 ci = cl->conn_info;
720
721 hci_dev_lock_bh(hdev);
722 list_for_each(p, &hdev->conn_hash.list) {
723 register struct hci_conn *c;
724 c = list_entry(p, struct hci_conn, list);
725
726 bacpy(&(ci + n)->bdaddr, &c->dst);
727 (ci + n)->handle = c->handle;
728 (ci + n)->type = c->type;
729 (ci + n)->out = c->out;
730 (ci + n)->state = c->state;
731 (ci + n)->link_mode = c->link_mode;
732 if (++n >= req.conn_num)
733 break;
734 }
735 hci_dev_unlock_bh(hdev);
736
737 cl->dev_id = hdev->id;
738 cl->conn_num = n;
739 size = sizeof(req) + n * sizeof(*ci);
740
741 hci_dev_put(hdev);
742
743 err = copy_to_user(arg, cl, size);
744 kfree(cl);
745
746 return err ? -EFAULT : 0;
747}
748
749int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
750{
751 struct hci_conn_info_req req;
752 struct hci_conn_info ci;
753 struct hci_conn *conn;
754 char __user *ptr = arg + sizeof(req);
755
756 if (copy_from_user(&req, arg, sizeof(req)))
757 return -EFAULT;
758
759 hci_dev_lock_bh(hdev);
760 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
761 if (conn) {
762 bacpy(&ci.bdaddr, &conn->dst);
763 ci.handle = conn->handle;
764 ci.type = conn->type;
765 ci.out = conn->out;
766 ci.state = conn->state;
767 ci.link_mode = conn->link_mode;
768 }
769 hci_dev_unlock_bh(hdev);
770
771 if (!conn)
772 return -ENOENT;
773
774 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
775}
Marcel Holtmann40be4922008-07-14 20:13:50 +0200776
777int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
778{
779 struct hci_auth_info_req req;
780 struct hci_conn *conn;
781
782 if (copy_from_user(&req, arg, sizeof(req)))
783 return -EFAULT;
784
785 hci_dev_lock_bh(hdev);
786 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
787 if (conn)
788 req.type = conn->auth_type;
789 hci_dev_unlock_bh(hdev);
790
791 if (!conn)
792 return -ENOENT;
793
794 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
795}