YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BlueZ - Bluetooth protocol stack for Linux |
Ron Shaffer | 2d0a034 | 2010-05-28 11:53:46 -0400 | [diff] [blame] | 3 | Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
| 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 Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 15 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI connection handling. */ |
| 26 | |
Gustavo Padovan | 8c520a5 | 2012-05-23 04:04:22 -0300 | [diff] [blame] | 27 | #include <linux/export.h> |
Marcel Holtmann | 23b9ceb | 2014-12-20 17:13:41 +0100 | [diff] [blame] | 28 | #include <linux/debugfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 32 | #include <net/bluetooth/l2cap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 34 | #include "hci_request.h" |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 35 | #include "smp.h" |
Marcel Holtmann | 7024728 | 2013-10-10 14:54:15 -0700 | [diff] [blame] | 36 | #include "a2mp.h" |
| 37 | |
Frédéric Dalleau | 2dea632 | 2013-08-19 14:24:03 +0200 | [diff] [blame] | 38 | struct sco_param { |
| 39 | u16 pkt_type; |
| 40 | u16 max_latency; |
Johan Hedberg | c7da579 | 2014-09-24 22:41:46 +0300 | [diff] [blame] | 41 | u8 retrans_effort; |
Frédéric Dalleau | 2dea632 | 2013-08-19 14:24:03 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Bernhard Thaler | 48e68ff | 2014-09-23 11:01:07 +0200 | [diff] [blame] | 44 | static const struct sco_param esco_param_cvsd[] = { |
Johan Hedberg | c7da579 | 2014-09-24 22:41:46 +0300 | [diff] [blame] | 45 | { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000a, 0x01 }, /* S3 */ |
| 46 | { EDR_ESCO_MASK & ~ESCO_2EV3, 0x0007, 0x01 }, /* S2 */ |
| 47 | { EDR_ESCO_MASK | ESCO_EV3, 0x0007, 0x01 }, /* S1 */ |
| 48 | { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0x01 }, /* D1 */ |
| 49 | { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0x01 }, /* D0 */ |
Frédéric Dalleau | 2dea632 | 2013-08-19 14:24:03 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
Bernhard Thaler | 48e68ff | 2014-09-23 11:01:07 +0200 | [diff] [blame] | 52 | static const struct sco_param sco_param_cvsd[] = { |
Johan Hedberg | c7da579 | 2014-09-24 22:41:46 +0300 | [diff] [blame] | 53 | { EDR_ESCO_MASK | ESCO_HV3, 0xffff, 0xff }, /* D1 */ |
| 54 | { EDR_ESCO_MASK | ESCO_HV1, 0xffff, 0xff }, /* D0 */ |
Bernhard Thaler | 48e68ff | 2014-09-23 11:01:07 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Johan Hedberg | 565766b | 2014-09-25 09:48:01 +0300 | [diff] [blame] | 57 | static const struct sco_param esco_param_msbc[] = { |
Johan Hedberg | c7da579 | 2014-09-24 22:41:46 +0300 | [diff] [blame] | 58 | { EDR_ESCO_MASK & ~ESCO_2EV3, 0x000d, 0x02 }, /* T2 */ |
| 59 | { EDR_ESCO_MASK | ESCO_EV3, 0x0008, 0x02 }, /* T1 */ |
Frédéric Dalleau | 2dea632 | 2013-08-19 14:24:03 +0200 | [diff] [blame] | 60 | }; |
| 61 | |
Vinicius Costa Gomes | 1aef866 | 2012-07-27 19:32:55 -0300 | [diff] [blame] | 62 | static void hci_le_create_connection_cancel(struct hci_conn *conn) |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 63 | { |
| 64 | hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); |
| 65 | } |
| 66 | |
Jakub Pawlowski | f75113a | 2015-08-07 20:22:53 +0200 | [diff] [blame] | 67 | /* This function requires the caller holds hdev->lock */ |
| 68 | static void hci_connect_le_scan_cleanup(struct hci_conn *conn) |
| 69 | { |
| 70 | struct hci_conn_params *params; |
| 71 | struct smp_irk *irk; |
| 72 | bdaddr_t *bdaddr; |
| 73 | u8 bdaddr_type; |
| 74 | |
| 75 | bdaddr = &conn->dst; |
| 76 | bdaddr_type = conn->dst_type; |
| 77 | |
| 78 | /* Check if we need to convert to identity address */ |
| 79 | irk = hci_get_irk(conn->hdev, bdaddr, bdaddr_type); |
| 80 | if (irk) { |
| 81 | bdaddr = &irk->bdaddr; |
| 82 | bdaddr_type = irk->addr_type; |
| 83 | } |
| 84 | |
| 85 | params = hci_explicit_connect_lookup(conn->hdev, bdaddr, bdaddr_type); |
| 86 | if (!params) |
| 87 | return; |
| 88 | |
| 89 | /* The connection attempt was doing scan for new RPA, and is |
| 90 | * in scan phase. If params are not associated with any other |
| 91 | * autoconnect action, remove them completely. If they are, just unmark |
| 92 | * them as waiting for connection, by clearing explicit_connect field. |
| 93 | */ |
Johan Hedberg | 9ad3e6f | 2015-10-16 10:07:53 +0300 | [diff] [blame] | 94 | params->explicit_connect = false; |
| 95 | |
| 96 | list_del_init(¶ms->action); |
| 97 | |
| 98 | switch (params->auto_connect) { |
| 99 | case HCI_AUTO_CONN_EXPLICIT: |
Jakub Pawlowski | f75113a | 2015-08-07 20:22:53 +0200 | [diff] [blame] | 100 | hci_conn_params_del(conn->hdev, bdaddr, bdaddr_type); |
Johan Hedberg | 9ad3e6f | 2015-10-16 10:07:53 +0300 | [diff] [blame] | 101 | /* return instead of break to avoid duplicate scan update */ |
| 102 | return; |
| 103 | case HCI_AUTO_CONN_DIRECT: |
| 104 | case HCI_AUTO_CONN_ALWAYS: |
| 105 | list_add(¶ms->action, &conn->hdev->pend_le_conns); |
| 106 | break; |
| 107 | case HCI_AUTO_CONN_REPORT: |
| 108 | list_add(¶ms->action, &conn->hdev->pend_le_reports); |
| 109 | break; |
| 110 | default: |
| 111 | break; |
Jakub Pawlowski | 168b8a2 | 2015-10-16 10:07:49 +0300 | [diff] [blame] | 112 | } |
Johan Hedberg | 9ad3e6f | 2015-10-16 10:07:53 +0300 | [diff] [blame] | 113 | |
| 114 | hci_update_background_scan(conn->hdev); |
Jakub Pawlowski | f75113a | 2015-08-07 20:22:53 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Johan Hedberg | b958f9a | 2015-10-16 10:07:50 +0300 | [diff] [blame] | 117 | static void hci_conn_cleanup(struct hci_conn *conn) |
| 118 | { |
| 119 | struct hci_dev *hdev = conn->hdev; |
| 120 | |
| 121 | if (test_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags)) |
| 122 | hci_conn_params_del(conn->hdev, &conn->dst, conn->dst_type); |
| 123 | |
| 124 | hci_chan_list_flush(conn); |
| 125 | |
| 126 | hci_conn_hash_del(hdev, conn); |
| 127 | |
| 128 | if (hdev->notify) |
| 129 | hdev->notify(hdev, HCI_NOTIFY_CONN_DEL); |
| 130 | |
| 131 | hci_conn_del_sysfs(conn); |
| 132 | |
| 133 | debugfs_remove_recursive(conn->debugfs); |
| 134 | |
| 135 | hci_dev_put(hdev); |
| 136 | |
| 137 | hci_conn_put(conn); |
| 138 | } |
| 139 | |
Jakub Pawlowski | f75113a | 2015-08-07 20:22:53 +0200 | [diff] [blame] | 140 | /* This function requires the caller holds hdev->lock */ |
| 141 | static void hci_connect_le_scan_remove(struct hci_conn *conn) |
| 142 | { |
| 143 | hci_connect_le_scan_cleanup(conn); |
| 144 | |
Johan Hedberg | b958f9a | 2015-10-16 10:07:50 +0300 | [diff] [blame] | 145 | /* We can't call hci_conn_del here since that would deadlock |
| 146 | * with trying to call cancel_delayed_work_sync(&conn->disc_work). |
| 147 | * Instead, call just hci_conn_cleanup() which contains the bare |
| 148 | * minimum cleanup operations needed for a connection in this |
| 149 | * state. |
| 150 | */ |
| 151 | hci_conn_cleanup(conn); |
Jakub Pawlowski | f75113a | 2015-08-07 20:22:53 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Vinicius Costa Gomes | 1aef866 | 2012-07-27 19:32:55 -0300 | [diff] [blame] | 154 | static void hci_acl_create_connection(struct hci_conn *conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
| 156 | struct hci_dev *hdev = conn->hdev; |
| 157 | struct inquiry_entry *ie; |
| 158 | struct hci_cp_create_conn cp; |
| 159 | |
Andrei Emeltchenko | 42d2d87 | 2012-02-17 11:40:57 +0200 | [diff] [blame] | 160 | BT_DBG("hcon %p", conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | conn->state = BT_CONNECT; |
Johan Hedberg | a0c808b | 2012-01-16 09:49:58 +0200 | [diff] [blame] | 163 | conn->out = true; |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 164 | conn->role = HCI_ROLE_MASTER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 166 | conn->attempt++; |
| 167 | |
Marcel Holtmann | e4e8e37 | 2008-07-14 20:13:47 +0200 | [diff] [blame] | 168 | conn->link_policy = hdev->link_policy; |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | memset(&cp, 0, sizeof(cp)); |
| 171 | bacpy(&cp.bdaddr, &conn->dst); |
| 172 | cp.pscan_rep_mode = 0x02; |
| 173 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 174 | ie = hci_inquiry_cache_lookup(hdev, &conn->dst); |
| 175 | if (ie) { |
Marcel Holtmann | 41a9621 | 2008-07-14 20:13:48 +0200 | [diff] [blame] | 176 | if (inquiry_entry_age(ie) <= INQUIRY_ENTRY_AGE_MAX) { |
| 177 | cp.pscan_rep_mode = ie->data.pscan_rep_mode; |
| 178 | cp.pscan_mode = ie->data.pscan_mode; |
| 179 | cp.clock_offset = ie->data.clock_offset | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 180 | cpu_to_le16(0x8000); |
Marcel Holtmann | 41a9621 | 2008-07-14 20:13:48 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | memcpy(conn->dev_class, ie->data.dev_class, 3); |
Johan Hedberg | 58a681e | 2012-01-16 06:47:28 +0200 | [diff] [blame] | 184 | if (ie->data.ssp_mode > 0) |
| 185 | set_bit(HCI_CONN_SSP_ENABLED, &conn->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 188 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (lmp_rswitch_capable(hdev) && !(hdev->link_mode & HCI_LM_MASTER)) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 190 | cp.role_switch = 0x01; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | else |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 192 | cp.role_switch = 0x00; |
Marcel Holtmann | 4c67bc7 | 2006-10-15 17:30:56 +0200 | [diff] [blame] | 193 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 194 | hci_send_cmd(hdev, HCI_OP_CREATE_CONN, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Vinicius Costa Gomes | 1aef866 | 2012-07-27 19:32:55 -0300 | [diff] [blame] | 197 | static void hci_acl_create_connection_cancel(struct hci_conn *conn) |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 198 | { |
| 199 | struct hci_cp_create_conn_cancel cp; |
| 200 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 201 | BT_DBG("hcon %p", conn); |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 202 | |
Andrei Emeltchenko | d095c1e | 2011-12-01 14:33:27 +0200 | [diff] [blame] | 203 | if (conn->hdev->hci_ver < BLUETOOTH_VER_1_2) |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 204 | return; |
| 205 | |
| 206 | bacpy(&cp.bdaddr, &conn->dst); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 207 | hci_send_cmd(conn->hdev, HCI_OP_CREATE_CONN_CANCEL, sizeof(cp), &cp); |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Claudio Takahasi | 93796fa | 2013-04-11 13:54:56 -0300 | [diff] [blame] | 210 | static void hci_reject_sco(struct hci_conn *conn) |
| 211 | { |
| 212 | struct hci_cp_reject_sync_conn_req cp; |
| 213 | |
Johan Hedberg | d41c15c | 2014-09-24 13:14:46 +0300 | [diff] [blame] | 214 | cp.reason = HCI_ERROR_REJ_LIMITED_RESOURCES; |
Claudio Takahasi | 93796fa | 2013-04-11 13:54:56 -0300 | [diff] [blame] | 215 | bacpy(&cp.bdaddr, &conn->dst); |
| 216 | |
| 217 | hci_send_cmd(conn->hdev, HCI_OP_REJECT_SYNC_CONN_REQ, sizeof(cp), &cp); |
| 218 | } |
| 219 | |
Johan Hedberg | e3b679d | 2014-08-18 20:33:32 +0300 | [diff] [blame] | 220 | int hci_disconnect(struct hci_conn *conn, __u8 reason) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
| 222 | struct hci_cp_disconnect cp; |
| 223 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 224 | BT_DBG("hcon %p", conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Johan Hedberg | 839035a | 2014-08-18 20:33:34 +0300 | [diff] [blame] | 226 | /* When we are master of an established connection and it enters |
| 227 | * the disconnect timeout, then go ahead and try to read the |
| 228 | * current clock offset. Processing of the result is done |
| 229 | * within the event handling and hci_clock_offset_evt function. |
| 230 | */ |
| 231 | if (conn->type == ACL_LINK && conn->role == HCI_ROLE_MASTER) { |
| 232 | struct hci_dev *hdev = conn->hdev; |
Fabian Frederick | 4f639ed | 2014-10-25 10:48:58 +0200 | [diff] [blame] | 233 | struct hci_cp_read_clock_offset clkoff_cp; |
Johan Hedberg | 839035a | 2014-08-18 20:33:34 +0300 | [diff] [blame] | 234 | |
Fabian Frederick | 4f639ed | 2014-10-25 10:48:58 +0200 | [diff] [blame] | 235 | clkoff_cp.handle = cpu_to_le16(conn->handle); |
| 236 | hci_send_cmd(hdev, HCI_OP_READ_CLOCK_OFFSET, sizeof(clkoff_cp), |
| 237 | &clkoff_cp); |
Johan Hedberg | 839035a | 2014-08-18 20:33:34 +0300 | [diff] [blame] | 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | conn->state = BT_DISCONN; |
| 241 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 242 | cp.handle = cpu_to_le16(conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | cp.reason = reason; |
Johan Hedberg | e3b679d | 2014-08-18 20:33:32 +0300 | [diff] [blame] | 244 | return hci_send_cmd(conn->hdev, HCI_OP_DISCONNECT, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Marcel Holtmann | a2b1976 | 2014-06-27 13:45:08 +0200 | [diff] [blame] | 247 | static void hci_amp_disconn(struct hci_conn *conn) |
Andrei Emeltchenko | 53502d6 | 2012-10-10 17:38:27 +0300 | [diff] [blame] | 248 | { |
| 249 | struct hci_cp_disconn_phy_link cp; |
| 250 | |
| 251 | BT_DBG("hcon %p", conn); |
| 252 | |
| 253 | conn->state = BT_DISCONN; |
| 254 | |
| 255 | cp.phy_handle = HCI_PHY_HANDLE(conn->handle); |
Marcel Holtmann | a2b1976 | 2014-06-27 13:45:08 +0200 | [diff] [blame] | 256 | cp.reason = hci_proto_disconn_ind(conn); |
Andrei Emeltchenko | 53502d6 | 2012-10-10 17:38:27 +0300 | [diff] [blame] | 257 | hci_send_cmd(conn->hdev, HCI_OP_DISCONN_PHY_LINK, |
| 258 | sizeof(cp), &cp); |
| 259 | } |
| 260 | |
Vinicius Costa Gomes | 57f5d0d | 2012-07-27 19:32:54 -0300 | [diff] [blame] | 261 | static void hci_add_sco(struct hci_conn *conn, __u16 handle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
| 263 | struct hci_dev *hdev = conn->hdev; |
| 264 | struct hci_cp_add_sco cp; |
| 265 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 266 | BT_DBG("hcon %p", conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
| 268 | conn->state = BT_CONNECT; |
Johan Hedberg | a0c808b | 2012-01-16 09:49:58 +0200 | [diff] [blame] | 269 | conn->out = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 271 | conn->attempt++; |
| 272 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 273 | cp.handle = cpu_to_le16(handle); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 274 | cp.pkt_type = cpu_to_le16(conn->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 276 | hci_send_cmd(hdev, HCI_OP_ADD_SCO, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Frédéric Dalleau | 2dea632 | 2013-08-19 14:24:03 +0200 | [diff] [blame] | 279 | bool hci_setup_sync(struct hci_conn *conn, __u16 handle) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 280 | { |
| 281 | struct hci_dev *hdev = conn->hdev; |
| 282 | struct hci_cp_setup_sync_conn cp; |
Frédéric Dalleau | 2dea632 | 2013-08-19 14:24:03 +0200 | [diff] [blame] | 283 | const struct sco_param *param; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 284 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 285 | BT_DBG("hcon %p", conn); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 286 | |
| 287 | conn->state = BT_CONNECT; |
Johan Hedberg | a0c808b | 2012-01-16 09:49:58 +0200 | [diff] [blame] | 288 | conn->out = true; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 289 | |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 290 | conn->attempt++; |
| 291 | |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 292 | cp.handle = cpu_to_le16(handle); |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 293 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 294 | cp.tx_bandwidth = cpu_to_le32(0x00001f40); |
| 295 | cp.rx_bandwidth = cpu_to_le32(0x00001f40); |
Frédéric Dalleau | 10c62dd | 2013-08-19 14:23:59 +0200 | [diff] [blame] | 296 | cp.voice_setting = cpu_to_le16(conn->setting); |
| 297 | |
| 298 | switch (conn->setting & SCO_AIRMODE_MASK) { |
| 299 | case SCO_AIRMODE_TRANSP: |
Johan Hedberg | 565766b | 2014-09-25 09:48:01 +0300 | [diff] [blame] | 300 | if (conn->attempt > ARRAY_SIZE(esco_param_msbc)) |
Frédéric Dalleau | 2dea632 | 2013-08-19 14:24:03 +0200 | [diff] [blame] | 301 | return false; |
Johan Hedberg | 565766b | 2014-09-25 09:48:01 +0300 | [diff] [blame] | 302 | param = &esco_param_msbc[conn->attempt - 1]; |
Frédéric Dalleau | 10c62dd | 2013-08-19 14:23:59 +0200 | [diff] [blame] | 303 | break; |
| 304 | case SCO_AIRMODE_CVSD: |
Bernhard Thaler | 48e68ff | 2014-09-23 11:01:07 +0200 | [diff] [blame] | 305 | if (lmp_esco_capable(conn->link)) { |
| 306 | if (conn->attempt > ARRAY_SIZE(esco_param_cvsd)) |
| 307 | return false; |
Bernhard Thaler | 48e68ff | 2014-09-23 11:01:07 +0200 | [diff] [blame] | 308 | param = &esco_param_cvsd[conn->attempt - 1]; |
| 309 | } else { |
| 310 | if (conn->attempt > ARRAY_SIZE(sco_param_cvsd)) |
| 311 | return false; |
Bernhard Thaler | 48e68ff | 2014-09-23 11:01:07 +0200 | [diff] [blame] | 312 | param = &sco_param_cvsd[conn->attempt - 1]; |
| 313 | } |
Frédéric Dalleau | 10c62dd | 2013-08-19 14:23:59 +0200 | [diff] [blame] | 314 | break; |
Frédéric Dalleau | 2dea632 | 2013-08-19 14:24:03 +0200 | [diff] [blame] | 315 | default: |
| 316 | return false; |
Frédéric Dalleau | 10c62dd | 2013-08-19 14:23:59 +0200 | [diff] [blame] | 317 | } |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 318 | |
Johan Hedberg | c7da579 | 2014-09-24 22:41:46 +0300 | [diff] [blame] | 319 | cp.retrans_effort = param->retrans_effort; |
Frédéric Dalleau | 2dea632 | 2013-08-19 14:24:03 +0200 | [diff] [blame] | 320 | cp.pkt_type = __cpu_to_le16(param->pkt_type); |
| 321 | cp.max_latency = __cpu_to_le16(param->max_latency); |
| 322 | |
| 323 | if (hci_send_cmd(hdev, HCI_OP_SETUP_SYNC_CONN, sizeof(cp), &cp) < 0) |
| 324 | return false; |
| 325 | |
| 326 | return true; |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Johan Hedberg | 7d6ca69 | 2014-07-02 17:37:31 +0300 | [diff] [blame] | 329 | u8 hci_le_conn_update(struct hci_conn *conn, u16 min, u16 max, u16 latency, |
| 330 | u16 to_multiplier) |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 331 | { |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 332 | struct hci_dev *hdev = conn->hdev; |
Marcel Holtmann | f044eb0 | 2014-06-29 16:43:26 +0200 | [diff] [blame] | 333 | struct hci_conn_params *params; |
| 334 | struct hci_cp_le_conn_update cp; |
| 335 | |
| 336 | hci_dev_lock(hdev); |
| 337 | |
| 338 | params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); |
| 339 | if (params) { |
| 340 | params->conn_min_interval = min; |
| 341 | params->conn_max_interval = max; |
| 342 | params->conn_latency = latency; |
| 343 | params->supervision_timeout = to_multiplier; |
| 344 | } |
| 345 | |
| 346 | hci_dev_unlock(hdev); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 347 | |
| 348 | memset(&cp, 0, sizeof(cp)); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 349 | cp.handle = cpu_to_le16(conn->handle); |
| 350 | cp.conn_interval_min = cpu_to_le16(min); |
| 351 | cp.conn_interval_max = cpu_to_le16(max); |
| 352 | cp.conn_latency = cpu_to_le16(latency); |
| 353 | cp.supervision_timeout = cpu_to_le16(to_multiplier); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 354 | cp.min_ce_len = cpu_to_le16(0x0000); |
| 355 | cp.max_ce_len = cpu_to_le16(0x0000); |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 356 | |
| 357 | hci_send_cmd(hdev, HCI_OP_LE_CONN_UPDATE, sizeof(cp), &cp); |
Johan Hedberg | 7d6ca69 | 2014-07-02 17:37:31 +0300 | [diff] [blame] | 358 | |
| 359 | if (params) |
| 360 | return 0x01; |
| 361 | |
| 362 | return 0x00; |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 363 | } |
Claudio Takahasi | 2ce603e | 2011-02-16 20:44:53 -0200 | [diff] [blame] | 364 | |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 365 | void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, |
Johan Hedberg | 8b76ce3 | 2015-06-08 18:14:39 +0300 | [diff] [blame] | 366 | __u8 ltk[16], __u8 key_size) |
Vinicius Costa Gomes | a7a595f | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 367 | { |
| 368 | struct hci_dev *hdev = conn->hdev; |
| 369 | struct hci_cp_le_start_enc cp; |
| 370 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 371 | BT_DBG("hcon %p", conn); |
Vinicius Costa Gomes | a7a595f | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 372 | |
| 373 | memset(&cp, 0, sizeof(cp)); |
| 374 | |
| 375 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 376 | cp.rand = rand; |
Vinicius Costa Gomes | a7a595f | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 377 | cp.ediv = ediv; |
Johan Hedberg | 8b76ce3 | 2015-06-08 18:14:39 +0300 | [diff] [blame] | 378 | memcpy(cp.ltk, ltk, key_size); |
Vinicius Costa Gomes | a7a595f | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 379 | |
| 380 | hci_send_cmd(hdev, HCI_OP_LE_START_ENC, sizeof(cp), &cp); |
| 381 | } |
Vinicius Costa Gomes | a7a595f | 2011-06-09 18:50:47 -0300 | [diff] [blame] | 382 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 383 | /* Device _must_ be locked */ |
| 384 | void hci_sco_setup(struct hci_conn *conn, __u8 status) |
| 385 | { |
| 386 | struct hci_conn *sco = conn->link; |
| 387 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 388 | if (!sco) |
| 389 | return; |
| 390 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 391 | BT_DBG("hcon %p", conn); |
| 392 | |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 393 | if (!status) { |
| 394 | if (lmp_esco_capable(conn->hdev)) |
| 395 | hci_setup_sync(sco, conn->handle); |
| 396 | else |
| 397 | hci_add_sco(sco, conn->handle); |
| 398 | } else { |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 399 | hci_connect_cfm(sco, status); |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 400 | hci_conn_del(sco); |
| 401 | } |
| 402 | } |
| 403 | |
Gustavo F. Padovan | 19c40e3 | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 404 | static void hci_conn_timeout(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
Gustavo F. Padovan | 19c40e3 | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 406 | struct hci_conn *conn = container_of(work, struct hci_conn, |
Gustavo Padovan | 5974e4c | 2012-05-17 00:36:25 -0300 | [diff] [blame] | 407 | disc_work.work); |
Lukasz Rymanowski | 1d56dc4 | 2014-06-17 13:04:20 +0200 | [diff] [blame] | 408 | int refcnt = atomic_read(&conn->refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 410 | BT_DBG("hcon %p state %s", conn, state_to_string(conn->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Lukasz Rymanowski | 1d56dc4 | 2014-06-17 13:04:20 +0200 | [diff] [blame] | 412 | WARN_ON(refcnt < 0); |
| 413 | |
| 414 | /* FIXME: It was observed that in pairing failed scenario, refcnt |
| 415 | * drops below 0. Probably this is because l2cap_conn_del calls |
| 416 | * l2cap_chan_del for each channel, and inside l2cap_chan_del conn is |
| 417 | * dropped. After that loop hci_chan_del is called which also drops |
| 418 | * conn. For now make sure that ACL is alive if refcnt is higher then 0, |
| 419 | * otherwise drop it. |
| 420 | */ |
| 421 | if (refcnt > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | return; |
| 423 | |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 424 | switch (conn->state) { |
| 425 | case BT_CONNECT: |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 426 | case BT_CONNECT2: |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 427 | if (conn->out) { |
| 428 | if (conn->type == ACL_LINK) |
Vinicius Costa Gomes | 1aef866 | 2012-07-27 19:32:55 -0300 | [diff] [blame] | 429 | hci_acl_create_connection_cancel(conn); |
Jakub Pawlowski | cc2b691 | 2015-08-07 20:22:55 +0200 | [diff] [blame] | 430 | else if (conn->type == LE_LINK) { |
| 431 | if (test_bit(HCI_CONN_SCANNING, &conn->flags)) |
| 432 | hci_connect_le_scan_remove(conn); |
| 433 | else |
| 434 | hci_le_create_connection_cancel(conn); |
| 435 | } |
Claudio Takahasi | 93796fa | 2013-04-11 13:54:56 -0300 | [diff] [blame] | 436 | } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) { |
| 437 | hci_reject_sco(conn); |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 438 | } |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 439 | break; |
Marcel Holtmann | 769be97 | 2008-07-14 20:13:49 +0200 | [diff] [blame] | 440 | case BT_CONFIG: |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 441 | case BT_CONNECTED: |
Marcel Holtmann | 40051e4 | 2014-06-27 13:45:09 +0200 | [diff] [blame] | 442 | if (conn->type == AMP_LINK) { |
| 443 | hci_amp_disconn(conn); |
| 444 | } else { |
| 445 | __u8 reason = hci_proto_disconn_ind(conn); |
| 446 | hci_disconnect(conn, reason); |
| 447 | } |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 448 | break; |
| 449 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | conn->state = BT_CLOSED; |
Marcel Holtmann | 6ac5934 | 2006-09-26 09:43:48 +0200 | [diff] [blame] | 451 | break; |
| 452 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Gustavo F. Padovan | 416dc94 | 2011-12-07 13:24:33 -0200 | [diff] [blame] | 455 | /* Enter sniff mode */ |
Johan Hedberg | a74a84f | 2013-10-16 18:11:40 +0300 | [diff] [blame] | 456 | static void hci_conn_idle(struct work_struct *work) |
Gustavo F. Padovan | 416dc94 | 2011-12-07 13:24:33 -0200 | [diff] [blame] | 457 | { |
Johan Hedberg | a74a84f | 2013-10-16 18:11:40 +0300 | [diff] [blame] | 458 | struct hci_conn *conn = container_of(work, struct hci_conn, |
| 459 | idle_work.work); |
Gustavo F. Padovan | 416dc94 | 2011-12-07 13:24:33 -0200 | [diff] [blame] | 460 | struct hci_dev *hdev = conn->hdev; |
| 461 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 462 | BT_DBG("hcon %p mode %d", conn, conn->mode); |
Gustavo F. Padovan | 416dc94 | 2011-12-07 13:24:33 -0200 | [diff] [blame] | 463 | |
Gustavo F. Padovan | 416dc94 | 2011-12-07 13:24:33 -0200 | [diff] [blame] | 464 | if (!lmp_sniff_capable(hdev) || !lmp_sniff_capable(conn)) |
| 465 | return; |
| 466 | |
| 467 | if (conn->mode != HCI_CM_ACTIVE || !(conn->link_policy & HCI_LP_SNIFF)) |
| 468 | return; |
| 469 | |
| 470 | if (lmp_sniffsubr_capable(hdev) && lmp_sniffsubr_capable(conn)) { |
| 471 | struct hci_cp_sniff_subrate cp; |
| 472 | cp.handle = cpu_to_le16(conn->handle); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 473 | cp.max_latency = cpu_to_le16(0); |
| 474 | cp.min_remote_timeout = cpu_to_le16(0); |
| 475 | cp.min_local_timeout = cpu_to_le16(0); |
Gustavo F. Padovan | 416dc94 | 2011-12-07 13:24:33 -0200 | [diff] [blame] | 476 | hci_send_cmd(hdev, HCI_OP_SNIFF_SUBRATE, sizeof(cp), &cp); |
| 477 | } |
| 478 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 479 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { |
Gustavo F. Padovan | 416dc94 | 2011-12-07 13:24:33 -0200 | [diff] [blame] | 480 | struct hci_cp_sniff_mode cp; |
| 481 | cp.handle = cpu_to_le16(conn->handle); |
| 482 | cp.max_interval = cpu_to_le16(hdev->sniff_max_interval); |
| 483 | cp.min_interval = cpu_to_le16(hdev->sniff_min_interval); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 484 | cp.attempt = cpu_to_le16(4); |
| 485 | cp.timeout = cpu_to_le16(1); |
Gustavo F. Padovan | 416dc94 | 2011-12-07 13:24:33 -0200 | [diff] [blame] | 486 | hci_send_cmd(hdev, HCI_OP_SNIFF_MODE, sizeof(cp), &cp); |
| 487 | } |
| 488 | } |
| 489 | |
Johan Hedberg | 7bc18d9 | 2013-10-16 18:11:39 +0300 | [diff] [blame] | 490 | static void hci_conn_auto_accept(struct work_struct *work) |
Johan Hedberg | 9f61656 | 2011-04-28 11:28:54 -0700 | [diff] [blame] | 491 | { |
Johan Hedberg | 7bc18d9 | 2013-10-16 18:11:39 +0300 | [diff] [blame] | 492 | struct hci_conn *conn = container_of(work, struct hci_conn, |
| 493 | auto_accept_work.work); |
Johan Hedberg | 9f61656 | 2011-04-28 11:28:54 -0700 | [diff] [blame] | 494 | |
Johan Hedberg | 7bc18d9 | 2013-10-16 18:11:39 +0300 | [diff] [blame] | 495 | hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), |
Gustavo Padovan | 5974e4c | 2012-05-17 00:36:25 -0300 | [diff] [blame] | 496 | &conn->dst); |
Johan Hedberg | 9f61656 | 2011-04-28 11:28:54 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Johan Hedberg | 9489eca | 2014-02-28 17:45:46 +0200 | [diff] [blame] | 499 | static void le_conn_timeout(struct work_struct *work) |
| 500 | { |
| 501 | struct hci_conn *conn = container_of(work, struct hci_conn, |
| 502 | le_conn_timeout.work); |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 503 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 9489eca | 2014-02-28 17:45:46 +0200 | [diff] [blame] | 504 | |
| 505 | BT_DBG(""); |
| 506 | |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 507 | /* We could end up here due to having done directed advertising, |
| 508 | * so clean up the state if necessary. This should however only |
| 509 | * happen with broken hardware or if low duty cycle was used |
| 510 | * (which doesn't have a timeout of its own). |
| 511 | */ |
Johan Hedberg | 0b1db38 | 2014-10-28 22:23:27 +0100 | [diff] [blame] | 512 | if (conn->role == HCI_ROLE_SLAVE) { |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 513 | u8 enable = 0x00; |
| 514 | hci_send_cmd(hdev, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), |
| 515 | &enable); |
| 516 | hci_le_conn_failed(conn, HCI_ERROR_ADVERTISING_TIMEOUT); |
| 517 | return; |
| 518 | } |
| 519 | |
Johan Hedberg | 9489eca | 2014-02-28 17:45:46 +0200 | [diff] [blame] | 520 | hci_le_create_connection_cancel(conn); |
| 521 | } |
| 522 | |
Johan Hedberg | a5c4e30 | 2014-07-16 11:56:07 +0300 | [diff] [blame] | 523 | struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst, |
| 524 | u8 role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | { |
| 526 | struct hci_conn *conn; |
| 527 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 528 | BT_DBG("%s dst %pMR", hdev->name, dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Johan Hedberg | 27f70f3 | 2014-07-21 10:50:06 +0300 | [diff] [blame] | 530 | conn = kzalloc(sizeof(*conn), GFP_KERNEL); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 531 | if (!conn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | |
| 534 | bacpy(&conn->dst, dst); |
Marcel Holtmann | 662e882 | 2013-10-13 05:23:59 -0700 | [diff] [blame] | 535 | bacpy(&conn->src, &hdev->bdaddr); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 536 | conn->hdev = hdev; |
| 537 | conn->type = type; |
Johan Hedberg | a5c4e30 | 2014-07-16 11:56:07 +0300 | [diff] [blame] | 538 | conn->role = role; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 539 | conn->mode = HCI_CM_ACTIVE; |
| 540 | conn->state = BT_OPEN; |
Andrei Emeltchenko | 93f19c9 | 2009-09-03 12:34:19 +0300 | [diff] [blame] | 541 | conn->auth_type = HCI_AT_GENERAL_BONDING; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 542 | conn->io_capability = hdev->io_capability; |
Johan Hedberg | a958355 | 2011-02-19 12:06:01 -0300 | [diff] [blame] | 543 | conn->remote_auth = 0xff; |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 544 | conn->key_type = 0xff; |
Johan Hedberg | ebf86aa | 2014-12-05 13:36:08 +0200 | [diff] [blame] | 545 | conn->rssi = HCI_RSSI_INVALID; |
Andrzej Kaczmarek | 5a134fa | 2014-05-09 21:35:28 +0200 | [diff] [blame] | 546 | conn->tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | d0455ed | 2014-05-14 13:43:05 +0200 | [diff] [blame] | 547 | conn->max_tx_power = HCI_TX_POWER_INVALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Johan Hedberg | 58a681e | 2012-01-16 06:47:28 +0200 | [diff] [blame] | 549 | set_bit(HCI_CONN_POWER_SAVE, &conn->flags); |
Marcel Holtmann | 052b30b | 2009-04-26 20:01:22 +0200 | [diff] [blame] | 550 | conn->disc_timeout = HCI_DISCONN_TIMEOUT; |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 551 | |
Johan Hedberg | a5c4e30 | 2014-07-16 11:56:07 +0300 | [diff] [blame] | 552 | if (conn->role == HCI_ROLE_MASTER) |
| 553 | conn->out = true; |
| 554 | |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 555 | switch (type) { |
| 556 | case ACL_LINK: |
| 557 | conn->pkt_type = hdev->pkt_type & ACL_PTYPE_MASK; |
| 558 | break; |
Johan Hedberg | 9c84d1d | 2014-03-24 20:21:50 +0200 | [diff] [blame] | 559 | case LE_LINK: |
| 560 | /* conn->src should reflect the local identity address */ |
| 561 | hci_copy_identity_address(hdev, &conn->src, &conn->src_type); |
| 562 | break; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 563 | case SCO_LINK: |
| 564 | if (lmp_esco_capable(hdev)) |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 565 | conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) | |
| 566 | (hdev->esco_type & EDR_ESCO_MASK); |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 567 | else |
| 568 | conn->pkt_type = hdev->pkt_type & SCO_PTYPE_MASK; |
| 569 | break; |
| 570 | case ESCO_LINK: |
Marcel Holtmann | efc7688 | 2009-02-06 09:13:37 +0100 | [diff] [blame] | 571 | conn->pkt_type = hdev->esco_type & ~EDR_ESCO_MASK; |
Marcel Holtmann | a874641 | 2008-07-14 20:13:46 +0200 | [diff] [blame] | 572 | break; |
| 573 | } |
| 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | skb_queue_head_init(&conn->data_q); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 576 | |
Marcel Holtmann | 70c1f20 | 2012-02-22 12:06:43 +0100 | [diff] [blame] | 577 | INIT_LIST_HEAD(&conn->chan_list); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 578 | |
Gustavo F. Padovan | 19c40e3 | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 579 | INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout); |
Johan Hedberg | 7bc18d9 | 2013-10-16 18:11:39 +0300 | [diff] [blame] | 580 | INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept); |
Johan Hedberg | a74a84f | 2013-10-16 18:11:40 +0300 | [diff] [blame] | 581 | INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle); |
Johan Hedberg | 9489eca | 2014-02-28 17:45:46 +0200 | [diff] [blame] | 582 | INIT_DELAYED_WORK(&conn->le_conn_timeout, le_conn_timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
| 584 | atomic_set(&conn->refcnt, 0); |
| 585 | |
| 586 | hci_dev_hold(hdev); |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | hci_conn_hash_add(hdev, conn); |
Gustavo F. Padovan | 3c54711 | 2011-12-14 22:58:44 -0200 | [diff] [blame] | 589 | if (hdev->notify) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | hdev->notify(hdev, HCI_NOTIFY_CONN_ADD); |
| 591 | |
Marcel Holtmann | a67e899 | 2009-05-02 18:24:06 -0700 | [diff] [blame] | 592 | hci_conn_init_sysfs(conn); |
| 593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | return conn; |
| 595 | } |
| 596 | |
| 597 | int hci_conn_del(struct hci_conn *conn) |
| 598 | { |
| 599 | struct hci_dev *hdev = conn->hdev; |
| 600 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 601 | BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
Gustavo F. Padovan | 19c40e3 | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 603 | cancel_delayed_work_sync(&conn->disc_work); |
Johan Hedberg | 7bc18d9 | 2013-10-16 18:11:39 +0300 | [diff] [blame] | 604 | cancel_delayed_work_sync(&conn->auto_accept_work); |
Johan Hedberg | a74a84f | 2013-10-16 18:11:40 +0300 | [diff] [blame] | 605 | cancel_delayed_work_sync(&conn->idle_work); |
Johan Hedberg | 9f61656 | 2011-04-28 11:28:54 -0700 | [diff] [blame] | 606 | |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 607 | if (conn->type == ACL_LINK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | struct hci_conn *sco = conn->link; |
| 609 | if (sco) |
| 610 | sco->link = NULL; |
| 611 | |
| 612 | /* Unacked frames */ |
| 613 | hdev->acl_cnt += conn->sent; |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 614 | } else if (conn->type == LE_LINK) { |
Johan Hedberg | 980ffc0 | 2014-10-28 22:23:26 +0100 | [diff] [blame] | 615 | cancel_delayed_work(&conn->le_conn_timeout); |
Johan Hedberg | 9489eca | 2014-02-28 17:45:46 +0200 | [diff] [blame] | 616 | |
Ville Tervo | 6ed58ec | 2011-02-10 22:38:48 -0300 | [diff] [blame] | 617 | if (hdev->le_pkts) |
| 618 | hdev->le_cnt += conn->sent; |
| 619 | else |
| 620 | hdev->acl_cnt += conn->sent; |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 621 | } else { |
| 622 | struct hci_conn *acl = conn->link; |
| 623 | if (acl) { |
| 624 | acl->link = NULL; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 625 | hci_conn_drop(acl); |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Andrei Emeltchenko | 9740e49 | 2012-05-29 13:59:02 +0300 | [diff] [blame] | 629 | if (conn->amp_mgr) |
| 630 | amp_mgr_put(conn->amp_mgr); |
| 631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | skb_queue_purge(&conn->data_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Johan Hedberg | b958f9a | 2015-10-16 10:07:50 +0300 | [diff] [blame] | 634 | /* Remove the connection from the list and cleanup its remaining |
| 635 | * state. This is a separate function since for some cases like |
| 636 | * BT_CONNECT_SCAN we *only* want the cleanup part without the |
| 637 | * rest of hci_conn_del. |
| 638 | */ |
| 639 | hci_conn_cleanup(conn); |
Tomas Targownik | 163f4da | 2011-06-30 16:30:44 -0300 | [diff] [blame] | 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src) |
| 645 | { |
| 646 | int use_src = bacmp(src, BDADDR_ANY); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 647 | struct hci_dev *hdev = NULL, *d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 649 | BT_DBG("%pMR -> %pMR", src, dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 651 | read_lock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 653 | list_for_each_entry(d, &hci_dev_list, list) { |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 654 | if (!test_bit(HCI_UP, &d->flags) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 655 | hci_dev_test_flag(d, HCI_USER_CHANNEL) || |
Andrei Emeltchenko | d300fa9 | 2012-06-19 15:21:21 +0300 | [diff] [blame] | 656 | d->dev_type != HCI_BREDR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | continue; |
| 658 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 659 | /* Simple routing: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | * No source address - find interface with bdaddr != dst |
| 661 | * Source address - find interface with bdaddr == src |
| 662 | */ |
| 663 | |
| 664 | if (use_src) { |
| 665 | if (!bacmp(&d->bdaddr, src)) { |
| 666 | hdev = d; break; |
| 667 | } |
| 668 | } else { |
| 669 | if (bacmp(&d->bdaddr, dst)) { |
| 670 | hdev = d; break; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | if (hdev) |
| 676 | hdev = hci_dev_hold(hdev); |
| 677 | |
Gustavo F. Padovan | f20d09d | 2011-12-22 16:30:27 -0200 | [diff] [blame] | 678 | read_unlock(&hci_dev_list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | return hdev; |
| 680 | } |
| 681 | EXPORT_SYMBOL(hci_get_route); |
| 682 | |
Andre Guedes | 9bb3c01 | 2014-01-30 18:22:08 -0300 | [diff] [blame] | 683 | /* This function requires the caller holds hdev->lock */ |
Andre Guedes | 06c053f | 2014-02-26 20:21:41 -0300 | [diff] [blame] | 684 | void hci_le_conn_failed(struct hci_conn *conn, u8 status) |
Andre Guedes | 9bb3c01 | 2014-01-30 18:22:08 -0300 | [diff] [blame] | 685 | { |
| 686 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | f161dd4 | 2014-08-15 21:06:54 +0300 | [diff] [blame] | 687 | struct hci_conn_params *params; |
| 688 | |
| 689 | params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst, |
| 690 | conn->dst_type); |
| 691 | if (params && params->conn) { |
| 692 | hci_conn_drop(params->conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 693 | hci_conn_put(params->conn); |
Johan Hedberg | f161dd4 | 2014-08-15 21:06:54 +0300 | [diff] [blame] | 694 | params->conn = NULL; |
| 695 | } |
Andre Guedes | 9bb3c01 | 2014-01-30 18:22:08 -0300 | [diff] [blame] | 696 | |
| 697 | conn->state = BT_CLOSED; |
| 698 | |
| 699 | mgmt_connect_failed(hdev, &conn->dst, conn->type, conn->dst_type, |
| 700 | status); |
| 701 | |
Johan Hedberg | 539c496 | 2015-02-18 14:53:57 +0200 | [diff] [blame] | 702 | hci_connect_cfm(conn, status); |
Andre Guedes | 9bb3c01 | 2014-01-30 18:22:08 -0300 | [diff] [blame] | 703 | |
| 704 | hci_conn_del(conn); |
Andre Guedes | a4790db | 2014-02-26 20:21:47 -0300 | [diff] [blame] | 705 | |
| 706 | /* Since we may have temporarily stopped the background scanning in |
| 707 | * favor of connection establishment, we should restart it. |
| 708 | */ |
| 709 | hci_update_background_scan(hdev); |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 710 | |
| 711 | /* Re-enable advertising in case this was a failed connection |
| 712 | * attempt as a peripheral. |
| 713 | */ |
| 714 | mgmt_reenable_advertising(hdev); |
Andre Guedes | 9bb3c01 | 2014-01-30 18:22:08 -0300 | [diff] [blame] | 715 | } |
| 716 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 717 | static void create_le_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Andre Guedes | 1d399ae | 2013-10-08 08:21:17 -0300 | [diff] [blame] | 718 | { |
| 719 | struct hci_conn *conn; |
| 720 | |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 721 | hci_dev_lock(hdev); |
| 722 | |
| 723 | conn = hci_lookup_le_connect(hdev); |
| 724 | |
| 725 | if (!status) { |
| 726 | hci_connect_le_scan_cleanup(conn); |
| 727 | goto done; |
| 728 | } |
Andre Guedes | 1d399ae | 2013-10-08 08:21:17 -0300 | [diff] [blame] | 729 | |
| 730 | BT_ERR("HCI request failed to create LE connection: status 0x%2.2x", |
| 731 | status); |
| 732 | |
Andre Guedes | 1d399ae | 2013-10-08 08:21:17 -0300 | [diff] [blame] | 733 | if (!conn) |
| 734 | goto done; |
| 735 | |
Andre Guedes | 06c053f | 2014-02-26 20:21:41 -0300 | [diff] [blame] | 736 | hci_le_conn_failed(conn, status); |
Andre Guedes | 1d399ae | 2013-10-08 08:21:17 -0300 | [diff] [blame] | 737 | |
| 738 | done: |
| 739 | hci_dev_unlock(hdev); |
| 740 | } |
| 741 | |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 742 | static void hci_req_add_le_create_conn(struct hci_request *req, |
| 743 | struct hci_conn *conn) |
| 744 | { |
| 745 | struct hci_cp_le_create_conn cp; |
| 746 | struct hci_dev *hdev = conn->hdev; |
| 747 | u8 own_addr_type; |
| 748 | |
| 749 | memset(&cp, 0, sizeof(cp)); |
| 750 | |
| 751 | /* Update random address, but set require_privacy to false so |
Marcel Holtmann | 9437d2e | 2014-12-07 20:13:17 +0100 | [diff] [blame] | 752 | * that we never connect with an non-resolvable address. |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 753 | */ |
| 754 | if (hci_update_random_address(req, false, &own_addr_type)) |
| 755 | return; |
| 756 | |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 757 | cp.scan_interval = cpu_to_le16(hdev->le_scan_interval); |
| 758 | cp.scan_window = cpu_to_le16(hdev->le_scan_window); |
| 759 | bacpy(&cp.peer_addr, &conn->dst); |
| 760 | cp.peer_addr_type = conn->dst_type; |
| 761 | cp.own_address_type = own_addr_type; |
| 762 | cp.conn_interval_min = cpu_to_le16(conn->le_conn_min_interval); |
| 763 | cp.conn_interval_max = cpu_to_le16(conn->le_conn_max_interval); |
Marcel Holtmann | 037fc41 | 2014-06-29 16:43:27 +0200 | [diff] [blame] | 764 | cp.conn_latency = cpu_to_le16(conn->le_conn_latency); |
| 765 | cp.supervision_timeout = cpu_to_le16(conn->le_supv_timeout); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 766 | cp.min_ce_len = cpu_to_le16(0x0000); |
| 767 | cp.max_ce_len = cpu_to_le16(0x0000); |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 768 | |
| 769 | hci_req_add(req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp); |
Johan Hedberg | b46e003 | 2014-02-28 12:54:15 +0200 | [diff] [blame] | 770 | |
| 771 | conn->state = BT_CONNECT; |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 772 | clear_bit(HCI_CONN_SCANNING, &conn->flags); |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 773 | } |
| 774 | |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 775 | static void hci_req_directed_advertising(struct hci_request *req, |
| 776 | struct hci_conn *conn) |
| 777 | { |
| 778 | struct hci_dev *hdev = req->hdev; |
| 779 | struct hci_cp_le_set_adv_param cp; |
| 780 | u8 own_addr_type; |
| 781 | u8 enable; |
| 782 | |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 783 | /* Clear the HCI_LE_ADV bit temporarily so that the |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 784 | * hci_update_random_address knows that it's safe to go ahead |
| 785 | * and write a new random address. The flag will be set back on |
| 786 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 787 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 788 | hci_dev_clear_flag(hdev, HCI_LE_ADV); |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 789 | |
| 790 | /* Set require_privacy to false so that the remote device has a |
| 791 | * chance of identifying us. |
| 792 | */ |
| 793 | if (hci_update_random_address(req, false, &own_addr_type) < 0) |
| 794 | return; |
| 795 | |
| 796 | memset(&cp, 0, sizeof(cp)); |
| 797 | cp.type = LE_ADV_DIRECT_IND; |
| 798 | cp.own_address_type = own_addr_type; |
| 799 | cp.direct_addr_type = conn->dst_type; |
| 800 | bacpy(&cp.direct_addr, &conn->dst); |
| 801 | cp.channel_map = hdev->le_adv_channel_map; |
| 802 | |
| 803 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 804 | |
| 805 | enable = 0x01; |
| 806 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 807 | |
| 808 | conn->state = BT_CONNECT; |
| 809 | } |
| 810 | |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 811 | struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 812 | u8 dst_type, u8 sec_level, u16 conn_timeout, |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 813 | u8 role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | { |
Andre Guedes | 4292f1f | 2014-02-03 13:56:19 -0300 | [diff] [blame] | 815 | struct hci_conn_params *params; |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 816 | struct hci_conn *conn, *conn_unfinished; |
Johan Hedberg | 1ebfcc1 | 2014-02-18 21:41:36 +0200 | [diff] [blame] | 817 | struct smp_irk *irk; |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 818 | struct hci_request req; |
Andre Guedes | 1d399ae | 2013-10-08 08:21:17 -0300 | [diff] [blame] | 819 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
Lukasz Rymanowski | 152d386 | 2015-02-11 12:31:40 +0100 | [diff] [blame] | 821 | /* Let's make sure that le is enabled.*/ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 822 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Lukasz Rymanowski | 152d386 | 2015-02-11 12:31:40 +0100 | [diff] [blame] | 823 | if (lmp_le_capable(hdev)) |
| 824 | return ERR_PTR(-ECONNREFUSED); |
| 825 | |
| 826 | return ERR_PTR(-EOPNOTSUPP); |
| 827 | } |
| 828 | |
Andre Guedes | 620ad52 | 2013-10-08 08:21:18 -0300 | [diff] [blame] | 829 | /* Some devices send ATT messages as soon as the physical link is |
| 830 | * established. To be able to handle these ATT messages, the user- |
| 831 | * space first establishes the connection and then starts the pairing |
| 832 | * process. |
| 833 | * |
| 834 | * So if a hci_conn object already exists for the following connection |
| 835 | * attempt, we simply update pending_sec_level and auth_type fields |
| 836 | * and return the object found. |
| 837 | */ |
Andre Guedes | f1e5d54 | 2013-10-03 18:25:44 -0300 | [diff] [blame] | 838 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 839 | conn_unfinished = NULL; |
Andre Guedes | 620ad52 | 2013-10-08 08:21:18 -0300 | [diff] [blame] | 840 | if (conn) { |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 841 | if (conn->state == BT_CONNECT && |
| 842 | test_bit(HCI_CONN_SCANNING, &conn->flags)) { |
| 843 | BT_DBG("will continue unfinished conn %pMR", dst); |
| 844 | conn_unfinished = conn; |
| 845 | } else { |
| 846 | if (conn->pending_sec_level < sec_level) |
| 847 | conn->pending_sec_level = sec_level; |
| 848 | goto done; |
| 849 | } |
Ville Tervo | fcd89c0 | 2011-02-10 22:38:47 -0300 | [diff] [blame] | 850 | } |
| 851 | |
Andre Guedes | 620ad52 | 2013-10-08 08:21:18 -0300 | [diff] [blame] | 852 | /* Since the controller supports only one LE connection attempt at a |
| 853 | * time, we return -EBUSY if there is any connection attempt running. |
| 854 | */ |
Jakub Pawlowski | e7d9ab7 | 2015-08-07 20:22:52 +0200 | [diff] [blame] | 855 | if (hci_lookup_le_connect(hdev)) |
Andre Guedes | 620ad52 | 2013-10-08 08:21:18 -0300 | [diff] [blame] | 856 | return ERR_PTR(-EBUSY); |
| 857 | |
Marcel Holtmann | edb4b46 | 2014-02-18 15:13:43 -0800 | [diff] [blame] | 858 | /* When given an identity address with existing identity |
| 859 | * resolving key, the connection needs to be established |
| 860 | * to a resolvable random address. |
| 861 | * |
Marcel Holtmann | edb4b46 | 2014-02-18 15:13:43 -0800 | [diff] [blame] | 862 | * Storing the resolvable random address is required here |
| 863 | * to handle connection failures. The address will later |
| 864 | * be resolved back into the original identity address |
| 865 | * from the connect request. |
| 866 | */ |
Johan Hedberg | 1ebfcc1 | 2014-02-18 21:41:36 +0200 | [diff] [blame] | 867 | irk = hci_find_irk_by_addr(hdev, dst, dst_type); |
| 868 | if (irk && bacmp(&irk->rpa, BDADDR_ANY)) { |
| 869 | dst = &irk->rpa; |
| 870 | dst_type = ADDR_LE_DEV_RANDOM; |
| 871 | } |
| 872 | |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 873 | if (conn_unfinished) { |
| 874 | conn = conn_unfinished; |
| 875 | bacpy(&conn->dst, dst); |
| 876 | } else { |
| 877 | conn = hci_conn_add(hdev, LE_LINK, dst, role); |
| 878 | } |
| 879 | |
Andre Guedes | 620ad52 | 2013-10-08 08:21:18 -0300 | [diff] [blame] | 880 | if (!conn) |
| 881 | return ERR_PTR(-ENOMEM); |
| 882 | |
Johan Hedberg | 1ebfcc1 | 2014-02-18 21:41:36 +0200 | [diff] [blame] | 883 | conn->dst_type = dst_type; |
Andre Guedes | 620ad52 | 2013-10-08 08:21:18 -0300 | [diff] [blame] | 884 | conn->sec_level = BT_SECURITY_LOW; |
Johan Hedberg | 09ae260 | 2014-07-06 13:41:15 +0300 | [diff] [blame] | 885 | conn->conn_timeout = conn_timeout; |
Andre Guedes | 4292f1f | 2014-02-03 13:56:19 -0300 | [diff] [blame] | 886 | |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 887 | if (!conn_unfinished) |
| 888 | conn->pending_sec_level = sec_level; |
| 889 | |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 890 | hci_req_init(&req, hdev); |
| 891 | |
Johan Hedberg | 376f54c | 2014-07-08 15:07:52 +0300 | [diff] [blame] | 892 | /* Disable advertising if we're active. For master role |
| 893 | * connections most controllers will refuse to connect if |
| 894 | * advertising is enabled, and for slave role connections we |
| 895 | * anyway have to disable it in order to start directed |
| 896 | * advertising. |
| 897 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 898 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) { |
Johan Hedberg | 376f54c | 2014-07-08 15:07:52 +0300 | [diff] [blame] | 899 | u8 enable = 0x00; |
| 900 | hci_req_add(&req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), |
| 901 | &enable); |
| 902 | } |
| 903 | |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 904 | /* If requested to connect as slave use directed advertising */ |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 905 | if (conn->role == HCI_ROLE_SLAVE) { |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 906 | /* If we're active scanning most controllers are unable |
| 907 | * to initiate advertising. Simply reject the attempt. |
| 908 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 909 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 910 | hdev->le_scan_type == LE_SCAN_ACTIVE) { |
| 911 | skb_queue_purge(&req.cmd_q); |
| 912 | hci_conn_del(conn); |
| 913 | return ERR_PTR(-EBUSY); |
| 914 | } |
| 915 | |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 916 | hci_req_directed_advertising(&req, conn); |
| 917 | goto create_conn; |
| 918 | } |
| 919 | |
Andre Guedes | 4292f1f | 2014-02-03 13:56:19 -0300 | [diff] [blame] | 920 | params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); |
| 921 | if (params) { |
| 922 | conn->le_conn_min_interval = params->conn_min_interval; |
| 923 | conn->le_conn_max_interval = params->conn_max_interval; |
Marcel Holtmann | 037fc41 | 2014-06-29 16:43:27 +0200 | [diff] [blame] | 924 | conn->le_conn_latency = params->conn_latency; |
| 925 | conn->le_supv_timeout = params->supervision_timeout; |
Andre Guedes | 4292f1f | 2014-02-03 13:56:19 -0300 | [diff] [blame] | 926 | } else { |
| 927 | conn->le_conn_min_interval = hdev->le_conn_min_interval; |
| 928 | conn->le_conn_max_interval = hdev->le_conn_max_interval; |
Marcel Holtmann | 04fb7d9 | 2014-06-30 12:34:36 +0200 | [diff] [blame] | 929 | conn->le_conn_latency = hdev->le_conn_latency; |
| 930 | conn->le_supv_timeout = hdev->le_supv_timeout; |
Andre Guedes | 4292f1f | 2014-02-03 13:56:19 -0300 | [diff] [blame] | 931 | } |
Vinicius Costa Gomes | d04aef4 | 2012-07-27 19:32:56 -0300 | [diff] [blame] | 932 | |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 933 | /* If controller is scanning, we stop it since some controllers are |
Johan Hedberg | 81ad6fd | 2014-02-28 20:26:13 +0200 | [diff] [blame] | 934 | * not able to scan and connect at the same time. Also set the |
| 935 | * HCI_LE_SCAN_INTERRUPTED flag so that the command complete |
| 936 | * handler for scan disabling knows to set the correct discovery |
| 937 | * state. |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 938 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 939 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 940 | hci_req_add_le_scan_disable(&req); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 941 | hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED); |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 942 | } |
| 943 | |
Johan Hedberg | 81ad6fd | 2014-02-28 20:26:13 +0200 | [diff] [blame] | 944 | hci_req_add_le_create_conn(&req, conn); |
| 945 | |
Johan Hedberg | 3c85775 | 2014-03-25 10:30:49 +0200 | [diff] [blame] | 946 | create_conn: |
Johan Hedberg | 81ad6fd | 2014-02-28 20:26:13 +0200 | [diff] [blame] | 947 | err = hci_req_run(&req, create_le_conn_complete); |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 948 | if (err) { |
| 949 | hci_conn_del(conn); |
Andre Guedes | 620ad52 | 2013-10-08 08:21:18 -0300 | [diff] [blame] | 950 | return ERR_PTR(err); |
Andre Guedes | 2acf3d9 | 2014-02-26 20:21:42 -0300 | [diff] [blame] | 951 | } |
Vinicius Costa Gomes | d04aef4 | 2012-07-27 19:32:56 -0300 | [diff] [blame] | 952 | |
Andre Guedes | 620ad52 | 2013-10-08 08:21:18 -0300 | [diff] [blame] | 953 | done: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 954 | /* If this is continuation of connect started by hci_connect_le_scan, |
| 955 | * it already called hci_conn_hold and calling it again would mess the |
| 956 | * counter. |
| 957 | */ |
| 958 | if (!conn_unfinished) |
| 959 | hci_conn_hold(conn); |
| 960 | |
Andre Guedes | f1e5d54 | 2013-10-03 18:25:44 -0300 | [diff] [blame] | 961 | return conn; |
Vinicius Costa Gomes | d04aef4 | 2012-07-27 19:32:56 -0300 | [diff] [blame] | 962 | } |
| 963 | |
Jakub Pawlowski | f75113a | 2015-08-07 20:22:53 +0200 | [diff] [blame] | 964 | static void hci_connect_le_scan_complete(struct hci_dev *hdev, u8 status, |
| 965 | u16 opcode) |
| 966 | { |
| 967 | struct hci_conn *conn; |
| 968 | |
| 969 | if (!status) |
| 970 | return; |
| 971 | |
| 972 | BT_ERR("Failed to add device to auto conn whitelist: status 0x%2.2x", |
| 973 | status); |
| 974 | |
| 975 | hci_dev_lock(hdev); |
| 976 | |
| 977 | conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); |
| 978 | if (conn) |
| 979 | hci_le_conn_failed(conn, status); |
| 980 | |
| 981 | hci_dev_unlock(hdev); |
| 982 | } |
| 983 | |
| 984 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 985 | { |
| 986 | struct hci_conn *conn; |
| 987 | |
| 988 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 989 | if (!conn) |
| 990 | return false; |
| 991 | |
| 992 | if (conn->dst_type != type) |
| 993 | return false; |
| 994 | |
| 995 | if (conn->state != BT_CONNECTED) |
| 996 | return false; |
| 997 | |
| 998 | return true; |
| 999 | } |
| 1000 | |
| 1001 | /* This function requires the caller holds hdev->lock */ |
| 1002 | static int hci_explicit_conn_params_set(struct hci_request *req, |
| 1003 | bdaddr_t *addr, u8 addr_type) |
| 1004 | { |
| 1005 | struct hci_dev *hdev = req->hdev; |
| 1006 | struct hci_conn_params *params; |
| 1007 | |
| 1008 | if (is_connected(hdev, addr, addr_type)) |
| 1009 | return -EISCONN; |
| 1010 | |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame^] | 1011 | params = hci_conn_params_lookup(hdev, addr, addr_type); |
| 1012 | if (!params) { |
| 1013 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 1014 | if (!params) |
| 1015 | return -ENOMEM; |
Jakub Pawlowski | f75113a | 2015-08-07 20:22:53 +0200 | [diff] [blame] | 1016 | |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame^] | 1017 | /* If we created new params, mark them to be deleted in |
| 1018 | * hci_connect_le_scan_cleanup. It's different case than |
| 1019 | * existing disabled params, those will stay after cleanup. |
| 1020 | */ |
| 1021 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 1022 | } |
| 1023 | |
| 1024 | /* We're trying to connect, so make sure params are at pend_le_conns */ |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 1025 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame^] | 1026 | params->auto_connect == HCI_AUTO_CONN_REPORT || |
| 1027 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Jakub Pawlowski | f75113a | 2015-08-07 20:22:53 +0200 | [diff] [blame] | 1028 | list_del_init(¶ms->action); |
| 1029 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 1030 | } |
| 1031 | |
| 1032 | params->explicit_connect = true; |
| 1033 | __hci_update_background_scan(req); |
| 1034 | |
| 1035 | BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, |
| 1036 | params->auto_connect); |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | /* This function requires the caller holds hdev->lock */ |
| 1042 | struct hci_conn *hci_connect_le_scan(struct hci_dev *hdev, bdaddr_t *dst, |
| 1043 | u8 dst_type, u8 sec_level, |
| 1044 | u16 conn_timeout, u8 role) |
| 1045 | { |
| 1046 | struct hci_conn *conn; |
| 1047 | struct hci_request req; |
| 1048 | int err; |
| 1049 | |
| 1050 | /* Let's make sure that le is enabled.*/ |
| 1051 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
| 1052 | if (lmp_le_capable(hdev)) |
| 1053 | return ERR_PTR(-ECONNREFUSED); |
| 1054 | |
| 1055 | return ERR_PTR(-EOPNOTSUPP); |
| 1056 | } |
| 1057 | |
| 1058 | /* Some devices send ATT messages as soon as the physical link is |
| 1059 | * established. To be able to handle these ATT messages, the user- |
| 1060 | * space first establishes the connection and then starts the pairing |
| 1061 | * process. |
| 1062 | * |
| 1063 | * So if a hci_conn object already exists for the following connection |
| 1064 | * attempt, we simply update pending_sec_level and auth_type fields |
| 1065 | * and return the object found. |
| 1066 | */ |
| 1067 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); |
| 1068 | if (conn) { |
| 1069 | if (conn->pending_sec_level < sec_level) |
| 1070 | conn->pending_sec_level = sec_level; |
| 1071 | goto done; |
| 1072 | } |
| 1073 | |
| 1074 | BT_DBG("requesting refresh of dst_addr"); |
| 1075 | |
| 1076 | conn = hci_conn_add(hdev, LE_LINK, dst, role); |
| 1077 | if (!conn) |
| 1078 | return ERR_PTR(-ENOMEM); |
| 1079 | |
| 1080 | hci_req_init(&req, hdev); |
| 1081 | |
| 1082 | if (hci_explicit_conn_params_set(&req, dst, dst_type) < 0) |
| 1083 | return ERR_PTR(-EBUSY); |
| 1084 | |
| 1085 | conn->state = BT_CONNECT; |
| 1086 | set_bit(HCI_CONN_SCANNING, &conn->flags); |
| 1087 | |
| 1088 | err = hci_req_run(&req, hci_connect_le_scan_complete); |
| 1089 | if (err && err != -ENODATA) { |
| 1090 | hci_conn_del(conn); |
| 1091 | return ERR_PTR(err); |
| 1092 | } |
| 1093 | |
| 1094 | conn->dst_type = dst_type; |
| 1095 | conn->sec_level = BT_SECURITY_LOW; |
| 1096 | conn->pending_sec_level = sec_level; |
| 1097 | conn->conn_timeout = conn_timeout; |
| 1098 | |
| 1099 | done: |
| 1100 | hci_conn_hold(conn); |
| 1101 | return conn; |
| 1102 | } |
| 1103 | |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 1104 | struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, |
| 1105 | u8 sec_level, u8 auth_type) |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 1106 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | struct hci_conn *acl; |
Marcel Holtmann | e73439d | 2010-07-26 10:06:00 -0400 | [diff] [blame] | 1108 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1109 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Lukasz Rymanowski | c411110 | 2015-02-11 12:31:41 +0100 | [diff] [blame] | 1110 | if (lmp_bredr_capable(hdev)) |
| 1111 | return ERR_PTR(-ECONNREFUSED); |
| 1112 | |
Johan Hedberg | beb19e4 | 2014-07-18 11:15:26 +0300 | [diff] [blame] | 1113 | return ERR_PTR(-EOPNOTSUPP); |
Lukasz Rymanowski | c411110 | 2015-02-11 12:31:41 +0100 | [diff] [blame] | 1114 | } |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); |
| 1117 | if (!acl) { |
Johan Hedberg | a5c4e30 | 2014-07-16 11:56:07 +0300 | [diff] [blame] | 1118 | acl = hci_conn_add(hdev, ACL_LINK, dst, HCI_ROLE_MASTER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | if (!acl) |
Johan Hedberg | 48c7aba | 2012-02-19 14:06:48 +0200 | [diff] [blame] | 1120 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | hci_conn_hold(acl); |
| 1124 | |
| 1125 | if (acl->state == BT_OPEN || acl->state == BT_CLOSED) { |
| 1126 | acl->sec_level = BT_SECURITY_LOW; |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 1127 | acl->pending_sec_level = sec_level; |
| 1128 | acl->auth_type = auth_type; |
Vinicius Costa Gomes | 1aef866 | 2012-07-27 19:32:55 -0300 | [diff] [blame] | 1129 | hci_acl_create_connection(acl); |
Nick Pelly | c390216 | 2009-11-13 14:16:32 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
Vinicius Costa Gomes | db47427 | 2012-07-28 22:35:59 -0300 | [diff] [blame] | 1132 | return acl; |
| 1133 | } |
| 1134 | |
Frédéric Dalleau | 10c62dd | 2013-08-19 14:23:59 +0200 | [diff] [blame] | 1135 | struct hci_conn *hci_connect_sco(struct hci_dev *hdev, int type, bdaddr_t *dst, |
| 1136 | __u16 setting) |
Vinicius Costa Gomes | db47427 | 2012-07-28 22:35:59 -0300 | [diff] [blame] | 1137 | { |
| 1138 | struct hci_conn *acl; |
| 1139 | struct hci_conn *sco; |
| 1140 | |
Frédéric Dalleau | e660ed6 | 2013-08-19 14:23:54 +0200 | [diff] [blame] | 1141 | acl = hci_connect_acl(hdev, dst, BT_SECURITY_LOW, HCI_AT_NO_BONDING); |
Vinicius Costa Gomes | db47427 | 2012-07-28 22:35:59 -0300 | [diff] [blame] | 1142 | if (IS_ERR(acl)) |
Marcel Holtmann | b6a0dc8 | 2007-10-20 14:55:10 +0200 | [diff] [blame] | 1143 | return acl; |
| 1144 | |
| 1145 | sco = hci_conn_hash_lookup_ba(hdev, type, dst); |
| 1146 | if (!sco) { |
Johan Hedberg | a5c4e30 | 2014-07-16 11:56:07 +0300 | [diff] [blame] | 1147 | sco = hci_conn_add(hdev, type, dst, HCI_ROLE_MASTER); |
Marcel Holtmann | 5b7f990 | 2007-07-11 09:51:55 +0200 | [diff] [blame] | 1148 | if (!sco) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 1149 | hci_conn_drop(acl); |
Johan Hedberg | 48c7aba | 2012-02-19 14:06:48 +0200 | [diff] [blame] | 1150 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | } |
| 1152 | } |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1153 | |
| 1154 | acl->link = sco; |
| 1155 | sco->link = acl; |
| 1156 | |
| 1157 | hci_conn_hold(sco); |
| 1158 | |
Frédéric Dalleau | 10c62dd | 2013-08-19 14:23:59 +0200 | [diff] [blame] | 1159 | sco->setting = setting; |
| 1160 | |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1161 | if (acl->state == BT_CONNECTED && |
Gustavo Padovan | 5974e4c | 2012-05-17 00:36:25 -0300 | [diff] [blame] | 1162 | (sco->state == BT_OPEN || sco->state == BT_CLOSED)) { |
Johan Hedberg | 58a681e | 2012-01-16 06:47:28 +0200 | [diff] [blame] | 1163 | set_bit(HCI_CONN_POWER_SAVE, &acl->flags); |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 1164 | hci_conn_enter_active_mode(acl, BT_POWER_FORCE_ACTIVE_ON); |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1165 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1166 | if (test_bit(HCI_CONN_MODE_CHANGE_PEND, &acl->flags)) { |
Marcel Holtmann | e7c29cb | 2008-09-09 07:19:20 +0200 | [diff] [blame] | 1167 | /* defer SCO setup until mode change completed */ |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1168 | set_bit(HCI_CONN_SCO_SETUP_PEND, &acl->flags); |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 1169 | return sco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | hci_sco_setup(acl, 0x00); |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 1173 | } |
Marcel Holtmann | 0684e5f | 2009-02-09 02:48:38 +0100 | [diff] [blame] | 1174 | |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 1175 | return sco; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | |
| 1178 | /* Check link security requirement */ |
| 1179 | int hci_conn_check_link_mode(struct hci_conn *conn) |
| 1180 | { |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1181 | BT_DBG("hcon %p", conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
Marcel Holtmann | 40b552a | 2014-03-19 14:10:25 -0700 | [diff] [blame] | 1183 | /* In Secure Connections Only mode, it is required that Secure |
| 1184 | * Connections is used and the link is encrypted with AES-CCM |
| 1185 | * using a P-256 authenticated combination key. |
| 1186 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1187 | if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | 40b552a | 2014-03-19 14:10:25 -0700 | [diff] [blame] | 1188 | if (!hci_conn_sc_enabled(conn) || |
| 1189 | !test_bit(HCI_CONN_AES_CCM, &conn->flags) || |
| 1190 | conn->key_type != HCI_LK_AUTH_COMBINATION_P256) |
| 1191 | return 0; |
| 1192 | } |
| 1193 | |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1194 | if (hci_conn_ssp_enabled(conn) && |
| 1195 | !test_bit(HCI_CONN_ENCRYPT, &conn->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | return 0; |
| 1197 | |
| 1198 | return 1; |
| 1199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | |
| 1201 | /* Authenticate remote device */ |
| 1202 | static int hci_conn_auth(struct hci_conn *conn, __u8 sec_level, __u8 auth_type) |
| 1203 | { |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1204 | BT_DBG("hcon %p", conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 1206 | if (conn->pending_sec_level > sec_level) |
| 1207 | sec_level = conn->pending_sec_level; |
| 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | if (sec_level > conn->sec_level) |
Johan Hedberg | 765c2a9 | 2011-01-19 12:06:52 +0530 | [diff] [blame] | 1210 | conn->pending_sec_level = sec_level; |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1211 | else if (test_bit(HCI_CONN_AUTH, &conn->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | return 1; |
| 1213 | |
Johan Hedberg | 65cf686 | 2011-01-19 12:06:49 +0530 | [diff] [blame] | 1214 | /* Make sure we preserve an existing MITM requirement*/ |
| 1215 | auth_type |= (conn->auth_type & 0x01); |
| 1216 | |
Marcel Holtmann | 96a3183 | 2009-02-12 16:23:03 +0100 | [diff] [blame] | 1217 | conn->auth_type = auth_type; |
| 1218 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1219 | if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | struct hci_cp_auth_requested cp; |
Peter Hurley | b7d05ba | 2012-01-13 15:11:30 +0100 | [diff] [blame] | 1221 | |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1222 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1223 | hci_send_cmd(conn->hdev, HCI_OP_AUTH_REQUESTED, |
Gustavo Padovan | 5974e4c | 2012-05-17 00:36:25 -0300 | [diff] [blame] | 1224 | sizeof(cp), &cp); |
Johan Hedberg | 09da1f3 | 2014-04-11 12:02:32 -0700 | [diff] [blame] | 1225 | |
| 1226 | /* If we're already encrypted set the REAUTH_PEND flag, |
| 1227 | * otherwise set the ENCRYPT_PEND. |
| 1228 | */ |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1229 | if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1230 | set_bit(HCI_CONN_REAUTH_PEND, &conn->flags); |
Johan Hedberg | 09da1f3 | 2014-04-11 12:02:32 -0700 | [diff] [blame] | 1231 | else |
| 1232 | set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | return 0; |
| 1236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1238 | /* Encrypt the the link */ |
| 1239 | static void hci_conn_encrypt(struct hci_conn *conn) |
| 1240 | { |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1241 | BT_DBG("hcon %p", conn); |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1242 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1243 | if (!test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) { |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1244 | struct hci_cp_set_conn_encrypt cp; |
| 1245 | cp.handle = cpu_to_le16(conn->handle); |
| 1246 | cp.encrypt = 0x01; |
| 1247 | hci_send_cmd(conn->hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp), |
Gustavo Padovan | 5974e4c | 2012-05-17 00:36:25 -0300 | [diff] [blame] | 1248 | &cp); |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1249 | } |
| 1250 | } |
| 1251 | |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1252 | /* Enable security */ |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 1253 | int hci_conn_security(struct hci_conn *conn, __u8 sec_level, __u8 auth_type, |
| 1254 | bool initiator) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | { |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1256 | BT_DBG("hcon %p", conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | |
Vinicius Costa Gomes | d8343f1 | 2012-08-23 21:32:44 -0300 | [diff] [blame] | 1258 | if (conn->type == LE_LINK) |
| 1259 | return smp_conn_security(conn, sec_level); |
| 1260 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1261 | /* For sdp we don't need the link key. */ |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1262 | if (sec_level == BT_SECURITY_SDP) |
| 1263 | return 1; |
| 1264 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1265 | /* For non 2.1 devices and low security level we don't need the link |
| 1266 | key. */ |
Johan Hedberg | aa64a8b | 2012-01-18 21:33:12 +0200 | [diff] [blame] | 1267 | if (sec_level == BT_SECURITY_LOW && !hci_conn_ssp_enabled(conn)) |
Marcel Holtmann | 3fdca1e | 2009-04-28 09:04:55 -0700 | [diff] [blame] | 1268 | return 1; |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1269 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1270 | /* For other security levels we need the link key. */ |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1271 | if (!test_bit(HCI_CONN_AUTH, &conn->flags)) |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1272 | goto auth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1273 | |
Marcel Holtmann | 7b5a924 | 2014-01-15 22:37:39 -0800 | [diff] [blame] | 1274 | /* An authenticated FIPS approved combination key has sufficient |
| 1275 | * security for security level 4. */ |
| 1276 | if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256 && |
| 1277 | sec_level == BT_SECURITY_FIPS) |
| 1278 | goto encrypt; |
| 1279 | |
| 1280 | /* An authenticated combination key has sufficient security for |
| 1281 | security level 3. */ |
| 1282 | if ((conn->key_type == HCI_LK_AUTH_COMBINATION_P192 || |
| 1283 | conn->key_type == HCI_LK_AUTH_COMBINATION_P256) && |
| 1284 | sec_level == BT_SECURITY_HIGH) |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1285 | goto encrypt; |
| 1286 | |
| 1287 | /* An unauthenticated combination key has sufficient security for |
| 1288 | security level 1 and 2. */ |
Marcel Holtmann | 66138ce | 2014-01-10 02:07:20 -0800 | [diff] [blame] | 1289 | if ((conn->key_type == HCI_LK_UNAUTH_COMBINATION_P192 || |
| 1290 | conn->key_type == HCI_LK_UNAUTH_COMBINATION_P256) && |
Gustavo Padovan | 5974e4c | 2012-05-17 00:36:25 -0300 | [diff] [blame] | 1291 | (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW)) |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1292 | goto encrypt; |
| 1293 | |
| 1294 | /* A combination key has always sufficient security for the security |
| 1295 | levels 1 or 2. High security level requires the combination key |
| 1296 | is generated using maximum PIN code length (16). |
| 1297 | For pre 2.1 units. */ |
| 1298 | if (conn->key_type == HCI_LK_COMBINATION && |
Marcel Holtmann | 7b5a924 | 2014-01-15 22:37:39 -0800 | [diff] [blame] | 1299 | (sec_level == BT_SECURITY_MEDIUM || sec_level == BT_SECURITY_LOW || |
| 1300 | conn->pin_length == 16)) |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1301 | goto encrypt; |
| 1302 | |
| 1303 | auth: |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1304 | if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | return 0; |
| 1306 | |
Johan Hedberg | 977f8fc | 2014-07-17 15:35:39 +0300 | [diff] [blame] | 1307 | if (initiator) |
| 1308 | set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags); |
| 1309 | |
Luiz Augusto von Dentz | 6fdf658 | 2011-06-13 15:37:35 +0300 | [diff] [blame] | 1310 | if (!hci_conn_auth(conn, sec_level, auth_type)) |
| 1311 | return 0; |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1312 | |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1313 | encrypt: |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1314 | if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) |
Waldemar Rymarkiewicz | 13d3931 | 2011-04-28 12:07:55 +0200 | [diff] [blame] | 1315 | return 1; |
| 1316 | |
| 1317 | hci_conn_encrypt(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | return 0; |
| 1319 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1320 | EXPORT_SYMBOL(hci_conn_security); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
Waldemar Rymarkiewicz | b3b1b06 | 2011-05-06 09:42:31 +0200 | [diff] [blame] | 1322 | /* Check secure link requirement */ |
| 1323 | int hci_conn_check_secure(struct hci_conn *conn, __u8 sec_level) |
| 1324 | { |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1325 | BT_DBG("hcon %p", conn); |
Waldemar Rymarkiewicz | b3b1b06 | 2011-05-06 09:42:31 +0200 | [diff] [blame] | 1326 | |
Marcel Holtmann | 9cb2e03 | 2014-02-01 11:32:25 -0800 | [diff] [blame] | 1327 | /* Accept if non-secure or higher security level is required */ |
| 1328 | if (sec_level != BT_SECURITY_HIGH && sec_level != BT_SECURITY_FIPS) |
Waldemar Rymarkiewicz | b3b1b06 | 2011-05-06 09:42:31 +0200 | [diff] [blame] | 1329 | return 1; |
| 1330 | |
Marcel Holtmann | 9cb2e03 | 2014-02-01 11:32:25 -0800 | [diff] [blame] | 1331 | /* Accept if secure or higher security level is already present */ |
| 1332 | if (conn->sec_level == BT_SECURITY_HIGH || |
| 1333 | conn->sec_level == BT_SECURITY_FIPS) |
| 1334 | return 1; |
| 1335 | |
| 1336 | /* Reject not secure link */ |
| 1337 | return 0; |
Waldemar Rymarkiewicz | b3b1b06 | 2011-05-06 09:42:31 +0200 | [diff] [blame] | 1338 | } |
| 1339 | EXPORT_SYMBOL(hci_conn_check_secure); |
| 1340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | /* Switch role */ |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1342 | int hci_conn_switch_role(struct hci_conn *conn, __u8 role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | { |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1344 | BT_DBG("hcon %p", conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1346 | if (role == conn->role) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | return 1; |
| 1348 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1349 | if (!test_and_set_bit(HCI_CONN_RSWITCH_PEND, &conn->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | struct hci_cp_switch_role cp; |
| 1351 | bacpy(&cp.bdaddr, &conn->dst); |
| 1352 | cp.role = role; |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1353 | hci_send_cmd(conn->hdev, HCI_OP_SWITCH_ROLE, sizeof(cp), &cp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | } |
Marcel Holtmann | 8c1b235 | 2009-01-15 21:58:04 +0100 | [diff] [blame] | 1355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | return 0; |
| 1357 | } |
| 1358 | EXPORT_SYMBOL(hci_conn_switch_role); |
| 1359 | |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1360 | /* Enter active mode */ |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 1361 | void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1362 | { |
| 1363 | struct hci_dev *hdev = conn->hdev; |
| 1364 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1365 | BT_DBG("hcon %p mode %d", conn, conn->mode); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1366 | |
Jaikumar Ganesh | 14b12d0 | 2011-05-23 18:06:04 -0700 | [diff] [blame] | 1367 | if (conn->mode != HCI_CM_SNIFF) |
| 1368 | goto timer; |
| 1369 | |
Johan Hedberg | 58a681e | 2012-01-16 06:47:28 +0200 | [diff] [blame] | 1370 | if (!test_bit(HCI_CONN_POWER_SAVE, &conn->flags) && !force_active) |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1371 | goto timer; |
| 1372 | |
Johan Hedberg | 51a8efd | 2012-01-16 06:10:31 +0200 | [diff] [blame] | 1373 | if (!test_and_set_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags)) { |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1374 | struct hci_cp_exit_sniff_mode cp; |
YOSHIFUJI Hideaki | aca3192 | 2007-03-25 20:12:50 -0700 | [diff] [blame] | 1375 | cp.handle = cpu_to_le16(conn->handle); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1376 | hci_send_cmd(hdev, HCI_OP_EXIT_SNIFF_MODE, sizeof(cp), &cp); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | timer: |
| 1380 | if (hdev->idle_timeout > 0) |
Johan Hedberg | a74a84f | 2013-10-16 18:11:40 +0300 | [diff] [blame] | 1381 | queue_delayed_work(hdev->workqueue, &conn->idle_work, |
| 1382 | msecs_to_jiffies(hdev->idle_timeout)); |
Marcel Holtmann | 04837f6 | 2006-07-03 10:02:33 +0200 | [diff] [blame] | 1383 | } |
| 1384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | /* Drop all connection on the device */ |
| 1386 | void hci_conn_hash_flush(struct hci_dev *hdev) |
| 1387 | { |
| 1388 | struct hci_conn_hash *h = &hdev->conn_hash; |
Andrei Emeltchenko | 3c4e0df | 2012-02-02 10:32:17 +0200 | [diff] [blame] | 1389 | struct hci_conn *c, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | |
| 1391 | BT_DBG("hdev %s", hdev->name); |
| 1392 | |
Andrei Emeltchenko | 3c4e0df | 2012-02-02 10:32:17 +0200 | [diff] [blame] | 1393 | list_for_each_entry_safe(c, n, &h->list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | c->state = BT_CLOSED; |
| 1395 | |
Johan Hedberg | 3a6d576 | 2015-02-18 14:53:58 +0200 | [diff] [blame] | 1396 | hci_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | hci_conn_del(c); |
| 1398 | } |
| 1399 | } |
| 1400 | |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1401 | /* Check pending connect attempts */ |
| 1402 | void hci_conn_check_pending(struct hci_dev *hdev) |
| 1403 | { |
| 1404 | struct hci_conn *conn; |
| 1405 | |
| 1406 | BT_DBG("hdev %s", hdev->name); |
| 1407 | |
| 1408 | hci_dev_lock(hdev); |
| 1409 | |
| 1410 | conn = hci_conn_hash_lookup_state(hdev, ACL_LINK, BT_CONNECT2); |
| 1411 | if (conn) |
Vinicius Costa Gomes | 1aef866 | 2012-07-27 19:32:55 -0300 | [diff] [blame] | 1412 | hci_acl_create_connection(conn); |
Marcel Holtmann | a9de924 | 2007-10-20 13:33:56 +0200 | [diff] [blame] | 1413 | |
| 1414 | hci_dev_unlock(hdev); |
| 1415 | } |
| 1416 | |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1417 | static u32 get_link_mode(struct hci_conn *conn) |
| 1418 | { |
| 1419 | u32 link_mode = 0; |
| 1420 | |
Johan Hedberg | 40bef30 | 2014-07-16 11:42:27 +0300 | [diff] [blame] | 1421 | if (conn->role == HCI_ROLE_MASTER) |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1422 | link_mode |= HCI_LM_MASTER; |
| 1423 | |
| 1424 | if (test_bit(HCI_CONN_ENCRYPT, &conn->flags)) |
| 1425 | link_mode |= HCI_LM_ENCRYPT; |
| 1426 | |
| 1427 | if (test_bit(HCI_CONN_AUTH, &conn->flags)) |
| 1428 | link_mode |= HCI_LM_AUTH; |
| 1429 | |
| 1430 | if (test_bit(HCI_CONN_SECURE, &conn->flags)) |
| 1431 | link_mode |= HCI_LM_SECURE; |
| 1432 | |
| 1433 | if (test_bit(HCI_CONN_FIPS, &conn->flags)) |
| 1434 | link_mode |= HCI_LM_FIPS; |
| 1435 | |
| 1436 | return link_mode; |
| 1437 | } |
| 1438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | int hci_get_conn_list(void __user *arg) |
| 1440 | { |
Gustavo Padovan | fc5fef6 | 2012-05-23 04:04:19 -0300 | [diff] [blame] | 1441 | struct hci_conn *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | struct hci_conn_list_req req, *cl; |
| 1443 | struct hci_conn_info *ci; |
| 1444 | struct hci_dev *hdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | int n = 0, size, err; |
| 1446 | |
| 1447 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1448 | return -EFAULT; |
| 1449 | |
| 1450 | if (!req.conn_num || req.conn_num > (PAGE_SIZE * 2) / sizeof(*ci)) |
| 1451 | return -EINVAL; |
| 1452 | |
| 1453 | size = sizeof(req) + req.conn_num * sizeof(*ci); |
| 1454 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1455 | cl = kmalloc(size, GFP_KERNEL); |
| 1456 | if (!cl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | return -ENOMEM; |
| 1458 | |
Andrei Emeltchenko | 70f23020 | 2010-12-01 16:58:25 +0200 | [diff] [blame] | 1459 | hdev = hci_dev_get(req.dev_id); |
| 1460 | if (!hdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | kfree(cl); |
| 1462 | return -ENODEV; |
| 1463 | } |
| 1464 | |
| 1465 | ci = cl->conn_info; |
| 1466 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1467 | hci_dev_lock(hdev); |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 1468 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | bacpy(&(ci + n)->bdaddr, &c->dst); |
| 1470 | (ci + n)->handle = c->handle; |
| 1471 | (ci + n)->type = c->type; |
| 1472 | (ci + n)->out = c->out; |
| 1473 | (ci + n)->state = c->state; |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1474 | (ci + n)->link_mode = get_link_mode(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | if (++n >= req.conn_num) |
| 1476 | break; |
| 1477 | } |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1478 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
| 1480 | cl->dev_id = hdev->id; |
| 1481 | cl->conn_num = n; |
| 1482 | size = sizeof(req) + n * sizeof(*ci); |
| 1483 | |
| 1484 | hci_dev_put(hdev); |
| 1485 | |
| 1486 | err = copy_to_user(arg, cl, size); |
| 1487 | kfree(cl); |
| 1488 | |
| 1489 | return err ? -EFAULT : 0; |
| 1490 | } |
| 1491 | |
| 1492 | int hci_get_conn_info(struct hci_dev *hdev, void __user *arg) |
| 1493 | { |
| 1494 | struct hci_conn_info_req req; |
| 1495 | struct hci_conn_info ci; |
| 1496 | struct hci_conn *conn; |
| 1497 | char __user *ptr = arg + sizeof(req); |
| 1498 | |
| 1499 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1500 | return -EFAULT; |
| 1501 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1502 | hci_dev_lock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1503 | conn = hci_conn_hash_lookup_ba(hdev, req.type, &req.bdaddr); |
| 1504 | if (conn) { |
| 1505 | bacpy(&ci.bdaddr, &conn->dst); |
| 1506 | ci.handle = conn->handle; |
| 1507 | ci.type = conn->type; |
| 1508 | ci.out = conn->out; |
| 1509 | ci.state = conn->state; |
Johan Hedberg | 4dae279 | 2014-06-24 17:03:50 +0300 | [diff] [blame] | 1510 | ci.link_mode = get_link_mode(conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | } |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1512 | hci_dev_unlock(hdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | |
| 1514 | if (!conn) |
| 1515 | return -ENOENT; |
| 1516 | |
| 1517 | return copy_to_user(ptr, &ci, sizeof(ci)) ? -EFAULT : 0; |
| 1518 | } |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1519 | |
| 1520 | int hci_get_auth_info(struct hci_dev *hdev, void __user *arg) |
| 1521 | { |
| 1522 | struct hci_auth_info_req req; |
| 1523 | struct hci_conn *conn; |
| 1524 | |
| 1525 | if (copy_from_user(&req, arg, sizeof(req))) |
| 1526 | return -EFAULT; |
| 1527 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1528 | hci_dev_lock(hdev); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1529 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &req.bdaddr); |
| 1530 | if (conn) |
| 1531 | req.type = conn->auth_type; |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1532 | hci_dev_unlock(hdev); |
Marcel Holtmann | 40be492 | 2008-07-14 20:13:50 +0200 | [diff] [blame] | 1533 | |
| 1534 | if (!conn) |
| 1535 | return -ENOENT; |
| 1536 | |
| 1537 | return copy_to_user(arg, &req, sizeof(req)) ? -EFAULT : 0; |
| 1538 | } |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1539 | |
| 1540 | struct hci_chan *hci_chan_create(struct hci_conn *conn) |
| 1541 | { |
| 1542 | struct hci_dev *hdev = conn->hdev; |
| 1543 | struct hci_chan *chan; |
| 1544 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1545 | BT_DBG("%s hcon %p", hdev->name, conn); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1546 | |
Johan Hedberg | f94b665 | 2014-08-18 00:41:44 +0300 | [diff] [blame] | 1547 | if (test_bit(HCI_CONN_DROP, &conn->flags)) { |
| 1548 | BT_DBG("Refusing to create new hci_chan"); |
| 1549 | return NULL; |
| 1550 | } |
| 1551 | |
Johan Hedberg | 27f70f3 | 2014-07-21 10:50:06 +0300 | [diff] [blame] | 1552 | chan = kzalloc(sizeof(*chan), GFP_KERNEL); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1553 | if (!chan) |
| 1554 | return NULL; |
| 1555 | |
Johan Hedberg | 6c388d3 | 2014-08-18 00:41:42 +0300 | [diff] [blame] | 1556 | chan->conn = hci_conn_get(conn); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1557 | skb_queue_head_init(&chan->data_q); |
Mat Martineau | 168df8e | 2012-10-23 15:24:13 -0700 | [diff] [blame] | 1558 | chan->state = BT_CONNECTED; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1559 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 1560 | list_add_rcu(&chan->list, &conn->chan_list); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1561 | |
| 1562 | return chan; |
| 1563 | } |
| 1564 | |
Andrei Emeltchenko | 9472007 | 2012-09-06 15:05:43 +0300 | [diff] [blame] | 1565 | void hci_chan_del(struct hci_chan *chan) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1566 | { |
| 1567 | struct hci_conn *conn = chan->conn; |
| 1568 | struct hci_dev *hdev = conn->hdev; |
| 1569 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1570 | BT_DBG("%s hcon %p chan %p", hdev->name, conn, chan); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1571 | |
Gustavo F. Padovan | 8192ede | 2011-12-14 15:08:48 -0200 | [diff] [blame] | 1572 | list_del_rcu(&chan->list); |
| 1573 | |
| 1574 | synchronize_rcu(); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1575 | |
Johan Hedberg | bcbb655 | 2014-08-18 20:33:27 +0300 | [diff] [blame] | 1576 | /* Prevent new hci_chan's to be created for this hci_conn */ |
Johan Hedberg | f94b665 | 2014-08-18 00:41:44 +0300 | [diff] [blame] | 1577 | set_bit(HCI_CONN_DROP, &conn->flags); |
Johan Hedberg | b3ff670 | 2014-08-18 00:41:43 +0300 | [diff] [blame] | 1578 | |
Johan Hedberg | 6c388d3 | 2014-08-18 00:41:42 +0300 | [diff] [blame] | 1579 | hci_conn_put(conn); |
Andrei Emeltchenko | e9b0274 | 2012-10-25 15:20:51 +0300 | [diff] [blame] | 1580 | |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1581 | skb_queue_purge(&chan->data_q); |
| 1582 | kfree(chan); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1583 | } |
| 1584 | |
Gustavo F. Padovan | 2c33c06 | 2011-12-14 13:02:51 -0200 | [diff] [blame] | 1585 | void hci_chan_list_flush(struct hci_conn *conn) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1586 | { |
Andrei Emeltchenko | 2a5a5ec | 2012-02-02 10:32:18 +0200 | [diff] [blame] | 1587 | struct hci_chan *chan, *n; |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1588 | |
Andrei Emeltchenko | 38b3fef | 2012-06-15 11:50:28 +0300 | [diff] [blame] | 1589 | BT_DBG("hcon %p", conn); |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1590 | |
Andrei Emeltchenko | 2a5a5ec | 2012-02-02 10:32:18 +0200 | [diff] [blame] | 1591 | list_for_each_entry_safe(chan, n, &conn->chan_list, list) |
Luiz Augusto von Dentz | 73d80de | 2011-11-02 15:52:01 +0200 | [diff] [blame] | 1592 | hci_chan_del(chan); |
| 1593 | } |
Andrei Emeltchenko | 42c4e53 | 2012-10-10 17:38:28 +0300 | [diff] [blame] | 1594 | |
| 1595 | static struct hci_chan *__hci_chan_lookup_handle(struct hci_conn *hcon, |
| 1596 | __u16 handle) |
| 1597 | { |
| 1598 | struct hci_chan *hchan; |
| 1599 | |
| 1600 | list_for_each_entry(hchan, &hcon->chan_list, list) { |
| 1601 | if (hchan->handle == handle) |
| 1602 | return hchan; |
| 1603 | } |
| 1604 | |
| 1605 | return NULL; |
| 1606 | } |
| 1607 | |
| 1608 | struct hci_chan *hci_chan_lookup_handle(struct hci_dev *hdev, __u16 handle) |
| 1609 | { |
| 1610 | struct hci_conn_hash *h = &hdev->conn_hash; |
| 1611 | struct hci_conn *hcon; |
| 1612 | struct hci_chan *hchan = NULL; |
| 1613 | |
| 1614 | rcu_read_lock(); |
| 1615 | |
| 1616 | list_for_each_entry_rcu(hcon, &h->list, list) { |
| 1617 | hchan = __hci_chan_lookup_handle(hcon, handle); |
| 1618 | if (hchan) |
| 1619 | break; |
| 1620 | } |
| 1621 | |
| 1622 | rcu_read_unlock(); |
| 1623 | |
| 1624 | return hchan; |
| 1625 | } |