blob: e6c6122a4bca7b9a9824628b72d8ddc60316b533 [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
Hemant Gupta0b6ad972013-06-08 19:07:57 +05303 Copyright (c) 2000-2001, The Linux Foundation. All rights reserved.
4 Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
6 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation;
11
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090016 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090021 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 SOFTWARE IS DISCLAIMED.
24*/
25
26/* Bluetooth HCI connection handling. */
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29
30#include <linux/types.h>
31#include <linux/errno.h>
32#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/slab.h>
34#include <linux/poll.h>
35#include <linux/fcntl.h>
36#include <linux/init.h>
37#include <linux/skbuff.h>
38#include <linux/interrupt.h>
39#include <linux/notifier.h>
40#include <net/sock.h>
41
42#include <asm/system.h>
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020043#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/unaligned.h>
45
46#include <net/bluetooth/bluetooth.h>
47#include <net/bluetooth/hci_core.h>
Brian Gixa94b6122012-02-23 16:07:10 -080048#include <net/bluetooth/l2cap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Brian Gixa94b6122012-02-23 16:07:10 -080050struct hci_conn *hci_le_connect(struct hci_dev *hdev, __u16 pkt_type,
51 bdaddr_t *dst, __u8 sec_level, __u8 auth_type,
52 struct bt_le_params *le_params)
Ville Tervofcd89c02011-02-10 22:38:47 -030053{
Sunny Kapdi320598f2012-07-30 14:52:56 -070054 struct hci_conn *le, *le_wlist_conn;
Ville Tervofcd89c02011-02-10 22:38:47 -030055 struct hci_cp_le_create_conn cp;
Brian Gixa94b6122012-02-23 16:07:10 -080056 struct adv_entry *entry;
57 struct link_key *key;
Ville Tervofcd89c02011-02-10 22:38:47 -030058
Brian Gixa94b6122012-02-23 16:07:10 -080059 BT_DBG("%p", hdev);
Brian Gix114f3a62011-09-27 14:02:20 -070060
Brian Gixa94b6122012-02-23 16:07:10 -080061 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
62 if (le) {
Sunny Kapdi320598f2012-07-30 14:52:56 -070063 le_wlist_conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
64 BDADDR_ANY);
65 if (!le_wlist_conn) {
66 hci_conn_hold(le);
67 return le;
68 } else {
69 BT_DBG("remove wlist conn");
70 le->out = 1;
71 le->link_mode |= HCI_LM_MASTER;
72 le->sec_level = BT_SECURITY_LOW;
73 le->type = LE_LINK;
74 hci_proto_connect_cfm(le, 0);
75 hci_conn_del(le_wlist_conn);
76 return le;
77 }
Brian Gixa94b6122012-02-23 16:07:10 -080078 }
79
80 key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK);
81 if (!key) {
82 entry = hci_find_adv_entry(hdev, dst);
83 if (entry)
84 le = hci_le_conn_add(hdev, dst,
85 entry->bdaddr_type);
86 else
87 le = hci_le_conn_add(hdev, dst, 0);
88 } else {
89 le = hci_le_conn_add(hdev, dst, key->addr_type);
90 }
91
92 if (!le)
93 return ERR_PTR(-ENOMEM);
94
95 hci_conn_hold(le);
96
97 le->state = BT_CONNECT;
98 le->out = 1;
99 le->link_mode |= HCI_LM_MASTER;
100 le->sec_level = BT_SECURITY_LOW;
101 le->type = LE_LINK;
Ville Tervofcd89c02011-02-10 22:38:47 -0300102
103 memset(&cp, 0, sizeof(cp));
Brian Gixa94b6122012-02-23 16:07:10 -0800104 if (l2cap_sock_le_params_valid(le_params)) {
105 cp.supervision_timeout =
106 cpu_to_le16(le_params->supervision_timeout);
107 cp.scan_interval = cpu_to_le16(le_params->scan_interval);
108 cp.scan_window = cpu_to_le16(le_params->scan_window);
109 cp.conn_interval_min = cpu_to_le16(le_params->interval_min);
110 cp.conn_interval_max = cpu_to_le16(le_params->interval_max);
111 cp.conn_latency = cpu_to_le16(le_params->latency);
112 cp.min_ce_len = cpu_to_le16(le_params->min_ce_len);
113 cp.max_ce_len = cpu_to_le16(le_params->max_ce_len);
114 le->conn_timeout = le_params->conn_timeout;
115 } else {
116 cp.supervision_timeout = cpu_to_le16(BT_LE_SUP_TO_DEFAULT);
117 cp.scan_interval = cpu_to_le16(BT_LE_SCAN_INTERVAL_DEF);
118 cp.scan_window = cpu_to_le16(BT_LE_SCAN_WINDOW_DEF);
119 cp.conn_interval_min = cpu_to_le16(BT_LE_CONN_INTERVAL_MIN_DEF);
120 cp.conn_interval_max = cpu_to_le16(BT_LE_CONN_INTERVAL_MAX_DEF);
121 cp.conn_latency = cpu_to_le16(BT_LE_LATENCY_DEF);
122 le->conn_timeout = 5;
123 }
Sunny Kapdi320598f2012-07-30 14:52:56 -0700124 if (!bacmp(&le->dst, BDADDR_ANY)) {
125 cp.filter_policy = 0x01;
126 le->conn_timeout = 0;
127 } else {
128 bacpy(&cp.peer_addr, &le->dst);
129 cp.peer_addr_type = le->dst_type;
130 }
Ville Tervofcd89c02011-02-10 22:38:47 -0300131
132 hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
Brian Gixa94b6122012-02-23 16:07:10 -0800133
134 return le;
Ville Tervofcd89c02011-02-10 22:38:47 -0300135}
Brian Gixa94b6122012-02-23 16:07:10 -0800136EXPORT_SYMBOL(hci_le_connect);
Ville Tervofcd89c02011-02-10 22:38:47 -0300137
138static void hci_le_connect_cancel(struct hci_conn *conn)
139{
140 hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
141}
142
Sunny Kapdi320598f2012-07-30 14:52:56 -0700143void hci_le_cancel_create_connect(struct hci_dev *hdev, bdaddr_t *dst)
144{
145 struct hci_conn *le;
146
147 BT_DBG("%p", hdev);
148
149 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
150 if (le) {
151 BT_DBG("send hci connect cancel");
152 hci_le_connect_cancel(le);
153 hci_conn_del(le);
154 }
155}
156EXPORT_SYMBOL(hci_le_cancel_create_connect);
157
158void hci_le_add_dev_white_list(struct hci_dev *hdev, bdaddr_t *dst)
159{
160 struct hci_cp_le_add_dev_white_list cp;
161 struct adv_entry *entry;
162 struct link_key *key;
163
164 BT_DBG("%p", hdev);
165
166 memset(&cp, 0, sizeof(cp));
167 bacpy(&cp.addr, dst);
168
169 key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK);
170 if (!key) {
171 entry = hci_find_adv_entry(hdev, dst);
172 if (entry)
173 cp.addr_type = entry->bdaddr_type;
174 else
175 cp.addr_type = 0x00;
176 } else {
177 cp.addr_type = key->addr_type;
178 }
179
180 hci_send_cmd(hdev, HCI_OP_LE_ADD_DEV_WHITE_LIST, sizeof(cp), &cp);
181}
182EXPORT_SYMBOL(hci_le_add_dev_white_list);
183
184void hci_le_remove_dev_white_list(struct hci_dev *hdev, bdaddr_t *dst)
185{
186 struct hci_cp_le_remove_dev_white_list cp;
187 struct adv_entry *entry;
188 struct link_key *key;
189
190 BT_DBG("%p", hdev);
191
192 memset(&cp, 0, sizeof(cp));
193 bacpy(&cp.addr, dst);
194
195 key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK);
196 if (!key) {
197 entry = hci_find_adv_entry(hdev, dst);
198 if (entry)
199 cp.addr_type = entry->bdaddr_type;
200 else
201 cp.addr_type = 0x00;
202 } else {
203 cp.addr_type = key->addr_type;
204 }
205
206 hci_send_cmd(hdev, HCI_OP_LE_REMOVE_DEV_WHITE_LIST, sizeof(cp), &cp);
207}
208EXPORT_SYMBOL(hci_le_remove_dev_white_list);
209
Mallikarjuna GB513ebdc2012-08-22 14:18:26 +0530210static inline bool is_role_switch_possible(struct hci_dev *hdev)
211{
212 if (hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECTED))
213 return false;
214 return true;
215}
216
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200217void hci_acl_connect(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct hci_dev *hdev = conn->hdev;
220 struct inquiry_entry *ie;
221 struct hci_cp_create_conn cp;
222
223 BT_DBG("%p", conn);
224
225 conn->state = BT_CONNECT;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200226 conn->out = 1;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 conn->link_mode = HCI_LM_MASTER;
229
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200230 conn->attempt++;
231
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200232 conn->link_policy = hdev->link_policy;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 memset(&cp, 0, sizeof(cp));
235 bacpy(&cp.bdaddr, &conn->dst);
236 cp.pscan_rep_mode = 0x02;
237
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200238 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
239 if (ie) {
Marcel Holtmann41a96212008-07-14 20:13:48 +0200240 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
241 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
242 cp.pscan_mode = ie->data.pscan_mode;
243 cp.clock_offset = ie->data.clock_offset |
244 cpu_to_le16(0x8000);
245 }
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 memcpy(conn->dev_class, ie->data.dev_class, 3);
Marcel Holtmann41a96212008-07-14 20:13:48 +0200248 conn->ssp_mode = ie->data.ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
Marcel Holtmanna8746412008-07-14 20:13:46 +0200251 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Mallikarjuna GB513ebdc2012-08-22 14:18:26 +0530252 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)
253 && is_role_switch_possible(hdev))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200254 cp.role_switch = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 else
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200256 cp.role_switch = 0x00;
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200257
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200258 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200261static void hci_acl_connect_cancel(struct hci_conn *conn)
262{
263 struct hci_cp_create_conn_cancel cp;
264
265 BT_DBG("%p", conn);
266
267 if (conn->hdev->hci_ver < 2)
268 return;
269
270 bacpy(&cp.bdaddr, &conn->dst);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200271 hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
275{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 BT_DBG("%p", conn);
277
278 conn->state = BT_DISCONN;
279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 if (conn->hdev->dev_type == HCI_BREDR) {
281 struct hci_cp_disconnect cp;
282 cp.handle = cpu_to_le16(conn->handle);
283 cp.reason = reason;
284 hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
285 } else {
286 struct hci_cp_disconn_phys_link cp;
287 cp.phy_handle = (u8) conn->handle;
288 cp.reason = reason;
289 hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHYS_LINK,
290 sizeof(cp), &cp);
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294void hci_add_sco(struct hci_conn *conn, __u16 handle)
295{
296 struct hci_dev *hdev = conn->hdev;
297 struct hci_cp_add_sco cp;
298
299 BT_DBG("%p", conn);
300
301 conn->state = BT_CONNECT;
302 conn->out = 1;
303
Marcel Holtmannefc76882009-02-06 09:13:37 +0100304 conn->attempt++;
305
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700306 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200307 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200309 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200312void hci_setup_sync(struct hci_conn *conn, __u16 handle)
313{
314 struct hci_dev *hdev = conn->hdev;
315 struct hci_cp_setup_sync_conn cp;
316
317 BT_DBG("%p", conn);
318
319 conn->state = BT_CONNECT;
320 conn->out = 1;
321
Marcel Holtmannefc76882009-02-06 09:13:37 +0100322 conn->attempt++;
323
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200324 cp.handle = cpu_to_le16(handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200325
326 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
327 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Kun Han Kim15b911f2011-07-08 09:30:28 -0700328 if (conn->hdev->is_wbs) {
329 /* Transparent Data */
330 uint16_t voice_setting = hdev->voice_setting | ACF_TRANS;
331 cp.max_latency = cpu_to_le16(0x000D);
332 cp.pkt_type = cpu_to_le16(ESCO_WBS);
333 cp.voice_setting = cpu_to_le16(voice_setting);
334 /* Retransmission Effort */
335 cp.retrans_effort = RE_LINK_QUALITY;
336 } else {
337 cp.max_latency = cpu_to_le16(0x000A);
338 cp.pkt_type = cpu_to_le16(conn->pkt_type);
339 cp.voice_setting = cpu_to_le16(hdev->voice_setting);
340 cp.retrans_effort = RE_POWER_CONSUMP;
341 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200342
343 hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
344}
345
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200346void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
347 u16 latency, u16 to_multiplier)
348{
349 struct hci_cp_le_conn_update cp;
350 struct hci_dev *hdev = conn->hdev;
351
352 memset(&cp, 0, sizeof(cp));
353
354 cp.handle = cpu_to_le16(conn->handle);
355 cp.conn_interval_min = cpu_to_le16(min);
356 cp.conn_interval_max = cpu_to_le16(max);
357 cp.conn_latency = cpu_to_le16(latency);
358 cp.supervision_timeout = cpu_to_le16(to_multiplier);
359 cp.min_ce_len = cpu_to_le16(0x0001);
360 cp.max_ce_len = cpu_to_le16(0x0001);
361
362 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
363}
364EXPORT_SYMBOL(hci_le_conn_update);
365
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800366void hci_read_rssi(struct hci_conn *conn)
367{
368 struct hci_cp_read_rssi cp;
369 struct hci_dev *hdev = conn->hdev;
370
371 memset(&cp, 0, sizeof(cp));
372 cp.handle = cpu_to_le16(conn->handle);
373
374 hci_send_cmd(hdev, HCI_OP_READ_RSSI, sizeof(cp), &cp);
375}
376EXPORT_SYMBOL(hci_read_rssi);
377
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300378void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
379 __u8 ltk[16])
380{
381 struct hci_dev *hdev = conn->hdev;
382 struct hci_cp_le_start_enc cp;
383
384 BT_DBG("%p", conn);
385
386 memset(&cp, 0, sizeof(cp));
387
388 cp.handle = cpu_to_le16(conn->handle);
389 memcpy(cp.ltk, ltk, sizeof(cp.ltk));
390 cp.ediv = ediv;
Brian Gix842bc5e2011-09-07 08:13:41 -0700391 memcpy(cp.rand, rand, sizeof(cp.rand));
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300392
393 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
394}
395EXPORT_SYMBOL(hci_le_start_enc);
396
397void hci_le_ltk_reply(struct hci_conn *conn, u8 ltk[16])
398{
399 struct hci_dev *hdev = conn->hdev;
400 struct hci_cp_le_ltk_reply cp;
401
402 BT_DBG("%p", conn);
403
404 memset(&cp, 0, sizeof(cp));
405
406 cp.handle = cpu_to_le16(conn->handle);
407 memcpy(cp.ltk, ltk, sizeof(ltk));
408
409 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
410}
411EXPORT_SYMBOL(hci_le_ltk_reply);
412
413void hci_le_ltk_neg_reply(struct hci_conn *conn)
414{
415 struct hci_dev *hdev = conn->hdev;
416 struct hci_cp_le_ltk_neg_reply cp;
417
418 BT_DBG("%p", conn);
419
420 memset(&cp, 0, sizeof(cp));
421
422 cp.handle = cpu_to_le16(conn->handle);
423
424 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(cp), &cp);
425}
426
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400427/* Device _must_ be locked */
428void hci_sco_setup(struct hci_conn *conn, __u8 status)
429{
430 struct hci_conn *sco = conn->link;
431
432 BT_DBG("%p", conn);
433
434 if (!sco)
435 return;
436
437 if (!status) {
438 if (lmp_esco_capable(conn->hdev))
439 hci_setup_sync(sco, conn->handle);
440 else
441 hci_add_sco(sco, conn->handle);
442 } else {
443 hci_proto_connect_cfm(sco, status);
444 hci_conn_del(sco);
445 }
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static void hci_conn_timeout(unsigned long arg)
449{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200450 struct hci_conn *conn = (void *) arg;
451 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100452 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 BT_DBG("conn %p state %d", conn, conn->state);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200457
458 switch (conn->state) {
459 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200460 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300461 if (conn->out) {
462 if (conn->type == ACL_LINK)
463 hci_acl_connect_cancel(conn);
464 else if (conn->type == LE_LINK)
465 hci_le_connect_cancel(conn);
466 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200467 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200468 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900469 case BT_CONNECTED:
Brian Gix114f3a62011-09-27 14:02:20 -0700470 if (!atomic_read(&conn->refcnt)) {
471 reason = hci_proto_disconn_ind(conn);
472 hci_acl_disconn(conn, reason);
473 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200474 break;
475 default:
Brian Gix114f3a62011-09-27 14:02:20 -0700476 if (!atomic_read(&conn->refcnt))
477 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200478 break;
479 }
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Marcel Holtmann04837f62006-07-03 10:02:33 +0200484static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200486 struct hci_conn *conn = (void *) arg;
487
488 BT_DBG("conn %p mode %d", conn, conn->mode);
489
490 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800493static void hci_conn_rssi_update(struct work_struct *work)
494{
495 struct delayed_work *delayed =
496 container_of(work, struct delayed_work, work);
497 struct hci_conn *conn =
498 container_of(delayed, struct hci_conn, rssi_update_work);
499
500 BT_DBG("conn %p mode %d", conn, conn->mode);
501
502 hci_read_rssi(conn);
503}
504
Prabhakaran Mcafface82012-04-10 11:38:35 +0530505static void encryption_disabled_timeout(unsigned long userdata)
506{
507 struct hci_conn *conn = (struct hci_conn *)userdata;
508 BT_INFO("conn %p Grace Prd Exp ", conn);
509
510 hci_encrypt_cfm(conn, 0, 0);
511
512 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
513 struct hci_cp_set_conn_encrypt cp;
514 BT_INFO("HCI_CONN_ENCRYPT_PEND is set");
515 cp.handle = cpu_to_le16(conn->handle);
516 cp.encrypt = 1;
517 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
518 sizeof(cp), &cp);
519 }
520
521}
522
Nick Pellybbcda3b2010-02-11 11:54:28 -0800523struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type,
524 __u16 pkt_type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 struct hci_conn *conn;
527
528 BT_DBG("%s dst %s", hdev->name, batostr(dst));
529
Marcel Holtmann04837f62006-07-03 10:02:33 +0200530 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
531 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200535 conn->hdev = hdev;
536 conn->type = type;
537 conn->mode = HCI_CM_ACTIVE;
538 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300539 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200540 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300541 conn->remote_auth = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Marcel Holtmann04837f62006-07-03 10:02:33 +0200543 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200544 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Srinivas Krovvidi231689b2012-09-03 18:47:50 +0530545 conn->conn_valid = true;
546 spin_lock_init(&conn->lock);
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530547 wake_lock_init(&conn->idle_lock, WAKE_LOCK_SUSPEND, "bt_idle");
Marcel Holtmann04837f62006-07-03 10:02:33 +0200548
Marcel Holtmanna8746412008-07-14 20:13:46 +0200549 switch (type) {
550 case ACL_LINK:
551 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
Rahul Kashyapd5553ba2012-01-02 19:27:19 +0530552 conn->link_policy = hdev->link_policy;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200553 break;
554 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800555 if (!pkt_type)
556 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200557 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800558 if (!pkt_type)
559 pkt_type = ALL_ESCO_MASK;
560 if (lmp_esco_capable(hdev)) {
561 /* HCI Setup Synchronous Connection Command uses
562 reverse logic on the EDR_ESCO_MASK bits */
563 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
564 hdev->esco_type;
565 } else {
566 /* Legacy HCI Add Sco Connection Command uses a
567 shifted bitmask */
568 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
569 SCO_PTYPE_MASK;
570 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200571 break;
572 }
573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200575
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800576 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
577 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800578 INIT_DELAYED_WORK(&conn->rssi_update_work, hci_conn_rssi_update);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530579 setup_timer(&conn->encrypt_pause_timer, encryption_disabled_timeout,
580 (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 atomic_set(&conn->refcnt, 0);
583
584 hci_dev_hold(hdev);
585
586 tasklet_disable(&hdev->tx_task);
587
588 hci_conn_hash_add(hdev, conn);
589 if (hdev->notify)
590 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
591
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700592 atomic_set(&conn->devref, 0);
593
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700594 hci_conn_init_sysfs(conn);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 tasklet_enable(&hdev->tx_task);
597
598 return conn;
599}
600
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst,
602 __u8 addr_type)
603{
604 struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst);
605 if (!conn)
606 return NULL;
607
608 conn->dst_type = addr_type;
609
610 return conn;
611}
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613int hci_conn_del(struct hci_conn *conn)
614{
615 struct hci_dev *hdev = conn->hdev;
616
617 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
618
Srinivas Krovvidi231689b2012-09-03 18:47:50 +0530619 spin_lock_bh(&conn->lock);
620 conn->conn_valid = false; /* conn data is being released */
621 spin_unlock_bh(&conn->lock);
622
Brian Gix3cd62042012-01-11 15:18:17 -0800623 /* Make sure no timers are running */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200624 del_timer(&conn->idle_timer);
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530625 wake_lock_destroy(&conn->idle_lock);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200626 del_timer(&conn->disc_timer);
Brian Gix3cd62042012-01-11 15:18:17 -0800627 del_timer(&conn->smp_timer);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800628 __cancel_delayed_work(&conn->rssi_update_work);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530629 del_timer(&conn->encrypt_pause_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200631 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct hci_conn *sco = conn->link;
633 if (sco)
634 sco->link = NULL;
635
636 /* Unacked frames */
637 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300638 } else if (conn->type == LE_LINK) {
639 if (hdev->le_pkts)
640 hdev->le_cnt += conn->sent;
641 else
642 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200643 } else {
644 struct hci_conn *acl = conn->link;
645 if (acl) {
646 acl->link = NULL;
647 hci_conn_put(acl);
648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650
651 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 hci_conn_hash_del(hdev, conn);
654 if (hdev->notify)
655 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200656
AnubhavGupta01d9e362011-12-17 17:14:51 +0530657 tasklet_schedule(&hdev->tx_task);
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700663 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800664
Hemant Gupta0b6ad972013-06-08 19:07:57 +0530665 if (conn->hidp_session_valid)
666 hci_conn_put_device(conn);
667
Marcel Holtmann384943e2009-05-08 18:20:43 -0700668 hci_dev_put(hdev);
669
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 return 0;
671}
672
673struct hci_chan *hci_chan_add(struct hci_dev *hdev)
674{
675 struct hci_chan *chan;
676
677 BT_DBG("%s", hdev->name);
678
679 chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC);
680 if (!chan)
681 return NULL;
682
683 atomic_set(&chan->refcnt, 0);
684
685 hci_dev_hold(hdev);
686
687 chan->hdev = hdev;
688
689 list_add(&chan->list, &hdev->chan_list.list);
690
691 return chan;
692}
Peter Krystada8417e62012-03-21 16:58:17 -0700693EXPORT_SYMBOL(hci_chan_add);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694
695int hci_chan_del(struct hci_chan *chan)
696{
697 BT_DBG("%s chan %p", chan->hdev->name, chan);
698
699 list_del(&chan->list);
700
701 hci_conn_put(chan->conn);
702 hci_dev_put(chan->hdev);
703
704 kfree(chan);
Tomas Targownik1be668d2011-06-30 16:30:44 -0300705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707}
708
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800709int hci_chan_put(struct hci_chan *chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710{
711 struct hci_cp_disconn_logical_link cp;
Mat Martineau9f8d4672011-12-14 12:10:46 -0800712 struct hci_conn *hcon;
713 u16 ll_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714
715 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
716 if (!atomic_dec_and_test(&chan->refcnt))
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800717 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718
Mat Martineau9f8d4672011-12-14 12:10:46 -0800719 hcon = chan->conn;
720 ll_handle = chan->ll_handle;
721
722 hci_chan_del(chan);
723
724 BT_DBG("chan->conn->state %d", hcon->state);
725 if (hcon->state == BT_CONNECTED) {
726 cp.log_handle = cpu_to_le16(ll_handle);
727 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 sizeof(cp), &cp);
Mat Martineau9f8d4672011-12-14 12:10:46 -0800729 }
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800730
731 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732}
733EXPORT_SYMBOL(hci_chan_put);
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
736{
737 int use_src = bacmp(src, BDADDR_ANY);
738 struct hci_dev *hdev = NULL;
739 struct list_head *p;
740
741 BT_DBG("%s -> %s", batostr(src), batostr(dst));
742
743 read_lock_bh(&hci_dev_list_lock);
744
745 list_for_each(p, &hci_dev_list) {
746 struct hci_dev *d = list_entry(p, struct hci_dev, list);
747
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 if (d->dev_type != HCI_BREDR)
749 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
751 continue;
752
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900753 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * No source address - find interface with bdaddr != dst
755 * Source address - find interface with bdaddr == src
756 */
757
758 if (use_src) {
759 if (!bacmp(&d->bdaddr, src)) {
760 hdev = d; break;
761 }
762 } else {
763 if (bacmp(&d->bdaddr, dst)) {
764 hdev = d; break;
765 }
766 }
767 }
768
769 if (hdev)
770 hdev = hci_dev_hold(hdev);
771
772 read_unlock_bh(&hci_dev_list_lock);
773 return hdev;
774}
775EXPORT_SYMBOL(hci_get_route);
776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777struct hci_dev *hci_dev_get_type(u8 amp_type)
778{
779 struct hci_dev *hdev = NULL;
780 struct hci_dev *d;
781
782 BT_DBG("amp_type %d", amp_type);
783
784 read_lock_bh(&hci_dev_list_lock);
785
786 list_for_each_entry(d, &hci_dev_list, list) {
787 if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) {
788 hdev = d;
789 break;
790 }
791 }
792
793 if (hdev)
794 hdev = hci_dev_hold(hdev);
795
796 read_unlock_bh(&hci_dev_list_lock);
797 return hdev;
798}
799EXPORT_SYMBOL(hci_dev_get_type);
800
801struct hci_dev *hci_dev_get_amp(bdaddr_t *dst)
802{
803 struct hci_dev *d;
804 struct hci_dev *hdev = NULL;
805
806 BT_DBG("%s dst %s", hdev->name, batostr(dst));
807
808 read_lock_bh(&hci_dev_list_lock);
809
810 list_for_each_entry(d, &hci_dev_list, list) {
811 struct hci_conn *conn;
812 if (d->dev_type == HCI_BREDR)
813 continue;
814 conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst);
815 if (conn) {
816 hdev = d;
817 break;
818 }
819 }
820
821 if (hdev)
822 hdev = hci_dev_hold(hdev);
823
824 read_unlock_bh(&hci_dev_list_lock);
825 return hdev;
826}
827EXPORT_SYMBOL(hci_dev_get_amp);
828
Ville Tervofcd89c02011-02-10 22:38:47 -0300829/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800831struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
832 __u16 pkt_type, bdaddr_t *dst,
833 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200836 struct hci_conn *sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 BT_DBG("%s dst %s", hdev->name, batostr(dst));
839
Brian Gixa94b6122012-02-23 16:07:10 -0800840 if (type == LE_LINK)
841 return hci_le_connect(hdev, pkt_type, dst, sec_level,
842 auth_type, NULL);
Ville Tervofcd89c02011-02-10 22:38:47 -0300843
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200844 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
845 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800846 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200847 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return NULL;
849 }
850
851 hci_conn_hold(acl);
852
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200853 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530854 acl->sec_level = BT_SECURITY_LOW;
855 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200856 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200860 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200862
Srinivas Krovvidi57fb9a32012-08-02 13:23:53 +0530863 /* type of connection already existing can be ESCO or SCO
864 * so check for both types before creating new */
865
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200866 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
Srinivas Krovvidi57fb9a32012-08-02 13:23:53 +0530867
868 if (!sco && type == ESCO_LINK) {
869 sco = hci_conn_hash_lookup_ba(hdev, SCO_LINK, dst);
870 } else if (!sco && type == SCO_LINK) {
871 /* this case can be practically not possible */
872 sco = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, dst);
873 }
874
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200875 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800876 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200877 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200878 hci_conn_put(acl);
879 return NULL;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200882
883 acl->link = sco;
884 sco->link = acl;
885
886 hci_conn_hold(sco);
887
888 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200889 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800890 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800892
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400893 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
894 /* defer SCO setup until mode change completed */
895 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
896 return sco;
897 }
898
899 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200900 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200901
902 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904EXPORT_SYMBOL(hci_connect);
905
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906void hci_disconnect(struct hci_conn *conn, __u8 reason)
907{
908 BT_DBG("conn %p", conn);
909
Mat Martineau3b9239a2012-02-16 11:54:30 -0800910 hci_proto_disconn_cfm(conn, reason, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911}
912EXPORT_SYMBOL(hci_disconnect);
913
914void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
915{
916 struct hci_dev *hdev = NULL;
917
918 BT_DBG("conn %p", conn);
919
920 read_lock_bh(&hci_dev_list_lock);
921
922 list_for_each_entry(hdev, &hci_dev_list, list) {
923 struct hci_conn *c;
924 if (hdev == conn->hdev)
925 continue;
926 if (hdev->amp_type == HCI_BREDR)
927 continue;
928 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
929 if (c)
930 hci_disconnect(c, reason);
931 }
932
933 read_unlock_bh(&hci_dev_list_lock);
934}
935
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200936/* Check link security requirement */
937int hci_conn_check_link_mode(struct hci_conn *conn)
938{
939 BT_DBG("conn %p", conn);
940
941 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
942 !(conn->link_mode & HCI_LM_ENCRYPT))
943 return 0;
944
945 return 1;
946}
947EXPORT_SYMBOL(hci_conn_check_link_mode);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100950static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 BT_DBG("conn %p", conn);
953
Johan Hedberg765c2a92011-01-19 12:06:52 +0530954 if (conn->pending_sec_level > sec_level)
955 sec_level = conn->pending_sec_level;
956
Marcel Holtmann96a31832009-02-12 16:23:03 +0100957 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530958 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100959 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 1;
961
Johan Hedberg65cf6862011-01-19 12:06:49 +0530962 /* Make sure we preserve an existing MITM requirement*/
963 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100964 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700965 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
968 struct hci_cp_auth_requested cp;
Peter Hurleya5e5b082012-01-13 15:11:30 +0100969
970 /* encrypt must be pending if auth is also pending */
971 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
972
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700973 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200974 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
975 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return 0;
979}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100981/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100982int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Brian Gixa68668b2011-08-11 15:49:36 -0700984 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100986 if (sec_level == BT_SECURITY_SDP)
987 return 1;
988
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700989 if (sec_level == BT_SECURITY_LOW &&
990 (!conn->ssp_mode || !conn->hdev->ssp_mode))
991 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100992
Brian Gix2e2f50d2011-09-13 12:36:04 -0700993 if (conn->type == LE_LINK) {
994 if (conn->pending_sec_level > sec_level)
995 sec_level = conn->pending_sec_level;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200996
Brian Gix2e2f50d2011-09-13 12:36:04 -0700997 if (sec_level > conn->sec_level)
998 conn->pending_sec_level = sec_level;
999 hci_proto_connect_cfm(conn, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000 return 0;
Brian Gix2e2f50d2011-09-13 12:36:04 -07001001 } else if (conn->link_mode & HCI_LM_ENCRYPT) {
1002 return hci_conn_auth(conn, sec_level, auth_type);
Ilia Kolomisnkyedc44dd2011-06-15 06:52:26 +03001003 } else if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
Brian Gix2e2f50d2011-09-13 12:36:04 -07001004 return 0;
1005 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006
1007 if (hci_conn_auth(conn, sec_level, auth_type)) {
1008 struct hci_cp_set_conn_encrypt cp;
Prabhakaran Mcafface82012-04-10 11:38:35 +05301009 if (timer_pending(&conn->encrypt_pause_timer)) {
1010 BT_INFO("encrypt_pause_timer is pending");
1011 return 0;
1012 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 cp.handle = cpu_to_le16(conn->handle);
1014 cp.encrypt = 1;
1015 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
1016 sizeof(cp), &cp);
1017 }
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001021EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023/* Change link key */
1024int hci_conn_change_link_key(struct hci_conn *conn)
1025{
1026 BT_DBG("conn %p", conn);
1027
1028 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
1029 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001030 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +02001031 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
1032 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 return 0;
1036}
1037EXPORT_SYMBOL(hci_conn_change_link_key);
1038
1039/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001040int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
1042 BT_DBG("conn %p", conn);
1043
1044 if (!role && conn->link_mode & HCI_LM_MASTER)
1045 return 1;
1046
1047 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
1048 struct hci_cp_switch_role cp;
1049 bacpy(&cp.bdaddr, &conn->dst);
1050 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001051 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 0;
1055}
1056EXPORT_SYMBOL(hci_conn_switch_role);
1057
Marcel Holtmann04837f62006-07-03 10:02:33 +02001058/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -07001059void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +02001060{
1061 struct hci_dev *hdev = conn->hdev;
1062
1063 BT_DBG("conn %p mode %d", conn, conn->mode);
1064
1065 if (test_bit(HCI_RAW, &hdev->flags))
1066 return;
1067
Sunny Kapdi39eaba32012-07-22 21:29:38 -07001068 if (conn->type == LE_LINK)
1069 return;
1070
Jaikumar Ganesh514abe62011-05-23 18:06:04 -07001071 if (conn->mode != HCI_CM_SNIFF)
1072 goto timer;
1073
1074 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +02001075 goto timer;
1076
1077 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1078 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001079 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001080 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001081 }
1082
1083timer:
Rahul Kashyapb44f9e22012-05-03 16:45:17 +05301084 if (hdev->idle_timeout > 0) {
Srinivas Krovvidi231689b2012-09-03 18:47:50 +05301085 spin_lock_bh(&conn->lock);
1086 if (conn->conn_valid) {
1087 mod_timer(&conn->idle_timer,
1088 jiffies + msecs_to_jiffies(hdev->idle_timeout));
1089 wake_lock(&conn->idle_lock);
1090 }
1091 spin_unlock_bh(&conn->lock);
Rahul Kashyapb44f9e22012-05-03 16:45:17 +05301092 }
Marcel Holtmann04837f62006-07-03 10:02:33 +02001093}
1094
Archana Ramachandran26a752b2011-12-20 11:27:40 -08001095static inline void hci_conn_stop_rssi_timer(struct hci_conn *conn)
1096{
1097 BT_DBG("conn %p", conn);
1098 cancel_delayed_work(&conn->rssi_update_work);
1099}
1100
1101static inline void hci_conn_start_rssi_timer(struct hci_conn *conn,
1102 u16 interval)
1103{
1104 struct hci_dev *hdev = conn->hdev;
1105 BT_DBG("conn %p, pending %d", conn,
1106 delayed_work_pending(&conn->rssi_update_work));
1107 if (!delayed_work_pending(&conn->rssi_update_work)) {
1108 queue_delayed_work(hdev->workqueue, &conn->rssi_update_work,
1109 msecs_to_jiffies(interval));
1110 }
1111}
1112
1113void hci_conn_set_rssi_reporter(struct hci_conn *conn,
1114 s8 rssi_threshold, u16 interval, u8 updateOnThreshExceed)
1115{
1116 if (conn) {
1117 conn->rssi_threshold = rssi_threshold;
1118 conn->rssi_update_interval = interval;
1119 conn->rssi_update_thresh_exceed = updateOnThreshExceed;
1120 hci_conn_start_rssi_timer(conn, interval);
1121 }
1122}
1123
1124void hci_conn_unset_rssi_reporter(struct hci_conn *conn)
1125{
1126 if (conn) {
1127 BT_DBG("Deleting the rssi_update_timer");
1128 hci_conn_stop_rssi_timer(conn);
1129 }
1130}
1131
Marcel Holtmann04837f62006-07-03 10:02:33 +02001132/* Enter sniff mode */
1133void hci_conn_enter_sniff_mode(struct hci_conn *conn)
1134{
1135 struct hci_dev *hdev = conn->hdev;
1136
1137 BT_DBG("conn %p mode %d", conn, conn->mode);
1138
1139 if (test_bit(HCI_RAW, &hdev->flags))
1140 return;
1141
Sunny Kapdi39eaba32012-07-22 21:29:38 -07001142 if (conn->type == LE_LINK)
1143 return;
1144
Marcel Holtmann04837f62006-07-03 10:02:33 +02001145 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
1146 return;
1147
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05301148 if (conn->mode != HCI_CM_ACTIVE ||
1149 !(conn->link_policy & HCI_LP_SNIFF) ||
1150 (hci_find_link_key(hdev, &conn->dst) == NULL))
Marcel Holtmann04837f62006-07-03 10:02:33 +02001151 return;
1152
1153 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
1154 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001155 cp.handle = cpu_to_le16(conn->handle);
1156 cp.max_latency = cpu_to_le16(0);
1157 cp.min_remote_timeout = cpu_to_le16(0);
1158 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001159 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001160 }
1161
1162 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1163 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001164 cp.handle = cpu_to_le16(conn->handle);
1165 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
1166 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
1167 cp.attempt = cpu_to_le16(4);
1168 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001169 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001170 }
1171}
1172
Peter Krystada8417e62012-03-21 16:58:17 -07001173struct hci_chan *hci_chan_create(struct hci_chan *chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1175{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 struct hci_cp_create_logical_link cp;
1177
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 chan->state = BT_CONNECT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001179 chan->tx_fs = *tx_fs;
1180 chan->rx_fs = *rx_fs;
1181 cp.phy_handle = chan->conn->handle;
1182 cp.tx_fs.id = chan->tx_fs.id;
1183 cp.tx_fs.type = chan->tx_fs.type;
1184 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
1185 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
1186 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
1187 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
1188 cp.rx_fs.id = chan->rx_fs.id;
1189 cp.rx_fs.type = chan->rx_fs.type;
1190 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
1191 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
1192 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
1193 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
1194 hci_conn_hold(chan->conn);
Peter Krystada8417e62012-03-21 16:58:17 -07001195 if (chan->conn->out)
1196 hci_send_cmd(chan->conn->hdev, HCI_OP_CREATE_LOGICAL_LINK,
1197 sizeof(cp), &cp);
1198 else
1199 hci_send_cmd(chan->conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK,
1200 sizeof(cp), &cp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201 return chan;
1202}
1203EXPORT_SYMBOL(hci_chan_create);
1204
1205void hci_chan_modify(struct hci_chan *chan,
1206 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1207{
1208 struct hci_cp_flow_spec_modify cp;
1209
1210 chan->tx_fs = *tx_fs;
1211 chan->rx_fs = *rx_fs;
1212 cp.log_handle = cpu_to_le16(chan->ll_handle);
1213 cp.tx_fs.id = tx_fs->id;
1214 cp.tx_fs.type = tx_fs->type;
1215 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
1216 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
1217 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
1218 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
1219 cp.rx_fs.id = rx_fs->id;
1220 cp.rx_fs.type = rx_fs->type;
1221 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
1222 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
1223 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
1224 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
1225 hci_conn_hold(chan->conn);
1226 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
1227 &cp);
1228}
1229EXPORT_SYMBOL(hci_chan_modify);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/* Drop all connection on the device */
Mat Martineau3b9239a2012-02-16 11:54:30 -08001232void hci_conn_hash_flush(struct hci_dev *hdev, u8 is_process)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 struct hci_conn_hash *h = &hdev->conn_hash;
1235 struct list_head *p;
1236
1237 BT_DBG("hdev %s", hdev->name);
1238
1239 p = h->list.next;
1240 while (p != &h->list) {
1241 struct hci_conn *c;
1242
1243 c = list_entry(p, struct hci_conn, list);
1244 p = p->next;
1245
1246 c->state = BT_CLOSED;
1247
Mat Martineau3b9239a2012-02-16 11:54:30 -08001248 hci_proto_disconn_cfm(c, 0x16, is_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 hci_conn_del(c);
1250 }
1251}
1252
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001253/* Check pending connect attempts */
1254void hci_conn_check_pending(struct hci_dev *hdev)
1255{
1256 struct hci_conn *conn;
1257
1258 BT_DBG("hdev %s", hdev->name);
1259
1260 hci_dev_lock(hdev);
1261
1262 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1263 if (conn)
1264 hci_acl_connect(conn);
1265
1266 hci_dev_unlock(hdev);
1267}
1268
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001269void hci_conn_hold_device(struct hci_conn *conn)
1270{
1271 atomic_inc(&conn->devref);
1272}
1273EXPORT_SYMBOL(hci_conn_hold_device);
1274
1275void hci_conn_put_device(struct hci_conn *conn)
1276{
Hemant Gupta0b6ad972013-06-08 19:07:57 +05301277 if (atomic_dec_and_test(&conn->devref)) {
1278 conn->hidp_session_valid = false;
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001279 hci_conn_del_sysfs(conn);
Hemant Gupta0b6ad972013-06-08 19:07:57 +05301280 }
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001281}
1282EXPORT_SYMBOL(hci_conn_put_device);
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284int hci_get_conn_list(void __user *arg)
1285{
1286 struct hci_conn_list_req req, *cl;
1287 struct hci_conn_info *ci;
1288 struct hci_dev *hdev;
1289 struct list_head *p;
1290 int n = 0, size, err;
1291
1292 if (copy_from_user(&req, arg, sizeof(req)))
1293 return -EFAULT;
1294
1295 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1296 return -EINVAL;
1297
1298 size = sizeof(req) + req.conn_num * sizeof(*ci);
1299
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001300 cl = kmalloc(size, GFP_KERNEL);
1301 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return -ENOMEM;
1303
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001304 hdev = hci_dev_get(req.dev_id);
1305 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 kfree(cl);
1307 return -ENODEV;
1308 }
1309
1310 ci = cl->conn_info;
1311
1312 hci_dev_lock_bh(hdev);
1313 list_for_each(p, &hdev->conn_hash.list) {
1314 register struct hci_conn *c;
1315 c = list_entry(p, struct hci_conn, list);
1316
1317 bacpy(&(ci + n)->bdaddr, &c->dst);
1318 (ci + n)->handle = c->handle;
1319 (ci + n)->type = c->type;
1320 (ci + n)->out = c->out;
1321 (ci + n)->state = c->state;
1322 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001323 if (c->type == SCO_LINK) {
1324 (ci + n)->mtu = hdev->sco_mtu;
1325 (ci + n)->cnt = hdev->sco_cnt;
1326 (ci + n)->pkts = hdev->sco_pkts;
1327 } else {
1328 (ci + n)->mtu = hdev->acl_mtu;
1329 (ci + n)->cnt = hdev->acl_cnt;
1330 (ci + n)->pkts = hdev->acl_pkts;
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (++n >= req.conn_num)
1333 break;
1334 }
1335 hci_dev_unlock_bh(hdev);
1336
1337 cl->dev_id = hdev->id;
1338 cl->conn_num = n;
1339 size = sizeof(req) + n * sizeof(*ci);
1340
1341 hci_dev_put(hdev);
1342
1343 err = copy_to_user(arg, cl, size);
1344 kfree(cl);
1345
1346 return err ? -EFAULT : 0;
1347}
1348
1349int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1350{
1351 struct hci_conn_info_req req;
1352 struct hci_conn_info ci;
1353 struct hci_conn *conn;
1354 char __user *ptr = arg + sizeof(req);
1355
1356 if (copy_from_user(&req, arg, sizeof(req)))
1357 return -EFAULT;
1358
1359 hci_dev_lock_bh(hdev);
1360 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1361 if (conn) {
1362 bacpy(&ci.bdaddr, &conn->dst);
1363 ci.handle = conn->handle;
1364 ci.type = conn->type;
1365 ci.out = conn->out;
1366 ci.state = conn->state;
1367 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001368 if (req.type == SCO_LINK) {
1369 ci.mtu = hdev->sco_mtu;
1370 ci.cnt = hdev->sco_cnt;
1371 ci.pkts = hdev->sco_pkts;
1372 } else {
1373 ci.mtu = hdev->acl_mtu;
1374 ci.cnt = hdev->acl_cnt;
1375 ci.pkts = hdev->acl_pkts;
1376 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377 ci.pending_sec_level = conn->pending_sec_level;
1378 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 hci_dev_unlock_bh(hdev);
1381
1382 if (!conn)
1383 return -ENOENT;
1384
1385 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1386}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001387
1388int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1389{
1390 struct hci_auth_info_req req;
1391 struct hci_conn *conn;
1392
1393 if (copy_from_user(&req, arg, sizeof(req)))
1394 return -EFAULT;
1395
1396 hci_dev_lock_bh(hdev);
1397 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1398 if (conn)
1399 req.type = conn->auth_type;
1400 hci_dev_unlock_bh(hdev);
1401
1402 if (!conn)
1403 return -ENOENT;
1404
1405 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1406}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407
1408int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1409{
1410 struct hci_auth_info_req req;
1411 struct hci_conn *conn;
1412
1413 if (copy_from_user(&req, arg, sizeof(req)))
1414 return -EFAULT;
1415
1416 hci_dev_lock_bh(hdev);
1417 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
Srinivas Krovvidi94bf88f2012-08-27 18:32:45 +05301418 if (conn) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419 conn->auth_type = req.type;
Srinivas Krovvidi94bf88f2012-08-27 18:32:45 +05301420 switch (conn->auth_type) {
1421 case HCI_AT_NO_BONDING:
1422 conn->pending_sec_level = BT_SECURITY_LOW;
1423 break;
1424 case HCI_AT_DEDICATED_BONDING:
1425 case HCI_AT_GENERAL_BONDING:
1426 conn->pending_sec_level = BT_SECURITY_MEDIUM;
1427 break;
1428 case HCI_AT_DEDICATED_BONDING_MITM:
1429 case HCI_AT_GENERAL_BONDING_MITM:
1430 conn->pending_sec_level = BT_SECURITY_HIGH;
1431 break;
1432 default:
1433 break;
1434 }
1435 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001436 hci_dev_unlock_bh(hdev);
1437
1438 if (!conn)
1439 return -ENOENT;
1440
1441 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1442}