blob: 1de9c1c2e365be2c2c4031345032d7a4e560d467 [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
Johan Hedberg9f616562011-04-28 11:28:54 -0700272static void hci_conn_auto_accept(unsigned long arg)
273{
274 struct hci_conn *conn = (void *) arg;
275 struct hci_dev *hdev = conn->hdev;
276
277 hci_dev_lock(hdev);
278
279 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
280 &conn->dst);
281
282 hci_dev_unlock(hdev);
283}
284
Nick Pellybbcda3b2010-02-11 11:54:28 -0800285struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type,
286 __u16 pkt_type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct hci_conn *conn;
289
290 BT_DBG("%s dst %s", hdev->name, batostr(dst));
291
Marcel Holtmann04837f62006-07-03 10:02:33 +0200292 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
293 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200297 conn->hdev = hdev;
298 conn->type = type;
299 conn->mode = HCI_CM_ACTIVE;
300 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300301 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200302 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300303 conn->remote_auth = 0xff;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200304 conn->key_type = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Marcel Holtmann04837f62006-07-03 10:02:33 +0200306 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200307 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200308
Marcel Holtmanna8746412008-07-14 20:13:46 +0200309 switch (type) {
310 case ACL_LINK:
311 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
312 break;
313 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800314 if (!pkt_type)
315 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200316 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800317 if (!pkt_type)
318 pkt_type = ALL_ESCO_MASK;
319 if (lmp_esco_capable(hdev)) {
320 /* HCI Setup Synchronous Connection Command uses
321 reverse logic on the EDR_ESCO_MASK bits */
322 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
323 hdev->esco_type;
324 } else {
325 /* Legacy HCI Add Sco Connection Command uses a
326 shifted bitmask */
327 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
328 SCO_PTYPE_MASK;
329 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200330 break;
331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200334
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800335 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
336 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Johan Hedberg9f616562011-04-28 11:28:54 -0700337 setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept,
338 (unsigned long) conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 atomic_set(&conn->refcnt, 0);
341
342 hci_dev_hold(hdev);
343
344 tasklet_disable(&hdev->tx_task);
345
346 hci_conn_hash_add(hdev, conn);
347 if (hdev->notify)
348 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
349
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700350 atomic_set(&conn->devref, 0);
351
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700352 hci_conn_init_sysfs(conn);
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 tasklet_enable(&hdev->tx_task);
355
356 return conn;
357}
358
359int hci_conn_del(struct hci_conn *conn)
360{
361 struct hci_dev *hdev = conn->hdev;
362
363 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
364
Marcel Holtmann04837f62006-07-03 10:02:33 +0200365 del_timer(&conn->idle_timer);
366
367 del_timer(&conn->disc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Johan Hedberg9f616562011-04-28 11:28:54 -0700369 del_timer(&conn->auto_accept_timer);
370
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200371 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct hci_conn *sco = conn->link;
373 if (sco)
374 sco->link = NULL;
375
376 /* Unacked frames */
377 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300378 } else if (conn->type == LE_LINK) {
379 if (hdev->le_pkts)
380 hdev->le_cnt += conn->sent;
381 else
382 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200383 } else {
384 struct hci_conn *acl = conn->link;
385 if (acl) {
386 acl->link = NULL;
387 hci_conn_put(acl);
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
391 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 hci_conn_hash_del(hdev, conn);
394 if (hdev->notify)
395 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700401 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800402
Marcel Holtmann384943e2009-05-08 18:20:43 -0700403 hci_dev_put(hdev);
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
406}
407
408struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
409{
410 int use_src = bacmp(src, BDADDR_ANY);
411 struct hci_dev *hdev = NULL;
412 struct list_head *p;
413
414 BT_DBG("%s -> %s", batostr(src), batostr(dst));
415
416 read_lock_bh(&hci_dev_list_lock);
417
418 list_for_each(p, &hci_dev_list) {
419 struct hci_dev *d = list_entry(p, struct hci_dev, list);
420
421 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
422 continue;
423
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900424 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 * No source address - find interface with bdaddr != dst
426 * Source address - find interface with bdaddr == src
427 */
428
429 if (use_src) {
430 if (!bacmp(&d->bdaddr, src)) {
431 hdev = d; break;
432 }
433 } else {
434 if (bacmp(&d->bdaddr, dst)) {
435 hdev = d; break;
436 }
437 }
438 }
439
440 if (hdev)
441 hdev = hci_dev_hold(hdev);
442
443 read_unlock_bh(&hci_dev_list_lock);
444 return hdev;
445}
446EXPORT_SYMBOL(hci_get_route);
447
Ville Tervofcd89c02011-02-10 22:38:47 -0300448/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800450struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
451 __u16 pkt_type, bdaddr_t *dst,
452 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
454 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200455 struct hci_conn *sco;
Ville Tervofcd89c02011-02-10 22:38:47 -0300456 struct hci_conn *le;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 BT_DBG("%s dst %s", hdev->name, batostr(dst));
459
Ville Tervofcd89c02011-02-10 22:38:47 -0300460 if (type == LE_LINK) {
Andre Guedes5e89ece2011-05-31 14:20:56 -0300461 struct adv_entry *entry;
462
Ville Tervofcd89c02011-02-10 22:38:47 -0300463 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
Anderson Briglia15c47942011-02-21 15:09:23 -0300464 if (le)
Ville Tervo30e76272011-02-22 16:10:53 -0300465 return ERR_PTR(-EBUSY);
Andre Guedes5e89ece2011-05-31 14:20:56 -0300466
467 entry = hci_find_adv_entry(hdev, dst);
468 if (!entry)
469 return ERR_PTR(-EHOSTUNREACH);
470
Nick Pellybbcda3b2010-02-11 11:54:28 -0800471 le = hci_conn_add(hdev, LE_LINK, 0, dst);
Ville Tervofcd89c02011-02-10 22:38:47 -0300472 if (!le)
Ville Tervo30e76272011-02-22 16:10:53 -0300473 return ERR_PTR(-ENOMEM);
Andre Guedes92398c82011-05-31 14:20:55 -0300474
Andre Guedes5e89ece2011-05-31 14:20:56 -0300475 le->dst_type = entry->bdaddr_type;
476
Andre Guedes92398c82011-05-31 14:20:55 -0300477 hci_le_connect(le);
Ville Tervofcd89c02011-02-10 22:38:47 -0300478
479 hci_conn_hold(le);
480
481 return le;
482 }
483
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200484 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
485 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800486 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200487 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return NULL;
489 }
490
491 hci_conn_hold(acl);
492
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200493 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530494 acl->sec_level = BT_SECURITY_LOW;
495 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200496 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200500 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200502
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200503 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
504 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800505 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200506 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200507 hci_conn_put(acl);
508 return NULL;
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200511
512 acl->link = sco;
513 sco->link = acl;
514
515 hci_conn_hold(sco);
516
517 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200518 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800519 acl->power_save = 1;
520 hci_conn_enter_active_mode(acl);
521
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400522 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
523 /* defer SCO setup until mode change completed */
524 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
525 return sco;
526 }
527
528 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200529 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200530
531 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533EXPORT_SYMBOL(hci_connect);
534
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200535/* Check link security requirement */
536int hci_conn_check_link_mode(struct hci_conn *conn)
537{
538 BT_DBG("conn %p", conn);
539
540 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
541 !(conn->link_mode & HCI_LM_ENCRYPT))
542 return 0;
543
544 return 1;
545}
546EXPORT_SYMBOL(hci_conn_check_link_mode);
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100549static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 BT_DBG("conn %p", conn);
552
Johan Hedberg765c2a92011-01-19 12:06:52 +0530553 if (conn->pending_sec_level > sec_level)
554 sec_level = conn->pending_sec_level;
555
Marcel Holtmann96a31832009-02-12 16:23:03 +0100556 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530557 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100558 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 1;
560
Johan Hedberg65cf6862011-01-19 12:06:49 +0530561 /* Make sure we preserve an existing MITM requirement*/
562 auth_type |= (conn->auth_type & 0x01);
563
Marcel Holtmann96a31832009-02-12 16:23:03 +0100564 conn->auth_type = auth_type;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
567 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700568 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200569 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
570 sizeof(cp), &cp);
Waldemar Rymarkiewicz62c5f522011-05-31 15:49:25 +0200571 if (conn->key_type != 0xff)
572 set_bit(HCI_CONN_REAUTH_PEND, &conn->pend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return 0;
576}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200578/* Encrypt the the link */
579static void hci_conn_encrypt(struct hci_conn *conn)
580{
581 BT_DBG("conn %p", conn);
582
583 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
584 struct hci_cp_set_conn_encrypt cp;
585 cp.handle = cpu_to_le16(conn->handle);
586 cp.encrypt = 0x01;
587 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
588 &cp);
589 }
590}
591
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100592/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100593int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 BT_DBG("conn %p", conn);
596
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200597 /* For sdp we don't need the link key. */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100598 if (sec_level == BT_SECURITY_SDP)
599 return 1;
600
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200601 /* For non 2.1 devices and low security level we don't need the link
602 key. */
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700603 if (sec_level == BT_SECURITY_LOW &&
604 (!conn->ssp_mode || !conn->hdev->ssp_mode))
605 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100606
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200607 /* For other security levels we need the link key. */
608 if (!(conn->link_mode & HCI_LM_AUTH))
609 goto auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200611 /* An authenticated combination key has sufficient security for any
612 security level. */
613 if (conn->key_type == HCI_LK_AUTH_COMBINATION)
614 goto encrypt;
615
616 /* An unauthenticated combination key has sufficient security for
617 security level 1 and 2. */
618 if (conn->key_type == HCI_LK_UNAUTH_COMBINATION &&
619 (sec_level == BT_SECURITY_MEDIUM ||
620 sec_level == BT_SECURITY_LOW))
621 goto encrypt;
622
623 /* A combination key has always sufficient security for the security
624 levels 1 or 2. High security level requires the combination key
625 is generated using maximum PIN code length (16).
626 For pre 2.1 units. */
627 if (conn->key_type == HCI_LK_COMBINATION &&
628 (sec_level != BT_SECURITY_HIGH ||
629 conn->pin_length == 16))
630 goto encrypt;
631
632auth:
Ilia Kolomisnky33060542011-06-15 06:52:26 +0300633 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 0;
635
Luiz Augusto von Dentz6fdf6582011-06-13 15:37:35 +0300636 if (!hci_conn_auth(conn, sec_level, auth_type))
637 return 0;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100638
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200639encrypt:
640 if (conn->link_mode & HCI_LM_ENCRYPT)
641 return 1;
642
643 hci_conn_encrypt(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 0;
645}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100646EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +0200648/* Check secure link requirement */
649int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
650{
651 BT_DBG("conn %p", conn);
652
653 if (sec_level != BT_SECURITY_HIGH)
654 return 1; /* Accept if non-secure is required */
655
656 if (conn->key_type == HCI_LK_AUTH_COMBINATION ||
657 (conn->key_type == HCI_LK_COMBINATION &&
658 conn->pin_length == 16))
659 return 1;
660
661 return 0; /* Reject not secure link */
662}
663EXPORT_SYMBOL(hci_conn_check_secure);
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/* Change link key */
666int hci_conn_change_link_key(struct hci_conn *conn)
667{
668 BT_DBG("conn %p", conn);
669
670 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
671 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700672 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200673 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
674 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 0;
678}
679EXPORT_SYMBOL(hci_conn_change_link_key);
680
681/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100682int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 BT_DBG("conn %p", conn);
685
686 if (!role && conn->link_mode & HCI_LM_MASTER)
687 return 1;
688
689 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
690 struct hci_cp_switch_role cp;
691 bacpy(&cp.bdaddr, &conn->dst);
692 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200693 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
697}
698EXPORT_SYMBOL(hci_conn_switch_role);
699
Marcel Holtmann04837f62006-07-03 10:02:33 +0200700/* Enter active mode */
701void hci_conn_enter_active_mode(struct hci_conn *conn)
702{
703 struct hci_dev *hdev = conn->hdev;
704
705 BT_DBG("conn %p mode %d", conn, conn->mode);
706
707 if (test_bit(HCI_RAW, &hdev->flags))
708 return;
709
710 if (conn->mode != HCI_CM_SNIFF || !conn->power_save)
711 goto timer;
712
713 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
714 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700715 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200716 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200717 }
718
719timer:
720 if (hdev->idle_timeout > 0)
721 mod_timer(&conn->idle_timer,
722 jiffies + msecs_to_jiffies(hdev->idle_timeout));
723}
724
725/* Enter sniff mode */
726void hci_conn_enter_sniff_mode(struct hci_conn *conn)
727{
728 struct hci_dev *hdev = conn->hdev;
729
730 BT_DBG("conn %p mode %d", conn, conn->mode);
731
732 if (test_bit(HCI_RAW, &hdev->flags))
733 return;
734
735 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
736 return;
737
738 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
739 return;
740
741 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
742 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700743 cp.handle = cpu_to_le16(conn->handle);
744 cp.max_latency = cpu_to_le16(0);
745 cp.min_remote_timeout = cpu_to_le16(0);
746 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200747 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200748 }
749
750 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
751 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700752 cp.handle = cpu_to_le16(conn->handle);
753 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
754 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
755 cp.attempt = cpu_to_le16(4);
756 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200757 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200758 }
759}
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761/* Drop all connection on the device */
762void hci_conn_hash_flush(struct hci_dev *hdev)
763{
764 struct hci_conn_hash *h = &hdev->conn_hash;
765 struct list_head *p;
766
767 BT_DBG("hdev %s", hdev->name);
768
769 p = h->list.next;
770 while (p != &h->list) {
771 struct hci_conn *c;
772
773 c = list_entry(p, struct hci_conn, list);
774 p = p->next;
775
776 c->state = BT_CLOSED;
777
Marcel Holtmann2950f212009-02-12 14:02:50 +0100778 hci_proto_disconn_cfm(c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 hci_conn_del(c);
780 }
781}
782
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200783/* Check pending connect attempts */
784void hci_conn_check_pending(struct hci_dev *hdev)
785{
786 struct hci_conn *conn;
787
788 BT_DBG("hdev %s", hdev->name);
789
790 hci_dev_lock(hdev);
791
792 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
793 if (conn)
794 hci_acl_connect(conn);
795
796 hci_dev_unlock(hdev);
797}
798
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700799void hci_conn_hold_device(struct hci_conn *conn)
800{
801 atomic_inc(&conn->devref);
802}
803EXPORT_SYMBOL(hci_conn_hold_device);
804
805void hci_conn_put_device(struct hci_conn *conn)
806{
807 if (atomic_dec_and_test(&conn->devref))
808 hci_conn_del_sysfs(conn);
809}
810EXPORT_SYMBOL(hci_conn_put_device);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812int hci_get_conn_list(void __user *arg)
813{
814 struct hci_conn_list_req req, *cl;
815 struct hci_conn_info *ci;
816 struct hci_dev *hdev;
817 struct list_head *p;
818 int n = 0, size, err;
819
820 if (copy_from_user(&req, arg, sizeof(req)))
821 return -EFAULT;
822
823 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
824 return -EINVAL;
825
826 size = sizeof(req) + req.conn_num * sizeof(*ci);
827
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200828 cl = kmalloc(size, GFP_KERNEL);
829 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -ENOMEM;
831
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200832 hdev = hci_dev_get(req.dev_id);
833 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 kfree(cl);
835 return -ENODEV;
836 }
837
838 ci = cl->conn_info;
839
840 hci_dev_lock_bh(hdev);
841 list_for_each(p, &hdev->conn_hash.list) {
842 register struct hci_conn *c;
843 c = list_entry(p, struct hci_conn, list);
844
845 bacpy(&(ci + n)->bdaddr, &c->dst);
846 (ci + n)->handle = c->handle;
847 (ci + n)->type = c->type;
848 (ci + n)->out = c->out;
849 (ci + n)->state = c->state;
850 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -0800851 if (c->type == SCO_LINK) {
852 (ci + n)->mtu = hdev->sco_mtu;
853 (ci + n)->cnt = hdev->sco_cnt;
854 (ci + n)->pkts = hdev->sco_pkts;
855 } else {
856 (ci + n)->mtu = hdev->acl_mtu;
857 (ci + n)->cnt = hdev->acl_cnt;
858 (ci + n)->pkts = hdev->acl_pkts;
859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (++n >= req.conn_num)
861 break;
862 }
863 hci_dev_unlock_bh(hdev);
864
865 cl->dev_id = hdev->id;
866 cl->conn_num = n;
867 size = sizeof(req) + n * sizeof(*ci);
868
869 hci_dev_put(hdev);
870
871 err = copy_to_user(arg, cl, size);
872 kfree(cl);
873
874 return err ? -EFAULT : 0;
875}
876
877int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
878{
879 struct hci_conn_info_req req;
880 struct hci_conn_info ci;
881 struct hci_conn *conn;
882 char __user *ptr = arg + sizeof(req);
883
884 if (copy_from_user(&req, arg, sizeof(req)))
885 return -EFAULT;
886
887 hci_dev_lock_bh(hdev);
888 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
889 if (conn) {
890 bacpy(&ci.bdaddr, &conn->dst);
891 ci.handle = conn->handle;
892 ci.type = conn->type;
893 ci.out = conn->out;
894 ci.state = conn->state;
895 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -0800896 if (req.type == SCO_LINK) {
897 ci.mtu = hdev->sco_mtu;
898 ci.cnt = hdev->sco_cnt;
899 ci.pkts = hdev->sco_pkts;
900 } else {
901 ci.mtu = hdev->acl_mtu;
902 ci.cnt = hdev->acl_cnt;
903 ci.pkts = hdev->acl_pkts;
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 hci_dev_unlock_bh(hdev);
907
908 if (!conn)
909 return -ENOENT;
910
911 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
912}
Marcel Holtmann40be4922008-07-14 20:13:50 +0200913
914int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
915{
916 struct hci_auth_info_req req;
917 struct hci_conn *conn;
918
919 if (copy_from_user(&req, arg, sizeof(req)))
920 return -EFAULT;
921
922 hci_dev_lock_bh(hdev);
923 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
924 if (conn)
925 req.type = conn->auth_type;
926 hci_dev_unlock_bh(hdev);
927
928 if (!conn)
929 return -ENOENT;
930
931 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
932}