blob: 6d0c52932525b4f317a9758cb868a1050f7045f5 [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);
Andre Guedesc7f0d992011-05-31 14:20:57 -030061 cp.peer_addr_type = conn->dst_type;
Ville Tervofcd89c02011-02-10 22:38:47 -030062 cp.conn_interval_min = cpu_to_le16(0x0008);
63 cp.conn_interval_max = cpu_to_le16(0x0100);
64 cp.supervision_timeout = cpu_to_le16(0x0064);
65 cp.min_ce_len = cpu_to_le16(0x0001);
66 cp.max_ce_len = cpu_to_le16(0x0001);
67
68 hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
69}
70
71static void hci_le_connect_cancel(struct hci_conn *conn)
72{
73 hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
74}
75
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020076void hci_acl_connect(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 struct hci_dev *hdev = conn->hdev;
79 struct inquiry_entry *ie;
80 struct hci_cp_create_conn cp;
81
82 BT_DBG("%p", conn);
83
84 conn->state = BT_CONNECT;
Marcel Holtmanna8746412008-07-14 20:13:46 +020085 conn->out = 1;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 conn->link_mode = HCI_LM_MASTER;
88
Marcel Holtmann4c67bc72006-10-15 17:30:56 +020089 conn->attempt++;
90
Marcel Holtmanne4e8e372008-07-14 20:13:47 +020091 conn->link_policy = hdev->link_policy;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 memset(&cp, 0, sizeof(cp));
94 bacpy(&cp.bdaddr, &conn->dst);
95 cp.pscan_rep_mode = 0x02;
96
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020097 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
98 if (ie) {
Marcel Holtmann41a96212008-07-14 20:13:48 +020099 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
100 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
101 cp.pscan_mode = ie->data.pscan_mode;
102 cp.clock_offset = ie->data.clock_offset |
103 cpu_to_le16(0x8000);
104 }
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 memcpy(conn->dev_class, ie->data.dev_class, 3);
Marcel Holtmann41a96212008-07-14 20:13:48 +0200107 conn->ssp_mode = ie->data.ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109
Marcel Holtmanna8746412008-07-14 20:13:46 +0200110 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200112 cp.role_switch = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 else
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200114 cp.role_switch = 0x00;
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200115
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200116 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200119static void hci_acl_connect_cancel(struct hci_conn *conn)
120{
121 struct hci_cp_create_conn_cancel cp;
122
123 BT_DBG("%p", conn);
124
125 if (conn->hdev->hci_ver < 2)
126 return;
127
128 bacpy(&cp.bdaddr, &conn->dst);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200129 hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
133{
134 struct hci_cp_disconnect cp;
135
136 BT_DBG("%p", conn);
137
138 conn->state = BT_DISCONN;
139
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700140 cp.handle = cpu_to_le16(conn->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 cp.reason = reason;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200142 hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145void hci_add_sco(struct hci_conn *conn, __u16 handle)
146{
147 struct hci_dev *hdev = conn->hdev;
148 struct hci_cp_add_sco cp;
149
150 BT_DBG("%p", conn);
151
152 conn->state = BT_CONNECT;
153 conn->out = 1;
154
Marcel Holtmannefc76882009-02-06 09:13:37 +0100155 conn->attempt++;
156
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700157 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200158 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200160 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200163void hci_setup_sync(struct hci_conn *conn, __u16 handle)
164{
165 struct hci_dev *hdev = conn->hdev;
166 struct hci_cp_setup_sync_conn cp;
167
168 BT_DBG("%p", conn);
169
170 conn->state = BT_CONNECT;
171 conn->out = 1;
172
Marcel Holtmannefc76882009-02-06 09:13:37 +0100173 conn->attempt++;
174
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200175 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200176 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200177
178 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
179 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
180 cp.max_latency = cpu_to_le16(0xffff);
181 cp.voice_setting = cpu_to_le16(hdev->voice_setting);
182 cp.retrans_effort = 0xff;
183
184 hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
185}
186
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200187void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
188 u16 latency, u16 to_multiplier)
189{
190 struct hci_cp_le_conn_update cp;
191 struct hci_dev *hdev = conn->hdev;
192
193 memset(&cp, 0, sizeof(cp));
194
195 cp.handle = cpu_to_le16(conn->handle);
196 cp.conn_interval_min = cpu_to_le16(min);
197 cp.conn_interval_max = cpu_to_le16(max);
198 cp.conn_latency = cpu_to_le16(latency);
199 cp.supervision_timeout = cpu_to_le16(to_multiplier);
200 cp.min_ce_len = cpu_to_le16(0x0001);
201 cp.max_ce_len = cpu_to_le16(0x0001);
202
203 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
204}
205EXPORT_SYMBOL(hci_le_conn_update);
206
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400207/* Device _must_ be locked */
208void hci_sco_setup(struct hci_conn *conn, __u8 status)
209{
210 struct hci_conn *sco = conn->link;
211
212 BT_DBG("%p", conn);
213
214 if (!sco)
215 return;
216
217 if (!status) {
218 if (lmp_esco_capable(conn->hdev))
219 hci_setup_sync(sco, conn->handle);
220 else
221 hci_add_sco(sco, conn->handle);
222 } else {
223 hci_proto_connect_cfm(sco, status);
224 hci_conn_del(sco);
225 }
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static void hci_conn_timeout(unsigned long arg)
229{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200230 struct hci_conn *conn = (void *) arg;
231 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100232 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 BT_DBG("conn %p state %d", conn, conn->state);
235
236 if (atomic_read(&conn->refcnt))
237 return;
238
239 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200240
241 switch (conn->state) {
242 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200243 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300244 if (conn->out) {
245 if (conn->type == ACL_LINK)
246 hci_acl_connect_cancel(conn);
247 else if (conn->type == LE_LINK)
248 hci_le_connect_cancel(conn);
249 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200250 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200251 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900252 case BT_CONNECTED:
Marcel Holtmann2950f212009-02-12 14:02:50 +0100253 reason = hci_proto_disconn_ind(conn);
254 hci_acl_disconn(conn, reason);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200255 break;
256 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200258 break;
259 }
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Marcel Holtmann04837f62006-07-03 10:02:33 +0200264static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200266 struct hci_conn *conn = (void *) arg;
267
268 BT_DBG("conn %p mode %d", conn, conn->mode);
269
270 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Johan Hedberg9f616562011-04-28 11:28:54 -0700273static void hci_conn_auto_accept(unsigned long arg)
274{
275 struct hci_conn *conn = (void *) arg;
276 struct hci_dev *hdev = conn->hdev;
277
278 hci_dev_lock(hdev);
279
280 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
281 &conn->dst);
282
283 hci_dev_unlock(hdev);
284}
285
Nick Pellybbcda3b2010-02-11 11:54:28 -0800286struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type,
287 __u16 pkt_type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct hci_conn *conn;
290
291 BT_DBG("%s dst %s", hdev->name, batostr(dst));
292
Marcel Holtmann04837f62006-07-03 10:02:33 +0200293 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
294 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200298 conn->hdev = hdev;
299 conn->type = type;
300 conn->mode = HCI_CM_ACTIVE;
301 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300302 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200303 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300304 conn->remote_auth = 0xff;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200305 conn->key_type = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Marcel Holtmann04837f62006-07-03 10:02:33 +0200307 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200308 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200309
Marcel Holtmanna8746412008-07-14 20:13:46 +0200310 switch (type) {
311 case ACL_LINK:
312 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
313 break;
314 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800315 if (!pkt_type)
316 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200317 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800318 if (!pkt_type)
319 pkt_type = ALL_ESCO_MASK;
320 if (lmp_esco_capable(hdev)) {
321 /* HCI Setup Synchronous Connection Command uses
322 reverse logic on the EDR_ESCO_MASK bits */
323 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
324 hdev->esco_type;
325 } else {
326 /* Legacy HCI Add Sco Connection Command uses a
327 shifted bitmask */
328 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
329 SCO_PTYPE_MASK;
330 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200331 break;
332 }
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200335
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800336 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
337 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Johan Hedberg9f616562011-04-28 11:28:54 -0700338 setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept,
339 (unsigned long) conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 atomic_set(&conn->refcnt, 0);
342
343 hci_dev_hold(hdev);
344
345 tasklet_disable(&hdev->tx_task);
346
347 hci_conn_hash_add(hdev, conn);
348 if (hdev->notify)
349 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
350
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700351 atomic_set(&conn->devref, 0);
352
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700353 hci_conn_init_sysfs(conn);
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 tasklet_enable(&hdev->tx_task);
356
357 return conn;
358}
359
360int hci_conn_del(struct hci_conn *conn)
361{
362 struct hci_dev *hdev = conn->hdev;
363
364 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
365
Marcel Holtmann04837f62006-07-03 10:02:33 +0200366 del_timer(&conn->idle_timer);
367
368 del_timer(&conn->disc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Johan Hedberg9f616562011-04-28 11:28:54 -0700370 del_timer(&conn->auto_accept_timer);
371
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200372 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct hci_conn *sco = conn->link;
374 if (sco)
375 sco->link = NULL;
376
377 /* Unacked frames */
378 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300379 } else if (conn->type == LE_LINK) {
380 if (hdev->le_pkts)
381 hdev->le_cnt += conn->sent;
382 else
383 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200384 } else {
385 struct hci_conn *acl = conn->link;
386 if (acl) {
387 acl->link = NULL;
388 hci_conn_put(acl);
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391
392 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 hci_conn_hash_del(hdev, conn);
395 if (hdev->notify)
396 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700402 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800403
Marcel Holtmann384943e2009-05-08 18:20:43 -0700404 hci_dev_put(hdev);
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407}
408
409struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
410{
411 int use_src = bacmp(src, BDADDR_ANY);
412 struct hci_dev *hdev = NULL;
413 struct list_head *p;
414
415 BT_DBG("%s -> %s", batostr(src), batostr(dst));
416
417 read_lock_bh(&hci_dev_list_lock);
418
419 list_for_each(p, &hci_dev_list) {
420 struct hci_dev *d = list_entry(p, struct hci_dev, list);
421
422 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
423 continue;
424
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900425 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * No source address - find interface with bdaddr != dst
427 * Source address - find interface with bdaddr == src
428 */
429
430 if (use_src) {
431 if (!bacmp(&d->bdaddr, src)) {
432 hdev = d; break;
433 }
434 } else {
435 if (bacmp(&d->bdaddr, dst)) {
436 hdev = d; break;
437 }
438 }
439 }
440
441 if (hdev)
442 hdev = hci_dev_hold(hdev);
443
444 read_unlock_bh(&hci_dev_list_lock);
445 return hdev;
446}
447EXPORT_SYMBOL(hci_get_route);
448
Ville Tervofcd89c02011-02-10 22:38:47 -0300449/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800451struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
452 __u16 pkt_type, bdaddr_t *dst,
453 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200456 struct hci_conn *sco;
Ville Tervofcd89c02011-02-10 22:38:47 -0300457 struct hci_conn *le;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 BT_DBG("%s dst %s", hdev->name, batostr(dst));
460
Ville Tervofcd89c02011-02-10 22:38:47 -0300461 if (type == LE_LINK) {
Andre Guedes5e89ece2011-05-31 14:20:56 -0300462 struct adv_entry *entry;
463
Ville Tervofcd89c02011-02-10 22:38:47 -0300464 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
Anderson Briglia15c47942011-02-21 15:09:23 -0300465 if (le)
Ville Tervo30e76272011-02-22 16:10:53 -0300466 return ERR_PTR(-EBUSY);
Andre Guedes5e89ece2011-05-31 14:20:56 -0300467
468 entry = hci_find_adv_entry(hdev, dst);
469 if (!entry)
470 return ERR_PTR(-EHOSTUNREACH);
471
Nick Pellybbcda3b2010-02-11 11:54:28 -0800472 le = hci_conn_add(hdev, LE_LINK, 0, dst);
Ville Tervofcd89c02011-02-10 22:38:47 -0300473 if (!le)
Ville Tervo30e76272011-02-22 16:10:53 -0300474 return ERR_PTR(-ENOMEM);
Andre Guedes92398c82011-05-31 14:20:55 -0300475
Andre Guedes5e89ece2011-05-31 14:20:56 -0300476 le->dst_type = entry->bdaddr_type;
477
Andre Guedes92398c82011-05-31 14:20:55 -0300478 hci_le_connect(le);
Ville Tervofcd89c02011-02-10 22:38:47 -0300479
480 hci_conn_hold(le);
481
482 return le;
483 }
484
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200485 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
486 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800487 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200488 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return NULL;
490 }
491
492 hci_conn_hold(acl);
493
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200494 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530495 acl->sec_level = BT_SECURITY_LOW;
496 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200497 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200501 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200503
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200504 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
505 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800506 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200507 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200508 hci_conn_put(acl);
509 return NULL;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200512
513 acl->link = sco;
514 sco->link = acl;
515
516 hci_conn_hold(sco);
517
518 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200519 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800520 acl->power_save = 1;
521 hci_conn_enter_active_mode(acl);
522
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400523 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
524 /* defer SCO setup until mode change completed */
525 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
526 return sco;
527 }
528
529 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200530 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200531
532 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534EXPORT_SYMBOL(hci_connect);
535
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200536/* Check link security requirement */
537int hci_conn_check_link_mode(struct hci_conn *conn)
538{
539 BT_DBG("conn %p", conn);
540
541 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
542 !(conn->link_mode & HCI_LM_ENCRYPT))
543 return 0;
544
545 return 1;
546}
547EXPORT_SYMBOL(hci_conn_check_link_mode);
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100550static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 BT_DBG("conn %p", conn);
553
Johan Hedberg765c2a92011-01-19 12:06:52 +0530554 if (conn->pending_sec_level > sec_level)
555 sec_level = conn->pending_sec_level;
556
Marcel Holtmann96a31832009-02-12 16:23:03 +0100557 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530558 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100559 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return 1;
561
Johan Hedberg65cf6862011-01-19 12:06:49 +0530562 /* Make sure we preserve an existing MITM requirement*/
563 auth_type |= (conn->auth_type & 0x01);
564
Marcel Holtmann96a31832009-02-12 16:23:03 +0100565 conn->auth_type = auth_type;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
568 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700569 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200570 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
571 sizeof(cp), &cp);
Waldemar Rymarkiewicz62c5f522011-05-31 15:49:25 +0200572 if (conn->key_type != 0xff)
573 set_bit(HCI_CONN_REAUTH_PEND, &conn->pend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200579/* Encrypt the the link */
580static void hci_conn_encrypt(struct hci_conn *conn)
581{
582 BT_DBG("conn %p", conn);
583
584 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
585 struct hci_cp_set_conn_encrypt cp;
586 cp.handle = cpu_to_le16(conn->handle);
587 cp.encrypt = 0x01;
588 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
589 &cp);
590 }
591}
592
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100593/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100594int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 BT_DBG("conn %p", conn);
597
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200598 /* For sdp we don't need the link key. */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100599 if (sec_level == BT_SECURITY_SDP)
600 return 1;
601
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200602 /* For non 2.1 devices and low security level we don't need the link
603 key. */
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700604 if (sec_level == BT_SECURITY_LOW &&
605 (!conn->ssp_mode || !conn->hdev->ssp_mode))
606 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100607
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200608 /* For other security levels we need the link key. */
609 if (!(conn->link_mode & HCI_LM_AUTH))
610 goto auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200612 /* An authenticated combination key has sufficient security for any
613 security level. */
614 if (conn->key_type == HCI_LK_AUTH_COMBINATION)
615 goto encrypt;
616
617 /* An unauthenticated combination key has sufficient security for
618 security level 1 and 2. */
619 if (conn->key_type == HCI_LK_UNAUTH_COMBINATION &&
620 (sec_level == BT_SECURITY_MEDIUM ||
621 sec_level == BT_SECURITY_LOW))
622 goto encrypt;
623
624 /* A combination key has always sufficient security for the security
625 levels 1 or 2. High security level requires the combination key
626 is generated using maximum PIN code length (16).
627 For pre 2.1 units. */
628 if (conn->key_type == HCI_LK_COMBINATION &&
629 (sec_level != BT_SECURITY_HIGH ||
630 conn->pin_length == 16))
631 goto encrypt;
632
633auth:
Ilia Kolomisnky33060542011-06-15 06:52:26 +0300634 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return 0;
636
Luiz Augusto von Dentz6fdf6582011-06-13 15:37:35 +0300637 if (!hci_conn_auth(conn, sec_level, auth_type))
638 return 0;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100639
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200640encrypt:
641 if (conn->link_mode & HCI_LM_ENCRYPT)
642 return 1;
643
644 hci_conn_encrypt(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100647EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +0200649/* Check secure link requirement */
650int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
651{
652 BT_DBG("conn %p", conn);
653
654 if (sec_level != BT_SECURITY_HIGH)
655 return 1; /* Accept if non-secure is required */
656
657 if (conn->key_type == HCI_LK_AUTH_COMBINATION ||
658 (conn->key_type == HCI_LK_COMBINATION &&
659 conn->pin_length == 16))
660 return 1;
661
662 return 0; /* Reject not secure link */
663}
664EXPORT_SYMBOL(hci_conn_check_secure);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/* Change link key */
667int hci_conn_change_link_key(struct hci_conn *conn)
668{
669 BT_DBG("conn %p", conn);
670
671 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
672 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700673 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200674 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
675 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return 0;
679}
680EXPORT_SYMBOL(hci_conn_change_link_key);
681
682/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100683int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 BT_DBG("conn %p", conn);
686
687 if (!role && conn->link_mode & HCI_LM_MASTER)
688 return 1;
689
690 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
691 struct hci_cp_switch_role cp;
692 bacpy(&cp.bdaddr, &conn->dst);
693 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200694 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699EXPORT_SYMBOL(hci_conn_switch_role);
700
Marcel Holtmann04837f62006-07-03 10:02:33 +0200701/* Enter active mode */
702void hci_conn_enter_active_mode(struct hci_conn *conn)
703{
704 struct hci_dev *hdev = conn->hdev;
705
706 BT_DBG("conn %p mode %d", conn, conn->mode);
707
708 if (test_bit(HCI_RAW, &hdev->flags))
709 return;
710
711 if (conn->mode != HCI_CM_SNIFF || !conn->power_save)
712 goto timer;
713
714 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
715 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700716 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200717 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200718 }
719
720timer:
721 if (hdev->idle_timeout > 0)
722 mod_timer(&conn->idle_timer,
723 jiffies + msecs_to_jiffies(hdev->idle_timeout));
724}
725
726/* Enter sniff mode */
727void hci_conn_enter_sniff_mode(struct hci_conn *conn)
728{
729 struct hci_dev *hdev = conn->hdev;
730
731 BT_DBG("conn %p mode %d", conn, conn->mode);
732
733 if (test_bit(HCI_RAW, &hdev->flags))
734 return;
735
736 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
737 return;
738
739 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
740 return;
741
742 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
743 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700744 cp.handle = cpu_to_le16(conn->handle);
745 cp.max_latency = cpu_to_le16(0);
746 cp.min_remote_timeout = cpu_to_le16(0);
747 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200748 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200749 }
750
751 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
752 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700753 cp.handle = cpu_to_le16(conn->handle);
754 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
755 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
756 cp.attempt = cpu_to_le16(4);
757 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200758 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200759 }
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/* Drop all connection on the device */
763void hci_conn_hash_flush(struct hci_dev *hdev)
764{
765 struct hci_conn_hash *h = &hdev->conn_hash;
766 struct list_head *p;
767
768 BT_DBG("hdev %s", hdev->name);
769
770 p = h->list.next;
771 while (p != &h->list) {
772 struct hci_conn *c;
773
774 c = list_entry(p, struct hci_conn, list);
775 p = p->next;
776
777 c->state = BT_CLOSED;
778
Marcel Holtmann2950f212009-02-12 14:02:50 +0100779 hci_proto_disconn_cfm(c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 hci_conn_del(c);
781 }
782}
783
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200784/* Check pending connect attempts */
785void hci_conn_check_pending(struct hci_dev *hdev)
786{
787 struct hci_conn *conn;
788
789 BT_DBG("hdev %s", hdev->name);
790
791 hci_dev_lock(hdev);
792
793 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
794 if (conn)
795 hci_acl_connect(conn);
796
797 hci_dev_unlock(hdev);
798}
799
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700800void hci_conn_hold_device(struct hci_conn *conn)
801{
802 atomic_inc(&conn->devref);
803}
804EXPORT_SYMBOL(hci_conn_hold_device);
805
806void hci_conn_put_device(struct hci_conn *conn)
807{
808 if (atomic_dec_and_test(&conn->devref))
809 hci_conn_del_sysfs(conn);
810}
811EXPORT_SYMBOL(hci_conn_put_device);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813int hci_get_conn_list(void __user *arg)
814{
815 struct hci_conn_list_req req, *cl;
816 struct hci_conn_info *ci;
817 struct hci_dev *hdev;
818 struct list_head *p;
819 int n = 0, size, err;
820
821 if (copy_from_user(&req, arg, sizeof(req)))
822 return -EFAULT;
823
824 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
825 return -EINVAL;
826
827 size = sizeof(req) + req.conn_num * sizeof(*ci);
828
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200829 cl = kmalloc(size, GFP_KERNEL);
830 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return -ENOMEM;
832
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200833 hdev = hci_dev_get(req.dev_id);
834 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 kfree(cl);
836 return -ENODEV;
837 }
838
839 ci = cl->conn_info;
840
841 hci_dev_lock_bh(hdev);
842 list_for_each(p, &hdev->conn_hash.list) {
843 register struct hci_conn *c;
844 c = list_entry(p, struct hci_conn, list);
845
846 bacpy(&(ci + n)->bdaddr, &c->dst);
847 (ci + n)->handle = c->handle;
848 (ci + n)->type = c->type;
849 (ci + n)->out = c->out;
850 (ci + n)->state = c->state;
851 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -0800852 if (c->type == SCO_LINK) {
853 (ci + n)->mtu = hdev->sco_mtu;
854 (ci + n)->cnt = hdev->sco_cnt;
855 (ci + n)->pkts = hdev->sco_pkts;
856 } else {
857 (ci + n)->mtu = hdev->acl_mtu;
858 (ci + n)->cnt = hdev->acl_cnt;
859 (ci + n)->pkts = hdev->acl_pkts;
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (++n >= req.conn_num)
862 break;
863 }
864 hci_dev_unlock_bh(hdev);
865
866 cl->dev_id = hdev->id;
867 cl->conn_num = n;
868 size = sizeof(req) + n * sizeof(*ci);
869
870 hci_dev_put(hdev);
871
872 err = copy_to_user(arg, cl, size);
873 kfree(cl);
874
875 return err ? -EFAULT : 0;
876}
877
878int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
879{
880 struct hci_conn_info_req req;
881 struct hci_conn_info ci;
882 struct hci_conn *conn;
883 char __user *ptr = arg + sizeof(req);
884
885 if (copy_from_user(&req, arg, sizeof(req)))
886 return -EFAULT;
887
888 hci_dev_lock_bh(hdev);
889 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
890 if (conn) {
891 bacpy(&ci.bdaddr, &conn->dst);
892 ci.handle = conn->handle;
893 ci.type = conn->type;
894 ci.out = conn->out;
895 ci.state = conn->state;
896 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -0800897 if (req.type == SCO_LINK) {
898 ci.mtu = hdev->sco_mtu;
899 ci.cnt = hdev->sco_cnt;
900 ci.pkts = hdev->sco_pkts;
901 } else {
902 ci.mtu = hdev->acl_mtu;
903 ci.cnt = hdev->acl_cnt;
904 ci.pkts = hdev->acl_pkts;
905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 hci_dev_unlock_bh(hdev);
908
909 if (!conn)
910 return -ENOENT;
911
912 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
913}
Marcel Holtmann40be4922008-07-14 20:13:50 +0200914
915int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
916{
917 struct hci_auth_info_req req;
918 struct hci_conn *conn;
919
920 if (copy_from_user(&req, arg, sizeof(req)))
921 return -EFAULT;
922
923 hci_dev_lock_bh(hdev);
924 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
925 if (conn)
926 req.type = conn->auth_type;
927 hci_dev_unlock_bh(hdev);
928
929 if (!conn)
930 return -ENOENT;
931
932 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
933}