blob: 963ef489406ae5d5df5560f4198a8642ce4f7167 [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
Brian Gix3cd62042012-01-11 15:18:17 -08003 Copyright (c) 2000-2001, 2010-2012 Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI connection handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
28
29#include <linux/types.h>
30#include <linux/errno.h>
31#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/slab.h>
33#include <linux/poll.h>
34#include <linux/fcntl.h>
35#include <linux/init.h>
36#include <linux/skbuff.h>
37#include <linux/interrupt.h>
38#include <linux/notifier.h>
39#include <net/sock.h>
40
41#include <asm/system.h>
Andrei Emeltchenko70f230202010-12-01 16:58:25 +020042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/unaligned.h>
44
45#include <net/bluetooth/bluetooth.h>
46#include <net/bluetooth/hci_core.h>
Brian Gixa94b6122012-02-23 16:07:10 -080047#include <net/bluetooth/l2cap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Brian Gixa94b6122012-02-23 16:07:10 -080049struct hci_conn *hci_le_connect(struct hci_dev *hdev, __u16 pkt_type,
50 bdaddr_t *dst, __u8 sec_level, __u8 auth_type,
51 struct bt_le_params *le_params)
Ville Tervofcd89c02011-02-10 22:38:47 -030052{
Brian Gixa94b6122012-02-23 16:07:10 -080053 struct hci_conn *le;
Ville Tervofcd89c02011-02-10 22:38:47 -030054 struct hci_cp_le_create_conn cp;
Brian Gixa94b6122012-02-23 16:07:10 -080055 struct adv_entry *entry;
56 struct link_key *key;
Ville Tervofcd89c02011-02-10 22:38:47 -030057
Brian Gixa94b6122012-02-23 16:07:10 -080058 BT_DBG("%p", hdev);
Brian Gix114f3a62011-09-27 14:02:20 -070059
Brian Gixa94b6122012-02-23 16:07:10 -080060 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
61 if (le) {
62 hci_conn_hold(le);
63 return le;
64 }
65
66 key = hci_find_link_key_type(hdev, dst, KEY_TYPE_LTK);
67 if (!key) {
68 entry = hci_find_adv_entry(hdev, dst);
69 if (entry)
70 le = hci_le_conn_add(hdev, dst,
71 entry->bdaddr_type);
72 else
73 le = hci_le_conn_add(hdev, dst, 0);
74 } else {
75 le = hci_le_conn_add(hdev, dst, key->addr_type);
76 }
77
78 if (!le)
79 return ERR_PTR(-ENOMEM);
80
81 hci_conn_hold(le);
82
83 le->state = BT_CONNECT;
84 le->out = 1;
85 le->link_mode |= HCI_LM_MASTER;
86 le->sec_level = BT_SECURITY_LOW;
87 le->type = LE_LINK;
Ville Tervofcd89c02011-02-10 22:38:47 -030088
89 memset(&cp, 0, sizeof(cp));
Brian Gixa94b6122012-02-23 16:07:10 -080090 if (l2cap_sock_le_params_valid(le_params)) {
91 cp.supervision_timeout =
92 cpu_to_le16(le_params->supervision_timeout);
93 cp.scan_interval = cpu_to_le16(le_params->scan_interval);
94 cp.scan_window = cpu_to_le16(le_params->scan_window);
95 cp.conn_interval_min = cpu_to_le16(le_params->interval_min);
96 cp.conn_interval_max = cpu_to_le16(le_params->interval_max);
97 cp.conn_latency = cpu_to_le16(le_params->latency);
98 cp.min_ce_len = cpu_to_le16(le_params->min_ce_len);
99 cp.max_ce_len = cpu_to_le16(le_params->max_ce_len);
100 le->conn_timeout = le_params->conn_timeout;
101 } else {
102 cp.supervision_timeout = cpu_to_le16(BT_LE_SUP_TO_DEFAULT);
103 cp.scan_interval = cpu_to_le16(BT_LE_SCAN_INTERVAL_DEF);
104 cp.scan_window = cpu_to_le16(BT_LE_SCAN_WINDOW_DEF);
105 cp.conn_interval_min = cpu_to_le16(BT_LE_CONN_INTERVAL_MIN_DEF);
106 cp.conn_interval_max = cpu_to_le16(BT_LE_CONN_INTERVAL_MAX_DEF);
107 cp.conn_latency = cpu_to_le16(BT_LE_LATENCY_DEF);
108 le->conn_timeout = 5;
109 }
110 bacpy(&cp.peer_addr, &le->dst);
111 cp.peer_addr_type = le->dst_type;
Ville Tervofcd89c02011-02-10 22:38:47 -0300112
113 hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
Brian Gixa94b6122012-02-23 16:07:10 -0800114
115 return le;
Ville Tervofcd89c02011-02-10 22:38:47 -0300116}
Brian Gixa94b6122012-02-23 16:07:10 -0800117EXPORT_SYMBOL(hci_le_connect);
Ville Tervofcd89c02011-02-10 22:38:47 -0300118
119static void hci_le_connect_cancel(struct hci_conn *conn)
120{
121 hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
122}
123
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200124void hci_acl_connect(struct hci_conn *conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct hci_dev *hdev = conn->hdev;
127 struct inquiry_entry *ie;
128 struct hci_cp_create_conn cp;
129
130 BT_DBG("%p", conn);
131
132 conn->state = BT_CONNECT;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200133 conn->out = 1;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 conn->link_mode = HCI_LM_MASTER;
136
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200137 conn->attempt++;
138
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200139 conn->link_policy = hdev->link_policy;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 memset(&cp, 0, sizeof(cp));
142 bacpy(&cp.bdaddr, &conn->dst);
143 cp.pscan_rep_mode = 0x02;
144
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200145 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
146 if (ie) {
Marcel Holtmann41a96212008-07-14 20:13:48 +0200147 if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) {
148 cp.pscan_rep_mode = ie->data.pscan_rep_mode;
149 cp.pscan_mode = ie->data.pscan_mode;
150 cp.clock_offset = ie->data.clock_offset |
151 cpu_to_le16(0x8000);
152 }
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 memcpy(conn->dev_class, ie->data.dev_class, 3);
Marcel Holtmann41a96212008-07-14 20:13:48 +0200155 conn->ssp_mode = ie->data.ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
Marcel Holtmanna8746412008-07-14 20:13:46 +0200158 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER))
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200160 cp.role_switch = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 else
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200162 cp.role_switch = 0x00;
Marcel Holtmann4c67bc72006-10-15 17:30:56 +0200163
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200164 hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200167static void hci_acl_connect_cancel(struct hci_conn *conn)
168{
169 struct hci_cp_create_conn_cancel cp;
170
171 BT_DBG("%p", conn);
172
173 if (conn->hdev->hci_ver < 2)
174 return;
175
176 bacpy(&cp.bdaddr, &conn->dst);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200177 hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180void hci_acl_disconn(struct hci_conn *conn, __u8 reason)
181{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 BT_DBG("%p", conn);
183
184 conn->state = BT_DISCONN;
185
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 if (conn->hdev->dev_type == HCI_BREDR) {
187 struct hci_cp_disconnect cp;
188 cp.handle = cpu_to_le16(conn->handle);
189 cp.reason = reason;
190 hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp);
191 } else {
192 struct hci_cp_disconn_phys_link cp;
193 cp.phy_handle = (u8) conn->handle;
194 cp.reason = reason;
195 hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHYS_LINK,
196 sizeof(cp), &cp);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200void hci_add_sco(struct hci_conn *conn, __u16 handle)
201{
202 struct hci_dev *hdev = conn->hdev;
203 struct hci_cp_add_sco cp;
204
205 BT_DBG("%p", conn);
206
207 conn->state = BT_CONNECT;
208 conn->out = 1;
209
Marcel Holtmannefc76882009-02-06 09:13:37 +0100210 conn->attempt++;
211
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700212 cp.handle = cpu_to_le16(handle);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200213 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200215 hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200218void hci_setup_sync(struct hci_conn *conn, __u16 handle)
219{
220 struct hci_dev *hdev = conn->hdev;
221 struct hci_cp_setup_sync_conn cp;
222
223 BT_DBG("%p", conn);
224
225 conn->state = BT_CONNECT;
226 conn->out = 1;
227
Marcel Holtmannefc76882009-02-06 09:13:37 +0100228 conn->attempt++;
229
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200230 cp.handle = cpu_to_le16(handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200231
232 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
233 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
Kun Han Kim15b911f2011-07-08 09:30:28 -0700234 if (conn->hdev->is_wbs) {
235 /* Transparent Data */
236 uint16_t voice_setting = hdev->voice_setting | ACF_TRANS;
237 cp.max_latency = cpu_to_le16(0x000D);
238 cp.pkt_type = cpu_to_le16(ESCO_WBS);
239 cp.voice_setting = cpu_to_le16(voice_setting);
240 /* Retransmission Effort */
241 cp.retrans_effort = RE_LINK_QUALITY;
242 } else {
243 cp.max_latency = cpu_to_le16(0x000A);
244 cp.pkt_type = cpu_to_le16(conn->pkt_type);
245 cp.voice_setting = cpu_to_le16(hdev->voice_setting);
246 cp.retrans_effort = RE_POWER_CONSUMP;
247 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200248
249 hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp);
250}
251
Claudio Takahasi2ce603e2011-02-16 20:44:53 -0200252void hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max,
253 u16 latency, u16 to_multiplier)
254{
255 struct hci_cp_le_conn_update cp;
256 struct hci_dev *hdev = conn->hdev;
257
258 memset(&cp, 0, sizeof(cp));
259
260 cp.handle = cpu_to_le16(conn->handle);
261 cp.conn_interval_min = cpu_to_le16(min);
262 cp.conn_interval_max = cpu_to_le16(max);
263 cp.conn_latency = cpu_to_le16(latency);
264 cp.supervision_timeout = cpu_to_le16(to_multiplier);
265 cp.min_ce_len = cpu_to_le16(0x0001);
266 cp.max_ce_len = cpu_to_le16(0x0001);
267
268 hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp);
269}
270EXPORT_SYMBOL(hci_le_conn_update);
271
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800272void hci_read_rssi(struct hci_conn *conn)
273{
274 struct hci_cp_read_rssi cp;
275 struct hci_dev *hdev = conn->hdev;
276
277 memset(&cp, 0, sizeof(cp));
278 cp.handle = cpu_to_le16(conn->handle);
279
280 hci_send_cmd(hdev, HCI_OP_READ_RSSI, sizeof(cp), &cp);
281}
282EXPORT_SYMBOL(hci_read_rssi);
283
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300284void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __u8 rand[8],
285 __u8 ltk[16])
286{
287 struct hci_dev *hdev = conn->hdev;
288 struct hci_cp_le_start_enc cp;
289
290 BT_DBG("%p", conn);
291
292 memset(&cp, 0, sizeof(cp));
293
294 cp.handle = cpu_to_le16(conn->handle);
295 memcpy(cp.ltk, ltk, sizeof(cp.ltk));
296 cp.ediv = ediv;
Brian Gix842bc5e2011-09-07 08:13:41 -0700297 memcpy(cp.rand, rand, sizeof(cp.rand));
Vinicius Costa Gomes735038c2011-06-09 18:50:47 -0300298
299 hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp);
300}
301EXPORT_SYMBOL(hci_le_start_enc);
302
303void hci_le_ltk_reply(struct hci_conn *conn, u8 ltk[16])
304{
305 struct hci_dev *hdev = conn->hdev;
306 struct hci_cp_le_ltk_reply cp;
307
308 BT_DBG("%p", conn);
309
310 memset(&cp, 0, sizeof(cp));
311
312 cp.handle = cpu_to_le16(conn->handle);
313 memcpy(cp.ltk, ltk, sizeof(ltk));
314
315 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
316}
317EXPORT_SYMBOL(hci_le_ltk_reply);
318
319void hci_le_ltk_neg_reply(struct hci_conn *conn)
320{
321 struct hci_dev *hdev = conn->hdev;
322 struct hci_cp_le_ltk_neg_reply cp;
323
324 BT_DBG("%p", conn);
325
326 memset(&cp, 0, sizeof(cp));
327
328 cp.handle = cpu_to_le16(conn->handle);
329
330 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(cp), &cp);
331}
332
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400333/* Device _must_ be locked */
334void hci_sco_setup(struct hci_conn *conn, __u8 status)
335{
336 struct hci_conn *sco = conn->link;
337
338 BT_DBG("%p", conn);
339
340 if (!sco)
341 return;
342
343 if (!status) {
344 if (lmp_esco_capable(conn->hdev))
345 hci_setup_sync(sco, conn->handle);
346 else
347 hci_add_sco(sco, conn->handle);
348 } else {
349 hci_proto_connect_cfm(sco, status);
350 hci_conn_del(sco);
351 }
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static void hci_conn_timeout(unsigned long arg)
355{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200356 struct hci_conn *conn = (void *) arg;
357 struct hci_dev *hdev = conn->hdev;
Marcel Holtmann2950f212009-02-12 14:02:50 +0100358 __u8 reason;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 BT_DBG("conn %p state %d", conn, conn->state);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 hci_dev_lock(hdev);
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200363
364 switch (conn->state) {
365 case BT_CONNECT:
Marcel Holtmann769be972008-07-14 20:13:49 +0200366 case BT_CONNECT2:
Ville Tervofcd89c02011-02-10 22:38:47 -0300367 if (conn->out) {
368 if (conn->type == ACL_LINK)
369 hci_acl_connect_cancel(conn);
370 else if (conn->type == LE_LINK)
371 hci_le_connect_cancel(conn);
372 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200373 break;
Marcel Holtmann769be972008-07-14 20:13:49 +0200374 case BT_CONFIG:
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900375 case BT_CONNECTED:
Brian Gix114f3a62011-09-27 14:02:20 -0700376 if (!atomic_read(&conn->refcnt)) {
377 reason = hci_proto_disconn_ind(conn);
378 hci_acl_disconn(conn, reason);
379 }
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200380 break;
381 default:
Brian Gix114f3a62011-09-27 14:02:20 -0700382 if (!atomic_read(&conn->refcnt))
383 conn->state = BT_CLOSED;
Marcel Holtmann6ac59342006-09-26 09:43:48 +0200384 break;
385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Marcel Holtmann04837f62006-07-03 10:02:33 +0200390static void hci_conn_idle(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Marcel Holtmann04837f62006-07-03 10:02:33 +0200392 struct hci_conn *conn = (void *) arg;
393
394 BT_DBG("conn %p mode %d", conn, conn->mode);
395
396 hci_conn_enter_sniff_mode(conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800399static void hci_conn_rssi_update(struct work_struct *work)
400{
401 struct delayed_work *delayed =
402 container_of(work, struct delayed_work, work);
403 struct hci_conn *conn =
404 container_of(delayed, struct hci_conn, rssi_update_work);
405
406 BT_DBG("conn %p mode %d", conn, conn->mode);
407
408 hci_read_rssi(conn);
409}
410
Prabhakaran Mcafface82012-04-10 11:38:35 +0530411static void encryption_disabled_timeout(unsigned long userdata)
412{
413 struct hci_conn *conn = (struct hci_conn *)userdata;
414 BT_INFO("conn %p Grace Prd Exp ", conn);
415
416 hci_encrypt_cfm(conn, 0, 0);
417
418 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
419 struct hci_cp_set_conn_encrypt cp;
420 BT_INFO("HCI_CONN_ENCRYPT_PEND is set");
421 cp.handle = cpu_to_le16(conn->handle);
422 cp.encrypt = 1;
423 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
424 sizeof(cp), &cp);
425 }
426
427}
428
Nick Pellybbcda3b2010-02-11 11:54:28 -0800429struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type,
430 __u16 pkt_type, bdaddr_t *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 struct hci_conn *conn;
433
434 BT_DBG("%s dst %s", hdev->name, batostr(dst));
435
Marcel Holtmann04837f62006-07-03 10:02:33 +0200436 conn = kzalloc(sizeof(struct hci_conn), GFP_ATOMIC);
437 if (!conn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 bacpy(&conn->dst, dst);
Marcel Holtmanna8746412008-07-14 20:13:46 +0200441 conn->hdev = hdev;
442 conn->type = type;
443 conn->mode = HCI_CM_ACTIVE;
444 conn->state = BT_OPEN;
Andrei Emeltchenko93f19c92009-09-03 12:34:19 +0300445 conn->auth_type = HCI_AT_GENERAL_BONDING;
Johan Hedberg17fa4b92011-01-25 13:28:33 +0200446 conn->io_capability = hdev->io_capability;
Johan Hedberga9583552011-02-19 12:06:01 -0300447 conn->remote_auth = 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Marcel Holtmann04837f62006-07-03 10:02:33 +0200449 conn->power_save = 1;
Marcel Holtmann052b30b2009-04-26 20:01:22 +0200450 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Srinivas Krovvidi231689b2012-09-03 18:47:50 +0530451 conn->conn_valid = true;
452 spin_lock_init(&conn->lock);
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530453 wake_lock_init(&conn->idle_lock, WAKE_LOCK_SUSPEND, "bt_idle");
Marcel Holtmann04837f62006-07-03 10:02:33 +0200454
Marcel Holtmanna8746412008-07-14 20:13:46 +0200455 switch (type) {
456 case ACL_LINK:
457 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
Rahul Kashyapd5553ba2012-01-02 19:27:19 +0530458 conn->link_policy = hdev->link_policy;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200459 break;
460 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800461 if (!pkt_type)
462 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200463 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800464 if (!pkt_type)
465 pkt_type = ALL_ESCO_MASK;
466 if (lmp_esco_capable(hdev)) {
467 /* HCI Setup Synchronous Connection Command uses
468 reverse logic on the EDR_ESCO_MASK bits */
469 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
470 hdev->esco_type;
471 } else {
472 /* Legacy HCI Add Sco Connection Command uses a
473 shifted bitmask */
474 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
475 SCO_PTYPE_MASK;
476 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200477 break;
478 }
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200481
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800482 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
483 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800484 INIT_DELAYED_WORK(&conn->rssi_update_work, hci_conn_rssi_update);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530485 setup_timer(&conn->encrypt_pause_timer, encryption_disabled_timeout,
486 (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 atomic_set(&conn->refcnt, 0);
489
490 hci_dev_hold(hdev);
491
492 tasklet_disable(&hdev->tx_task);
493
494 hci_conn_hash_add(hdev, conn);
495 if (hdev->notify)
496 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
497
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700498 atomic_set(&conn->devref, 0);
499
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700500 hci_conn_init_sysfs(conn);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 tasklet_enable(&hdev->tx_task);
503
504 return conn;
505}
506
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst,
508 __u8 addr_type)
509{
510 struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst);
511 if (!conn)
512 return NULL;
513
514 conn->dst_type = addr_type;
515
516 return conn;
517}
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519int hci_conn_del(struct hci_conn *conn)
520{
521 struct hci_dev *hdev = conn->hdev;
522
523 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
524
Srinivas Krovvidi231689b2012-09-03 18:47:50 +0530525 spin_lock_bh(&conn->lock);
526 conn->conn_valid = false; /* conn data is being released */
527 spin_unlock_bh(&conn->lock);
528
Brian Gix3cd62042012-01-11 15:18:17 -0800529 /* Make sure no timers are running */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200530 del_timer(&conn->idle_timer);
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530531 wake_lock_destroy(&conn->idle_lock);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200532 del_timer(&conn->disc_timer);
Brian Gix3cd62042012-01-11 15:18:17 -0800533 del_timer(&conn->smp_timer);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800534 __cancel_delayed_work(&conn->rssi_update_work);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530535 del_timer(&conn->encrypt_pause_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200537 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct hci_conn *sco = conn->link;
539 if (sco)
540 sco->link = NULL;
541
542 /* Unacked frames */
543 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300544 } else if (conn->type == LE_LINK) {
545 if (hdev->le_pkts)
546 hdev->le_cnt += conn->sent;
547 else
548 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200549 } else {
550 struct hci_conn *acl = conn->link;
551 if (acl) {
552 acl->link = NULL;
553 hci_conn_put(acl);
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
557 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 hci_conn_hash_del(hdev, conn);
560 if (hdev->notify)
561 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200562
AnubhavGupta01d9e362011-12-17 17:14:51 +0530563 tasklet_schedule(&hdev->tx_task);
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700569 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800570
Marcel Holtmann384943e2009-05-08 18:20:43 -0700571 hci_dev_put(hdev);
572
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 return 0;
574}
575
576struct hci_chan *hci_chan_add(struct hci_dev *hdev)
577{
578 struct hci_chan *chan;
579
580 BT_DBG("%s", hdev->name);
581
582 chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC);
583 if (!chan)
584 return NULL;
585
586 atomic_set(&chan->refcnt, 0);
587
588 hci_dev_hold(hdev);
589
590 chan->hdev = hdev;
591
592 list_add(&chan->list, &hdev->chan_list.list);
593
594 return chan;
595}
Peter Krystada8417e62012-03-21 16:58:17 -0700596EXPORT_SYMBOL(hci_chan_add);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597
598int hci_chan_del(struct hci_chan *chan)
599{
600 BT_DBG("%s chan %p", chan->hdev->name, chan);
601
602 list_del(&chan->list);
603
604 hci_conn_put(chan->conn);
605 hci_dev_put(chan->hdev);
606
607 kfree(chan);
Tomas Targownik1be668d2011-06-30 16:30:44 -0300608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return 0;
610}
611
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800612int hci_chan_put(struct hci_chan *chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700613{
614 struct hci_cp_disconn_logical_link cp;
Mat Martineau9f8d4672011-12-14 12:10:46 -0800615 struct hci_conn *hcon;
616 u16 ll_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617
618 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
619 if (!atomic_dec_and_test(&chan->refcnt))
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800620 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621
Mat Martineau9f8d4672011-12-14 12:10:46 -0800622 hcon = chan->conn;
623 ll_handle = chan->ll_handle;
624
625 hci_chan_del(chan);
626
627 BT_DBG("chan->conn->state %d", hcon->state);
628 if (hcon->state == BT_CONNECTED) {
629 cp.log_handle = cpu_to_le16(ll_handle);
630 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700631 sizeof(cp), &cp);
Mat Martineau9f8d4672011-12-14 12:10:46 -0800632 }
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800633
634 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635}
636EXPORT_SYMBOL(hci_chan_put);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
639{
640 int use_src = bacmp(src, BDADDR_ANY);
641 struct hci_dev *hdev = NULL;
642 struct list_head *p;
643
644 BT_DBG("%s -> %s", batostr(src), batostr(dst));
645
646 read_lock_bh(&hci_dev_list_lock);
647
648 list_for_each(p, &hci_dev_list) {
649 struct hci_dev *d = list_entry(p, struct hci_dev, list);
650
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 if (d->dev_type != HCI_BREDR)
652 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
654 continue;
655
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900656 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * No source address - find interface with bdaddr != dst
658 * Source address - find interface with bdaddr == src
659 */
660
661 if (use_src) {
662 if (!bacmp(&d->bdaddr, src)) {
663 hdev = d; break;
664 }
665 } else {
666 if (bacmp(&d->bdaddr, dst)) {
667 hdev = d; break;
668 }
669 }
670 }
671
672 if (hdev)
673 hdev = hci_dev_hold(hdev);
674
675 read_unlock_bh(&hci_dev_list_lock);
676 return hdev;
677}
678EXPORT_SYMBOL(hci_get_route);
679
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680struct hci_dev *hci_dev_get_type(u8 amp_type)
681{
682 struct hci_dev *hdev = NULL;
683 struct hci_dev *d;
684
685 BT_DBG("amp_type %d", amp_type);
686
687 read_lock_bh(&hci_dev_list_lock);
688
689 list_for_each_entry(d, &hci_dev_list, list) {
690 if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) {
691 hdev = d;
692 break;
693 }
694 }
695
696 if (hdev)
697 hdev = hci_dev_hold(hdev);
698
699 read_unlock_bh(&hci_dev_list_lock);
700 return hdev;
701}
702EXPORT_SYMBOL(hci_dev_get_type);
703
704struct hci_dev *hci_dev_get_amp(bdaddr_t *dst)
705{
706 struct hci_dev *d;
707 struct hci_dev *hdev = NULL;
708
709 BT_DBG("%s dst %s", hdev->name, batostr(dst));
710
711 read_lock_bh(&hci_dev_list_lock);
712
713 list_for_each_entry(d, &hci_dev_list, list) {
714 struct hci_conn *conn;
715 if (d->dev_type == HCI_BREDR)
716 continue;
717 conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst);
718 if (conn) {
719 hdev = d;
720 break;
721 }
722 }
723
724 if (hdev)
725 hdev = hci_dev_hold(hdev);
726
727 read_unlock_bh(&hci_dev_list_lock);
728 return hdev;
729}
730EXPORT_SYMBOL(hci_dev_get_amp);
731
Ville Tervofcd89c02011-02-10 22:38:47 -0300732/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800734struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
735 __u16 pkt_type, bdaddr_t *dst,
736 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200739 struct hci_conn *sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 BT_DBG("%s dst %s", hdev->name, batostr(dst));
742
Brian Gixa94b6122012-02-23 16:07:10 -0800743 if (type == LE_LINK)
744 return hci_le_connect(hdev, pkt_type, dst, sec_level,
745 auth_type, NULL);
Ville Tervofcd89c02011-02-10 22:38:47 -0300746
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200747 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
748 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800749 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200750 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return NULL;
752 }
753
754 hci_conn_hold(acl);
755
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200756 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530757 acl->sec_level = BT_SECURITY_LOW;
758 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200759 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200763 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200765
Srinivas Krovvidi57fb9a32012-08-02 13:23:53 +0530766 /* type of connection already existing can be ESCO or SCO
767 * so check for both types before creating new */
768
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200769 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
Srinivas Krovvidi57fb9a32012-08-02 13:23:53 +0530770
771 if (!sco && type == ESCO_LINK) {
772 sco = hci_conn_hash_lookup_ba(hdev, SCO_LINK, dst);
773 } else if (!sco && type == SCO_LINK) {
774 /* this case can be practically not possible */
775 sco = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, dst);
776 }
777
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200778 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800779 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200780 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200781 hci_conn_put(acl);
782 return NULL;
783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200785
786 acl->link = sco;
787 sco->link = acl;
788
789 hci_conn_hold(sco);
790
791 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200792 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800793 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800795
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400796 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
797 /* defer SCO setup until mode change completed */
798 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
799 return sco;
800 }
801
802 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200803 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200804
805 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807EXPORT_SYMBOL(hci_connect);
808
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700809void hci_disconnect(struct hci_conn *conn, __u8 reason)
810{
811 BT_DBG("conn %p", conn);
812
Mat Martineau3b9239a2012-02-16 11:54:30 -0800813 hci_proto_disconn_cfm(conn, reason, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814}
815EXPORT_SYMBOL(hci_disconnect);
816
817void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
818{
819 struct hci_dev *hdev = NULL;
820
821 BT_DBG("conn %p", conn);
822
823 read_lock_bh(&hci_dev_list_lock);
824
825 list_for_each_entry(hdev, &hci_dev_list, list) {
826 struct hci_conn *c;
827 if (hdev == conn->hdev)
828 continue;
829 if (hdev->amp_type == HCI_BREDR)
830 continue;
831 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
832 if (c)
833 hci_disconnect(c, reason);
834 }
835
836 read_unlock_bh(&hci_dev_list_lock);
837}
838
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200839/* Check link security requirement */
840int hci_conn_check_link_mode(struct hci_conn *conn)
841{
842 BT_DBG("conn %p", conn);
843
844 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
845 !(conn->link_mode & HCI_LM_ENCRYPT))
846 return 0;
847
848 return 1;
849}
850EXPORT_SYMBOL(hci_conn_check_link_mode);
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100853static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 BT_DBG("conn %p", conn);
856
Johan Hedberg765c2a92011-01-19 12:06:52 +0530857 if (conn->pending_sec_level > sec_level)
858 sec_level = conn->pending_sec_level;
859
Marcel Holtmann96a31832009-02-12 16:23:03 +0100860 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530861 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100862 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return 1;
864
Johan Hedberg65cf6862011-01-19 12:06:49 +0530865 /* Make sure we preserve an existing MITM requirement*/
866 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100867 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700868 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
871 struct hci_cp_auth_requested cp;
Peter Hurleya5e5b082012-01-13 15:11:30 +0100872
873 /* encrypt must be pending if auth is also pending */
874 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
875
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700876 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200877 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
878 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return 0;
882}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100884/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100885int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Brian Gixa68668b2011-08-11 15:49:36 -0700887 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100889 if (sec_level == BT_SECURITY_SDP)
890 return 1;
891
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700892 if (sec_level == BT_SECURITY_LOW &&
893 (!conn->ssp_mode || !conn->hdev->ssp_mode))
894 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100895
Brian Gix2e2f50d2011-09-13 12:36:04 -0700896 if (conn->type == LE_LINK) {
897 if (conn->pending_sec_level > sec_level)
898 sec_level = conn->pending_sec_level;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200899
Brian Gix2e2f50d2011-09-13 12:36:04 -0700900 if (sec_level > conn->sec_level)
901 conn->pending_sec_level = sec_level;
902 hci_proto_connect_cfm(conn, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700903 return 0;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700904 } else if (conn->link_mode & HCI_LM_ENCRYPT) {
905 return hci_conn_auth(conn, sec_level, auth_type);
Ilia Kolomisnkyedc44dd2011-06-15 06:52:26 +0300906 } else if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
Brian Gix2e2f50d2011-09-13 12:36:04 -0700907 return 0;
908 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909
910 if (hci_conn_auth(conn, sec_level, auth_type)) {
911 struct hci_cp_set_conn_encrypt cp;
Prabhakaran Mcafface82012-04-10 11:38:35 +0530912 if (timer_pending(&conn->encrypt_pause_timer)) {
913 BT_INFO("encrypt_pause_timer is pending");
914 return 0;
915 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916 cp.handle = cpu_to_le16(conn->handle);
917 cp.encrypt = 1;
918 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
919 sizeof(cp), &cp);
920 }
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 return 0;
923}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100924EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926/* Change link key */
927int hci_conn_change_link_key(struct hci_conn *conn)
928{
929 BT_DBG("conn %p", conn);
930
931 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
932 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700933 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200934 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
935 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return 0;
939}
940EXPORT_SYMBOL(hci_conn_change_link_key);
941
942/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100943int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 BT_DBG("conn %p", conn);
946
947 if (!role && conn->link_mode & HCI_LM_MASTER)
948 return 1;
949
950 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
951 struct hci_cp_switch_role cp;
952 bacpy(&cp.bdaddr, &conn->dst);
953 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200954 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return 0;
958}
959EXPORT_SYMBOL(hci_conn_switch_role);
960
Marcel Holtmann04837f62006-07-03 10:02:33 +0200961/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700962void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200963{
964 struct hci_dev *hdev = conn->hdev;
965
966 BT_DBG("conn %p mode %d", conn, conn->mode);
967
968 if (test_bit(HCI_RAW, &hdev->flags))
969 return;
970
Sunny Kapdi39eaba32012-07-22 21:29:38 -0700971 if (conn->type == LE_LINK)
972 return;
973
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700974 if (conn->mode != HCI_CM_SNIFF)
975 goto timer;
976
977 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200978 goto timer;
979
980 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
981 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700982 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200983 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200984 }
985
986timer:
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530987 if (hdev->idle_timeout > 0) {
Srinivas Krovvidi231689b2012-09-03 18:47:50 +0530988 spin_lock_bh(&conn->lock);
989 if (conn->conn_valid) {
990 mod_timer(&conn->idle_timer,
991 jiffies + msecs_to_jiffies(hdev->idle_timeout));
992 wake_lock(&conn->idle_lock);
993 }
994 spin_unlock_bh(&conn->lock);
Rahul Kashyapb44f9e22012-05-03 16:45:17 +0530995 }
Marcel Holtmann04837f62006-07-03 10:02:33 +0200996}
997
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800998static inline void hci_conn_stop_rssi_timer(struct hci_conn *conn)
999{
1000 BT_DBG("conn %p", conn);
1001 cancel_delayed_work(&conn->rssi_update_work);
1002}
1003
1004static inline void hci_conn_start_rssi_timer(struct hci_conn *conn,
1005 u16 interval)
1006{
1007 struct hci_dev *hdev = conn->hdev;
1008 BT_DBG("conn %p, pending %d", conn,
1009 delayed_work_pending(&conn->rssi_update_work));
1010 if (!delayed_work_pending(&conn->rssi_update_work)) {
1011 queue_delayed_work(hdev->workqueue, &conn->rssi_update_work,
1012 msecs_to_jiffies(interval));
1013 }
1014}
1015
1016void hci_conn_set_rssi_reporter(struct hci_conn *conn,
1017 s8 rssi_threshold, u16 interval, u8 updateOnThreshExceed)
1018{
1019 if (conn) {
1020 conn->rssi_threshold = rssi_threshold;
1021 conn->rssi_update_interval = interval;
1022 conn->rssi_update_thresh_exceed = updateOnThreshExceed;
1023 hci_conn_start_rssi_timer(conn, interval);
1024 }
1025}
1026
1027void hci_conn_unset_rssi_reporter(struct hci_conn *conn)
1028{
1029 if (conn) {
1030 BT_DBG("Deleting the rssi_update_timer");
1031 hci_conn_stop_rssi_timer(conn);
1032 }
1033}
1034
Marcel Holtmann04837f62006-07-03 10:02:33 +02001035/* Enter sniff mode */
1036void hci_conn_enter_sniff_mode(struct hci_conn *conn)
1037{
1038 struct hci_dev *hdev = conn->hdev;
1039
1040 BT_DBG("conn %p mode %d", conn, conn->mode);
1041
1042 if (test_bit(HCI_RAW, &hdev->flags))
1043 return;
1044
Sunny Kapdi39eaba32012-07-22 21:29:38 -07001045 if (conn->type == LE_LINK)
1046 return;
1047
Marcel Holtmann04837f62006-07-03 10:02:33 +02001048 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
1049 return;
1050
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05301051 if (conn->mode != HCI_CM_ACTIVE ||
1052 !(conn->link_policy & HCI_LP_SNIFF) ||
1053 (hci_find_link_key(hdev, &conn->dst) == NULL))
Marcel Holtmann04837f62006-07-03 10:02:33 +02001054 return;
1055
1056 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
1057 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001058 cp.handle = cpu_to_le16(conn->handle);
1059 cp.max_latency = cpu_to_le16(0);
1060 cp.min_remote_timeout = cpu_to_le16(0);
1061 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001062 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001063 }
1064
1065 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1066 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001067 cp.handle = cpu_to_le16(conn->handle);
1068 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
1069 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
1070 cp.attempt = cpu_to_le16(4);
1071 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001072 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001073 }
1074}
1075
Peter Krystada8417e62012-03-21 16:58:17 -07001076struct hci_chan *hci_chan_create(struct hci_chan *chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1078{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001079 struct hci_cp_create_logical_link cp;
1080
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001081 chan->state = BT_CONNECT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 chan->tx_fs = *tx_fs;
1083 chan->rx_fs = *rx_fs;
1084 cp.phy_handle = chan->conn->handle;
1085 cp.tx_fs.id = chan->tx_fs.id;
1086 cp.tx_fs.type = chan->tx_fs.type;
1087 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
1088 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
1089 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
1090 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
1091 cp.rx_fs.id = chan->rx_fs.id;
1092 cp.rx_fs.type = chan->rx_fs.type;
1093 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
1094 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
1095 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
1096 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
1097 hci_conn_hold(chan->conn);
Peter Krystada8417e62012-03-21 16:58:17 -07001098 if (chan->conn->out)
1099 hci_send_cmd(chan->conn->hdev, HCI_OP_CREATE_LOGICAL_LINK,
1100 sizeof(cp), &cp);
1101 else
1102 hci_send_cmd(chan->conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK,
1103 sizeof(cp), &cp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104 return chan;
1105}
1106EXPORT_SYMBOL(hci_chan_create);
1107
1108void hci_chan_modify(struct hci_chan *chan,
1109 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1110{
1111 struct hci_cp_flow_spec_modify cp;
1112
1113 chan->tx_fs = *tx_fs;
1114 chan->rx_fs = *rx_fs;
1115 cp.log_handle = cpu_to_le16(chan->ll_handle);
1116 cp.tx_fs.id = tx_fs->id;
1117 cp.tx_fs.type = tx_fs->type;
1118 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
1119 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
1120 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
1121 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
1122 cp.rx_fs.id = rx_fs->id;
1123 cp.rx_fs.type = rx_fs->type;
1124 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
1125 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
1126 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
1127 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
1128 hci_conn_hold(chan->conn);
1129 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
1130 &cp);
1131}
1132EXPORT_SYMBOL(hci_chan_modify);
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134/* Drop all connection on the device */
Mat Martineau3b9239a2012-02-16 11:54:30 -08001135void hci_conn_hash_flush(struct hci_dev *hdev, u8 is_process)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
1137 struct hci_conn_hash *h = &hdev->conn_hash;
1138 struct list_head *p;
1139
1140 BT_DBG("hdev %s", hdev->name);
1141
1142 p = h->list.next;
1143 while (p != &h->list) {
1144 struct hci_conn *c;
1145
1146 c = list_entry(p, struct hci_conn, list);
1147 p = p->next;
1148
1149 c->state = BT_CLOSED;
1150
Mat Martineau3b9239a2012-02-16 11:54:30 -08001151 hci_proto_disconn_cfm(c, 0x16, is_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 hci_conn_del(c);
1153 }
1154}
1155
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001156/* Check pending connect attempts */
1157void hci_conn_check_pending(struct hci_dev *hdev)
1158{
1159 struct hci_conn *conn;
1160
1161 BT_DBG("hdev %s", hdev->name);
1162
1163 hci_dev_lock(hdev);
1164
1165 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1166 if (conn)
1167 hci_acl_connect(conn);
1168
1169 hci_dev_unlock(hdev);
1170}
1171
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001172void hci_conn_hold_device(struct hci_conn *conn)
1173{
1174 atomic_inc(&conn->devref);
1175}
1176EXPORT_SYMBOL(hci_conn_hold_device);
1177
1178void hci_conn_put_device(struct hci_conn *conn)
1179{
1180 if (atomic_dec_and_test(&conn->devref))
1181 hci_conn_del_sysfs(conn);
1182}
1183EXPORT_SYMBOL(hci_conn_put_device);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185int hci_get_conn_list(void __user *arg)
1186{
1187 struct hci_conn_list_req req, *cl;
1188 struct hci_conn_info *ci;
1189 struct hci_dev *hdev;
1190 struct list_head *p;
1191 int n = 0, size, err;
1192
1193 if (copy_from_user(&req, arg, sizeof(req)))
1194 return -EFAULT;
1195
1196 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1197 return -EINVAL;
1198
1199 size = sizeof(req) + req.conn_num * sizeof(*ci);
1200
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001201 cl = kmalloc(size, GFP_KERNEL);
1202 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -ENOMEM;
1204
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001205 hdev = hci_dev_get(req.dev_id);
1206 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 kfree(cl);
1208 return -ENODEV;
1209 }
1210
1211 ci = cl->conn_info;
1212
1213 hci_dev_lock_bh(hdev);
1214 list_for_each(p, &hdev->conn_hash.list) {
1215 register struct hci_conn *c;
1216 c = list_entry(p, struct hci_conn, list);
1217
1218 bacpy(&(ci + n)->bdaddr, &c->dst);
1219 (ci + n)->handle = c->handle;
1220 (ci + n)->type = c->type;
1221 (ci + n)->out = c->out;
1222 (ci + n)->state = c->state;
1223 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001224 if (c->type == SCO_LINK) {
1225 (ci + n)->mtu = hdev->sco_mtu;
1226 (ci + n)->cnt = hdev->sco_cnt;
1227 (ci + n)->pkts = hdev->sco_pkts;
1228 } else {
1229 (ci + n)->mtu = hdev->acl_mtu;
1230 (ci + n)->cnt = hdev->acl_cnt;
1231 (ci + n)->pkts = hdev->acl_pkts;
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (++n >= req.conn_num)
1234 break;
1235 }
1236 hci_dev_unlock_bh(hdev);
1237
1238 cl->dev_id = hdev->id;
1239 cl->conn_num = n;
1240 size = sizeof(req) + n * sizeof(*ci);
1241
1242 hci_dev_put(hdev);
1243
1244 err = copy_to_user(arg, cl, size);
1245 kfree(cl);
1246
1247 return err ? -EFAULT : 0;
1248}
1249
1250int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1251{
1252 struct hci_conn_info_req req;
1253 struct hci_conn_info ci;
1254 struct hci_conn *conn;
1255 char __user *ptr = arg + sizeof(req);
1256
1257 if (copy_from_user(&req, arg, sizeof(req)))
1258 return -EFAULT;
1259
1260 hci_dev_lock_bh(hdev);
1261 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1262 if (conn) {
1263 bacpy(&ci.bdaddr, &conn->dst);
1264 ci.handle = conn->handle;
1265 ci.type = conn->type;
1266 ci.out = conn->out;
1267 ci.state = conn->state;
1268 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001269 if (req.type == SCO_LINK) {
1270 ci.mtu = hdev->sco_mtu;
1271 ci.cnt = hdev->sco_cnt;
1272 ci.pkts = hdev->sco_pkts;
1273 } else {
1274 ci.mtu = hdev->acl_mtu;
1275 ci.cnt = hdev->acl_cnt;
1276 ci.pkts = hdev->acl_pkts;
1277 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278 ci.pending_sec_level = conn->pending_sec_level;
1279 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281 hci_dev_unlock_bh(hdev);
1282
1283 if (!conn)
1284 return -ENOENT;
1285
1286 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1287}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001288
1289int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1290{
1291 struct hci_auth_info_req req;
1292 struct hci_conn *conn;
1293
1294 if (copy_from_user(&req, arg, sizeof(req)))
1295 return -EFAULT;
1296
1297 hci_dev_lock_bh(hdev);
1298 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1299 if (conn)
1300 req.type = conn->auth_type;
1301 hci_dev_unlock_bh(hdev);
1302
1303 if (!conn)
1304 return -ENOENT;
1305
1306 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1307}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001308
1309int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1310{
1311 struct hci_auth_info_req req;
1312 struct hci_conn *conn;
1313
1314 if (copy_from_user(&req, arg, sizeof(req)))
1315 return -EFAULT;
1316
1317 hci_dev_lock_bh(hdev);
1318 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1319 if (conn)
1320 conn->auth_type = req.type;
1321 hci_dev_unlock_bh(hdev);
1322
1323 if (!conn)
1324 return -ENOENT;
1325
1326 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1327}