blob: 25b559b19bd1a7ae6946920767af3887e3cfddff [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;
Marcel Holtmann04837f62006-07-03 10:02:33 +0200451
Marcel Holtmanna8746412008-07-14 20:13:46 +0200452 switch (type) {
453 case ACL_LINK:
454 conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK;
Rahul Kashyapd5553ba2012-01-02 19:27:19 +0530455 conn->link_policy = hdev->link_policy;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200456 break;
457 case SCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800458 if (!pkt_type)
459 pkt_type = SCO_ESCO_MASK;
Marcel Holtmanna8746412008-07-14 20:13:46 +0200460 case ESCO_LINK:
Nick Pellybbcda3b2010-02-11 11:54:28 -0800461 if (!pkt_type)
462 pkt_type = ALL_ESCO_MASK;
463 if (lmp_esco_capable(hdev)) {
464 /* HCI Setup Synchronous Connection Command uses
465 reverse logic on the EDR_ESCO_MASK bits */
466 conn->pkt_type = (pkt_type ^ EDR_ESCO_MASK) &
467 hdev->esco_type;
468 } else {
469 /* Legacy HCI Add Sco Connection Command uses a
470 shifted bitmask */
471 conn->pkt_type = (pkt_type << 5) & hdev->pkt_type &
472 SCO_PTYPE_MASK;
473 }
Marcel Holtmanna8746412008-07-14 20:13:46 +0200474 break;
475 }
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 skb_queue_head_init(&conn->data_q);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200478
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -0800479 setup_timer(&conn->disc_timer, hci_conn_timeout, (unsigned long)conn);
480 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800481 INIT_DELAYED_WORK(&conn->rssi_update_work, hci_conn_rssi_update);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530482 setup_timer(&conn->encrypt_pause_timer, encryption_disabled_timeout,
483 (unsigned long)conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 atomic_set(&conn->refcnt, 0);
486
487 hci_dev_hold(hdev);
488
489 tasklet_disable(&hdev->tx_task);
490
491 hci_conn_hash_add(hdev, conn);
492 if (hdev->notify)
493 hdev->notify(hdev, HCI_NOTIFY_CONN_ADD);
494
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700495 atomic_set(&conn->devref, 0);
496
Marcel Holtmanna67e8992009-05-02 18:24:06 -0700497 hci_conn_init_sysfs(conn);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 tasklet_enable(&hdev->tx_task);
500
501 return conn;
502}
503
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700504struct hci_conn *hci_le_conn_add(struct hci_dev *hdev, bdaddr_t *dst,
505 __u8 addr_type)
506{
507 struct hci_conn *conn = hci_conn_add(hdev, LE_LINK, 0, dst);
508 if (!conn)
509 return NULL;
510
511 conn->dst_type = addr_type;
512
513 return conn;
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516int hci_conn_del(struct hci_conn *conn)
517{
518 struct hci_dev *hdev = conn->hdev;
519
520 BT_DBG("%s conn %p handle %d", hdev->name, conn, conn->handle);
521
Brian Gix3cd62042012-01-11 15:18:17 -0800522 /* Make sure no timers are running */
Marcel Holtmann04837f62006-07-03 10:02:33 +0200523 del_timer(&conn->idle_timer);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200524 del_timer(&conn->disc_timer);
Brian Gix3cd62042012-01-11 15:18:17 -0800525 del_timer(&conn->smp_timer);
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800526 __cancel_delayed_work(&conn->rssi_update_work);
Prabhakaran Mcafface82012-04-10 11:38:35 +0530527 del_timer(&conn->encrypt_pause_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200529 if (conn->type == ACL_LINK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct hci_conn *sco = conn->link;
531 if (sco)
532 sco->link = NULL;
533
534 /* Unacked frames */
535 hdev->acl_cnt += conn->sent;
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300536 } else if (conn->type == LE_LINK) {
537 if (hdev->le_pkts)
538 hdev->le_cnt += conn->sent;
539 else
540 hdev->acl_cnt += conn->sent;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200541 } else {
542 struct hci_conn *acl = conn->link;
543 if (acl) {
544 acl->link = NULL;
545 hci_conn_put(acl);
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 tasklet_disable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 hci_conn_hash_del(hdev, conn);
552 if (hdev->notify)
553 hdev->notify(hdev, HCI_NOTIFY_CONN_DEL);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200554
AnubhavGupta01d9e362011-12-17 17:14:51 +0530555 tasklet_schedule(&hdev->tx_task);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 tasklet_enable(&hdev->tx_task);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +0200558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 skb_queue_purge(&conn->data_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Marcel Holtmann9eba32b2009-08-22 14:19:26 -0700561 hci_conn_put_device(conn);
Dave Young2ae9a6b2009-02-21 16:13:34 +0800562
Marcel Holtmann384943e2009-05-08 18:20:43 -0700563 hci_dev_put(hdev);
564
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700565 return 0;
566}
567
568struct hci_chan *hci_chan_add(struct hci_dev *hdev)
569{
570 struct hci_chan *chan;
571
572 BT_DBG("%s", hdev->name);
573
574 chan = kzalloc(sizeof(struct hci_chan), GFP_ATOMIC);
575 if (!chan)
576 return NULL;
577
578 atomic_set(&chan->refcnt, 0);
579
580 hci_dev_hold(hdev);
581
582 chan->hdev = hdev;
583
584 list_add(&chan->list, &hdev->chan_list.list);
585
586 return chan;
587}
Peter Krystada8417e62012-03-21 16:58:17 -0700588EXPORT_SYMBOL(hci_chan_add);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700589
590int hci_chan_del(struct hci_chan *chan)
591{
592 BT_DBG("%s chan %p", chan->hdev->name, chan);
593
594 list_del(&chan->list);
595
596 hci_conn_put(chan->conn);
597 hci_dev_put(chan->hdev);
598
599 kfree(chan);
Tomas Targownik1be668d2011-06-30 16:30:44 -0300600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602}
603
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800604int hci_chan_put(struct hci_chan *chan)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605{
606 struct hci_cp_disconn_logical_link cp;
Mat Martineau9f8d4672011-12-14 12:10:46 -0800607 struct hci_conn *hcon;
608 u16 ll_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609
610 BT_DBG("chan %p refcnt %d", chan, atomic_read(&chan->refcnt));
611 if (!atomic_dec_and_test(&chan->refcnt))
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800612 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700613
Mat Martineau9f8d4672011-12-14 12:10:46 -0800614 hcon = chan->conn;
615 ll_handle = chan->ll_handle;
616
617 hci_chan_del(chan);
618
619 BT_DBG("chan->conn->state %d", hcon->state);
620 if (hcon->state == BT_CONNECTED) {
621 cp.log_handle = cpu_to_le16(ll_handle);
622 hci_send_cmd(hcon->hdev, HCI_OP_DISCONN_LOGICAL_LINK,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623 sizeof(cp), &cp);
Mat Martineau9f8d4672011-12-14 12:10:46 -0800624 }
Peter Krystadd6a9ceb2011-12-01 15:44:54 -0800625
626 return 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627}
628EXPORT_SYMBOL(hci_chan_put);
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
631{
632 int use_src = bacmp(src, BDADDR_ANY);
633 struct hci_dev *hdev = NULL;
634 struct list_head *p;
635
636 BT_DBG("%s -> %s", batostr(src), batostr(dst));
637
638 read_lock_bh(&hci_dev_list_lock);
639
640 list_for_each(p, &hci_dev_list) {
641 struct hci_dev *d = list_entry(p, struct hci_dev, list);
642
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 if (d->dev_type != HCI_BREDR)
644 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!test_bit(HCI_UP, &d->flags) || test_bit(HCI_RAW, &d->flags))
646 continue;
647
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +0900648 /* Simple routing:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * No source address - find interface with bdaddr != dst
650 * Source address - find interface with bdaddr == src
651 */
652
653 if (use_src) {
654 if (!bacmp(&d->bdaddr, src)) {
655 hdev = d; break;
656 }
657 } else {
658 if (bacmp(&d->bdaddr, dst)) {
659 hdev = d; break;
660 }
661 }
662 }
663
664 if (hdev)
665 hdev = hci_dev_hold(hdev);
666
667 read_unlock_bh(&hci_dev_list_lock);
668 return hdev;
669}
670EXPORT_SYMBOL(hci_get_route);
671
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672struct hci_dev *hci_dev_get_type(u8 amp_type)
673{
674 struct hci_dev *hdev = NULL;
675 struct hci_dev *d;
676
677 BT_DBG("amp_type %d", amp_type);
678
679 read_lock_bh(&hci_dev_list_lock);
680
681 list_for_each_entry(d, &hci_dev_list, list) {
682 if ((d->amp_type == amp_type) && test_bit(HCI_UP, &d->flags)) {
683 hdev = d;
684 break;
685 }
686 }
687
688 if (hdev)
689 hdev = hci_dev_hold(hdev);
690
691 read_unlock_bh(&hci_dev_list_lock);
692 return hdev;
693}
694EXPORT_SYMBOL(hci_dev_get_type);
695
696struct hci_dev *hci_dev_get_amp(bdaddr_t *dst)
697{
698 struct hci_dev *d;
699 struct hci_dev *hdev = NULL;
700
701 BT_DBG("%s dst %s", hdev->name, batostr(dst));
702
703 read_lock_bh(&hci_dev_list_lock);
704
705 list_for_each_entry(d, &hci_dev_list, list) {
706 struct hci_conn *conn;
707 if (d->dev_type == HCI_BREDR)
708 continue;
709 conn = hci_conn_hash_lookup_ba(d, ACL_LINK, dst);
710 if (conn) {
711 hdev = d;
712 break;
713 }
714 }
715
716 if (hdev)
717 hdev = hci_dev_hold(hdev);
718
719 read_unlock_bh(&hci_dev_list_lock);
720 return hdev;
721}
722EXPORT_SYMBOL(hci_dev_get_amp);
723
Ville Tervofcd89c02011-02-10 22:38:47 -0300724/* Create SCO, ACL or LE connection.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 * Device _must_ be locked */
Nick Pellybbcda3b2010-02-11 11:54:28 -0800726struct hci_conn *hci_connect(struct hci_dev *hdev, int type,
727 __u16 pkt_type, bdaddr_t *dst,
728 __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 struct hci_conn *acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200731 struct hci_conn *sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 BT_DBG("%s dst %s", hdev->name, batostr(dst));
734
Brian Gixa94b6122012-02-23 16:07:10 -0800735 if (type == LE_LINK)
736 return hci_le_connect(hdev, pkt_type, dst, sec_level,
737 auth_type, NULL);
Ville Tervofcd89c02011-02-10 22:38:47 -0300738
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200739 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
740 if (!acl) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800741 acl = hci_conn_add(hdev, ACL_LINK, 0, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200742 if (!acl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return NULL;
744 }
745
746 hci_conn_hold(acl);
747
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200748 if (acl->state == BT_OPEN || acl->state == BT_CLOSED) {
Johan Hedberg765c2a92011-01-19 12:06:52 +0530749 acl->sec_level = BT_SECURITY_LOW;
750 acl->pending_sec_level = sec_level;
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200751 acl->auth_type = auth_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 hci_acl_connect(acl);
Marcel Holtmann09ab6f42008-09-09 07:19:20 +0200753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200755 if (type == ACL_LINK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return acl;
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200757
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200758 sco = hci_conn_hash_lookup_ba(hdev, type, dst);
759 if (!sco) {
Nick Pellybbcda3b2010-02-11 11:54:28 -0800760 sco = hci_conn_add(hdev, type, pkt_type, dst);
Andrei Emeltchenko70f230202010-12-01 16:58:25 +0200761 if (!sco) {
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200762 hci_conn_put(acl);
763 return NULL;
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200766
767 acl->link = sco;
768 sco->link = acl;
769
770 hci_conn_hold(sco);
771
772 if (acl->state == BT_CONNECTED &&
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200773 (sco->state == BT_OPEN || sco->state == BT_CLOSED)) {
Nick Pellyc3902162009-11-13 14:16:32 -0800774 acl->power_save = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775 hci_conn_enter_active_mode(acl, 1);
Nick Pellyc3902162009-11-13 14:16:32 -0800776
Marcel Holtmanne73439d2010-07-26 10:06:00 -0400777 if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->pend)) {
778 /* defer SCO setup until mode change completed */
779 set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->pend);
780 return sco;
781 }
782
783 hci_sco_setup(acl, 0x00);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +0200784 }
Marcel Holtmann5b7f9902007-07-11 09:51:55 +0200785
786 return sco;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788EXPORT_SYMBOL(hci_connect);
789
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790void hci_disconnect(struct hci_conn *conn, __u8 reason)
791{
792 BT_DBG("conn %p", conn);
793
Mat Martineau3b9239a2012-02-16 11:54:30 -0800794 hci_proto_disconn_cfm(conn, reason, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795}
796EXPORT_SYMBOL(hci_disconnect);
797
798void hci_disconnect_amp(struct hci_conn *conn, __u8 reason)
799{
800 struct hci_dev *hdev = NULL;
801
802 BT_DBG("conn %p", conn);
803
804 read_lock_bh(&hci_dev_list_lock);
805
806 list_for_each_entry(hdev, &hci_dev_list, list) {
807 struct hci_conn *c;
808 if (hdev == conn->hdev)
809 continue;
810 if (hdev->amp_type == HCI_BREDR)
811 continue;
812 c = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &conn->dst);
813 if (c)
814 hci_disconnect(c, reason);
815 }
816
817 read_unlock_bh(&hci_dev_list_lock);
818}
819
Marcel Holtmanne7c29cb2008-09-09 07:19:20 +0200820/* Check link security requirement */
821int hci_conn_check_link_mode(struct hci_conn *conn)
822{
823 BT_DBG("conn %p", conn);
824
825 if (conn->ssp_mode > 0 && conn->hdev->ssp_mode > 0 &&
826 !(conn->link_mode & HCI_LM_ENCRYPT))
827 return 0;
828
829 return 1;
830}
831EXPORT_SYMBOL(hci_conn_check_link_mode);
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833/* Authenticate remote device */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100834static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 BT_DBG("conn %p", conn);
837
Johan Hedberg765c2a92011-01-19 12:06:52 +0530838 if (conn->pending_sec_level > sec_level)
839 sec_level = conn->pending_sec_level;
840
Marcel Holtmann96a31832009-02-12 16:23:03 +0100841 if (sec_level > conn->sec_level)
Johan Hedberg765c2a92011-01-19 12:06:52 +0530842 conn->pending_sec_level = sec_level;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100843 else if (conn->link_mode & HCI_LM_AUTH)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 1;
845
Johan Hedberg65cf6862011-01-19 12:06:49 +0530846 /* Make sure we preserve an existing MITM requirement*/
847 auth_type |= (conn->auth_type & 0x01);
Marcel Holtmann96a31832009-02-12 16:23:03 +0100848 conn->auth_type = auth_type;
Brian Gixa68668b2011-08-11 15:49:36 -0700849 conn->auth_initiator = 1;
Marcel Holtmann96a31832009-02-12 16:23:03 +0100850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
852 struct hci_cp_auth_requested cp;
Peter Hurleya5e5b082012-01-13 15:11:30 +0100853
854 /* encrypt must be pending if auth is also pending */
855 set_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend);
856
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700857 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200858 hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED,
859 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 0;
863}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100865/* Enable security */
Marcel Holtmann0684e5f2009-02-09 02:48:38 +0100866int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Brian Gixa68668b2011-08-11 15:49:36 -0700868 BT_DBG("conn %p %d %d", conn, sec_level, auth_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100870 if (sec_level == BT_SECURITY_SDP)
871 return 1;
872
Marcel Holtmann3fdca1e2009-04-28 09:04:55 -0700873 if (sec_level == BT_SECURITY_LOW &&
874 (!conn->ssp_mode || !conn->hdev->ssp_mode))
875 return 1;
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100876
Brian Gix2e2f50d2011-09-13 12:36:04 -0700877 if (conn->type == LE_LINK) {
878 if (conn->pending_sec_level > sec_level)
879 sec_level = conn->pending_sec_level;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +0200880
Brian Gix2e2f50d2011-09-13 12:36:04 -0700881 if (sec_level > conn->sec_level)
882 conn->pending_sec_level = sec_level;
883 hci_proto_connect_cfm(conn, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884 return 0;
Brian Gix2e2f50d2011-09-13 12:36:04 -0700885 } else if (conn->link_mode & HCI_LM_ENCRYPT) {
886 return hci_conn_auth(conn, sec_level, auth_type);
Ilia Kolomisnkyedc44dd2011-06-15 06:52:26 +0300887 } else if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->pend)) {
Brian Gix2e2f50d2011-09-13 12:36:04 -0700888 return 0;
889 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890
891 if (hci_conn_auth(conn, sec_level, auth_type)) {
892 struct hci_cp_set_conn_encrypt cp;
Prabhakaran Mcafface82012-04-10 11:38:35 +0530893 if (timer_pending(&conn->encrypt_pause_timer)) {
894 BT_INFO("encrypt_pause_timer is pending");
895 return 0;
896 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 cp.handle = cpu_to_le16(conn->handle);
898 cp.encrypt = 1;
899 hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT,
900 sizeof(cp), &cp);
901 }
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
904}
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100905EXPORT_SYMBOL(hci_conn_security);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907/* Change link key */
908int hci_conn_change_link_key(struct hci_conn *conn)
909{
910 BT_DBG("conn %p", conn);
911
912 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->pend)) {
913 struct hci_cp_change_conn_link_key cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700914 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmann40be4922008-07-14 20:13:50 +0200915 hci_send_cmd(conn->hdev, HCI_OP_CHANGE_CONN_LINK_KEY,
916 sizeof(cp), &cp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return 0;
920}
921EXPORT_SYMBOL(hci_conn_change_link_key);
922
923/* Switch role */
Marcel Holtmann8c1b2352009-01-15 21:58:04 +0100924int hci_conn_switch_role(struct hci_conn *conn, __u8 role)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
926 BT_DBG("conn %p", conn);
927
928 if (!role && conn->link_mode & HCI_LM_MASTER)
929 return 1;
930
931 if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->pend)) {
932 struct hci_cp_switch_role cp;
933 bacpy(&cp.bdaddr, &conn->dst);
934 cp.role = role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200935 hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, 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_switch_role);
941
Marcel Holtmann04837f62006-07-03 10:02:33 +0200942/* Enter active mode */
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700943void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200944{
945 struct hci_dev *hdev = conn->hdev;
946
947 BT_DBG("conn %p mode %d", conn, conn->mode);
948
949 if (test_bit(HCI_RAW, &hdev->flags))
950 return;
951
Jaikumar Ganesh514abe62011-05-23 18:06:04 -0700952 if (conn->mode != HCI_CM_SNIFF)
953 goto timer;
954
955 if (!conn->power_save && !force_active)
Marcel Holtmann04837f62006-07-03 10:02:33 +0200956 goto timer;
957
958 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
959 struct hci_cp_exit_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -0700960 cp.handle = cpu_to_le16(conn->handle);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200961 hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +0200962 }
963
964timer:
965 if (hdev->idle_timeout > 0)
966 mod_timer(&conn->idle_timer,
967 jiffies + msecs_to_jiffies(hdev->idle_timeout));
968}
969
Archana Ramachandran26a752b2011-12-20 11:27:40 -0800970static inline void hci_conn_stop_rssi_timer(struct hci_conn *conn)
971{
972 BT_DBG("conn %p", conn);
973 cancel_delayed_work(&conn->rssi_update_work);
974}
975
976static inline void hci_conn_start_rssi_timer(struct hci_conn *conn,
977 u16 interval)
978{
979 struct hci_dev *hdev = conn->hdev;
980 BT_DBG("conn %p, pending %d", conn,
981 delayed_work_pending(&conn->rssi_update_work));
982 if (!delayed_work_pending(&conn->rssi_update_work)) {
983 queue_delayed_work(hdev->workqueue, &conn->rssi_update_work,
984 msecs_to_jiffies(interval));
985 }
986}
987
988void hci_conn_set_rssi_reporter(struct hci_conn *conn,
989 s8 rssi_threshold, u16 interval, u8 updateOnThreshExceed)
990{
991 if (conn) {
992 conn->rssi_threshold = rssi_threshold;
993 conn->rssi_update_interval = interval;
994 conn->rssi_update_thresh_exceed = updateOnThreshExceed;
995 hci_conn_start_rssi_timer(conn, interval);
996 }
997}
998
999void hci_conn_unset_rssi_reporter(struct hci_conn *conn)
1000{
1001 if (conn) {
1002 BT_DBG("Deleting the rssi_update_timer");
1003 hci_conn_stop_rssi_timer(conn);
1004 }
1005}
1006
Marcel Holtmann04837f62006-07-03 10:02:33 +02001007/* Enter sniff mode */
1008void hci_conn_enter_sniff_mode(struct hci_conn *conn)
1009{
1010 struct hci_dev *hdev = conn->hdev;
1011
1012 BT_DBG("conn %p mode %d", conn, conn->mode);
1013
1014 if (test_bit(HCI_RAW, &hdev->flags))
1015 return;
1016
1017 if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn))
1018 return;
1019
Srinivas Krovvidi32ba9352012-01-26 10:48:08 +05301020 if (conn->mode != HCI_CM_ACTIVE ||
1021 !(conn->link_policy & HCI_LP_SNIFF) ||
1022 (hci_find_link_key(hdev, &conn->dst) == NULL))
Marcel Holtmann04837f62006-07-03 10:02:33 +02001023 return;
1024
1025 if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) {
1026 struct hci_cp_sniff_subrate cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001027 cp.handle = cpu_to_le16(conn->handle);
1028 cp.max_latency = cpu_to_le16(0);
1029 cp.min_remote_timeout = cpu_to_le16(0);
1030 cp.min_local_timeout = cpu_to_le16(0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001031 hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001032 }
1033
1034 if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->pend)) {
1035 struct hci_cp_sniff_mode cp;
YOSHIFUJI Hideakiaca31922007-03-25 20:12:50 -07001036 cp.handle = cpu_to_le16(conn->handle);
1037 cp.max_interval = cpu_to_le16(hdev->sniff_max_interval);
1038 cp.min_interval = cpu_to_le16(hdev->sniff_min_interval);
1039 cp.attempt = cpu_to_le16(4);
1040 cp.timeout = cpu_to_le16(1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001041 hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp);
Marcel Holtmann04837f62006-07-03 10:02:33 +02001042 }
1043}
1044
Peter Krystada8417e62012-03-21 16:58:17 -07001045struct hci_chan *hci_chan_create(struct hci_chan *chan,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1047{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 struct hci_cp_create_logical_link cp;
1049
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 chan->state = BT_CONNECT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051 chan->tx_fs = *tx_fs;
1052 chan->rx_fs = *rx_fs;
1053 cp.phy_handle = chan->conn->handle;
1054 cp.tx_fs.id = chan->tx_fs.id;
1055 cp.tx_fs.type = chan->tx_fs.type;
1056 cp.tx_fs.max_sdu = cpu_to_le16(chan->tx_fs.max_sdu);
1057 cp.tx_fs.sdu_arr_time = cpu_to_le32(chan->tx_fs.sdu_arr_time);
1058 cp.tx_fs.acc_latency = cpu_to_le32(chan->tx_fs.acc_latency);
1059 cp.tx_fs.flush_to = cpu_to_le32(chan->tx_fs.flush_to);
1060 cp.rx_fs.id = chan->rx_fs.id;
1061 cp.rx_fs.type = chan->rx_fs.type;
1062 cp.rx_fs.max_sdu = cpu_to_le16(chan->rx_fs.max_sdu);
1063 cp.rx_fs.sdu_arr_time = cpu_to_le32(chan->rx_fs.sdu_arr_time);
1064 cp.rx_fs.acc_latency = cpu_to_le32(chan->rx_fs.acc_latency);
1065 cp.rx_fs.flush_to = cpu_to_le32(chan->rx_fs.flush_to);
1066 hci_conn_hold(chan->conn);
Peter Krystada8417e62012-03-21 16:58:17 -07001067 if (chan->conn->out)
1068 hci_send_cmd(chan->conn->hdev, HCI_OP_CREATE_LOGICAL_LINK,
1069 sizeof(cp), &cp);
1070 else
1071 hci_send_cmd(chan->conn->hdev, HCI_OP_ACCEPT_LOGICAL_LINK,
1072 sizeof(cp), &cp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073 return chan;
1074}
1075EXPORT_SYMBOL(hci_chan_create);
1076
1077void hci_chan_modify(struct hci_chan *chan,
1078 struct hci_ext_fs *tx_fs, struct hci_ext_fs *rx_fs)
1079{
1080 struct hci_cp_flow_spec_modify cp;
1081
1082 chan->tx_fs = *tx_fs;
1083 chan->rx_fs = *rx_fs;
1084 cp.log_handle = cpu_to_le16(chan->ll_handle);
1085 cp.tx_fs.id = tx_fs->id;
1086 cp.tx_fs.type = tx_fs->type;
1087 cp.tx_fs.max_sdu = cpu_to_le16(tx_fs->max_sdu);
1088 cp.tx_fs.sdu_arr_time = cpu_to_le32(tx_fs->sdu_arr_time);
1089 cp.tx_fs.acc_latency = cpu_to_le32(tx_fs->acc_latency);
1090 cp.tx_fs.flush_to = cpu_to_le32(tx_fs->flush_to);
1091 cp.rx_fs.id = rx_fs->id;
1092 cp.rx_fs.type = rx_fs->type;
1093 cp.rx_fs.max_sdu = cpu_to_le16(rx_fs->max_sdu);
1094 cp.rx_fs.sdu_arr_time = cpu_to_le32(rx_fs->sdu_arr_time);
1095 cp.rx_fs.acc_latency = cpu_to_le32(rx_fs->acc_latency);
1096 cp.rx_fs.flush_to = cpu_to_le32(rx_fs->flush_to);
1097 hci_conn_hold(chan->conn);
1098 hci_send_cmd(chan->conn->hdev, HCI_OP_FLOW_SPEC_MODIFY, sizeof(cp),
1099 &cp);
1100}
1101EXPORT_SYMBOL(hci_chan_modify);
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103/* Drop all connection on the device */
Mat Martineau3b9239a2012-02-16 11:54:30 -08001104void hci_conn_hash_flush(struct hci_dev *hdev, u8 is_process)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
1106 struct hci_conn_hash *h = &hdev->conn_hash;
1107 struct list_head *p;
1108
1109 BT_DBG("hdev %s", hdev->name);
1110
1111 p = h->list.next;
1112 while (p != &h->list) {
1113 struct hci_conn *c;
1114
1115 c = list_entry(p, struct hci_conn, list);
1116 p = p->next;
1117
1118 c->state = BT_CLOSED;
1119
Mat Martineau3b9239a2012-02-16 11:54:30 -08001120 hci_proto_disconn_cfm(c, 0x16, is_process);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 hci_conn_del(c);
1122 }
1123}
1124
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001125/* Check pending connect attempts */
1126void hci_conn_check_pending(struct hci_dev *hdev)
1127{
1128 struct hci_conn *conn;
1129
1130 BT_DBG("hdev %s", hdev->name);
1131
1132 hci_dev_lock(hdev);
1133
1134 conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2);
1135 if (conn)
1136 hci_acl_connect(conn);
1137
1138 hci_dev_unlock(hdev);
1139}
1140
Marcel Holtmann9eba32b2009-08-22 14:19:26 -07001141void hci_conn_hold_device(struct hci_conn *conn)
1142{
1143 atomic_inc(&conn->devref);
1144}
1145EXPORT_SYMBOL(hci_conn_hold_device);
1146
1147void hci_conn_put_device(struct hci_conn *conn)
1148{
1149 if (atomic_dec_and_test(&conn->devref))
1150 hci_conn_del_sysfs(conn);
1151}
1152EXPORT_SYMBOL(hci_conn_put_device);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154int hci_get_conn_list(void __user *arg)
1155{
1156 struct hci_conn_list_req req, *cl;
1157 struct hci_conn_info *ci;
1158 struct hci_dev *hdev;
1159 struct list_head *p;
1160 int n = 0, size, err;
1161
1162 if (copy_from_user(&req, arg, sizeof(req)))
1163 return -EFAULT;
1164
1165 if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci))
1166 return -EINVAL;
1167
1168 size = sizeof(req) + req.conn_num * sizeof(*ci);
1169
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001170 cl = kmalloc(size, GFP_KERNEL);
1171 if (!cl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return -ENOMEM;
1173
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02001174 hdev = hci_dev_get(req.dev_id);
1175 if (!hdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 kfree(cl);
1177 return -ENODEV;
1178 }
1179
1180 ci = cl->conn_info;
1181
1182 hci_dev_lock_bh(hdev);
1183 list_for_each(p, &hdev->conn_hash.list) {
1184 register struct hci_conn *c;
1185 c = list_entry(p, struct hci_conn, list);
1186
1187 bacpy(&(ci + n)->bdaddr, &c->dst);
1188 (ci + n)->handle = c->handle;
1189 (ci + n)->type = c->type;
1190 (ci + n)->out = c->out;
1191 (ci + n)->state = c->state;
1192 (ci + n)->link_mode = c->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001193 if (c->type == SCO_LINK) {
1194 (ci + n)->mtu = hdev->sco_mtu;
1195 (ci + n)->cnt = hdev->sco_cnt;
1196 (ci + n)->pkts = hdev->sco_pkts;
1197 } else {
1198 (ci + n)->mtu = hdev->acl_mtu;
1199 (ci + n)->cnt = hdev->acl_cnt;
1200 (ci + n)->pkts = hdev->acl_pkts;
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (++n >= req.conn_num)
1203 break;
1204 }
1205 hci_dev_unlock_bh(hdev);
1206
1207 cl->dev_id = hdev->id;
1208 cl->conn_num = n;
1209 size = sizeof(req) + n * sizeof(*ci);
1210
1211 hci_dev_put(hdev);
1212
1213 err = copy_to_user(arg, cl, size);
1214 kfree(cl);
1215
1216 return err ? -EFAULT : 0;
1217}
1218
1219int hci_get_conn_info(struct hci_dev *hdev, void __user *arg)
1220{
1221 struct hci_conn_info_req req;
1222 struct hci_conn_info ci;
1223 struct hci_conn *conn;
1224 char __user *ptr = arg + sizeof(req);
1225
1226 if (copy_from_user(&req, arg, sizeof(req)))
1227 return -EFAULT;
1228
1229 hci_dev_lock_bh(hdev);
1230 conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr);
1231 if (conn) {
1232 bacpy(&ci.bdaddr, &conn->dst);
1233 ci.handle = conn->handle;
1234 ci.type = conn->type;
1235 ci.out = conn->out;
1236 ci.state = conn->state;
1237 ci.link_mode = conn->link_mode;
Nick Pellyc1728492009-12-09 00:15:41 -08001238 if (req.type == SCO_LINK) {
1239 ci.mtu = hdev->sco_mtu;
1240 ci.cnt = hdev->sco_cnt;
1241 ci.pkts = hdev->sco_pkts;
1242 } else {
1243 ci.mtu = hdev->acl_mtu;
1244 ci.cnt = hdev->acl_cnt;
1245 ci.pkts = hdev->acl_pkts;
1246 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001247 ci.pending_sec_level = conn->pending_sec_level;
1248 ci.ssp_mode = conn->ssp_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250 hci_dev_unlock_bh(hdev);
1251
1252 if (!conn)
1253 return -ENOENT;
1254
1255 return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0;
1256}
Marcel Holtmann40be4922008-07-14 20:13:50 +02001257
1258int hci_get_auth_info(struct hci_dev *hdev, void __user *arg)
1259{
1260 struct hci_auth_info_req req;
1261 struct hci_conn *conn;
1262
1263 if (copy_from_user(&req, arg, sizeof(req)))
1264 return -EFAULT;
1265
1266 hci_dev_lock_bh(hdev);
1267 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1268 if (conn)
1269 req.type = conn->auth_type;
1270 hci_dev_unlock_bh(hdev);
1271
1272 if (!conn)
1273 return -ENOENT;
1274
1275 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1276}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277
1278int hci_set_auth_info(struct hci_dev *hdev, void __user *arg)
1279{
1280 struct hci_auth_info_req req;
1281 struct hci_conn *conn;
1282
1283 if (copy_from_user(&req, arg, sizeof(req)))
1284 return -EFAULT;
1285
1286 hci_dev_lock_bh(hdev);
1287 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr);
1288 if (conn)
1289 conn->auth_type = req.type;
1290 hci_dev_unlock_bh(hdev);
1291
1292 if (!conn)
1293 return -ENOENT;
1294
1295 return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0;
1296}