blob: 18193831bbf12681a9a37eaf630ca2d55df232ba [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 Guedes6d3ce0e72011-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
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -0300207void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
208 __u8 ltk[16])
209{
210 struct hci_dev *hdev = conn->hdev;
211 struct hci_cp_le_start_enc cp;
212
213 BT_DBG("%p", conn);
214
215 memset(&cp, 0, sizeof(cp));
216
217 cp.handle = cpu_to_le16(conn->handle);
218 memcpy(cp.ltk, ltk, sizeof(cp.ltk));
219 cp.ediv = ediv;
220 memcpy(cp.rand, rand, sizeof(rand));
221
222 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
223}
224EXPORT_SYMBOL(hci_le_start_enc);
225
226void hci_le_ltk_reply(struct hci_conn *conn, u8 ltk[16])
227{
228 struct hci_dev *hdev = conn->hdev;
229 struct hci_cp_le_ltk_reply cp;
230
231 BT_DBG("%p", conn);
232
233 memset(&cp, 0, sizeof(cp));
234
235 cp.handle = cpu_to_le16(conn->handle);
236 memcpy(cp.ltk, ltk, sizeof(ltk));
237
238 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
239}
240EXPORT_SYMBOL(hci_le_ltk_reply);
241
242void hci_le_ltk_neg_reply(struct hci_conn *conn)
243{
244 struct hci_dev *hdev = conn->hdev;
245 struct hci_cp_le_ltk_neg_reply cp;
246
247 BT_DBG("%p", conn);
248
249 memset(&cp, 0, sizeof(cp));
250
251 cp.handle = cpu_to_le16(conn->handle);
252
253 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(cp), &cp);
254}
255
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400256/* Device _must_ be locked */
257void hci_sco_setup(struct hci_conn *conn, __u8 status)
258{
259 struct hci_conn *sco = conn->link;
260
261 BT_DBG("%p", conn);
262
263 if (!sco)
264 return;
265
266 if (!status) {
267 if (lmp_esco_capable(conn->hdev))
268 hci_setup_sync(sco, conn->handle);
269 else
270 hci_add_sco(sco, conn->handle);
271 } else {
272 hci_proto_connect_cfm(sco, status);
273 hci_conn_del(sco);
274 }
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static void hci_conn_timeout(unsigned long arg)
278{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200279 struct hci_conn *conn = (void *) arg;
280 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100281 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 BT_DBG("conn %p state %d", conn, conn->state);
284
285 if (atomic_read(&conn->refcnt))
286 return;
287
288 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200289
290 switch (conn->state) {
291 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200292 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300293 if (conn->out) {
294 if (conn->type == ACL_LINK)
295 hci_acl_connect_cancel(conn);
296 else if (conn->type == LE_LINK)
297 hci_le_connect_cancel(conn);
298 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200299 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200300 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900301 case BT_CONNECTED:
Marcel Holtmann2950f212009-02-12 14:02:50 +0100302 reason = hci_proto_disconn_ind(conn);
303 hci_acl_disconn(conn, reason);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200304 break;
305 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200307 break;
308 }
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Marcel Holtmann04837f62006-07-03 10:02:33 +0200313static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200315 struct hci_conn *conn = (void *) arg;
316
317 BT_DBG("conn %p mode %d", conn, conn->mode);
318
319 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Johan Hedberg9f616562011-04-28 11:28:54 -0700322static void hci_conn_auto_accept(unsigned long arg)
323{
324 struct hci_conn *conn = (void *) arg;
325 struct hci_dev *hdev = conn->hdev;
326
327 hci_dev_lock(hdev);
328
329 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
330 &conn->dst);
331
332 hci_dev_unlock(hdev);
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
336{
337 struct hci_conn *conn;
338
339 BT_DBG("%s dst %s", hdev->name, batostr(dst));
340
Marcel Holtmann04837f62006-07-03 10:02:33 +0200341 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
342 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200346 conn->hdev = hdev;
347 conn->type = type;
348 conn->mode = HCI_CM_ACTIVE;
349 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300350 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200351 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300352 conn->remote_auth = 0xff;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200353 conn->key_type = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Marcel Holtmann04837f62006-07-03 10:02:33 +0200355 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200356 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200357
Marcel Holtmanna8746412008-07-14 20:13:46 +0200358 switch (type) {
359 case ACL_LINK:
360 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
361 break;
362 case SCO_LINK:
363 if (lmp_esco_capable(hdev))
Marcel Holtmannefc76882009-02-06 09:13:37 +0100364 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
365 (hdev->esco_type & EDR_ESCO_MASK);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200366 else
367 conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK;
368 break;
369 case ESCO_LINK:
Marcel Holtmannefc76882009-02-06 09:13:37 +0100370 conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200371 break;
372 }
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200375
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800376 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
377 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Johan Hedberg9f616562011-04-28 11:28:54 -0700378 setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept,
379 (unsigned long) conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 atomic_set(&conn->refcnt, 0);
382
383 hci_dev_hold(hdev);
384
385 tasklet_disable(&hdev->tx_task);
386
387 hci_conn_hash_add(hdev, conn);
388 if (hdev->notify)
389 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
390
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700391 atomic_set(&conn->devref, 0);
392
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700393 hci_conn_init_sysfs(conn);
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 tasklet_enable(&hdev->tx_task);
396
397 return conn;
398}
399
400int hci_conn_del(struct hci_conn *conn)
401{
402 struct hci_dev *hdev = conn->hdev;
403
404 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
405
Marcel Holtmann04837f62006-07-03 10:02:33 +0200406 del_timer(&conn->idle_timer);
407
408 del_timer(&conn->disc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Johan Hedberg9f616562011-04-28 11:28:54 -0700410 del_timer(&conn->auto_accept_timer);
411
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200412 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct hci_conn *sco = conn->link;
414 if (sco)
415 sco->link = NULL;
416
417 /* Unacked frames */
418 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300419 } else if (conn->type == LE_LINK) {
420 if (hdev->le_pkts)
421 hdev->le_cnt += conn->sent;
422 else
423 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200424 } else {
425 struct hci_conn *acl = conn->link;
426 if (acl) {
427 acl->link = NULL;
428 hci_conn_put(acl);
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 hci_conn_hash_del(hdev, conn);
435 if (hdev->notify)
436 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700442 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800443
Marcel Holtmann384943e2009-05-08 18:20:43 -0700444 hci_dev_put(hdev);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
447}
448
449struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
450{
451 int use_src = bacmp(src, BDADDR_ANY);
452 struct hci_dev *hdev = NULL;
453 struct list_head *p;
454
455 BT_DBG("%s -> %s", batostr(src), batostr(dst));
456
457 read_lock_bh(&hci_dev_list_lock);
458
459 list_for_each(p, &hci_dev_list) {
460 struct hci_dev *d = list_entry(p, struct hci_dev, list);
461
462 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
463 continue;
464
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900465 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * No source address - find interface with bdaddr != dst
467 * Source address - find interface with bdaddr == src
468 */
469
470 if (use_src) {
471 if (!bacmp(&d->bdaddr, src)) {
472 hdev = d; break;
473 }
474 } else {
475 if (bacmp(&d->bdaddr, dst)) {
476 hdev = d; break;
477 }
478 }
479 }
480
481 if (hdev)
482 hdev = hci_dev_hold(hdev);
483
484 read_unlock_bh(&hci_dev_list_lock);
485 return hdev;
486}
487EXPORT_SYMBOL(hci_get_route);
488
Ville Tervofcd89c02011-02-10 22:38:47 -0300489/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * Device _must_ be locked */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100491struct 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 -0700492{
493 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200494 struct hci_conn *sco;
Ville Tervofcd89c02011-02-10 22:38:47 -0300495 struct hci_conn *le;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 BT_DBG("%s dst %s", hdev->name, batostr(dst));
498
Ville Tervofcd89c02011-02-10 22:38:47 -0300499 if (type == LE_LINK) {
Andre Guedeseda42b52011-05-31 14:20:56 -0300500 struct adv_entry *entry;
501
Ville Tervofcd89c02011-02-10 22:38:47 -0300502 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
Anderson Briglia15c47942011-02-21 15:09:23 -0300503 if (le)
Ville Tervo30e76272011-02-22 16:10:53 -0300504 return ERR_PTR(-EBUSY);
Andre Guedeseda42b52011-05-31 14:20:56 -0300505
506 entry = hci_find_adv_entry(hdev, dst);
507 if (!entry)
508 return ERR_PTR(-EHOSTUNREACH);
509
Anderson Briglia15c47942011-02-21 15:09:23 -0300510 le = hci_conn_add(hdev, LE_LINK, dst);
Ville Tervofcd89c02011-02-10 22:38:47 -0300511 if (!le)
Ville Tervo30e76272011-02-22 16:10:53 -0300512 return ERR_PTR(-ENOMEM);
Andre Guedes893d6752011-05-31 14:20:55 -0300513
Andre Guedeseda42b52011-05-31 14:20:56 -0300514 le->dst_type = entry->bdaddr_type;
515
Andre Guedes893d6752011-05-31 14:20:55 -0300516 hci_le_connect(le);
Ville Tervofcd89c02011-02-10 22:38:47 -0300517
518 hci_conn_hold(le);
519
520 return le;
521 }
522
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200523 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
524 if (!acl) {
525 acl = hci_conn_add(hdev, ACL_LINK, dst);
526 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return NULL;
528 }
529
530 hci_conn_hold(acl);
531
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200532 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530533 acl->sec_level = BT_SECURITY_LOW;
534 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200535 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200539 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200541
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200542 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
543 if (!sco) {
544 sco = hci_conn_add(hdev, type, dst);
545 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200546 hci_conn_put(acl);
547 return NULL;
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200550
551 acl->link = sco;
552 sco->link = acl;
553
554 hci_conn_hold(sco);
555
556 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200557 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800558 acl->power_save = 1;
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700559 hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON);
Nick Pellyc3902162009-11-13 14:16:32 -0800560
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400561 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
562 /* defer SCO setup until mode change completed */
563 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
564 return sco;
565 }
566
567 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200568 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200569
570 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572EXPORT_SYMBOL(hci_connect);
573
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200574/* Check link security requirement */
575int hci_conn_check_link_mode(struct hci_conn *conn)
576{
577 BT_DBG("conn %p", conn);
578
579 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
580 !(conn->link_mode & HCI_LM_ENCRYPT))
581 return 0;
582
583 return 1;
584}
585EXPORT_SYMBOL(hci_conn_check_link_mode);
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100588static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 BT_DBG("conn %p", conn);
591
Johan Hedberg765c2a92011-01-19 12:06:52 +0530592 if (conn->pending_sec_level > sec_level)
593 sec_level = conn->pending_sec_level;
594
Marcel Holtmann96a31832009-02-12 16:23:03 +0100595 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530596 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100597 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 1;
599
Johan Hedberg65cf6862011-01-19 12:06:49 +0530600 /* Make sure we preserve an existing MITM requirement*/
601 auth_type |= (conn->auth_type & 0x01);
602
Marcel Holtmann96a31832009-02-12 16:23:03 +0100603 conn->auth_type = auth_type;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
606 struct hci_cp_auth_requested cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700607 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200608 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
609 sizeof(cp), &cp);
Waldemar Rymarkiewicz19f8def2011-05-31 15:49:25 +0200610 if (conn->key_type != 0xff)
611 set_bit(HCI_CONN_REAUTH_PEND, &conn->pend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return 0;
615}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200617/* Encrypt the the link */
618static void hci_conn_encrypt(struct hci_conn *conn)
619{
620 BT_DBG("conn %p", conn);
621
622 if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
623 struct hci_cp_set_conn_encrypt cp;
624 cp.handle = cpu_to_le16(conn->handle);
625 cp.encrypt = 0x01;
626 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
627 &cp);
628 }
629}
630
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100631/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100632int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 BT_DBG("conn %p", conn);
635
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200636 /* For sdp we don't need the link key. */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100637 if (sec_level == BT_SECURITY_SDP)
638 return 1;
639
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200640 /* For non 2.1 devices and low security level we don't need the link
641 key. */
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700642 if (sec_level == BT_SECURITY_LOW &&
643 (!conn->ssp_mode || !conn->hdev->ssp_mode))
644 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100645
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200646 /* For other security levels we need the link key. */
647 if (!(conn->link_mode & HCI_LM_AUTH))
648 goto auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200650 /* An authenticated combination key has sufficient security for any
651 security level. */
652 if (conn->key_type == HCI_LK_AUTH_COMBINATION)
653 goto encrypt;
654
655 /* An unauthenticated combination key has sufficient security for
656 security level 1 and 2. */
657 if (conn->key_type == HCI_LK_UNAUTH_COMBINATION &&
658 (sec_level == BT_SECURITY_MEDIUM ||
659 sec_level == BT_SECURITY_LOW))
660 goto encrypt;
661
662 /* A combination key has always sufficient security for the security
663 levels 1 or 2. High security level requires the combination key
664 is generated using maximum PIN code length (16).
665 For pre 2.1 units. */
666 if (conn->key_type == HCI_LK_COMBINATION &&
667 (sec_level != BT_SECURITY_HIGH ||
668 conn->pin_length == 16))
669 goto encrypt;
670
671auth:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend))
673 return 0;
674
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200675 hci_conn_auth(conn, sec_level, auth_type);
676 return 0;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100677
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200678encrypt:
679 if (conn->link_mode & HCI_LM_ENCRYPT)
680 return 1;
681
682 hci_conn_encrypt(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return 0;
684}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100685EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +0200687/* Check secure link requirement */
688int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level)
689{
690 BT_DBG("conn %p", conn);
691
692 if (sec_level != BT_SECURITY_HIGH)
693 return 1; /* Accept if non-secure is required */
694
Waldemar Rymarkiewiczef4177e2011-06-02 14:24:52 +0200695 if (conn->sec_level == BT_SECURITY_HIGH)
Waldemar Rymarkiewiczb3b1b062011-05-06 09:42:31 +0200696 return 1;
697
698 return 0; /* Reject not secure link */
699}
700EXPORT_SYMBOL(hci_conn_check_secure);
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702/* Change link key */
703int hci_conn_change_link_key(struct hci_conn *conn)
704{
705 BT_DBG("conn %p", conn);
706
707 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
708 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700709 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200710 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
711 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return 0;
715}
716EXPORT_SYMBOL(hci_conn_change_link_key);
717
718/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100719int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 BT_DBG("conn %p", conn);
722
723 if (!role && conn->link_mode & HCI_LM_MASTER)
724 return 1;
725
726 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
727 struct hci_cp_switch_role cp;
728 bacpy(&cp.bdaddr, &conn->dst);
729 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200730 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return 0;
734}
735EXPORT_SYMBOL(hci_conn_switch_role);
736
Marcel Holtmann04837f62006-07-03 10:02:33 +0200737/* Enter active mode */
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700738void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200739{
740 struct hci_dev *hdev = conn->hdev;
741
742 BT_DBG("conn %p mode %d", conn, conn->mode);
743
744 if (test_bit(HCI_RAW, &hdev->flags))
745 return;
746
Jaikumar Ganesh14b12d02011-05-23 18:06:04 -0700747 if (conn->mode != HCI_CM_SNIFF)
748 goto timer;
749
750 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200751 goto timer;
752
753 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
754 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700755 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200756 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200757 }
758
759timer:
760 if (hdev->idle_timeout > 0)
761 mod_timer(&conn->idle_timer,
762 jiffies + msecs_to_jiffies(hdev->idle_timeout));
763}
764
765/* Enter sniff mode */
766void hci_conn_enter_sniff_mode(struct hci_conn *conn)
767{
768 struct hci_dev *hdev = conn->hdev;
769
770 BT_DBG("conn %p mode %d", conn, conn->mode);
771
772 if (test_bit(HCI_RAW, &hdev->flags))
773 return;
774
775 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
776 return;
777
778 if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF))
779 return;
780
781 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
782 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700783 cp.handle = cpu_to_le16(conn->handle);
784 cp.max_latency = cpu_to_le16(0);
785 cp.min_remote_timeout = cpu_to_le16(0);
786 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200787 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200788 }
789
790 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
791 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700792 cp.handle = cpu_to_le16(conn->handle);
793 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
794 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
795 cp.attempt = cpu_to_le16(4);
796 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200797 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200798 }
799}
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/* Drop all connection on the device */
802void hci_conn_hash_flush(struct hci_dev *hdev)
803{
804 struct hci_conn_hash *h = &hdev->conn_hash;
805 struct list_head *p;
806
807 BT_DBG("hdev %s", hdev->name);
808
809 p = h->list.next;
810 while (p != &h->list) {
811 struct hci_conn *c;
812
813 c = list_entry(p, struct hci_conn, list);
814 p = p->next;
815
816 c->state = BT_CLOSED;
817
Marcel Holtmann2950f212009-02-12 14:02:50 +0100818 hci_proto_disconn_cfm(c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 hci_conn_del(c);
820 }
821}
822
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200823/* Check pending connect attempts */
824void hci_conn_check_pending(struct hci_dev *hdev)
825{
826 struct hci_conn *conn;
827
828 BT_DBG("hdev %s", hdev->name);
829
830 hci_dev_lock(hdev);
831
832 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
833 if (conn)
834 hci_acl_connect(conn);
835
836 hci_dev_unlock(hdev);
837}
838
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700839void hci_conn_hold_device(struct hci_conn *conn)
840{
841 atomic_inc(&conn->devref);
842}
843EXPORT_SYMBOL(hci_conn_hold_device);
844
845void hci_conn_put_device(struct hci_conn *conn)
846{
847 if (atomic_dec_and_test(&conn->devref))
848 hci_conn_del_sysfs(conn);
849}
850EXPORT_SYMBOL(hci_conn_put_device);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852int hci_get_conn_list(void __user *arg)
853{
854 struct hci_conn_list_req req, *cl;
855 struct hci_conn_info *ci;
856 struct hci_dev *hdev;
857 struct list_head *p;
858 int n = 0, size, err;
859
860 if (copy_from_user(&req, arg, sizeof(req)))
861 return -EFAULT;
862
863 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
864 return -EINVAL;
865
866 size = sizeof(req) + req.conn_num * sizeof(*ci);
867
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200868 cl = kmalloc(size, GFP_KERNEL);
869 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return -ENOMEM;
871
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200872 hdev = hci_dev_get(req.dev_id);
873 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 kfree(cl);
875 return -ENODEV;
876 }
877
878 ci = cl->conn_info;
879
880 hci_dev_lock_bh(hdev);
881 list_for_each(p, &hdev->conn_hash.list) {
882 register struct hci_conn *c;
883 c = list_entry(p, struct hci_conn, list);
884
885 bacpy(&(ci + n)->bdaddr, &c->dst);
886 (ci + n)->handle = c->handle;
887 (ci + n)->type = c->type;
888 (ci + n)->out = c->out;
889 (ci + n)->state = c->state;
890 (ci + n)->link_mode = c->link_mode;
891 if (++n >= req.conn_num)
892 break;
893 }
894 hci_dev_unlock_bh(hdev);
895
896 cl->dev_id = hdev->id;
897 cl->conn_num = n;
898 size = sizeof(req) + n * sizeof(*ci);
899
900 hci_dev_put(hdev);
901
902 err = copy_to_user(arg, cl, size);
903 kfree(cl);
904
905 return err ? -EFAULT : 0;
906}
907
908int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
909{
910 struct hci_conn_info_req req;
911 struct hci_conn_info ci;
912 struct hci_conn *conn;
913 char __user *ptr = arg + sizeof(req);
914
915 if (copy_from_user(&req, arg, sizeof(req)))
916 return -EFAULT;
917
918 hci_dev_lock_bh(hdev);
919 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
920 if (conn) {
921 bacpy(&ci.bdaddr, &conn->dst);
922 ci.handle = conn->handle;
923 ci.type = conn->type;
924 ci.out = conn->out;
925 ci.state = conn->state;
926 ci.link_mode = conn->link_mode;
927 }
928 hci_dev_unlock_bh(hdev);
929
930 if (!conn)
931 return -ENOENT;
932
933 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
934}
Marcel Holtmann40be4922008-07-14 20:13:50 +0200935
936int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
937{
938 struct hci_auth_info_req req;
939 struct hci_conn *conn;
940
941 if (copy_from_user(&req, arg, sizeof(req)))
942 return -EFAULT;
943
944 hci_dev_lock_bh(hdev);
945 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
946 if (conn)
947 req.type = conn->auth_type;
948 hci_dev_unlock_bh(hdev);
949
950 if (!conn)
951 return -ENOENT;
952
953 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
954}