11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds BlueZ - Bluetooth protocol stack for Linux 32d0a0346SRon Shaffer Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved. 41da177e4SLinus Torvalds 51da177e4SLinus Torvalds Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com> 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify 81da177e4SLinus Torvalds it under the terms of the GNU General Public License version 2 as 91da177e4SLinus Torvalds published by the Free Software Foundation; 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 121da177e4SLinus Torvalds OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 131da177e4SLinus Torvalds FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 141da177e4SLinus Torvalds IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 151da177e4SLinus Torvalds CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 161da177e4SLinus Torvalds WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 171da177e4SLinus Torvalds ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 181da177e4SLinus Torvalds OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 211da177e4SLinus Torvalds COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 221da177e4SLinus Torvalds SOFTWARE IS DISCLAIMED. 231da177e4SLinus Torvalds */ 241da177e4SLinus Torvalds 251da177e4SLinus Torvalds /* Bluetooth HCI connection handling. */ 261da177e4SLinus Torvalds 278c520a59SGustavo Padovan #include <linux/export.h> 2823b9ceb7SMarcel Holtmann #include <linux/debugfs.h> 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h> 311da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h> 324bc58f51SJohan Hedberg #include <net/bluetooth/l2cap.h> 331da177e4SLinus Torvalds 340857dd3bSJohan Hedberg #include "hci_request.h" 35ac4b7236SMarcel Holtmann #include "smp.h" 367024728eSMarcel Holtmann #include "a2mp.h" 377024728eSMarcel Holtmann 382dea632fSFrédéric Dalleau struct sco_param { 392dea632fSFrédéric Dalleau u16 pkt_type; 402dea632fSFrédéric Dalleau u16 max_latency; 41c7da5797SJohan Hedberg u8 retrans_effort; 422dea632fSFrédéric Dalleau }; 432dea632fSFrédéric Dalleau 4448e68ff5SBernhard Thaler static const struct sco_param esco_param_cvsd[] = { 45c7da5797SJohan Hedberg { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */ 46c7da5797SJohan Hedberg { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */ 47c7da5797SJohan Hedberg { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */ 48c7da5797SJohan Hedberg { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */ 49c7da5797SJohan Hedberg { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */ 502dea632fSFrédéric Dalleau }; 512dea632fSFrédéric Dalleau 5248e68ff5SBernhard Thaler static const struct sco_param sco_param_cvsd[] = { 53c7da5797SJohan Hedberg { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */ 54c7da5797SJohan Hedberg { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */ 5548e68ff5SBernhard Thaler }; 5648e68ff5SBernhard Thaler 57565766b0SJohan Hedberg static const struct sco_param esco_param_msbc[] = { 58c7da5797SJohan Hedberg { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */ 59c7da5797SJohan Hedberg { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */ 602dea632fSFrédéric Dalleau }; 612dea632fSFrédéric Dalleau 62f75113a2SJakub Pawlowski /* This function requires the caller holds hdev->lock */ 63f75113a2SJakub Pawlowski static void hci_connect_le_scan_cleanup(struct hci_conn *conn) 64f75113a2SJakub Pawlowski { 65f75113a2SJakub Pawlowski struct hci_conn_params *params; 66b5c2b621SJohan Hedberg struct hci_dev *hdev = conn->hdev; 67f75113a2SJakub Pawlowski struct smp_irk *irk; 68f75113a2SJakub Pawlowski bdaddr_t *bdaddr; 69f75113a2SJakub Pawlowski u8 bdaddr_type; 70f75113a2SJakub Pawlowski 71f75113a2SJakub Pawlowski bdaddr = &conn->dst; 72f75113a2SJakub Pawlowski bdaddr_type = conn->dst_type; 73f75113a2SJakub Pawlowski 74f75113a2SJakub Pawlowski /* Check if we need to convert to identity address */ 75b5c2b621SJohan Hedberg irk = hci_get_irk(hdev, bdaddr, bdaddr_type); 76f75113a2SJakub Pawlowski if (irk) { 77f75113a2SJakub Pawlowski bdaddr = &irk->bdaddr; 78f75113a2SJakub Pawlowski bdaddr_type = irk->addr_type; 79f75113a2SJakub Pawlowski } 80f75113a2SJakub Pawlowski 8117bc08f0SJohan Hedberg params = hci_pend_le_action_lookup(&hdev->pend_le_conns, bdaddr, 8217bc08f0SJohan Hedberg bdaddr_type); 8317bc08f0SJohan Hedberg if (!params || !params->explicit_connect) 84f75113a2SJakub Pawlowski return; 85f75113a2SJakub Pawlowski 86f75113a2SJakub Pawlowski /* The connection attempt was doing scan for new RPA, and is 87f75113a2SJakub Pawlowski * in scan phase. If params are not associated with any other 88f75113a2SJakub Pawlowski * autoconnect action, remove them completely. If they are, just unmark 89f75113a2SJakub Pawlowski * them as waiting for connection, by clearing explicit_connect field. 90f75113a2SJakub Pawlowski */ 91f75113a2SJakub Pawlowski params->explicit_connect = false; 929ad3e6ffSJohan Hedberg 939ad3e6ffSJohan Hedberg list_del_init(¶ms->action); 949ad3e6ffSJohan Hedberg 959ad3e6ffSJohan Hedberg switch (params->auto_connect) { 969ad3e6ffSJohan Hedberg case HCI_AUTO_CONN_EXPLICIT: 97b5c2b621SJohan Hedberg hci_conn_params_del(hdev, bdaddr, bdaddr_type); 989ad3e6ffSJohan Hedberg /* return instead of break to avoid duplicate scan update */ 999ad3e6ffSJohan Hedberg return; 1009ad3e6ffSJohan Hedberg case HCI_AUTO_CONN_DIRECT: 1019ad3e6ffSJohan Hedberg case HCI_AUTO_CONN_ALWAYS: 102b5c2b621SJohan Hedberg list_add(¶ms->action, &hdev->pend_le_conns); 1039ad3e6ffSJohan Hedberg break; 1049ad3e6ffSJohan Hedberg case HCI_AUTO_CONN_REPORT: 105b5c2b621SJohan Hedberg list_add(¶ms->action, &hdev->pend_le_reports); 1069ad3e6ffSJohan Hedberg break; 1079ad3e6ffSJohan Hedberg default: 1089ad3e6ffSJohan Hedberg break; 109168b8a25SJakub Pawlowski } 1109ad3e6ffSJohan Hedberg 111b5c2b621SJohan Hedberg hci_update_background_scan(hdev); 112f75113a2SJakub Pawlowski } 113f75113a2SJakub Pawlowski 114b958f9a3SJohan Hedberg static void hci_conn_cleanup(struct hci_conn *conn) 115b958f9a3SJohan Hedberg { 116b958f9a3SJohan Hedberg struct hci_dev *hdev = conn->hdev; 117b958f9a3SJohan Hedberg 118b958f9a3SJohan Hedberg if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags)) 119b958f9a3SJohan Hedberg hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type); 120b958f9a3SJohan Hedberg 121b958f9a3SJohan Hedberg hci_chan_list_flush(conn); 122b958f9a3SJohan Hedberg 123b958f9a3SJohan Hedberg hci_conn_hash_del(hdev, conn); 124b958f9a3SJohan Hedberg 125b958f9a3SJohan Hedberg if (hdev->notify) 126b958f9a3SJohan Hedberg hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); 127b958f9a3SJohan Hedberg 128b958f9a3SJohan Hedberg hci_conn_del_sysfs(conn); 129b958f9a3SJohan Hedberg 130b958f9a3SJohan Hedberg debugfs_remove_recursive(conn->debugfs); 131b958f9a3SJohan Hedberg 132b958f9a3SJohan Hedberg hci_dev_put(hdev); 133b958f9a3SJohan Hedberg 134b958f9a3SJohan Hedberg hci_conn_put(conn); 135b958f9a3SJohan Hedberg } 136b958f9a3SJohan Hedberg 1378ce783dcSJohan Hedberg static void le_scan_cleanup(struct work_struct *work) 1388ce783dcSJohan Hedberg { 1398ce783dcSJohan Hedberg struct hci_conn *conn = container_of(work, struct hci_conn, 1408ce783dcSJohan Hedberg le_scan_cleanup); 1418ce783dcSJohan Hedberg struct hci_dev *hdev = conn->hdev; 1428ce783dcSJohan Hedberg struct hci_conn *c = NULL; 1438ce783dcSJohan Hedberg 1448ce783dcSJohan Hedberg BT_DBG("%s hcon %p", hdev->name, conn); 1458ce783dcSJohan Hedberg 1468ce783dcSJohan Hedberg hci_dev_lock(hdev); 1478ce783dcSJohan Hedberg 1488ce783dcSJohan Hedberg /* Check that the hci_conn is still around */ 1498ce783dcSJohan Hedberg rcu_read_lock(); 1508ce783dcSJohan Hedberg list_for_each_entry_rcu(c, &hdev->conn_hash.list, list) { 1518ce783dcSJohan Hedberg if (c == conn) 1528ce783dcSJohan Hedberg break; 1538ce783dcSJohan Hedberg } 1548ce783dcSJohan Hedberg rcu_read_unlock(); 1558ce783dcSJohan Hedberg 1568ce783dcSJohan Hedberg if (c == conn) { 1578ce783dcSJohan Hedberg hci_connect_le_scan_cleanup(conn); 1588ce783dcSJohan Hedberg hci_conn_cleanup(conn); 1598ce783dcSJohan Hedberg } 1608ce783dcSJohan Hedberg 1618ce783dcSJohan Hedberg hci_dev_unlock(hdev); 1628ce783dcSJohan Hedberg hci_dev_put(hdev); 1638ce783dcSJohan Hedberg hci_conn_put(conn); 1648ce783dcSJohan Hedberg } 1658ce783dcSJohan Hedberg 166f75113a2SJakub Pawlowski static void hci_connect_le_scan_remove(struct hci_conn *conn) 167f75113a2SJakub Pawlowski { 1688ce783dcSJohan Hedberg BT_DBG("%s hcon %p", conn->hdev->name, conn); 169f75113a2SJakub Pawlowski 1708ce783dcSJohan Hedberg /* We can't call hci_conn_del/hci_conn_cleanup here since that 1718ce783dcSJohan Hedberg * could deadlock with another hci_conn_del() call that's holding 1728ce783dcSJohan Hedberg * hci_dev_lock and doing cancel_delayed_work_sync(&conn->disc_work). 1738ce783dcSJohan Hedberg * Instead, grab temporary extra references to the hci_dev and 1748ce783dcSJohan Hedberg * hci_conn and perform the necessary cleanup in a separate work 1758ce783dcSJohan Hedberg * callback. 176b958f9a3SJohan Hedberg */ 1778ce783dcSJohan Hedberg 1788ce783dcSJohan Hedberg hci_dev_hold(conn->hdev); 1798ce783dcSJohan Hedberg hci_conn_get(conn); 1808ce783dcSJohan Hedberg 1810ebc1818SJohan Hedberg /* Even though we hold a reference to the hdev, many other 1820ebc1818SJohan Hedberg * things might get cleaned up meanwhile, including the hdev's 1830ebc1818SJohan Hedberg * own workqueue, so we can't use that for scheduling. 1840ebc1818SJohan Hedberg */ 1858ce783dcSJohan Hedberg schedule_work(&conn->le_scan_cleanup); 186f75113a2SJakub Pawlowski } 187f75113a2SJakub Pawlowski 1881aef8669SVinicius Costa Gomes static void hci_acl_create_connection(struct hci_conn *conn) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds struct hci_dev *hdev = conn->hdev; 1911da177e4SLinus Torvalds struct inquiry_entry *ie; 1921da177e4SLinus Torvalds struct hci_cp_create_conn cp; 1931da177e4SLinus Torvalds 19442d2d87cSAndrei Emeltchenko BT_DBG("hcon %p", conn); 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds conn->state = BT_CONNECT; 197a0c808b3SJohan Hedberg conn->out = true; 19840bef302SJohan Hedberg conn->role = HCI_ROLE_MASTER; 1991da177e4SLinus Torvalds 2004c67bc74SMarcel Holtmann conn->attempt++; 2014c67bc74SMarcel Holtmann 202e4e8e37cSMarcel Holtmann conn->link_policy = hdev->link_policy; 203e4e8e37cSMarcel Holtmann 2041da177e4SLinus Torvalds memset(&cp, 0, sizeof(cp)); 2051da177e4SLinus Torvalds bacpy(&cp.bdaddr, &conn->dst); 2061da177e4SLinus Torvalds cp.pscan_rep_mode = 0x02; 2071da177e4SLinus Torvalds 20870f23020SAndrei Emeltchenko ie = hci_inquiry_cache_lookup(hdev, &conn->dst); 20970f23020SAndrei Emeltchenko if (ie) { 21041a96212SMarcel Holtmann if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { 2111da177e4SLinus Torvalds cp.pscan_rep_mode = ie->data.pscan_rep_mode; 2121da177e4SLinus Torvalds cp.pscan_mode = ie->data.pscan_mode; 21341a96212SMarcel Holtmann cp.clock_offset = ie->data.clock_offset | 214dcf4adbfSJoe Perches cpu_to_le16(0x8000); 21541a96212SMarcel Holtmann } 21641a96212SMarcel Holtmann 2171da177e4SLinus Torvalds memcpy(conn->dev_class, ie->data.dev_class, 3); 21858a681efSJohan Hedberg if (ie->data.ssp_mode > 0) 21958a681efSJohan Hedberg set_bit(HCI_CONN_SSP_ENABLED, &conn->flags); 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 222a8746417SMarcel Holtmann cp.pkt_type = cpu_to_le16(conn->pkt_type); 2231da177e4SLinus Torvalds if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) 2241da177e4SLinus Torvalds cp.role_switch = 0x01; 2251da177e4SLinus Torvalds else 2261da177e4SLinus Torvalds cp.role_switch = 0x00; 2271da177e4SLinus Torvalds 228a9de9248SMarcel Holtmann hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 231e3b679d5SJohan Hedberg int hci_disconnect(struct hci_conn *conn, __u8 reason) 2321da177e4SLinus Torvalds { 23338b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 2341da177e4SLinus Torvalds 235839035a7SJohan Hedberg /* When we are master of an established connection and it enters 236839035a7SJohan Hedberg * the disconnect timeout, then go ahead and try to read the 237839035a7SJohan Hedberg * current clock offset. Processing of the result is done 238839035a7SJohan Hedberg * within the event handling and hci_clock_offset_evt function. 239839035a7SJohan Hedberg */ 24088d07febSJohan Hedberg if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER && 24188d07febSJohan Hedberg (conn->state == BT_CONNECTED || conn->state == BT_CONFIG)) { 242839035a7SJohan Hedberg struct hci_dev *hdev = conn->hdev; 2434f639edeSFabian Frederick struct hci_cp_read_clock_offset clkoff_cp; 244839035a7SJohan Hedberg 2454f639edeSFabian Frederick clkoff_cp.handle = cpu_to_le16(conn->handle); 2464f639edeSFabian Frederick hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp), 2474f639edeSFabian Frederick &clkoff_cp); 248839035a7SJohan Hedberg } 249839035a7SJohan Hedberg 25088d07febSJohan Hedberg return hci_abort_conn(conn, reason); 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 25357f5d0d1SVinicius Costa Gomes static void hci_add_sco(struct hci_conn *conn, __u16 handle) 2541da177e4SLinus Torvalds { 2551da177e4SLinus Torvalds struct hci_dev *hdev = conn->hdev; 2561da177e4SLinus Torvalds struct hci_cp_add_sco cp; 2571da177e4SLinus Torvalds 25838b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds conn->state = BT_CONNECT; 261a0c808b3SJohan Hedberg conn->out = true; 2621da177e4SLinus Torvalds 263efc7688bSMarcel Holtmann conn->attempt++; 264efc7688bSMarcel Holtmann 265aca3192cSYOSHIFUJI Hideaki cp.handle = cpu_to_le16(handle); 266a8746417SMarcel Holtmann cp.pkt_type = cpu_to_le16(conn->pkt_type); 2671da177e4SLinus Torvalds 268a9de9248SMarcel Holtmann hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2712dea632fSFrédéric Dalleau bool hci_setup_sync(struct hci_conn *conn, __u16 handle) 272b6a0dc82SMarcel Holtmann { 273b6a0dc82SMarcel Holtmann struct hci_dev *hdev = conn->hdev; 274b6a0dc82SMarcel Holtmann struct hci_cp_setup_sync_conn cp; 2752dea632fSFrédéric Dalleau const struct sco_param *param; 276b6a0dc82SMarcel Holtmann 27738b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 278b6a0dc82SMarcel Holtmann 279b6a0dc82SMarcel Holtmann conn->state = BT_CONNECT; 280a0c808b3SJohan Hedberg conn->out = true; 281b6a0dc82SMarcel Holtmann 282efc7688bSMarcel Holtmann conn->attempt++; 283efc7688bSMarcel Holtmann 284b6a0dc82SMarcel Holtmann cp.handle = cpu_to_le16(handle); 285b6a0dc82SMarcel Holtmann 286dcf4adbfSJoe Perches cp.tx_bandwidth = cpu_to_le32(0x00001f40); 287dcf4adbfSJoe Perches cp.rx_bandwidth = cpu_to_le32(0x00001f40); 28810c62ddcSFrédéric Dalleau cp.voice_setting = cpu_to_le16(conn->setting); 28910c62ddcSFrédéric Dalleau 29010c62ddcSFrédéric Dalleau switch (conn->setting & SCO_AIRMODE_MASK) { 29110c62ddcSFrédéric Dalleau case SCO_AIRMODE_TRANSP: 292565766b0SJohan Hedberg if (conn->attempt > ARRAY_SIZE(esco_param_msbc)) 2932dea632fSFrédéric Dalleau return false; 294565766b0SJohan Hedberg param = &esco_param_msbc[conn->attempt - 1]; 29510c62ddcSFrédéric Dalleau break; 29610c62ddcSFrédéric Dalleau case SCO_AIRMODE_CVSD: 29748e68ff5SBernhard Thaler if (lmp_esco_capable(conn->link)) { 29848e68ff5SBernhard Thaler if (conn->attempt > ARRAY_SIZE(esco_param_cvsd)) 2992dea632fSFrédéric Dalleau return false; 30048e68ff5SBernhard Thaler param = &esco_param_cvsd[conn->attempt - 1]; 30148e68ff5SBernhard Thaler } else { 30248e68ff5SBernhard Thaler if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) 30348e68ff5SBernhard Thaler return false; 3042dea632fSFrédéric Dalleau param = &sco_param_cvsd[conn->attempt - 1]; 30548e68ff5SBernhard Thaler } 30610c62ddcSFrédéric Dalleau break; 3072dea632fSFrédéric Dalleau default: 3082dea632fSFrédéric Dalleau return false; 30910c62ddcSFrédéric Dalleau } 310b6a0dc82SMarcel Holtmann 311c7da5797SJohan Hedberg cp.retrans_effort = param->retrans_effort; 3122dea632fSFrédéric Dalleau cp.pkt_type = __cpu_to_le16(param->pkt_type); 3132dea632fSFrédéric Dalleau cp.max_latency = __cpu_to_le16(param->max_latency); 3142dea632fSFrédéric Dalleau 3152dea632fSFrédéric Dalleau if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) 3162dea632fSFrédéric Dalleau return false; 3172dea632fSFrédéric Dalleau 3182dea632fSFrédéric Dalleau return true; 319b6a0dc82SMarcel Holtmann } 320b6a0dc82SMarcel Holtmann 3217d6ca693SJohan Hedberg u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency, 3227d6ca693SJohan Hedberg u16 to_multiplier) 3232ce603ebSClaudio Takahasi { 3242ce603ebSClaudio Takahasi struct hci_dev *hdev = conn->hdev; 325f044eb05SMarcel Holtmann struct hci_conn_params *params; 326f044eb05SMarcel Holtmann struct hci_cp_le_conn_update cp; 327f044eb05SMarcel Holtmann 328f044eb05SMarcel Holtmann hci_dev_lock(hdev); 329f044eb05SMarcel Holtmann 330f044eb05SMarcel Holtmann params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); 331f044eb05SMarcel Holtmann if (params) { 332f044eb05SMarcel Holtmann params->conn_min_interval = min; 333f044eb05SMarcel Holtmann params->conn_max_interval = max; 334f044eb05SMarcel Holtmann params->conn_latency = latency; 335f044eb05SMarcel Holtmann params->supervision_timeout = to_multiplier; 336f044eb05SMarcel Holtmann } 337f044eb05SMarcel Holtmann 338f044eb05SMarcel Holtmann hci_dev_unlock(hdev); 3392ce603ebSClaudio Takahasi 3402ce603ebSClaudio Takahasi memset(&cp, 0, sizeof(cp)); 3412ce603ebSClaudio Takahasi cp.handle = cpu_to_le16(conn->handle); 3422ce603ebSClaudio Takahasi cp.conn_interval_min = cpu_to_le16(min); 3432ce603ebSClaudio Takahasi cp.conn_interval_max = cpu_to_le16(max); 3442ce603ebSClaudio Takahasi cp.conn_latency = cpu_to_le16(latency); 3452ce603ebSClaudio Takahasi cp.supervision_timeout = cpu_to_le16(to_multiplier); 346dcf4adbfSJoe Perches cp.min_ce_len = cpu_to_le16(0x0000); 347dcf4adbfSJoe Perches cp.max_ce_len = cpu_to_le16(0x0000); 3482ce603ebSClaudio Takahasi 3492ce603ebSClaudio Takahasi hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); 3507d6ca693SJohan Hedberg 3517d6ca693SJohan Hedberg if (params) 3527d6ca693SJohan Hedberg return 0x01; 3537d6ca693SJohan Hedberg 3547d6ca693SJohan Hedberg return 0x00; 3552ce603ebSClaudio Takahasi } 3562ce603ebSClaudio Takahasi 357fe39c7b2SMarcel Holtmann void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, 3588b76ce34SJohan Hedberg __u8 ltk[16], __u8 key_size) 359a7a595f6SVinicius Costa Gomes { 360a7a595f6SVinicius Costa Gomes struct hci_dev *hdev = conn->hdev; 361a7a595f6SVinicius Costa Gomes struct hci_cp_le_start_enc cp; 362a7a595f6SVinicius Costa Gomes 36338b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 364a7a595f6SVinicius Costa Gomes 365a7a595f6SVinicius Costa Gomes memset(&cp, 0, sizeof(cp)); 366a7a595f6SVinicius Costa Gomes 367a7a595f6SVinicius Costa Gomes cp.handle = cpu_to_le16(conn->handle); 368fe39c7b2SMarcel Holtmann cp.rand = rand; 369a7a595f6SVinicius Costa Gomes cp.ediv = ediv; 3708b76ce34SJohan Hedberg memcpy(cp.ltk, ltk, key_size); 371a7a595f6SVinicius Costa Gomes 372a7a595f6SVinicius Costa Gomes hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); 373a7a595f6SVinicius Costa Gomes } 374a7a595f6SVinicius Costa Gomes 375e73439d8SMarcel Holtmann /* Device _must_ be locked */ 376e73439d8SMarcel Holtmann void hci_sco_setup(struct hci_conn *conn, __u8 status) 377e73439d8SMarcel Holtmann { 378e73439d8SMarcel Holtmann struct hci_conn *sco = conn->link; 379e73439d8SMarcel Holtmann 380e73439d8SMarcel Holtmann if (!sco) 381e73439d8SMarcel Holtmann return; 382e73439d8SMarcel Holtmann 38338b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 38438b3fef1SAndrei Emeltchenko 385e73439d8SMarcel Holtmann if (!status) { 386e73439d8SMarcel Holtmann if (lmp_esco_capable(conn->hdev)) 387e73439d8SMarcel Holtmann hci_setup_sync(sco, conn->handle); 388e73439d8SMarcel Holtmann else 389e73439d8SMarcel Holtmann hci_add_sco(sco, conn->handle); 390e73439d8SMarcel Holtmann } else { 391539c496dSJohan Hedberg hci_connect_cfm(sco, status); 392e73439d8SMarcel Holtmann hci_conn_del(sco); 393e73439d8SMarcel Holtmann } 394e73439d8SMarcel Holtmann } 395e73439d8SMarcel Holtmann 39619c40e3bSGustavo F. Padovan static void hci_conn_timeout(struct work_struct *work) 3971da177e4SLinus Torvalds { 39819c40e3bSGustavo F. Padovan struct hci_conn *conn = container_of(work, struct hci_conn, 39919c40e3bSGustavo F. Padovan disc_work.work); 4001d56dc4fSLukasz Rymanowski int refcnt = atomic_read(&conn->refcnt); 4011da177e4SLinus Torvalds 40238b3fef1SAndrei Emeltchenko BT_DBG("hcon %p state %s", conn, state_to_string(conn->state)); 4031da177e4SLinus Torvalds 4041d56dc4fSLukasz Rymanowski WARN_ON(refcnt < 0); 4051d56dc4fSLukasz Rymanowski 4061d56dc4fSLukasz Rymanowski /* FIXME: It was observed that in pairing failed scenario, refcnt 4071d56dc4fSLukasz Rymanowski * drops below 0. Probably this is because l2cap_conn_del calls 4081d56dc4fSLukasz Rymanowski * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is 4091d56dc4fSLukasz Rymanowski * dropped. After that loop hci_chan_del is called which also drops 4101d56dc4fSLukasz Rymanowski * conn. For now make sure that ACL is alive if refcnt is higher then 0, 4111d56dc4fSLukasz Rymanowski * otherwise drop it. 4121d56dc4fSLukasz Rymanowski */ 4131d56dc4fSLukasz Rymanowski if (refcnt > 0) 4141da177e4SLinus Torvalds return; 4151da177e4SLinus Torvalds 41689e0ccc8SJohan Hedberg /* LE connections in scanning state need special handling */ 41789e0ccc8SJohan Hedberg if (conn->state == BT_CONNECT && conn->type == LE_LINK && 41889e0ccc8SJohan Hedberg test_bit(HCI_CONN_SCANNING, &conn->flags)) { 419cc2b6911SJakub Pawlowski hci_connect_le_scan_remove(conn); 42089e0ccc8SJohan Hedberg return; 421cc2b6911SJakub Pawlowski } 42289e0ccc8SJohan Hedberg 42389e0ccc8SJohan Hedberg hci_abort_conn(conn, hci_proto_disconn_ind(conn)); 4241da177e4SLinus Torvalds } 4251da177e4SLinus Torvalds 426416dc94bSGustavo F. Padovan /* Enter sniff mode */ 427a74a84f6SJohan Hedberg static void hci_conn_idle(struct work_struct *work) 428416dc94bSGustavo F. Padovan { 429a74a84f6SJohan Hedberg struct hci_conn *conn = container_of(work, struct hci_conn, 430a74a84f6SJohan Hedberg idle_work.work); 431416dc94bSGustavo F. Padovan struct hci_dev *hdev = conn->hdev; 432416dc94bSGustavo F. Padovan 43338b3fef1SAndrei Emeltchenko BT_DBG("hcon %p mode %d", conn, conn->mode); 434416dc94bSGustavo F. Padovan 435416dc94bSGustavo F. Padovan if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) 436416dc94bSGustavo F. Padovan return; 437416dc94bSGustavo F. Padovan 438416dc94bSGustavo F. Padovan if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) 439416dc94bSGustavo F. Padovan return; 440416dc94bSGustavo F. Padovan 441416dc94bSGustavo F. Padovan if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { 442416dc94bSGustavo F. Padovan struct hci_cp_sniff_subrate cp; 443416dc94bSGustavo F. Padovan cp.handle = cpu_to_le16(conn->handle); 444dcf4adbfSJoe Perches cp.max_latency = cpu_to_le16(0); 445dcf4adbfSJoe Perches cp.min_remote_timeout = cpu_to_le16(0); 446dcf4adbfSJoe Perches cp.min_local_timeout = cpu_to_le16(0); 447416dc94bSGustavo F. Padovan hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); 448416dc94bSGustavo F. Padovan } 449416dc94bSGustavo F. Padovan 45051a8efd7SJohan Hedberg if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { 451416dc94bSGustavo F. Padovan struct hci_cp_sniff_mode cp; 452416dc94bSGustavo F. Padovan cp.handle = cpu_to_le16(conn->handle); 453416dc94bSGustavo F. Padovan cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); 454416dc94bSGustavo F. Padovan cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); 455dcf4adbfSJoe Perches cp.attempt = cpu_to_le16(4); 456dcf4adbfSJoe Perches cp.timeout = cpu_to_le16(1); 457416dc94bSGustavo F. Padovan hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); 458416dc94bSGustavo F. Padovan } 459416dc94bSGustavo F. Padovan } 460416dc94bSGustavo F. Padovan 4617bc18d9dSJohan Hedberg static void hci_conn_auto_accept(struct work_struct *work) 4629f61656aSJohan Hedberg { 4637bc18d9dSJohan Hedberg struct hci_conn *conn = container_of(work, struct hci_conn, 4647bc18d9dSJohan Hedberg auto_accept_work.work); 4659f61656aSJohan Hedberg 4667bc18d9dSJohan Hedberg hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), 4679f61656aSJohan Hedberg &conn->dst); 4689f61656aSJohan Hedberg } 4699f61656aSJohan Hedberg 4709489eca4SJohan Hedberg static void le_conn_timeout(struct work_struct *work) 4719489eca4SJohan Hedberg { 4729489eca4SJohan Hedberg struct hci_conn *conn = container_of(work, struct hci_conn, 4739489eca4SJohan Hedberg le_conn_timeout.work); 4743c857757SJohan Hedberg struct hci_dev *hdev = conn->hdev; 4759489eca4SJohan Hedberg 4769489eca4SJohan Hedberg BT_DBG(""); 4779489eca4SJohan Hedberg 4783c857757SJohan Hedberg /* We could end up here due to having done directed advertising, 4793c857757SJohan Hedberg * so clean up the state if necessary. This should however only 4803c857757SJohan Hedberg * happen with broken hardware or if low duty cycle was used 4813c857757SJohan Hedberg * (which doesn't have a timeout of its own). 4823c857757SJohan Hedberg */ 4830b1db38cSJohan Hedberg if (conn->role == HCI_ROLE_SLAVE) { 4843c857757SJohan Hedberg u8 enable = 0x00; 4853c857757SJohan Hedberg hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), 4863c857757SJohan Hedberg &enable); 4873c857757SJohan Hedberg hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT); 4883c857757SJohan Hedberg return; 4893c857757SJohan Hedberg } 4903c857757SJohan Hedberg 49189e0ccc8SJohan Hedberg hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); 4929489eca4SJohan Hedberg } 4939489eca4SJohan Hedberg 494a5c4e309SJohan Hedberg struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst, 495a5c4e309SJohan Hedberg u8 role) 4961da177e4SLinus Torvalds { 4971da177e4SLinus Torvalds struct hci_conn *conn; 4981da177e4SLinus Torvalds 4996ed93dc6SAndrei Emeltchenko BT_DBG("%s dst %pMR", hdev->name, dst); 5001da177e4SLinus Torvalds 50127f70f3eSJohan Hedberg conn = kzalloc(sizeof(*conn), GFP_KERNEL); 50204837f64SMarcel Holtmann if (!conn) 5031da177e4SLinus Torvalds return NULL; 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds bacpy(&conn->dst, dst); 506662e8820SMarcel Holtmann bacpy(&conn->src, &hdev->bdaddr); 5071da177e4SLinus Torvalds conn->hdev = hdev; 50804837f64SMarcel Holtmann conn->type = type; 509a5c4e309SJohan Hedberg conn->role = role; 51004837f64SMarcel Holtmann conn->mode = HCI_CM_ACTIVE; 5111da177e4SLinus Torvalds conn->state = BT_OPEN; 51293f19c9fSAndrei Emeltchenko conn->auth_type = HCI_AT_GENERAL_BONDING; 51317fa4b9dSJohan Hedberg conn->io_capability = hdev->io_capability; 514a9583556SJohan Hedberg conn->remote_auth = 0xff; 51513d39315SWaldemar Rymarkiewicz conn->key_type = 0xff; 516ebf86aa3SJohan Hedberg conn->rssi = HCI_RSSI_INVALID; 5175a134faeSAndrzej Kaczmarek conn->tx_power = HCI_TX_POWER_INVALID; 518d0455ed9SAndrzej Kaczmarek conn->max_tx_power = HCI_TX_POWER_INVALID; 5191da177e4SLinus Torvalds 52058a681efSJohan Hedberg set_bit(HCI_CONN_POWER_SAVE, &conn->flags); 521052b30b0SMarcel Holtmann conn->disc_timeout = HCI_DISCONN_TIMEOUT; 52204837f64SMarcel Holtmann 523a5c4e309SJohan Hedberg if (conn->role == HCI_ROLE_MASTER) 524a5c4e309SJohan Hedberg conn->out = true; 525a5c4e309SJohan Hedberg 526a8746417SMarcel Holtmann switch (type) { 527a8746417SMarcel Holtmann case ACL_LINK: 528a8746417SMarcel Holtmann conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; 529a8746417SMarcel Holtmann break; 5309c84d1daSJohan Hedberg case LE_LINK: 5319c84d1daSJohan Hedberg /* conn->src should reflect the local identity address */ 5329c84d1daSJohan Hedberg hci_copy_identity_address(hdev, &conn->src, &conn->src_type); 5339c84d1daSJohan Hedberg break; 534a8746417SMarcel Holtmann case SCO_LINK: 535a8746417SMarcel Holtmann if (lmp_esco_capable(hdev)) 536efc7688bSMarcel Holtmann conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | 537efc7688bSMarcel Holtmann (hdev->esco_type & EDR_ESCO_MASK); 538a8746417SMarcel Holtmann else 539a8746417SMarcel Holtmann conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; 540a8746417SMarcel Holtmann break; 541a8746417SMarcel Holtmann case ESCO_LINK: 542efc7688bSMarcel Holtmann conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; 543a8746417SMarcel Holtmann break; 544a8746417SMarcel Holtmann } 545a8746417SMarcel Holtmann 5461da177e4SLinus Torvalds skb_queue_head_init(&conn->data_q); 54704837f64SMarcel Holtmann 54870c1f20bSMarcel Holtmann INIT_LIST_HEAD(&conn->chan_list); 54973d80debSLuiz Augusto von Dentz 55019c40e3bSGustavo F. Padovan INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout); 5517bc18d9dSJohan Hedberg INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept); 552a74a84f6SJohan Hedberg INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle); 5539489eca4SJohan Hedberg INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout); 5548ce783dcSJohan Hedberg INIT_WORK(&conn->le_scan_cleanup, le_scan_cleanup); 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds atomic_set(&conn->refcnt, 0); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds hci_dev_hold(hdev); 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds hci_conn_hash_add(hdev, conn); 5613c54711cSGustavo F. Padovan if (hdev->notify) 5621da177e4SLinus Torvalds hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); 5631da177e4SLinus Torvalds 564a67e899cSMarcel Holtmann hci_conn_init_sysfs(conn); 565a67e899cSMarcel Holtmann 5661da177e4SLinus Torvalds return conn; 5671da177e4SLinus Torvalds } 5681da177e4SLinus Torvalds 5691da177e4SLinus Torvalds int hci_conn_del(struct hci_conn *conn) 5701da177e4SLinus Torvalds { 5711da177e4SLinus Torvalds struct hci_dev *hdev = conn->hdev; 5721da177e4SLinus Torvalds 57338b3fef1SAndrei Emeltchenko BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle); 5741da177e4SLinus Torvalds 57519c40e3bSGustavo F. Padovan cancel_delayed_work_sync(&conn->disc_work); 5767bc18d9dSJohan Hedberg cancel_delayed_work_sync(&conn->auto_accept_work); 577a74a84f6SJohan Hedberg cancel_delayed_work_sync(&conn->idle_work); 5789f61656aSJohan Hedberg 5795b7f9909SMarcel Holtmann if (conn->type == ACL_LINK) { 5801da177e4SLinus Torvalds struct hci_conn *sco = conn->link; 5811da177e4SLinus Torvalds if (sco) 5821da177e4SLinus Torvalds sco->link = NULL; 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds /* Unacked frames */ 5851da177e4SLinus Torvalds hdev->acl_cnt += conn->sent; 5866ed58ec5SVille Tervo } else if (conn->type == LE_LINK) { 587980ffc0aSJohan Hedberg cancel_delayed_work(&conn->le_conn_timeout); 5889489eca4SJohan Hedberg 5896ed58ec5SVille Tervo if (hdev->le_pkts) 5906ed58ec5SVille Tervo hdev->le_cnt += conn->sent; 5916ed58ec5SVille Tervo else 5926ed58ec5SVille Tervo hdev->acl_cnt += conn->sent; 5935b7f9909SMarcel Holtmann } else { 5945b7f9909SMarcel Holtmann struct hci_conn *acl = conn->link; 5955b7f9909SMarcel Holtmann if (acl) { 5965b7f9909SMarcel Holtmann acl->link = NULL; 59776a68ba0SDavid Herrmann hci_conn_drop(acl); 5985b7f9909SMarcel Holtmann } 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6019740e49dSAndrei Emeltchenko if (conn->amp_mgr) 6029740e49dSAndrei Emeltchenko amp_mgr_put(conn->amp_mgr); 6039740e49dSAndrei Emeltchenko 6041da177e4SLinus Torvalds skb_queue_purge(&conn->data_q); 6051da177e4SLinus Torvalds 606b958f9a3SJohan Hedberg /* Remove the connection from the list and cleanup its remaining 607b958f9a3SJohan Hedberg * state. This is a separate function since for some cases like 608b958f9a3SJohan Hedberg * BT_CONNECT_SCAN we *only* want the cleanup part without the 609b958f9a3SJohan Hedberg * rest of hci_conn_del. 610b958f9a3SJohan Hedberg */ 611b958f9a3SJohan Hedberg hci_conn_cleanup(conn); 612163f4dabSTomas Targownik 6131da177e4SLinus Torvalds return 0; 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds 616*39385cb5SJohan Hedberg struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src, uint8_t src_type) 6171da177e4SLinus Torvalds { 6181da177e4SLinus Torvalds int use_src = bacmp(src, BDADDR_ANY); 6198035ded4SLuiz Augusto von Dentz struct hci_dev *hdev = NULL, *d; 6201da177e4SLinus Torvalds 6216ed93dc6SAndrei Emeltchenko BT_DBG("%pMR -> %pMR", src, dst); 6221da177e4SLinus Torvalds 623f20d09d5SGustavo F. Padovan read_lock(&hci_dev_list_lock); 6241da177e4SLinus Torvalds 6258035ded4SLuiz Augusto von Dentz list_for_each_entry(d, &hci_dev_list, list) { 6268fc9ced3SGustavo Padovan if (!test_bit(HCI_UP, &d->flags) || 627d7a5a11dSMarcel Holtmann hci_dev_test_flag(d, HCI_USER_CHANNEL) || 628ca8bee5dSMarcel Holtmann d->dev_type != HCI_PRIMARY) 6291da177e4SLinus Torvalds continue; 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds /* Simple routing: 6321da177e4SLinus Torvalds * No source address - find interface with bdaddr != dst 6331da177e4SLinus Torvalds * Source address - find interface with bdaddr == src 6341da177e4SLinus Torvalds */ 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds if (use_src) { 637*39385cb5SJohan Hedberg bdaddr_t id_addr; 638*39385cb5SJohan Hedberg u8 id_addr_type; 639*39385cb5SJohan Hedberg 640*39385cb5SJohan Hedberg if (src_type == BDADDR_BREDR) { 641*39385cb5SJohan Hedberg if (!lmp_bredr_capable(d)) 642*39385cb5SJohan Hedberg continue; 643*39385cb5SJohan Hedberg bacpy(&id_addr, &d->bdaddr); 644*39385cb5SJohan Hedberg id_addr_type = BDADDR_BREDR; 645*39385cb5SJohan Hedberg } else { 646*39385cb5SJohan Hedberg if (!lmp_le_capable(d)) 647*39385cb5SJohan Hedberg continue; 648*39385cb5SJohan Hedberg 649*39385cb5SJohan Hedberg hci_copy_identity_address(d, &id_addr, 650*39385cb5SJohan Hedberg &id_addr_type); 651*39385cb5SJohan Hedberg 652*39385cb5SJohan Hedberg /* Convert from HCI to three-value type */ 653*39385cb5SJohan Hedberg if (id_addr_type == ADDR_LE_DEV_PUBLIC) 654*39385cb5SJohan Hedberg id_addr_type = BDADDR_LE_PUBLIC; 655*39385cb5SJohan Hedberg else 656*39385cb5SJohan Hedberg id_addr_type = BDADDR_LE_RANDOM; 657*39385cb5SJohan Hedberg } 658*39385cb5SJohan Hedberg 659*39385cb5SJohan Hedberg if (!bacmp(&id_addr, src) && id_addr_type == src_type) { 6601da177e4SLinus Torvalds hdev = d; break; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds } else { 6631da177e4SLinus Torvalds if (bacmp(&d->bdaddr, dst)) { 6641da177e4SLinus Torvalds hdev = d; break; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds } 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds if (hdev) 6701da177e4SLinus Torvalds hdev = hci_dev_hold(hdev); 6711da177e4SLinus Torvalds 672f20d09d5SGustavo F. Padovan read_unlock(&hci_dev_list_lock); 6731da177e4SLinus Torvalds return hdev; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds EXPORT_SYMBOL(hci_get_route); 6761da177e4SLinus Torvalds 6779bb3c01fSAndre Guedes /* This function requires the caller holds hdev->lock */ 67806c053fbSAndre Guedes void hci_le_conn_failed(struct hci_conn *conn, u8 status) 6799bb3c01fSAndre Guedes { 6809bb3c01fSAndre Guedes struct hci_dev *hdev = conn->hdev; 681f161dd41SJohan Hedberg struct hci_conn_params *params; 682f161dd41SJohan Hedberg 683f161dd41SJohan Hedberg params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst, 684f161dd41SJohan Hedberg conn->dst_type); 685f161dd41SJohan Hedberg if (params && params->conn) { 686f161dd41SJohan Hedberg hci_conn_drop(params->conn); 687f8aaf9b6SJohan Hedberg hci_conn_put(params->conn); 688f161dd41SJohan Hedberg params->conn = NULL; 689f161dd41SJohan Hedberg } 6909bb3c01fSAndre Guedes 6919bb3c01fSAndre Guedes conn->state = BT_CLOSED; 6929bb3c01fSAndre Guedes 693acb9f911SJohan Hedberg /* If the status indicates successful cancellation of 694acb9f911SJohan Hedberg * the attempt (i.e. Unkown Connection Id) there's no point of 695acb9f911SJohan Hedberg * notifying failure since we'll go back to keep trying to 696acb9f911SJohan Hedberg * connect. The only exception is explicit connect requests 697acb9f911SJohan Hedberg * where a timeout + cancel does indicate an actual failure. 698acb9f911SJohan Hedberg */ 699acb9f911SJohan Hedberg if (status != HCI_ERROR_UNKNOWN_CONN_ID || 700acb9f911SJohan Hedberg (params && params->explicit_connect)) 701acb9f911SJohan Hedberg mgmt_connect_failed(hdev, &conn->dst, conn->type, 702acb9f911SJohan Hedberg conn->dst_type, status); 7039bb3c01fSAndre Guedes 704539c496dSJohan Hedberg hci_connect_cfm(conn, status); 7059bb3c01fSAndre Guedes 7069bb3c01fSAndre Guedes hci_conn_del(conn); 707a4790dbdSAndre Guedes 708a4790dbdSAndre Guedes /* Since we may have temporarily stopped the background scanning in 709a4790dbdSAndre Guedes * favor of connection establishment, we should restart it. 710a4790dbdSAndre Guedes */ 711a4790dbdSAndre Guedes hci_update_background_scan(hdev); 7123c857757SJohan Hedberg 7133c857757SJohan Hedberg /* Re-enable advertising in case this was a failed connection 7143c857757SJohan Hedberg * attempt as a peripheral. 7153c857757SJohan Hedberg */ 716f2252570SJohan Hedberg hci_req_reenable_advertising(hdev); 7179bb3c01fSAndre Guedes } 7189bb3c01fSAndre Guedes 7191904a853SMarcel Holtmann static void create_le_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode) 7201d399ae5SAndre Guedes { 7211d399ae5SAndre Guedes struct hci_conn *conn; 7221d399ae5SAndre Guedes 72328a667c9SJakub Pawlowski hci_dev_lock(hdev); 72428a667c9SJakub Pawlowski 72528a667c9SJakub Pawlowski conn = hci_lookup_le_connect(hdev); 72628a667c9SJakub Pawlowski 72728a667c9SJakub Pawlowski if (!status) { 72828a667c9SJakub Pawlowski hci_connect_le_scan_cleanup(conn); 72928a667c9SJakub Pawlowski goto done; 73028a667c9SJakub Pawlowski } 7311d399ae5SAndre Guedes 7321d399ae5SAndre Guedes BT_ERR("HCI request failed to create LE connection: status 0x%2.2x", 7331d399ae5SAndre Guedes status); 7341d399ae5SAndre Guedes 7351d399ae5SAndre Guedes if (!conn) 7361d399ae5SAndre Guedes goto done; 7371d399ae5SAndre Guedes 73806c053fbSAndre Guedes hci_le_conn_failed(conn, status); 7391d399ae5SAndre Guedes 7401d399ae5SAndre Guedes done: 7411d399ae5SAndre Guedes hci_dev_unlock(hdev); 7421d399ae5SAndre Guedes } 7431d399ae5SAndre Guedes 74482a37adeSJohan Hedberg static bool conn_use_rpa(struct hci_conn *conn) 74582a37adeSJohan Hedberg { 74682a37adeSJohan Hedberg struct hci_dev *hdev = conn->hdev; 74782a37adeSJohan Hedberg 74882a37adeSJohan Hedberg return hci_dev_test_flag(hdev, HCI_PRIVACY); 74982a37adeSJohan Hedberg } 75082a37adeSJohan Hedberg 7512acf3d90SAndre Guedes static void hci_req_add_le_create_conn(struct hci_request *req, 7522acf3d90SAndre Guedes struct hci_conn *conn) 7532acf3d90SAndre Guedes { 7542acf3d90SAndre Guedes struct hci_cp_le_create_conn cp; 7552acf3d90SAndre Guedes struct hci_dev *hdev = conn->hdev; 7562acf3d90SAndre Guedes u8 own_addr_type; 7572acf3d90SAndre Guedes 7582acf3d90SAndre Guedes /* Update random address, but set require_privacy to false so 7599437d2edSMarcel Holtmann * that we never connect with an non-resolvable address. 7602acf3d90SAndre Guedes */ 76182a37adeSJohan Hedberg if (hci_update_random_address(req, false, conn_use_rpa(conn), 76282a37adeSJohan Hedberg &own_addr_type)) 7632acf3d90SAndre Guedes return; 7642acf3d90SAndre Guedes 765eec7a01dSJohan Hedberg memset(&cp, 0, sizeof(cp)); 766eec7a01dSJohan Hedberg 7672f99536aSJohan Hedberg /* Set window to be the same value as the interval to enable 7682f99536aSJohan Hedberg * continuous scanning. 7692f99536aSJohan Hedberg */ 7702acf3d90SAndre Guedes cp.scan_interval = cpu_to_le16(hdev->le_scan_interval); 7712f99536aSJohan Hedberg cp.scan_window = cp.scan_interval; 7722f99536aSJohan Hedberg 7732acf3d90SAndre Guedes bacpy(&cp.peer_addr, &conn->dst); 7742acf3d90SAndre Guedes cp.peer_addr_type = conn->dst_type; 7752acf3d90SAndre Guedes cp.own_address_type = own_addr_type; 7762acf3d90SAndre Guedes cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval); 7772acf3d90SAndre Guedes cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval); 778037fc415SMarcel Holtmann cp.conn_latency = cpu_to_le16(conn->le_conn_latency); 779037fc415SMarcel Holtmann cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout); 780dcf4adbfSJoe Perches cp.min_ce_len = cpu_to_le16(0x0000); 781dcf4adbfSJoe Perches cp.max_ce_len = cpu_to_le16(0x0000); 7822acf3d90SAndre Guedes 7832acf3d90SAndre Guedes hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); 784b46e0030SJohan Hedberg 785b46e0030SJohan Hedberg conn->state = BT_CONNECT; 78628a667c9SJakub Pawlowski clear_bit(HCI_CONN_SCANNING, &conn->flags); 7872acf3d90SAndre Guedes } 7882acf3d90SAndre Guedes 7893c857757SJohan Hedberg static void hci_req_directed_advertising(struct hci_request *req, 7903c857757SJohan Hedberg struct hci_conn *conn) 7913c857757SJohan Hedberg { 7923c857757SJohan Hedberg struct hci_dev *hdev = req->hdev; 7933c857757SJohan Hedberg struct hci_cp_le_set_adv_param cp; 7943c857757SJohan Hedberg u8 own_addr_type; 7953c857757SJohan Hedberg u8 enable; 7963c857757SJohan Hedberg 7975ce194c4SJohan Hedberg /* Clear the HCI_LE_ADV bit temporarily so that the 7983c857757SJohan Hedberg * hci_update_random_address knows that it's safe to go ahead 7993c857757SJohan Hedberg * and write a new random address. The flag will be set back on 8003c857757SJohan Hedberg * as soon as the SET_ADV_ENABLE HCI command completes. 8013c857757SJohan Hedberg */ 802a358dc11SMarcel Holtmann hci_dev_clear_flag(hdev, HCI_LE_ADV); 8033c857757SJohan Hedberg 8043c857757SJohan Hedberg /* Set require_privacy to false so that the remote device has a 8053c857757SJohan Hedberg * chance of identifying us. 8063c857757SJohan Hedberg */ 80782a37adeSJohan Hedberg if (hci_update_random_address(req, false, conn_use_rpa(conn), 80882a37adeSJohan Hedberg &own_addr_type) < 0) 8093c857757SJohan Hedberg return; 8103c857757SJohan Hedberg 8113c857757SJohan Hedberg memset(&cp, 0, sizeof(cp)); 8123c857757SJohan Hedberg cp.type = LE_ADV_DIRECT_IND; 8133c857757SJohan Hedberg cp.own_address_type = own_addr_type; 8143c857757SJohan Hedberg cp.direct_addr_type = conn->dst_type; 8153c857757SJohan Hedberg bacpy(&cp.direct_addr, &conn->dst); 8163c857757SJohan Hedberg cp.channel_map = hdev->le_adv_channel_map; 8173c857757SJohan Hedberg 8183c857757SJohan Hedberg hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); 8193c857757SJohan Hedberg 8203c857757SJohan Hedberg enable = 0x01; 8213c857757SJohan Hedberg hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); 8223c857757SJohan Hedberg 8233c857757SJohan Hedberg conn->state = BT_CONNECT; 8243c857757SJohan Hedberg } 8253c857757SJohan Hedberg 82604a6c589SAndre Guedes struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, 827cdd6275eSJohan Hedberg u8 dst_type, u8 sec_level, u16 conn_timeout, 828e804d25dSJohan Hedberg u8 role) 8291da177e4SLinus Torvalds { 8304292f1f3SAndre Guedes struct hci_conn_params *params; 831e2caced4SJohan Hedberg struct hci_conn *conn; 8321ebfcc1fSJohan Hedberg struct smp_irk *irk; 8332acf3d90SAndre Guedes struct hci_request req; 8341d399ae5SAndre Guedes int err; 8351da177e4SLinus Torvalds 836152d386eSLukasz Rymanowski /* Let's make sure that le is enabled.*/ 837d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 838152d386eSLukasz Rymanowski if (lmp_le_capable(hdev)) 839152d386eSLukasz Rymanowski return ERR_PTR(-ECONNREFUSED); 840152d386eSLukasz Rymanowski 841152d386eSLukasz Rymanowski return ERR_PTR(-EOPNOTSUPP); 842152d386eSLukasz Rymanowski } 843152d386eSLukasz Rymanowski 844658aead9SJohan Hedberg /* Since the controller supports only one LE connection attempt at a 845658aead9SJohan Hedberg * time, we return -EBUSY if there is any connection attempt running. 846658aead9SJohan Hedberg */ 847658aead9SJohan Hedberg if (hci_lookup_le_connect(hdev)) 848658aead9SJohan Hedberg return ERR_PTR(-EBUSY); 849658aead9SJohan Hedberg 850e2caced4SJohan Hedberg /* If there's already a connection object but it's not in 851e2caced4SJohan Hedberg * scanning state it means it must already be established, in 852e2caced4SJohan Hedberg * which case we can't do anything else except report a failure 853e2caced4SJohan Hedberg * to connect. 854620ad521SAndre Guedes */ 8559d4c1cc1SJohan Hedberg conn = hci_conn_hash_lookup_le(hdev, dst, dst_type); 856e2caced4SJohan Hedberg if (conn && !test_bit(HCI_CONN_SCANNING, &conn->flags)) { 857e2caced4SJohan Hedberg return ERR_PTR(-EBUSY); 85828a667c9SJakub Pawlowski } 859620ad521SAndre Guedes 860edb4b466SMarcel Holtmann /* When given an identity address with existing identity 861edb4b466SMarcel Holtmann * resolving key, the connection needs to be established 862edb4b466SMarcel Holtmann * to a resolvable random address. 863edb4b466SMarcel Holtmann * 864edb4b466SMarcel Holtmann * Storing the resolvable random address is required here 865edb4b466SMarcel Holtmann * to handle connection failures. The address will later 866edb4b466SMarcel Holtmann * be resolved back into the original identity address 867edb4b466SMarcel Holtmann * from the connect request. 868edb4b466SMarcel Holtmann */ 8691ebfcc1fSJohan Hedberg irk = hci_find_irk_by_addr(hdev, dst, dst_type); 8701ebfcc1fSJohan Hedberg if (irk && bacmp(&irk->rpa, BDADDR_ANY)) { 8711ebfcc1fSJohan Hedberg dst = &irk->rpa; 8721ebfcc1fSJohan Hedberg dst_type = ADDR_LE_DEV_RANDOM; 8731ebfcc1fSJohan Hedberg } 8741ebfcc1fSJohan Hedberg 875e2caced4SJohan Hedberg if (conn) { 87628a667c9SJakub Pawlowski bacpy(&conn->dst, dst); 87728a667c9SJakub Pawlowski } else { 878a5c4e309SJohan Hedberg conn = hci_conn_add(hdev, LE_LINK, dst, role); 879f1e5d547SAndre Guedes if (!conn) 88030e76272SVille Tervo return ERR_PTR(-ENOMEM); 881e2caced4SJohan Hedberg hci_conn_hold(conn); 882e2caced4SJohan Hedberg conn->pending_sec_level = sec_level; 883e2caced4SJohan Hedberg } 884893d6751SAndre Guedes 8851ebfcc1fSJohan Hedberg conn->dst_type = dst_type; 88646a190cbSAndre Guedes conn->sec_level = BT_SECURITY_LOW; 88709ae260bSJohan Hedberg conn->conn_timeout = conn_timeout; 8884292f1f3SAndre Guedes 8893c857757SJohan Hedberg hci_req_init(&req, hdev); 8903c857757SJohan Hedberg 891376f54c1SJohan Hedberg /* Disable advertising if we're active. For master role 892376f54c1SJohan Hedberg * connections most controllers will refuse to connect if 893376f54c1SJohan Hedberg * advertising is enabled, and for slave role connections we 894376f54c1SJohan Hedberg * anyway have to disable it in order to start directed 895376f54c1SJohan Hedberg * advertising. 896376f54c1SJohan Hedberg */ 897d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_LE_ADV)) { 898376f54c1SJohan Hedberg u8 enable = 0x00; 899376f54c1SJohan Hedberg hci_req_add(&req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), 900376f54c1SJohan Hedberg &enable); 901376f54c1SJohan Hedberg } 902376f54c1SJohan Hedberg 903cdd6275eSJohan Hedberg /* If requested to connect as slave use directed advertising */ 904e804d25dSJohan Hedberg if (conn->role == HCI_ROLE_SLAVE) { 905e8bb6b97SJohan Hedberg /* If we're active scanning most controllers are unable 906e8bb6b97SJohan Hedberg * to initiate advertising. Simply reject the attempt. 907e8bb6b97SJohan Hedberg */ 908d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && 909e8bb6b97SJohan Hedberg hdev->le_scan_type == LE_SCAN_ACTIVE) { 910e8bb6b97SJohan Hedberg skb_queue_purge(&req.cmd_q); 911e8bb6b97SJohan Hedberg hci_conn_del(conn); 912e8bb6b97SJohan Hedberg return ERR_PTR(-EBUSY); 913e8bb6b97SJohan Hedberg } 914e8bb6b97SJohan Hedberg 9153c857757SJohan Hedberg hci_req_directed_advertising(&req, conn); 9163c857757SJohan Hedberg goto create_conn; 9173c857757SJohan Hedberg } 9183c857757SJohan Hedberg 9194292f1f3SAndre Guedes params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); 9204292f1f3SAndre Guedes if (params) { 9214292f1f3SAndre Guedes conn->le_conn_min_interval = params->conn_min_interval; 9224292f1f3SAndre Guedes conn->le_conn_max_interval = params->conn_max_interval; 923037fc415SMarcel Holtmann conn->le_conn_latency = params->conn_latency; 924037fc415SMarcel Holtmann conn->le_supv_timeout = params->supervision_timeout; 9254292f1f3SAndre Guedes } else { 9261e406eefSAndre Guedes conn->le_conn_min_interval = hdev->le_conn_min_interval; 9271e406eefSAndre Guedes conn->le_conn_max_interval = hdev->le_conn_max_interval; 92804fb7d90SMarcel Holtmann conn->le_conn_latency = hdev->le_conn_latency; 92904fb7d90SMarcel Holtmann conn->le_supv_timeout = hdev->le_supv_timeout; 9304292f1f3SAndre Guedes } 93146a190cbSAndre Guedes 9322acf3d90SAndre Guedes /* If controller is scanning, we stop it since some controllers are 93381ad6fd9SJohan Hedberg * not able to scan and connect at the same time. Also set the 93481ad6fd9SJohan Hedberg * HCI_LE_SCAN_INTERRUPTED flag so that the command complete 93581ad6fd9SJohan Hedberg * handler for scan disabling knows to set the correct discovery 93681ad6fd9SJohan Hedberg * state. 9372acf3d90SAndre Guedes */ 938d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { 9392acf3d90SAndre Guedes hci_req_add_le_scan_disable(&req); 940a1536da2SMarcel Holtmann hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED); 9412acf3d90SAndre Guedes } 9422acf3d90SAndre Guedes 94381ad6fd9SJohan Hedberg hci_req_add_le_create_conn(&req, conn); 94481ad6fd9SJohan Hedberg 9453c857757SJohan Hedberg create_conn: 94681ad6fd9SJohan Hedberg err = hci_req_run(&req, create_le_conn_complete); 9472acf3d90SAndre Guedes if (err) { 9482acf3d90SAndre Guedes hci_conn_del(conn); 9491d399ae5SAndre Guedes return ERR_PTR(err); 9502acf3d90SAndre Guedes } 9519f0caeb1SVinicius Costa Gomes 952f1e5d547SAndre Guedes return conn; 953fcd89c09SVille Tervo } 954fcd89c09SVille Tervo 955f75113a2SJakub Pawlowski static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) 956f75113a2SJakub Pawlowski { 957f75113a2SJakub Pawlowski struct hci_conn *conn; 958f75113a2SJakub Pawlowski 9599d4c1cc1SJohan Hedberg conn = hci_conn_hash_lookup_le(hdev, addr, type); 960f75113a2SJakub Pawlowski if (!conn) 961f75113a2SJakub Pawlowski return false; 962f75113a2SJakub Pawlowski 963f75113a2SJakub Pawlowski if (conn->state != BT_CONNECTED) 964f75113a2SJakub Pawlowski return false; 965f75113a2SJakub Pawlowski 966f75113a2SJakub Pawlowski return true; 967f75113a2SJakub Pawlowski } 968f75113a2SJakub Pawlowski 969f75113a2SJakub Pawlowski /* This function requires the caller holds hdev->lock */ 97084235d22SJohan Hedberg static int hci_explicit_conn_params_set(struct hci_dev *hdev, 971f75113a2SJakub Pawlowski bdaddr_t *addr, u8 addr_type) 972f75113a2SJakub Pawlowski { 973f75113a2SJakub Pawlowski struct hci_conn_params *params; 974f75113a2SJakub Pawlowski 975f75113a2SJakub Pawlowski if (is_connected(hdev, addr, addr_type)) 976f75113a2SJakub Pawlowski return -EISCONN; 977f75113a2SJakub Pawlowski 9785157b8a5SJakub Pawlowski params = hci_conn_params_lookup(hdev, addr, addr_type); 9795157b8a5SJakub Pawlowski if (!params) { 980f75113a2SJakub Pawlowski params = hci_conn_params_add(hdev, addr, addr_type); 981f75113a2SJakub Pawlowski if (!params) 9825157b8a5SJakub Pawlowski return -ENOMEM; 983f75113a2SJakub Pawlowski 9845157b8a5SJakub Pawlowski /* If we created new params, mark them to be deleted in 9855157b8a5SJakub Pawlowski * hci_connect_le_scan_cleanup. It's different case than 9865157b8a5SJakub Pawlowski * existing disabled params, those will stay after cleanup. 987f75113a2SJakub Pawlowski */ 9885157b8a5SJakub Pawlowski params->auto_connect = HCI_AUTO_CONN_EXPLICIT; 9895157b8a5SJakub Pawlowski } 9905157b8a5SJakub Pawlowski 9915157b8a5SJakub Pawlowski /* We're trying to connect, so make sure params are at pend_le_conns */ 99249c50922SJohan Hedberg if (params->auto_connect == HCI_AUTO_CONN_DISABLED || 9935157b8a5SJakub Pawlowski params->auto_connect == HCI_AUTO_CONN_REPORT || 9945157b8a5SJakub Pawlowski params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { 995f75113a2SJakub Pawlowski list_del_init(¶ms->action); 996f75113a2SJakub Pawlowski list_add(¶ms->action, &hdev->pend_le_conns); 997f75113a2SJakub Pawlowski } 998f75113a2SJakub Pawlowski 999f75113a2SJakub Pawlowski params->explicit_connect = true; 1000f75113a2SJakub Pawlowski 1001f75113a2SJakub Pawlowski BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, 1002f75113a2SJakub Pawlowski params->auto_connect); 1003f75113a2SJakub Pawlowski 1004f75113a2SJakub Pawlowski return 0; 1005f75113a2SJakub Pawlowski } 1006f75113a2SJakub Pawlowski 1007f75113a2SJakub Pawlowski /* This function requires the caller holds hdev->lock */ 1008f75113a2SJakub Pawlowski struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst, 1009f75113a2SJakub Pawlowski u8 dst_type, u8 sec_level, 10100ad06aa6SJohan Hedberg u16 conn_timeout) 1011f75113a2SJakub Pawlowski { 1012f75113a2SJakub Pawlowski struct hci_conn *conn; 1013f75113a2SJakub Pawlowski 1014f75113a2SJakub Pawlowski /* Let's make sure that le is enabled.*/ 1015f75113a2SJakub Pawlowski if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 1016f75113a2SJakub Pawlowski if (lmp_le_capable(hdev)) 1017f75113a2SJakub Pawlowski return ERR_PTR(-ECONNREFUSED); 1018f75113a2SJakub Pawlowski 1019f75113a2SJakub Pawlowski return ERR_PTR(-EOPNOTSUPP); 1020f75113a2SJakub Pawlowski } 1021f75113a2SJakub Pawlowski 1022f75113a2SJakub Pawlowski /* Some devices send ATT messages as soon as the physical link is 1023f75113a2SJakub Pawlowski * established. To be able to handle these ATT messages, the user- 1024f75113a2SJakub Pawlowski * space first establishes the connection and then starts the pairing 1025f75113a2SJakub Pawlowski * process. 1026f75113a2SJakub Pawlowski * 1027f75113a2SJakub Pawlowski * So if a hci_conn object already exists for the following connection 1028f75113a2SJakub Pawlowski * attempt, we simply update pending_sec_level and auth_type fields 1029f75113a2SJakub Pawlowski * and return the object found. 1030f75113a2SJakub Pawlowski */ 10319d4c1cc1SJohan Hedberg conn = hci_conn_hash_lookup_le(hdev, dst, dst_type); 1032f75113a2SJakub Pawlowski if (conn) { 1033f75113a2SJakub Pawlowski if (conn->pending_sec_level < sec_level) 1034f75113a2SJakub Pawlowski conn->pending_sec_level = sec_level; 1035f75113a2SJakub Pawlowski goto done; 1036f75113a2SJakub Pawlowski } 1037f75113a2SJakub Pawlowski 1038f75113a2SJakub Pawlowski BT_DBG("requesting refresh of dst_addr"); 1039f75113a2SJakub Pawlowski 10400ad06aa6SJohan Hedberg conn = hci_conn_add(hdev, LE_LINK, dst, HCI_ROLE_MASTER); 1041f75113a2SJakub Pawlowski if (!conn) 1042f75113a2SJakub Pawlowski return ERR_PTR(-ENOMEM); 1043f75113a2SJakub Pawlowski 104484235d22SJohan Hedberg if (hci_explicit_conn_params_set(hdev, dst, dst_type) < 0) 1045f75113a2SJakub Pawlowski return ERR_PTR(-EBUSY); 1046f75113a2SJakub Pawlowski 1047f75113a2SJakub Pawlowski conn->state = BT_CONNECT; 1048f75113a2SJakub Pawlowski set_bit(HCI_CONN_SCANNING, &conn->flags); 1049f75113a2SJakub Pawlowski conn->dst_type = dst_type; 1050f75113a2SJakub Pawlowski conn->sec_level = BT_SECURITY_LOW; 1051f75113a2SJakub Pawlowski conn->pending_sec_level = sec_level; 1052f75113a2SJakub Pawlowski conn->conn_timeout = conn_timeout; 1053f75113a2SJakub Pawlowski 105484235d22SJohan Hedberg hci_update_background_scan(hdev); 105584235d22SJohan Hedberg 1056f75113a2SJakub Pawlowski done: 1057f75113a2SJakub Pawlowski hci_conn_hold(conn); 1058f75113a2SJakub Pawlowski return conn; 1059f75113a2SJakub Pawlowski } 1060f75113a2SJakub Pawlowski 106104a6c589SAndre Guedes struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, 1062db474275SVinicius Costa Gomes u8 sec_level, u8 auth_type) 1063d04aef4cSVinicius Costa Gomes { 1064d04aef4cSVinicius Costa Gomes struct hci_conn *acl; 1065d04aef4cSVinicius Costa Gomes 1066d7a5a11dSMarcel Holtmann if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 1067c411110eSLukasz Rymanowski if (lmp_bredr_capable(hdev)) 1068c411110eSLukasz Rymanowski return ERR_PTR(-ECONNREFUSED); 1069c411110eSLukasz Rymanowski 1070beb19e4cSJohan Hedberg return ERR_PTR(-EOPNOTSUPP); 1071c411110eSLukasz Rymanowski } 107256f87901SJohan Hedberg 107370f23020SAndrei Emeltchenko acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); 107470f23020SAndrei Emeltchenko if (!acl) { 1075a5c4e309SJohan Hedberg acl = hci_conn_add(hdev, ACL_LINK, dst, HCI_ROLE_MASTER); 107670f23020SAndrei Emeltchenko if (!acl) 107748c7aba9SJohan Hedberg return ERR_PTR(-ENOMEM); 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds hci_conn_hold(acl); 10811da177e4SLinus Torvalds 108209ab6f4cSMarcel Holtmann if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { 1083765c2a96SJohan Hedberg acl->sec_level = BT_SECURITY_LOW; 1084765c2a96SJohan Hedberg acl->pending_sec_level = sec_level; 108509ab6f4cSMarcel Holtmann acl->auth_type = auth_type; 10861aef8669SVinicius Costa Gomes hci_acl_create_connection(acl); 108709ab6f4cSMarcel Holtmann } 10881da177e4SLinus Torvalds 1089db474275SVinicius Costa Gomes return acl; 1090db474275SVinicius Costa Gomes } 1091db474275SVinicius Costa Gomes 109210c62ddcSFrédéric Dalleau struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst, 109310c62ddcSFrédéric Dalleau __u16 setting) 1094db474275SVinicius Costa Gomes { 1095db474275SVinicius Costa Gomes struct hci_conn *acl; 1096db474275SVinicius Costa Gomes struct hci_conn *sco; 1097db474275SVinicius Costa Gomes 1098e660ed6cSFrédéric Dalleau acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING); 1099db474275SVinicius Costa Gomes if (IS_ERR(acl)) 11005b7f9909SMarcel Holtmann return acl; 11011da177e4SLinus Torvalds 110270f23020SAndrei Emeltchenko sco = hci_conn_hash_lookup_ba(hdev, type, dst); 110370f23020SAndrei Emeltchenko if (!sco) { 1104a5c4e309SJohan Hedberg sco = hci_conn_add(hdev, type, dst, HCI_ROLE_MASTER); 110570f23020SAndrei Emeltchenko if (!sco) { 110676a68ba0SDavid Herrmann hci_conn_drop(acl); 110748c7aba9SJohan Hedberg return ERR_PTR(-ENOMEM); 11081da177e4SLinus Torvalds } 11091da177e4SLinus Torvalds } 11105b7f9909SMarcel Holtmann 11111da177e4SLinus Torvalds acl->link = sco; 11121da177e4SLinus Torvalds sco->link = acl; 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds hci_conn_hold(sco); 11151da177e4SLinus Torvalds 111610c62ddcSFrédéric Dalleau sco->setting = setting; 111710c62ddcSFrédéric Dalleau 11181da177e4SLinus Torvalds if (acl->state == BT_CONNECTED && 1119b6a0dc82SMarcel Holtmann (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { 112058a681efSJohan Hedberg set_bit(HCI_CONN_POWER_SAVE, &acl->flags); 112114b12d0bSJaikumar Ganesh hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); 1122c390216bSNick Pelly 112351a8efd7SJohan Hedberg if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) { 1124e73439d8SMarcel Holtmann /* defer SCO setup until mode change completed */ 112551a8efd7SJohan Hedberg set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags); 1126e73439d8SMarcel Holtmann return sco; 1127e73439d8SMarcel Holtmann } 1128e73439d8SMarcel Holtmann 1129e73439d8SMarcel Holtmann hci_sco_setup(acl, 0x00); 1130b6a0dc82SMarcel Holtmann } 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds return sco; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds 1135e7c29cb1SMarcel Holtmann /* Check link security requirement */ 1136e7c29cb1SMarcel Holtmann int hci_conn_check_link_mode(struct hci_conn *conn) 1137e7c29cb1SMarcel Holtmann { 113838b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 1139e7c29cb1SMarcel Holtmann 114040b552aaSMarcel Holtmann /* In Secure Connections Only mode, it is required that Secure 114140b552aaSMarcel Holtmann * Connections is used and the link is encrypted with AES-CCM 114240b552aaSMarcel Holtmann * using a P-256 authenticated combination key. 114340b552aaSMarcel Holtmann */ 1144d7a5a11dSMarcel Holtmann if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) { 114540b552aaSMarcel Holtmann if (!hci_conn_sc_enabled(conn) || 114640b552aaSMarcel Holtmann !test_bit(HCI_CONN_AES_CCM, &conn->flags) || 114740b552aaSMarcel Holtmann conn->key_type != HCI_LK_AUTH_COMBINATION_P256) 114840b552aaSMarcel Holtmann return 0; 114940b552aaSMarcel Holtmann } 115040b552aaSMarcel Holtmann 11514dae2798SJohan Hedberg if (hci_conn_ssp_enabled(conn) && 11524dae2798SJohan Hedberg !test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 1153e7c29cb1SMarcel Holtmann return 0; 1154e7c29cb1SMarcel Holtmann 1155e7c29cb1SMarcel Holtmann return 1; 1156e7c29cb1SMarcel Holtmann } 1157e7c29cb1SMarcel Holtmann 11581da177e4SLinus Torvalds /* Authenticate remote device */ 11590684e5f9SMarcel Holtmann static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) 11601da177e4SLinus Torvalds { 116138b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 11621da177e4SLinus Torvalds 1163765c2a96SJohan Hedberg if (conn->pending_sec_level > sec_level) 1164765c2a96SJohan Hedberg sec_level = conn->pending_sec_level; 1165765c2a96SJohan Hedberg 116696a31833SMarcel Holtmann if (sec_level > conn->sec_level) 1167765c2a96SJohan Hedberg conn->pending_sec_level = sec_level; 11684dae2798SJohan Hedberg else if (test_bit(HCI_CONN_AUTH, &conn->flags)) 11691da177e4SLinus Torvalds return 1; 11701da177e4SLinus Torvalds 117165cf686eSJohan Hedberg /* Make sure we preserve an existing MITM requirement*/ 117265cf686eSJohan Hedberg auth_type |= (conn->auth_type & 0x01); 117365cf686eSJohan Hedberg 117496a31833SMarcel Holtmann conn->auth_type = auth_type; 117596a31833SMarcel Holtmann 117651a8efd7SJohan Hedberg if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { 11771da177e4SLinus Torvalds struct hci_cp_auth_requested cp; 1178b7d05badSPeter Hurley 1179aca3192cSYOSHIFUJI Hideaki cp.handle = cpu_to_le16(conn->handle); 118040be492fSMarcel Holtmann hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, 118140be492fSMarcel Holtmann sizeof(cp), &cp); 118209da1f34SJohan Hedberg 118309da1f34SJohan Hedberg /* If we're already encrypted set the REAUTH_PEND flag, 118409da1f34SJohan Hedberg * otherwise set the ENCRYPT_PEND. 118509da1f34SJohan Hedberg */ 11864dae2798SJohan Hedberg if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 118751a8efd7SJohan Hedberg set_bit(HCI_CONN_REAUTH_PEND, &conn->flags); 118809da1f34SJohan Hedberg else 118909da1f34SJohan Hedberg set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags); 11901da177e4SLinus Torvalds } 11918c1b2355SMarcel Holtmann 11921da177e4SLinus Torvalds return 0; 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 119513d39315SWaldemar Rymarkiewicz /* Encrypt the the link */ 119613d39315SWaldemar Rymarkiewicz static void hci_conn_encrypt(struct hci_conn *conn) 119713d39315SWaldemar Rymarkiewicz { 119838b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 119913d39315SWaldemar Rymarkiewicz 120051a8efd7SJohan Hedberg if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) { 120113d39315SWaldemar Rymarkiewicz struct hci_cp_set_conn_encrypt cp; 120213d39315SWaldemar Rymarkiewicz cp.handle = cpu_to_le16(conn->handle); 120313d39315SWaldemar Rymarkiewicz cp.encrypt = 0x01; 120413d39315SWaldemar Rymarkiewicz hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), 120513d39315SWaldemar Rymarkiewicz &cp); 120613d39315SWaldemar Rymarkiewicz } 120713d39315SWaldemar Rymarkiewicz } 120813d39315SWaldemar Rymarkiewicz 12098c1b2355SMarcel Holtmann /* Enable security */ 1210e7cafc45SJohan Hedberg int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type, 1211e7cafc45SJohan Hedberg bool initiator) 12121da177e4SLinus Torvalds { 121338b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 12141da177e4SLinus Torvalds 1215d8343f12SVinicius Costa Gomes if (conn->type == LE_LINK) 1216d8343f12SVinicius Costa Gomes return smp_conn_security(conn, sec_level); 1217d8343f12SVinicius Costa Gomes 121813d39315SWaldemar Rymarkiewicz /* For sdp we don't need the link key. */ 12198c1b2355SMarcel Holtmann if (sec_level == BT_SECURITY_SDP) 12208c1b2355SMarcel Holtmann return 1; 12218c1b2355SMarcel Holtmann 122213d39315SWaldemar Rymarkiewicz /* For non 2.1 devices and low security level we don't need the link 122313d39315SWaldemar Rymarkiewicz key. */ 1224aa64a8b5SJohan Hedberg if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn)) 12258c1b2355SMarcel Holtmann return 1; 12268c1b2355SMarcel Holtmann 122713d39315SWaldemar Rymarkiewicz /* For other security levels we need the link key. */ 12284dae2798SJohan Hedberg if (!test_bit(HCI_CONN_AUTH, &conn->flags)) 122913d39315SWaldemar Rymarkiewicz goto auth; 12301da177e4SLinus Torvalds 12317b5a9241SMarcel Holtmann /* An authenticated FIPS approved combination key has sufficient 12327b5a9241SMarcel Holtmann * security for security level 4. */ 12337b5a9241SMarcel Holtmann if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256 && 12347b5a9241SMarcel Holtmann sec_level == BT_SECURITY_FIPS) 12357b5a9241SMarcel Holtmann goto encrypt; 12367b5a9241SMarcel Holtmann 12377b5a9241SMarcel Holtmann /* An authenticated combination key has sufficient security for 12387b5a9241SMarcel Holtmann security level 3. */ 12397b5a9241SMarcel Holtmann if ((conn->key_type == HCI_LK_AUTH_COMBINATION_P192 || 12407b5a9241SMarcel Holtmann conn->key_type == HCI_LK_AUTH_COMBINATION_P256) && 12417b5a9241SMarcel Holtmann sec_level == BT_SECURITY_HIGH) 124213d39315SWaldemar Rymarkiewicz goto encrypt; 124313d39315SWaldemar Rymarkiewicz 124413d39315SWaldemar Rymarkiewicz /* An unauthenticated combination key has sufficient security for 124513d39315SWaldemar Rymarkiewicz security level 1 and 2. */ 124666138ce8SMarcel Holtmann if ((conn->key_type == HCI_LK_UNAUTH_COMBINATION_P192 || 124766138ce8SMarcel Holtmann conn->key_type == HCI_LK_UNAUTH_COMBINATION_P256) && 12485974e4c4SGustavo Padovan (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW)) 124913d39315SWaldemar Rymarkiewicz goto encrypt; 125013d39315SWaldemar Rymarkiewicz 125113d39315SWaldemar Rymarkiewicz /* A combination key has always sufficient security for the security 125213d39315SWaldemar Rymarkiewicz levels 1 or 2. High security level requires the combination key 125313d39315SWaldemar Rymarkiewicz is generated using maximum PIN code length (16). 125413d39315SWaldemar Rymarkiewicz For pre 2.1 units. */ 125513d39315SWaldemar Rymarkiewicz if (conn->key_type == HCI_LK_COMBINATION && 12567b5a9241SMarcel Holtmann (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW || 12577b5a9241SMarcel Holtmann conn->pin_length == 16)) 125813d39315SWaldemar Rymarkiewicz goto encrypt; 125913d39315SWaldemar Rymarkiewicz 126013d39315SWaldemar Rymarkiewicz auth: 126151a8efd7SJohan Hedberg if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) 12621da177e4SLinus Torvalds return 0; 12631da177e4SLinus Torvalds 1264977f8fceSJohan Hedberg if (initiator) 1265977f8fceSJohan Hedberg set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags); 1266977f8fceSJohan Hedberg 12676fdf658cSLuiz Augusto von Dentz if (!hci_conn_auth(conn, sec_level, auth_type)) 126813d39315SWaldemar Rymarkiewicz return 0; 12698c1b2355SMarcel Holtmann 127013d39315SWaldemar Rymarkiewicz encrypt: 12714dae2798SJohan Hedberg if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 127213d39315SWaldemar Rymarkiewicz return 1; 127313d39315SWaldemar Rymarkiewicz 127413d39315SWaldemar Rymarkiewicz hci_conn_encrypt(conn); 12751da177e4SLinus Torvalds return 0; 12761da177e4SLinus Torvalds } 12778c1b2355SMarcel Holtmann EXPORT_SYMBOL(hci_conn_security); 12781da177e4SLinus Torvalds 1279b3b1b061SWaldemar Rymarkiewicz /* Check secure link requirement */ 1280b3b1b061SWaldemar Rymarkiewicz int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) 1281b3b1b061SWaldemar Rymarkiewicz { 128238b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 1283b3b1b061SWaldemar Rymarkiewicz 12849cb2e030SMarcel Holtmann /* Accept if non-secure or higher security level is required */ 12859cb2e030SMarcel Holtmann if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS) 1286b3b1b061SWaldemar Rymarkiewicz return 1; 1287b3b1b061SWaldemar Rymarkiewicz 12889cb2e030SMarcel Holtmann /* Accept if secure or higher security level is already present */ 12899cb2e030SMarcel Holtmann if (conn->sec_level == BT_SECURITY_HIGH || 12909cb2e030SMarcel Holtmann conn->sec_level == BT_SECURITY_FIPS) 12919cb2e030SMarcel Holtmann return 1; 12929cb2e030SMarcel Holtmann 12939cb2e030SMarcel Holtmann /* Reject not secure link */ 12949cb2e030SMarcel Holtmann return 0; 1295b3b1b061SWaldemar Rymarkiewicz } 1296b3b1b061SWaldemar Rymarkiewicz EXPORT_SYMBOL(hci_conn_check_secure); 1297b3b1b061SWaldemar Rymarkiewicz 12981da177e4SLinus Torvalds /* Switch role */ 12998c1b2355SMarcel Holtmann int hci_conn_switch_role(struct hci_conn *conn, __u8 role) 13001da177e4SLinus Torvalds { 130138b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 13021da177e4SLinus Torvalds 130340bef302SJohan Hedberg if (role == conn->role) 13041da177e4SLinus Torvalds return 1; 13051da177e4SLinus Torvalds 130651a8efd7SJohan Hedberg if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) { 13071da177e4SLinus Torvalds struct hci_cp_switch_role cp; 13081da177e4SLinus Torvalds bacpy(&cp.bdaddr, &conn->dst); 13091da177e4SLinus Torvalds cp.role = role; 1310a9de9248SMarcel Holtmann hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); 13111da177e4SLinus Torvalds } 13128c1b2355SMarcel Holtmann 13131da177e4SLinus Torvalds return 0; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds EXPORT_SYMBOL(hci_conn_switch_role); 13161da177e4SLinus Torvalds 131704837f64SMarcel Holtmann /* Enter active mode */ 131814b12d0bSJaikumar Ganesh void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) 131904837f64SMarcel Holtmann { 132004837f64SMarcel Holtmann struct hci_dev *hdev = conn->hdev; 132104837f64SMarcel Holtmann 132238b3fef1SAndrei Emeltchenko BT_DBG("hcon %p mode %d", conn, conn->mode); 132304837f64SMarcel Holtmann 132414b12d0bSJaikumar Ganesh if (conn->mode != HCI_CM_SNIFF) 132514b12d0bSJaikumar Ganesh goto timer; 132614b12d0bSJaikumar Ganesh 132758a681efSJohan Hedberg if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active) 132804837f64SMarcel Holtmann goto timer; 132904837f64SMarcel Holtmann 133051a8efd7SJohan Hedberg if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { 133104837f64SMarcel Holtmann struct hci_cp_exit_sniff_mode cp; 1332aca3192cSYOSHIFUJI Hideaki cp.handle = cpu_to_le16(conn->handle); 1333a9de9248SMarcel Holtmann hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); 133404837f64SMarcel Holtmann } 133504837f64SMarcel Holtmann 133604837f64SMarcel Holtmann timer: 133704837f64SMarcel Holtmann if (hdev->idle_timeout > 0) 1338a74a84f6SJohan Hedberg queue_delayed_work(hdev->workqueue, &conn->idle_work, 1339a74a84f6SJohan Hedberg msecs_to_jiffies(hdev->idle_timeout)); 134004837f64SMarcel Holtmann } 134104837f64SMarcel Holtmann 13421da177e4SLinus Torvalds /* Drop all connection on the device */ 13431da177e4SLinus Torvalds void hci_conn_hash_flush(struct hci_dev *hdev) 13441da177e4SLinus Torvalds { 13451da177e4SLinus Torvalds struct hci_conn_hash *h = &hdev->conn_hash; 13463c4e0df0SAndrei Emeltchenko struct hci_conn *c, *n; 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds BT_DBG("hdev %s", hdev->name); 13491da177e4SLinus Torvalds 13503c4e0df0SAndrei Emeltchenko list_for_each_entry_safe(c, n, &h->list, list) { 13511da177e4SLinus Torvalds c->state = BT_CLOSED; 13521da177e4SLinus Torvalds 13533a6d576bSJohan Hedberg hci_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM); 13541da177e4SLinus Torvalds hci_conn_del(c); 13551da177e4SLinus Torvalds } 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds 1358a9de9248SMarcel Holtmann /* Check pending connect attempts */ 1359a9de9248SMarcel Holtmann void hci_conn_check_pending(struct hci_dev *hdev) 1360a9de9248SMarcel Holtmann { 1361a9de9248SMarcel Holtmann struct hci_conn *conn; 1362a9de9248SMarcel Holtmann 1363a9de9248SMarcel Holtmann BT_DBG("hdev %s", hdev->name); 1364a9de9248SMarcel Holtmann 1365a9de9248SMarcel Holtmann hci_dev_lock(hdev); 1366a9de9248SMarcel Holtmann 1367a9de9248SMarcel Holtmann conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); 1368a9de9248SMarcel Holtmann if (conn) 13691aef8669SVinicius Costa Gomes hci_acl_create_connection(conn); 1370a9de9248SMarcel Holtmann 1371a9de9248SMarcel Holtmann hci_dev_unlock(hdev); 1372a9de9248SMarcel Holtmann } 1373a9de9248SMarcel Holtmann 13744dae2798SJohan Hedberg static u32 get_link_mode(struct hci_conn *conn) 13754dae2798SJohan Hedberg { 13764dae2798SJohan Hedberg u32 link_mode = 0; 13774dae2798SJohan Hedberg 137840bef302SJohan Hedberg if (conn->role == HCI_ROLE_MASTER) 13794dae2798SJohan Hedberg link_mode |= HCI_LM_MASTER; 13804dae2798SJohan Hedberg 13814dae2798SJohan Hedberg if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) 13824dae2798SJohan Hedberg link_mode |= HCI_LM_ENCRYPT; 13834dae2798SJohan Hedberg 13844dae2798SJohan Hedberg if (test_bit(HCI_CONN_AUTH, &conn->flags)) 13854dae2798SJohan Hedberg link_mode |= HCI_LM_AUTH; 13864dae2798SJohan Hedberg 13874dae2798SJohan Hedberg if (test_bit(HCI_CONN_SECURE, &conn->flags)) 13884dae2798SJohan Hedberg link_mode |= HCI_LM_SECURE; 13894dae2798SJohan Hedberg 13904dae2798SJohan Hedberg if (test_bit(HCI_CONN_FIPS, &conn->flags)) 13914dae2798SJohan Hedberg link_mode |= HCI_LM_FIPS; 13924dae2798SJohan Hedberg 13934dae2798SJohan Hedberg return link_mode; 13944dae2798SJohan Hedberg } 13954dae2798SJohan Hedberg 13961da177e4SLinus Torvalds int hci_get_conn_list(void __user *arg) 13971da177e4SLinus Torvalds { 1398fc5fef61SGustavo Padovan struct hci_conn *c; 13991da177e4SLinus Torvalds struct hci_conn_list_req req, *cl; 14001da177e4SLinus Torvalds struct hci_conn_info *ci; 14011da177e4SLinus Torvalds struct hci_dev *hdev; 14021da177e4SLinus Torvalds int n = 0, size, err; 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds if (copy_from_user(&req, arg, sizeof(req))) 14051da177e4SLinus Torvalds return -EFAULT; 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) 14081da177e4SLinus Torvalds return -EINVAL; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds size = sizeof(req) + req.conn_num * sizeof(*ci); 14111da177e4SLinus Torvalds 141270f23020SAndrei Emeltchenko cl = kmalloc(size, GFP_KERNEL); 141370f23020SAndrei Emeltchenko if (!cl) 14141da177e4SLinus Torvalds return -ENOMEM; 14151da177e4SLinus Torvalds 141670f23020SAndrei Emeltchenko hdev = hci_dev_get(req.dev_id); 141770f23020SAndrei Emeltchenko if (!hdev) { 14181da177e4SLinus Torvalds kfree(cl); 14191da177e4SLinus Torvalds return -ENODEV; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds ci = cl->conn_info; 14231da177e4SLinus Torvalds 142409fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 14258035ded4SLuiz Augusto von Dentz list_for_each_entry(c, &hdev->conn_hash.list, list) { 14261da177e4SLinus Torvalds bacpy(&(ci + n)->bdaddr, &c->dst); 14271da177e4SLinus Torvalds (ci + n)->handle = c->handle; 14281da177e4SLinus Torvalds (ci + n)->type = c->type; 14291da177e4SLinus Torvalds (ci + n)->out = c->out; 14301da177e4SLinus Torvalds (ci + n)->state = c->state; 14314dae2798SJohan Hedberg (ci + n)->link_mode = get_link_mode(c); 14321da177e4SLinus Torvalds if (++n >= req.conn_num) 14331da177e4SLinus Torvalds break; 14341da177e4SLinus Torvalds } 143509fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds cl->dev_id = hdev->id; 14381da177e4SLinus Torvalds cl->conn_num = n; 14391da177e4SLinus Torvalds size = sizeof(req) + n * sizeof(*ci); 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds hci_dev_put(hdev); 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds err = copy_to_user(arg, cl, size); 14441da177e4SLinus Torvalds kfree(cl); 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds return err ? -EFAULT : 0; 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) 14501da177e4SLinus Torvalds { 14511da177e4SLinus Torvalds struct hci_conn_info_req req; 14521da177e4SLinus Torvalds struct hci_conn_info ci; 14531da177e4SLinus Torvalds struct hci_conn *conn; 14541da177e4SLinus Torvalds char __user *ptr = arg + sizeof(req); 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds if (copy_from_user(&req, arg, sizeof(req))) 14571da177e4SLinus Torvalds return -EFAULT; 14581da177e4SLinus Torvalds 145909fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 14601da177e4SLinus Torvalds conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); 14611da177e4SLinus Torvalds if (conn) { 14621da177e4SLinus Torvalds bacpy(&ci.bdaddr, &conn->dst); 14631da177e4SLinus Torvalds ci.handle = conn->handle; 14641da177e4SLinus Torvalds ci.type = conn->type; 14651da177e4SLinus Torvalds ci.out = conn->out; 14661da177e4SLinus Torvalds ci.state = conn->state; 14674dae2798SJohan Hedberg ci.link_mode = get_link_mode(conn); 14681da177e4SLinus Torvalds } 146909fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds if (!conn) 14721da177e4SLinus Torvalds return -ENOENT; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; 14751da177e4SLinus Torvalds } 147640be492fSMarcel Holtmann 147740be492fSMarcel Holtmann int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) 147840be492fSMarcel Holtmann { 147940be492fSMarcel Holtmann struct hci_auth_info_req req; 148040be492fSMarcel Holtmann struct hci_conn *conn; 148140be492fSMarcel Holtmann 148240be492fSMarcel Holtmann if (copy_from_user(&req, arg, sizeof(req))) 148340be492fSMarcel Holtmann return -EFAULT; 148440be492fSMarcel Holtmann 148509fd0de5SGustavo F. Padovan hci_dev_lock(hdev); 148640be492fSMarcel Holtmann conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); 148740be492fSMarcel Holtmann if (conn) 148840be492fSMarcel Holtmann req.type = conn->auth_type; 148909fd0de5SGustavo F. Padovan hci_dev_unlock(hdev); 149040be492fSMarcel Holtmann 149140be492fSMarcel Holtmann if (!conn) 149240be492fSMarcel Holtmann return -ENOENT; 149340be492fSMarcel Holtmann 149440be492fSMarcel Holtmann return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; 149540be492fSMarcel Holtmann } 149673d80debSLuiz Augusto von Dentz 149773d80debSLuiz Augusto von Dentz struct hci_chan *hci_chan_create(struct hci_conn *conn) 149873d80debSLuiz Augusto von Dentz { 149973d80debSLuiz Augusto von Dentz struct hci_dev *hdev = conn->hdev; 150073d80debSLuiz Augusto von Dentz struct hci_chan *chan; 150173d80debSLuiz Augusto von Dentz 150238b3fef1SAndrei Emeltchenko BT_DBG("%s hcon %p", hdev->name, conn); 150373d80debSLuiz Augusto von Dentz 1504f94b665dSJohan Hedberg if (test_bit(HCI_CONN_DROP, &conn->flags)) { 1505f94b665dSJohan Hedberg BT_DBG("Refusing to create new hci_chan"); 1506f94b665dSJohan Hedberg return NULL; 1507f94b665dSJohan Hedberg } 1508f94b665dSJohan Hedberg 150927f70f3eSJohan Hedberg chan = kzalloc(sizeof(*chan), GFP_KERNEL); 151073d80debSLuiz Augusto von Dentz if (!chan) 151173d80debSLuiz Augusto von Dentz return NULL; 151273d80debSLuiz Augusto von Dentz 15136c388d32SJohan Hedberg chan->conn = hci_conn_get(conn); 151473d80debSLuiz Augusto von Dentz skb_queue_head_init(&chan->data_q); 1515168df8e5SMat Martineau chan->state = BT_CONNECTED; 151673d80debSLuiz Augusto von Dentz 15178192edefSGustavo F. Padovan list_add_rcu(&chan->list, &conn->chan_list); 151873d80debSLuiz Augusto von Dentz 151973d80debSLuiz Augusto von Dentz return chan; 152073d80debSLuiz Augusto von Dentz } 152173d80debSLuiz Augusto von Dentz 15229472007cSAndrei Emeltchenko void hci_chan_del(struct hci_chan *chan) 152373d80debSLuiz Augusto von Dentz { 152473d80debSLuiz Augusto von Dentz struct hci_conn *conn = chan->conn; 152573d80debSLuiz Augusto von Dentz struct hci_dev *hdev = conn->hdev; 152673d80debSLuiz Augusto von Dentz 152738b3fef1SAndrei Emeltchenko BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan); 152873d80debSLuiz Augusto von Dentz 15298192edefSGustavo F. Padovan list_del_rcu(&chan->list); 15308192edefSGustavo F. Padovan 15318192edefSGustavo F. Padovan synchronize_rcu(); 153273d80debSLuiz Augusto von Dentz 1533bcbb655aSJohan Hedberg /* Prevent new hci_chan's to be created for this hci_conn */ 1534f94b665dSJohan Hedberg set_bit(HCI_CONN_DROP, &conn->flags); 1535b3ff670aSJohan Hedberg 15366c388d32SJohan Hedberg hci_conn_put(conn); 1537e9b02748SAndrei Emeltchenko 153873d80debSLuiz Augusto von Dentz skb_queue_purge(&chan->data_q); 153973d80debSLuiz Augusto von Dentz kfree(chan); 154073d80debSLuiz Augusto von Dentz } 154173d80debSLuiz Augusto von Dentz 15422c33c06aSGustavo F. Padovan void hci_chan_list_flush(struct hci_conn *conn) 154373d80debSLuiz Augusto von Dentz { 15442a5a5ec6SAndrei Emeltchenko struct hci_chan *chan, *n; 154573d80debSLuiz Augusto von Dentz 154638b3fef1SAndrei Emeltchenko BT_DBG("hcon %p", conn); 154773d80debSLuiz Augusto von Dentz 15482a5a5ec6SAndrei Emeltchenko list_for_each_entry_safe(chan, n, &conn->chan_list, list) 154973d80debSLuiz Augusto von Dentz hci_chan_del(chan); 155073d80debSLuiz Augusto von Dentz } 155142c4e53eSAndrei Emeltchenko 155242c4e53eSAndrei Emeltchenko static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon, 155342c4e53eSAndrei Emeltchenko __u16 handle) 155442c4e53eSAndrei Emeltchenko { 155542c4e53eSAndrei Emeltchenko struct hci_chan *hchan; 155642c4e53eSAndrei Emeltchenko 155742c4e53eSAndrei Emeltchenko list_for_each_entry(hchan, &hcon->chan_list, list) { 155842c4e53eSAndrei Emeltchenko if (hchan->handle == handle) 155942c4e53eSAndrei Emeltchenko return hchan; 156042c4e53eSAndrei Emeltchenko } 156142c4e53eSAndrei Emeltchenko 156242c4e53eSAndrei Emeltchenko return NULL; 156342c4e53eSAndrei Emeltchenko } 156442c4e53eSAndrei Emeltchenko 156542c4e53eSAndrei Emeltchenko struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle) 156642c4e53eSAndrei Emeltchenko { 156742c4e53eSAndrei Emeltchenko struct hci_conn_hash *h = &hdev->conn_hash; 156842c4e53eSAndrei Emeltchenko struct hci_conn *hcon; 156942c4e53eSAndrei Emeltchenko struct hci_chan *hchan = NULL; 157042c4e53eSAndrei Emeltchenko 157142c4e53eSAndrei Emeltchenko rcu_read_lock(); 157242c4e53eSAndrei Emeltchenko 157342c4e53eSAndrei Emeltchenko list_for_each_entry_rcu(hcon, &h->list, list) { 157442c4e53eSAndrei Emeltchenko hchan = __hci_chan_lookup_handle(hcon, handle); 157542c4e53eSAndrei Emeltchenko if (hchan) 157642c4e53eSAndrei Emeltchenko break; 157742c4e53eSAndrei Emeltchenko } 157842c4e53eSAndrei Emeltchenko 157942c4e53eSAndrei Emeltchenko rcu_read_unlock(); 158042c4e53eSAndrei Emeltchenko 158142c4e53eSAndrei Emeltchenko return hchan; 158242c4e53eSAndrei Emeltchenko } 1583