blob: 9ffd0f1ab18f39bfb54d48ba62834b3af8fefa2e [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 Gupta6bca5cd2013-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 Kapdi93bef892012-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 Kapdi93bef892012-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 Kapdi93bef892012-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 Kapdi93bef892012-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 GB0478d652012-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 GB0478d652012-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 Krovvidi6aadc412012-09-03 18:47:50 +0530545 conn->conn_valid = true;
546 spin_lock_init(&conn->lock);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200547
Marcel Holtmanna8746412008-07-14 20:13:46 +0200548 switch (type) {
549 case ACL_LINK:
550 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
Rahul Kashyapd5553ba2012-01-02 19:27:19 +0530551 conn->link_policy = hdev->link_policy;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200552 break;
553 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800554 if (!pkt_type)
555 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200556 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800557 if (!pkt_type)
558 pkt_type = ALL_ESCO_MASK;
559 if (lmp_esco_capable(hdev)) {
560 /* HCI Setup Synchronous Connection Command uses
561 reverse logic on the EDR_ESCO_MASK bits */
562 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
563 hdev->esco_type;
564 } else {
565 /* Legacy HCI Add Sco Connection Command uses a
566 shifted bitmask */
567 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
568 SCO_PTYPE_MASK;
569 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200570 break;
571 }
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200574
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800575 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
576 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800577 INIT_DELAYED_WORK(&conn->rssi_update_work, hci_conn_rssi_update);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530578 setup_timer(&conn->encrypt_pause_timer, encryption_disabled_timeout,
579 (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 atomic_set(&conn->refcnt, 0);
582
583 hci_dev_hold(hdev);
584
585 tasklet_disable(&hdev->tx_task);
586
587 hci_conn_hash_add(hdev, conn);
588 if (hdev->notify)
589 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
590
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700591 atomic_set(&conn->devref, 0);
592
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700593 hci_conn_init_sysfs(conn);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 tasklet_enable(&hdev->tx_task);
596
597 return conn;
598}
599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst,
601 __u8 addr_type)
602{
603 struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst);
604 if (!conn)
605 return NULL;
606
607 conn->dst_type = addr_type;
608
609 return conn;
610}
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612int hci_conn_del(struct hci_conn *conn)
613{
614 struct hci_dev *hdev = conn->hdev;
615
616 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
617
Srinivas Krovvidi6aadc412012-09-03 18:47:50 +0530618 spin_lock_bh(&conn->lock);
619 conn->conn_valid = false; /* conn data is being released */
620 spin_unlock_bh(&conn->lock);
621
Brian Gix3cd62042012-01-11 15:18:17 -0800622 /* Make sure no timers are running */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200623 del_timer(&conn->idle_timer);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200624 del_timer(&conn->disc_timer);
Brian Gix3cd62042012-01-11 15:18:17 -0800625 del_timer(&conn->smp_timer);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800626 __cancel_delayed_work(&conn->rssi_update_work);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530627 del_timer(&conn->encrypt_pause_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200629 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 struct hci_conn *sco = conn->link;
631 if (sco)
632 sco->link = NULL;
633
634 /* Unacked frames */
635 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300636 } else if (conn->type == LE_LINK) {
637 if (hdev->le_pkts)
638 hdev->le_cnt += conn->sent;
639 else
640 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200641 } else {
642 struct hci_conn *acl = conn->link;
643 if (acl) {
644 acl->link = NULL;
645 hci_conn_put(acl);
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
649 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 hci_conn_hash_del(hdev, conn);
652 if (hdev->notify)
653 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200654
AnubhavGupta01d9e362011-12-17 17:14:51 +0530655 tasklet_schedule(&hdev->tx_task);
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700661 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800662
Hemant Gupta6bca5cd2013-06-08 19:07:57 +0530663 if (conn->hidp_session_valid)
664 hci_conn_put_device(conn);
665
Marcel Holtmann384943e2009-05-08 18:20:43 -0700666 hci_dev_put(hdev);
667
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668 return 0;
669}
670
671struct hci_chan *hci_chan_add(struct hci_dev *hdev)
672{
673 struct hci_chan *chan;
674
675 BT_DBG("%s", hdev->name);
676
677 chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC);
678 if (!chan)
679 return NULL;
680
681 atomic_set(&chan->refcnt, 0);
682
683 hci_dev_hold(hdev);
684
685 chan->hdev = hdev;
686
687 list_add(&chan->list, &hdev->chan_list.list);
688
689 return chan;
690}
Peter Krystada8417e62012-03-21 16:58:17 -0700691EXPORT_SYMBOL(hci_chan_add);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692
693int hci_chan_del(struct hci_chan *chan)
694{
695 BT_DBG("%s chan %p", chan->hdev->name, chan);
696
697 list_del(&chan->list);
698
699 hci_conn_put(chan->conn);
700 hci_dev_put(chan->hdev);
701
702 kfree(chan);
Tomas Targownik1be668d2011-06-30 16:30:44 -0300703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return 0;
705}
706
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800707int hci_chan_put(struct hci_chan *chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708{
709 struct hci_cp_disconn_logical_link cp;
Mat Martineau9f8d4672011-12-14 12:10:46 -0800710 struct hci_conn *hcon;
711 u16 ll_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712
713 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
714 if (!atomic_dec_and_test(&chan->refcnt))
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800715 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716
Mat Martineau9f8d4672011-12-14 12:10:46 -0800717 hcon = chan->conn;
718 ll_handle = chan->ll_handle;
719
720 hci_chan_del(chan);
721
722 BT_DBG("chan->conn->state %d", hcon->state);
723 if (hcon->state == BT_CONNECTED) {
724 cp.log_handle = cpu_to_le16(ll_handle);
725 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 sizeof(cp), &cp);
Mat Martineau9f8d4672011-12-14 12:10:46 -0800727 }
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800728
729 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730}
731EXPORT_SYMBOL(hci_chan_put);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
734{
735 int use_src = bacmp(src, BDADDR_ANY);
736 struct hci_dev *hdev = NULL;
737 struct list_head *p;
738
739 BT_DBG("%s -> %s", batostr(src), batostr(dst));
740
741 read_lock_bh(&hci_dev_list_lock);
742
743 list_for_each(p, &hci_dev_list) {
744 struct hci_dev *d = list_entry(p, struct hci_dev, list);
745
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700746 if (d->dev_type != HCI_BREDR)
747 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
749 continue;
750
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900751 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * No source address - find interface with bdaddr != dst
753 * Source address - find interface with bdaddr == src
754 */
755
756 if (use_src) {
757 if (!bacmp(&d->bdaddr, src)) {
758 hdev = d; break;
759 }
760 } else {
761 if (bacmp(&d->bdaddr, dst)) {
762 hdev = d; break;
763 }
764 }
765 }
766
767 if (hdev)
768 hdev = hci_dev_hold(hdev);
769
770 read_unlock_bh(&hci_dev_list_lock);
771 return hdev;
772}
773EXPORT_SYMBOL(hci_get_route);
774
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775struct hci_dev *hci_dev_get_type(u8 amp_type)
776{
777 struct hci_dev *hdev = NULL;
778 struct hci_dev *d;
779
780 BT_DBG("amp_type %d", amp_type);
781
782 read_lock_bh(&hci_dev_list_lock);
783
784 list_for_each_entry(d, &hci_dev_list, list) {
785 if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) {
786 hdev = d;
787 break;
788 }
789 }
790
791 if (hdev)
792 hdev = hci_dev_hold(hdev);
793
794 read_unlock_bh(&hci_dev_list_lock);
795 return hdev;
796}
797EXPORT_SYMBOL(hci_dev_get_type);
798
799struct hci_dev *hci_dev_get_amp(bdaddr_t *dst)
800{
801 struct hci_dev *d;
802 struct hci_dev *hdev = NULL;
803
804 BT_DBG("%s dst %s", hdev->name, batostr(dst));
805
806 read_lock_bh(&hci_dev_list_lock);
807
808 list_for_each_entry(d, &hci_dev_list, list) {
809 struct hci_conn *conn;
810 if (d->dev_type == HCI_BREDR)
811 continue;
812 conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst);
813 if (conn) {
814 hdev = d;
815 break;
816 }
817 }
818
819 if (hdev)
820 hdev = hci_dev_hold(hdev);
821
822 read_unlock_bh(&hci_dev_list_lock);
823 return hdev;
824}
825EXPORT_SYMBOL(hci_dev_get_amp);
826
Ville Tervofcd89c02011-02-10 22:38:47 -0300827/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800829struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
830 __u16 pkt_type, bdaddr_t *dst,
831 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 struct hci_conn *acl;
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200834 struct hci_conn *sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 BT_DBG("%s dst %s", hdev->name, batostr(dst));
837
Brian Gixa94b6122012-02-23 16:07:10 -0800838 if (type == LE_LINK)
839 return hci_le_connect(hdev, pkt_type, dst, sec_level,
840 auth_type, NULL);
Ville Tervofcd89c02011-02-10 22:38:47 -0300841
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200842 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
843 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800844 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200845 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return NULL;
847 }
848
849 hci_conn_hold(acl);
850
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200851 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530852 acl->sec_level = BT_SECURITY_LOW;
853 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200854 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200858 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return acl;
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200860
Srinivas Krovvidi645f4d42012-08-02 13:23:53 +0530861 /* type of connection already existing can be ESCO or SCO
862 * so check for both types before creating new */
863
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200864 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
Srinivas Krovvidi645f4d42012-08-02 13:23:53 +0530865
866 if (!sco && type == ESCO_LINK) {
867 sco = hci_conn_hash_lookup_ba(hdev, SCO_LINK, dst);
868 } else if (!sco && type == SCO_LINK) {
869 /* this case can be practically not possible */
870 sco = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, dst);
871 }
872
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200873 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800874 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200875 if (!sco) {
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200876 hci_conn_put(acl);
877 return NULL;
878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200880
881 acl->link = sco;
882 sco->link = acl;
883
884 hci_conn_hold(sco);
885
886 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200887 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800888 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800890
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400891 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
892 /* defer SCO setup until mode change completed */
893 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
894 return sco;
895 }
896
897 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200898 }
Marcel Holtmann5b7f99092007-07-11 09:51:55 +0200899
900 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902EXPORT_SYMBOL(hci_connect);
903
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904void hci_disconnect(struct hci_conn *conn, __u8 reason)
905{
906 BT_DBG("conn %p", conn);
907
Mat Martineau3b9239a2012-02-16 11:54:30 -0800908 hci_proto_disconn_cfm(conn, reason, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909}
910EXPORT_SYMBOL(hci_disconnect);
911
912void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
913{
914 struct hci_dev *hdev = NULL;
915
916 BT_DBG("conn %p", conn);
917
918 read_lock_bh(&hci_dev_list_lock);
919
920 list_for_each_entry(hdev, &hci_dev_list, list) {
921 struct hci_conn *c;
922 if (hdev == conn->hdev)
923 continue;
924 if (hdev->amp_type == HCI_BREDR)
925 continue;
926 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
927 if (c)
928 hci_disconnect(c, reason);
929 }
930
931 read_unlock_bh(&hci_dev_list_lock);
932}
933
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200934/* Check link security requirement */
935int hci_conn_check_link_mode(struct hci_conn *conn)
936{
937 BT_DBG("conn %p", conn);
938
939 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
940 !(conn->link_mode & HCI_LM_ENCRYPT))
941 return 0;
942
943 return 1;
944}
945EXPORT_SYMBOL(hci_conn_check_link_mode);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100948static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 BT_DBG("conn %p", conn);
951
Johan Hedberg765c2a92011-01-19 12:06:52 +0530952 if (conn->pending_sec_level > sec_level)
953 sec_level = conn->pending_sec_level;
954
Marcel Holtmann96a31832009-02-12 16:23:03 +0100955 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530956 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100957 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 1;
959
Johan Hedberg65cf6862011-01-19 12:06:49 +0530960 /* Make sure we preserve an existing MITM requirement*/
961 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100962 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700963 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
966 struct hci_cp_auth_requested cp;
Peter Hurleya5e5b082012-01-13 15:11:30 +0100967
968 /* encrypt must be pending if auth is also pending */
969 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
970
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700971 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200972 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
973 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return 0;
977}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100979/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100980int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Brian Gixa68668b2011-08-11 15:49:36 -0700982 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100984 if (sec_level == BT_SECURITY_SDP)
985 return 1;
986
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700987 if (sec_level == BT_SECURITY_LOW &&
988 (!conn->ssp_mode || !conn->hdev->ssp_mode))
989 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100990
Brian Gix2e2f50d2011-09-13 12:36:04 -0700991 if (conn->type == LE_LINK) {
992 if (conn->pending_sec_level > sec_level)
993 sec_level = conn->pending_sec_level;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200994
Brian Gix2e2f50d2011-09-13 12:36:04 -0700995 if (sec_level > conn->sec_level)
996 conn->pending_sec_level = sec_level;
997 hci_proto_connect_cfm(conn, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 return 0;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700999 } else if (conn->link_mode & HCI_LM_ENCRYPT) {
1000 return hci_conn_auth(conn, sec_level, auth_type);
Ilia Kolomisnkyedc44dd2011-06-15 06:52:26 +03001001 } else if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
Brian Gix2e2f50d2011-09-13 12:36:04 -07001002 return 0;
1003 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004
1005 if (hci_conn_auth(conn, sec_level, auth_type)) {
1006 struct hci_cp_set_conn_encrypt cp;
Prabhakaran Mcafface82012-04-10 11:38:35 +05301007 if (timer_pending(&conn->encrypt_pause_timer)) {
1008 BT_INFO("encrypt_pause_timer is pending");
1009 return 0;
1010 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011 cp.handle = cpu_to_le16(conn->handle);
1012 cp.encrypt = 1;
1013 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
1014 sizeof(cp), &cp);
1015 }
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
1018}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001019EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021/* Change link key */
1022int hci_conn_change_link_key(struct hci_conn *conn)
1023{
1024 BT_DBG("conn %p", conn);
1025
1026 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
1027 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001028 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +02001029 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
1030 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return 0;
1034}
1035EXPORT_SYMBOL(hci_conn_change_link_key);
1036
1037/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001038int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 BT_DBG("conn %p", conn);
1041
1042 if (!role && conn->link_mode & HCI_LM_MASTER)
1043 return 1;
1044
1045 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
1046 struct hci_cp_switch_role cp;
1047 bacpy(&cp.bdaddr, &conn->dst);
1048 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001049 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +01001051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return 0;
1053}
1054EXPORT_SYMBOL(hci_conn_switch_role);
1055
Marcel Holtmann04837f62006-07-03 10:02:33 +02001056/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -07001057void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +02001058{
1059 struct hci_dev *hdev = conn->hdev;
1060
1061 BT_DBG("conn %p mode %d", conn, conn->mode);
1062
1063 if (test_bit(HCI_RAW, &hdev->flags))
1064 return;
1065
Sunny Kapdi39eaba32012-07-22 21:29:38 -07001066 if (conn->type == LE_LINK)
1067 return;
1068
Jaikumar Ganesh514abe62011-05-23 18:06:04 -07001069 if (conn->mode != HCI_CM_SNIFF)
1070 goto timer;
1071
1072 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +02001073 goto timer;
1074
1075 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1076 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001077 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001078 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001079 }
1080
1081timer:
Rahul Kashyapb44f9e22012-05-03 16:45:17 +05301082 if (hdev->idle_timeout > 0) {
Srinivas Krovvidi6aadc412012-09-03 18:47:50 +05301083 spin_lock_bh(&conn->lock);
1084 if (conn->conn_valid) {
1085 mod_timer(&conn->idle_timer,
1086 jiffies + msecs_to_jiffies(hdev->idle_timeout));
Srinivas Krovvidi6aadc412012-09-03 18:47:50 +05301087 }
1088 spin_unlock_bh(&conn->lock);
Rahul Kashyapb44f9e22012-05-03 16:45:17 +05301089 }
Marcel Holtmann04837f62006-07-03 10:02:33 +02001090}
1091
Archana Ramachandran26a752b2011-12-20 11:27:40 -08001092static inline void hci_conn_stop_rssi_timer(struct hci_conn *conn)
1093{
1094 BT_DBG("conn %p", conn);
1095 cancel_delayed_work(&conn->rssi_update_work);
1096}
1097
1098static inline void hci_conn_start_rssi_timer(struct hci_conn *conn,
1099 u16 interval)
1100{
1101 struct hci_dev *hdev = conn->hdev;
1102 BT_DBG("conn %p, pending %d", conn,
1103 delayed_work_pending(&conn->rssi_update_work));
1104 if (!delayed_work_pending(&conn->rssi_update_work)) {
1105 queue_delayed_work(hdev->workqueue, &conn->rssi_update_work,
1106 msecs_to_jiffies(interval));
1107 }
1108}
1109
1110void hci_conn_set_rssi_reporter(struct hci_conn *conn,
1111 s8 rssi_threshold, u16 interval, u8 updateOnThreshExceed)
1112{
1113 if (conn) {
1114 conn->rssi_threshold = rssi_threshold;
1115 conn->rssi_update_interval = interval;
1116 conn->rssi_update_thresh_exceed = updateOnThreshExceed;
1117 hci_conn_start_rssi_timer(conn, interval);
1118 }
1119}
1120
1121void hci_conn_unset_rssi_reporter(struct hci_conn *conn)
1122{
1123 if (conn) {
1124 BT_DBG("Deleting the rssi_update_timer");
1125 hci_conn_stop_rssi_timer(conn);
1126 }
1127}
1128
Marcel Holtmann04837f62006-07-03 10:02:33 +02001129/* Enter sniff mode */
1130void hci_conn_enter_sniff_mode(struct hci_conn *conn)
1131{
1132 struct hci_dev *hdev = conn->hdev;
1133
1134 BT_DBG("conn %p mode %d", conn, conn->mode);
1135
1136 if (test_bit(HCI_RAW, &hdev->flags))
1137 return;
1138
Sunny Kapdi39eaba32012-07-22 21:29:38 -07001139 if (conn->type == LE_LINK)
1140 return;
1141
Marcel Holtmann04837f62006-07-03 10:02:33 +02001142 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
1143 return;
1144
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05301145 if (conn->mode != HCI_CM_ACTIVE ||
1146 !(conn->link_policy & HCI_LP_SNIFF) ||
1147 (hci_find_link_key(hdev, &conn->dst) == NULL))
Marcel Holtmann04837f62006-07-03 10:02:33 +02001148 return;
1149
1150 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
1151 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001152 cp.handle = cpu_to_le16(conn->handle);
1153 cp.max_latency = cpu_to_le16(0);
1154 cp.min_remote_timeout = cpu_to_le16(0);
1155 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001156 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001157 }
1158
1159 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1160 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001161 cp.handle = cpu_to_le16(conn->handle);
1162 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
1163 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
1164 cp.attempt = cpu_to_le16(4);
1165 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001166 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001167 }
1168}
1169
Peter Krystada8417e62012-03-21 16:58:17 -07001170struct hci_chan *hci_chan_create(struct hci_chan *chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001171 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1172{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001173 struct hci_cp_create_logical_link cp;
1174
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175 chan->state = BT_CONNECT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 chan->tx_fs = *tx_fs;
1177 chan->rx_fs = *rx_fs;
1178 cp.phy_handle = chan->conn->handle;
1179 cp.tx_fs.id = chan->tx_fs.id;
1180 cp.tx_fs.type = chan->tx_fs.type;
1181 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
1182 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
1183 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
1184 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
1185 cp.rx_fs.id = chan->rx_fs.id;
1186 cp.rx_fs.type = chan->rx_fs.type;
1187 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
1188 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
1189 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
1190 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
1191 hci_conn_hold(chan->conn);
Peter Krystada8417e62012-03-21 16:58:17 -07001192 if (chan->conn->out)
1193 hci_send_cmd(chan->conn->hdev, HCI_OP_CREATE_LOGICAL_LINK,
1194 sizeof(cp), &cp);
1195 else
1196 hci_send_cmd(chan->conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK,
1197 sizeof(cp), &cp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198 return chan;
1199}
1200EXPORT_SYMBOL(hci_chan_create);
1201
1202void hci_chan_modify(struct hci_chan *chan,
1203 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1204{
1205 struct hci_cp_flow_spec_modify cp;
1206
1207 chan->tx_fs = *tx_fs;
1208 chan->rx_fs = *rx_fs;
1209 cp.log_handle = cpu_to_le16(chan->ll_handle);
1210 cp.tx_fs.id = tx_fs->id;
1211 cp.tx_fs.type = tx_fs->type;
1212 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
1213 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
1214 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
1215 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
1216 cp.rx_fs.id = rx_fs->id;
1217 cp.rx_fs.type = rx_fs->type;
1218 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
1219 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
1220 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
1221 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
1222 hci_conn_hold(chan->conn);
1223 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
1224 &cp);
1225}
1226EXPORT_SYMBOL(hci_chan_modify);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228/* Drop all connection on the device */
Mat Martineau3b9239a2012-02-16 11:54:30 -08001229void hci_conn_hash_flush(struct hci_dev *hdev, u8 is_process)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct hci_conn_hash *h = &hdev->conn_hash;
1232 struct list_head *p;
1233
1234 BT_DBG("hdev %s", hdev->name);
1235
1236 p = h->list.next;
1237 while (p != &h->list) {
1238 struct hci_conn *c;
1239
1240 c = list_entry(p, struct hci_conn, list);
1241 p = p->next;
1242
1243 c->state = BT_CLOSED;
1244
Mat Martineau3b9239a2012-02-16 11:54:30 -08001245 hci_proto_disconn_cfm(c, 0x16, is_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 hci_conn_del(c);
1247 }
1248}
1249
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001250/* Check pending connect attempts */
1251void hci_conn_check_pending(struct hci_dev *hdev)
1252{
1253 struct hci_conn *conn;
1254
1255 BT_DBG("hdev %s", hdev->name);
1256
1257 hci_dev_lock(hdev);
1258
1259 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1260 if (conn)
1261 hci_acl_connect(conn);
1262
1263 hci_dev_unlock(hdev);
1264}
1265
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001266void hci_conn_hold_device(struct hci_conn *conn)
1267{
1268 atomic_inc(&conn->devref);
1269}
1270EXPORT_SYMBOL(hci_conn_hold_device);
1271
1272void hci_conn_put_device(struct hci_conn *conn)
1273{
Hemant Gupta6bca5cd2013-06-08 19:07:57 +05301274 if (atomic_dec_and_test(&conn->devref)) {
1275 conn->hidp_session_valid = false;
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001276 hci_conn_del_sysfs(conn);
Hemant Gupta6bca5cd2013-06-08 19:07:57 +05301277 }
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001278}
1279EXPORT_SYMBOL(hci_conn_put_device);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281int hci_get_conn_list(void __user *arg)
1282{
1283 struct hci_conn_list_req req, *cl;
1284 struct hci_conn_info *ci;
1285 struct hci_dev *hdev;
1286 struct list_head *p;
1287 int n = 0, size, err;
1288
1289 if (copy_from_user(&req, arg, sizeof(req)))
1290 return -EFAULT;
1291
1292 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1293 return -EINVAL;
1294
1295 size = sizeof(req) + req.conn_num * sizeof(*ci);
1296
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001297 cl = kmalloc(size, GFP_KERNEL);
1298 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -ENOMEM;
1300
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001301 hdev = hci_dev_get(req.dev_id);
1302 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 kfree(cl);
1304 return -ENODEV;
1305 }
1306
1307 ci = cl->conn_info;
1308
1309 hci_dev_lock_bh(hdev);
1310 list_for_each(p, &hdev->conn_hash.list) {
1311 register struct hci_conn *c;
1312 c = list_entry(p, struct hci_conn, list);
1313
1314 bacpy(&(ci + n)->bdaddr, &c->dst);
1315 (ci + n)->handle = c->handle;
1316 (ci + n)->type = c->type;
1317 (ci + n)->out = c->out;
1318 (ci + n)->state = c->state;
1319 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001320 if (c->type == SCO_LINK) {
1321 (ci + n)->mtu = hdev->sco_mtu;
1322 (ci + n)->cnt = hdev->sco_cnt;
1323 (ci + n)->pkts = hdev->sco_pkts;
1324 } else {
1325 (ci + n)->mtu = hdev->acl_mtu;
1326 (ci + n)->cnt = hdev->acl_cnt;
1327 (ci + n)->pkts = hdev->acl_pkts;
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (++n >= req.conn_num)
1330 break;
1331 }
1332 hci_dev_unlock_bh(hdev);
1333
1334 cl->dev_id = hdev->id;
1335 cl->conn_num = n;
1336 size = sizeof(req) + n * sizeof(*ci);
1337
1338 hci_dev_put(hdev);
1339
1340 err = copy_to_user(arg, cl, size);
1341 kfree(cl);
1342
1343 return err ? -EFAULT : 0;
1344}
1345
1346int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1347{
1348 struct hci_conn_info_req req;
1349 struct hci_conn_info ci;
1350 struct hci_conn *conn;
1351 char __user *ptr = arg + sizeof(req);
1352
1353 if (copy_from_user(&req, arg, sizeof(req)))
1354 return -EFAULT;
1355
1356 hci_dev_lock_bh(hdev);
1357 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1358 if (conn) {
1359 bacpy(&ci.bdaddr, &conn->dst);
1360 ci.handle = conn->handle;
1361 ci.type = conn->type;
1362 ci.out = conn->out;
1363 ci.state = conn->state;
1364 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001365 if (req.type == SCO_LINK) {
1366 ci.mtu = hdev->sco_mtu;
1367 ci.cnt = hdev->sco_cnt;
1368 ci.pkts = hdev->sco_pkts;
1369 } else {
1370 ci.mtu = hdev->acl_mtu;
1371 ci.cnt = hdev->acl_cnt;
1372 ci.pkts = hdev->acl_pkts;
1373 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374 ci.pending_sec_level = conn->pending_sec_level;
1375 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 hci_dev_unlock_bh(hdev);
1378
1379 if (!conn)
1380 return -ENOENT;
1381
1382 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1383}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001384
1385int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1386{
1387 struct hci_auth_info_req req;
1388 struct hci_conn *conn;
1389
1390 if (copy_from_user(&req, arg, sizeof(req)))
1391 return -EFAULT;
1392
1393 hci_dev_lock_bh(hdev);
1394 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1395 if (conn)
1396 req.type = conn->auth_type;
1397 hci_dev_unlock_bh(hdev);
1398
1399 if (!conn)
1400 return -ENOENT;
1401
1402 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1403}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001404
1405int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1406{
1407 struct hci_auth_info_req req;
1408 struct hci_conn *conn;
1409
1410 if (copy_from_user(&req, arg, sizeof(req)))
1411 return -EFAULT;
1412
1413 hci_dev_lock_bh(hdev);
1414 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
Srinivas Krovvidi52b05ba2012-08-27 18:32:45 +05301415 if (conn) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001416 conn->auth_type = req.type;
Srinivas Krovvidi52b05ba2012-08-27 18:32:45 +05301417 switch (conn->auth_type) {
1418 case HCI_AT_NO_BONDING:
1419 conn->pending_sec_level = BT_SECURITY_LOW;
1420 break;
1421 case HCI_AT_DEDICATED_BONDING:
1422 case HCI_AT_GENERAL_BONDING:
1423 conn->pending_sec_level = BT_SECURITY_MEDIUM;
1424 break;
1425 case HCI_AT_DEDICATED_BONDING_MITM:
1426 case HCI_AT_GENERAL_BONDING_MITM:
1427 conn->pending_sec_level = BT_SECURITY_HIGH;
1428 break;
1429 default:
1430 break;
1431 }
1432 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 hci_dev_unlock_bh(hdev);
1434
1435 if (!conn)
1436 return -ENOENT;
1437
1438 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1439}