Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 3 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4 | Copyright (C) 2010 Nokia Corporation |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 5 | Copyright (C) 2011-2012 Intel Corporation |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 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 |
| 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 |
| 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
| 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI Management interface */ |
| 26 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 32 | #include <net/bluetooth/hci_sock.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 33 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 34 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 35 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 36 | #include "hci_request.h" |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 37 | #include "smp.h" |
Johan Hedberg | a380b6c | 2015-03-17 13:48:48 +0200 | [diff] [blame] | 38 | #include "mgmt_util.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 39 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 40 | #define MGMT_VERSION 1 |
Szymon Janc | 3310230 | 2016-09-18 12:50:07 +0200 | [diff] [blame] | 41 | #define MGMT_REVISION 14 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 42 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 43 | static const u16 mgmt_commands[] = { |
| 44 | MGMT_OP_READ_INDEX_LIST, |
| 45 | MGMT_OP_READ_INFO, |
| 46 | MGMT_OP_SET_POWERED, |
| 47 | MGMT_OP_SET_DISCOVERABLE, |
| 48 | MGMT_OP_SET_CONNECTABLE, |
| 49 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 50 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 51 | MGMT_OP_SET_LINK_SECURITY, |
| 52 | MGMT_OP_SET_SSP, |
| 53 | MGMT_OP_SET_HS, |
| 54 | MGMT_OP_SET_LE, |
| 55 | MGMT_OP_SET_DEV_CLASS, |
| 56 | MGMT_OP_SET_LOCAL_NAME, |
| 57 | MGMT_OP_ADD_UUID, |
| 58 | MGMT_OP_REMOVE_UUID, |
| 59 | MGMT_OP_LOAD_LINK_KEYS, |
| 60 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 61 | MGMT_OP_DISCONNECT, |
| 62 | MGMT_OP_GET_CONNECTIONS, |
| 63 | MGMT_OP_PIN_CODE_REPLY, |
| 64 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 65 | MGMT_OP_SET_IO_CAPABILITY, |
| 66 | MGMT_OP_PAIR_DEVICE, |
| 67 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 68 | MGMT_OP_UNPAIR_DEVICE, |
| 69 | MGMT_OP_USER_CONFIRM_REPLY, |
| 70 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 71 | MGMT_OP_USER_PASSKEY_REPLY, |
| 72 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 73 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 74 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 75 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 76 | MGMT_OP_START_DISCOVERY, |
| 77 | MGMT_OP_STOP_DISCOVERY, |
| 78 | MGMT_OP_CONFIRM_NAME, |
| 79 | MGMT_OP_BLOCK_DEVICE, |
| 80 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 82 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 83 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 84 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 85 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 86 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 87 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 88 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 89 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 90 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 91 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 92 | MGMT_OP_ADD_DEVICE, |
| 93 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 94 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 95 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 96 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 97 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 98 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 99 | MGMT_OP_START_SERVICE_DISCOVERY, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 100 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 101 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 102 | MGMT_OP_READ_ADV_FEATURES, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 103 | MGMT_OP_ADD_ADVERTISING, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 104 | MGMT_OP_REMOVE_ADVERTISING, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 105 | MGMT_OP_GET_ADV_SIZE_INFO, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 106 | MGMT_OP_START_LIMITED_DISCOVERY, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 107 | MGMT_OP_READ_EXT_INFO, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 108 | MGMT_OP_SET_APPEARANCE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | static const u16 mgmt_events[] = { |
| 112 | MGMT_EV_CONTROLLER_ERROR, |
| 113 | MGMT_EV_INDEX_ADDED, |
| 114 | MGMT_EV_INDEX_REMOVED, |
| 115 | MGMT_EV_NEW_SETTINGS, |
| 116 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 117 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 118 | MGMT_EV_NEW_LINK_KEY, |
| 119 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 120 | MGMT_EV_DEVICE_CONNECTED, |
| 121 | MGMT_EV_DEVICE_DISCONNECTED, |
| 122 | MGMT_EV_CONNECT_FAILED, |
| 123 | MGMT_EV_PIN_CODE_REQUEST, |
| 124 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 125 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 126 | MGMT_EV_AUTH_FAILED, |
| 127 | MGMT_EV_DEVICE_FOUND, |
| 128 | MGMT_EV_DISCOVERING, |
| 129 | MGMT_EV_DEVICE_BLOCKED, |
| 130 | MGMT_EV_DEVICE_UNBLOCKED, |
| 131 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 132 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 133 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 134 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 135 | MGMT_EV_DEVICE_ADDED, |
| 136 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 137 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 138 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 139 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 140 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 141 | MGMT_EV_EXT_INDEX_ADDED, |
| 142 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 143 | MGMT_EV_LOCAL_OOB_DATA_UPDATED, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 144 | MGMT_EV_ADVERTISING_ADDED, |
| 145 | MGMT_EV_ADVERTISING_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 146 | MGMT_EV_EXT_INFO_CHANGED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 149 | static const u16 mgmt_untrusted_commands[] = { |
| 150 | MGMT_OP_READ_INDEX_LIST, |
| 151 | MGMT_OP_READ_INFO, |
| 152 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 153 | MGMT_OP_READ_CONFIG_INFO, |
| 154 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 155 | MGMT_OP_READ_EXT_INFO, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | static const u16 mgmt_untrusted_events[] = { |
| 159 | MGMT_EV_INDEX_ADDED, |
| 160 | MGMT_EV_INDEX_REMOVED, |
| 161 | MGMT_EV_NEW_SETTINGS, |
| 162 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 163 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 164 | MGMT_EV_UNCONF_INDEX_ADDED, |
| 165 | MGMT_EV_UNCONF_INDEX_REMOVED, |
| 166 | MGMT_EV_NEW_CONFIG_OPTIONS, |
| 167 | MGMT_EV_EXT_INDEX_ADDED, |
| 168 | MGMT_EV_EXT_INDEX_REMOVED, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 169 | MGMT_EV_EXT_INFO_CHANGED, |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 172 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 173 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 174 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 175 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 176 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 177 | /* HCI to MGMT error code conversion table */ |
| 178 | static u8 mgmt_status_table[] = { |
| 179 | MGMT_STATUS_SUCCESS, |
| 180 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 181 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 182 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 183 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 184 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 185 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 186 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 187 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 188 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 189 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 190 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 191 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 192 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 193 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 194 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 195 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 196 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 197 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 198 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 199 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 200 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 201 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 202 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 203 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 204 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 205 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 206 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 207 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 208 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 209 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 210 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 211 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 212 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 213 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 214 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 215 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 216 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 217 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 218 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 219 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 220 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 221 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 222 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 223 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 224 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 225 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 226 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 227 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 228 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 229 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 230 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 231 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 232 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 233 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 234 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 235 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 236 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 237 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 238 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 239 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 240 | }; |
| 241 | |
| 242 | static u8 mgmt_status(u8 hci_status) |
| 243 | { |
| 244 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 245 | return mgmt_status_table[hci_status]; |
| 246 | |
| 247 | return MGMT_STATUS_FAILED; |
| 248 | } |
| 249 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 250 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 251 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 252 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 253 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 254 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 257 | static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, |
| 258 | u16 len, int flag, struct sock *skip_sk) |
| 259 | { |
| 260 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 261 | flag, skip_sk); |
| 262 | } |
| 263 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 264 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 265 | struct sock *skip_sk) |
| 266 | { |
| 267 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 268 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 271 | static u8 le_addr_type(u8 mgmt_addr_type) |
| 272 | { |
| 273 | if (mgmt_addr_type == BDADDR_LE_PUBLIC) |
| 274 | return ADDR_LE_DEV_PUBLIC; |
| 275 | else |
| 276 | return ADDR_LE_DEV_RANDOM; |
| 277 | } |
| 278 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 279 | void mgmt_fill_version_info(void *ver) |
| 280 | { |
| 281 | struct mgmt_rp_read_version *rp = ver; |
| 282 | |
| 283 | rp->version = MGMT_VERSION; |
| 284 | rp->revision = cpu_to_le16(MGMT_REVISION); |
| 285 | } |
| 286 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 287 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 288 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 289 | { |
| 290 | struct mgmt_rp_read_version rp; |
| 291 | |
| 292 | BT_DBG("sock %p", sk); |
| 293 | |
Marcel Holtmann | 03c979c | 2016-08-27 20:23:39 +0200 | [diff] [blame] | 294 | mgmt_fill_version_info(&rp); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 295 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 296 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 297 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 298 | } |
| 299 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 300 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 301 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 302 | { |
| 303 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 304 | u16 num_commands, num_events; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 305 | size_t rp_size; |
| 306 | int i, err; |
| 307 | |
| 308 | BT_DBG("sock %p", sk); |
| 309 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 310 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 311 | num_commands = ARRAY_SIZE(mgmt_commands); |
| 312 | num_events = ARRAY_SIZE(mgmt_events); |
| 313 | } else { |
| 314 | num_commands = ARRAY_SIZE(mgmt_untrusted_commands); |
| 315 | num_events = ARRAY_SIZE(mgmt_untrusted_events); |
| 316 | } |
| 317 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 318 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 319 | |
| 320 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 321 | if (!rp) |
| 322 | return -ENOMEM; |
| 323 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 324 | rp->num_commands = cpu_to_le16(num_commands); |
| 325 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 326 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 327 | if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { |
| 328 | __le16 *opcode = rp->opcodes; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 329 | |
Marcel Holtmann | 99c679a | 2015-03-24 17:31:03 -0700 | [diff] [blame] | 330 | for (i = 0; i < num_commands; i++, opcode++) |
| 331 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 332 | |
| 333 | for (i = 0; i < num_events; i++, opcode++) |
| 334 | put_unaligned_le16(mgmt_events[i], opcode); |
| 335 | } else { |
| 336 | __le16 *opcode = rp->opcodes; |
| 337 | |
| 338 | for (i = 0; i < num_commands; i++, opcode++) |
| 339 | put_unaligned_le16(mgmt_untrusted_commands[i], opcode); |
| 340 | |
| 341 | for (i = 0; i < num_events; i++, opcode++) |
| 342 | put_unaligned_le16(mgmt_untrusted_events[i], opcode); |
| 343 | } |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 344 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 345 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 346 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 347 | kfree(rp); |
| 348 | |
| 349 | return err; |
| 350 | } |
| 351 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 352 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 353 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 354 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 355 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 356 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 357 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 358 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 359 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 360 | |
| 361 | BT_DBG("sock %p", sk); |
| 362 | |
| 363 | read_lock(&hci_dev_list_lock); |
| 364 | |
| 365 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 366 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 367 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 368 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 369 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 372 | rp_len = sizeof(*rp) + (2 * count); |
| 373 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 374 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 375 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 376 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 377 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 378 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 379 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 380 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 381 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 382 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 383 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 384 | continue; |
| 385 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 386 | /* Devices marked as raw-only are neither configured |
| 387 | * nor unconfigured controllers. |
| 388 | */ |
| 389 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 390 | continue; |
| 391 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 392 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 393 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 394 | rp->index[count++] = cpu_to_le16(d->id); |
| 395 | BT_DBG("Added hci%u", d->id); |
| 396 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 399 | rp->num_controllers = cpu_to_le16(count); |
| 400 | rp_len = sizeof(*rp) + (2 * count); |
| 401 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 402 | read_unlock(&hci_dev_list_lock); |
| 403 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 404 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 405 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 406 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 407 | kfree(rp); |
| 408 | |
| 409 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 412 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 413 | void *data, u16 data_len) |
| 414 | { |
| 415 | struct mgmt_rp_read_unconf_index_list *rp; |
| 416 | struct hci_dev *d; |
| 417 | size_t rp_len; |
| 418 | u16 count; |
| 419 | int err; |
| 420 | |
| 421 | BT_DBG("sock %p", sk); |
| 422 | |
| 423 | read_lock(&hci_dev_list_lock); |
| 424 | |
| 425 | count = 0; |
| 426 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 427 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 428 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 429 | count++; |
| 430 | } |
| 431 | |
| 432 | rp_len = sizeof(*rp) + (2 * count); |
| 433 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 434 | if (!rp) { |
| 435 | read_unlock(&hci_dev_list_lock); |
| 436 | return -ENOMEM; |
| 437 | } |
| 438 | |
| 439 | count = 0; |
| 440 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 441 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 442 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 443 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 444 | continue; |
| 445 | |
| 446 | /* Devices marked as raw-only are neither configured |
| 447 | * nor unconfigured controllers. |
| 448 | */ |
| 449 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 450 | continue; |
| 451 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 452 | if (d->dev_type == HCI_PRIMARY && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 453 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 454 | rp->index[count++] = cpu_to_le16(d->id); |
| 455 | BT_DBG("Added hci%u", d->id); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | rp->num_controllers = cpu_to_le16(count); |
| 460 | rp_len = sizeof(*rp) + (2 * count); |
| 461 | |
| 462 | read_unlock(&hci_dev_list_lock); |
| 463 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 464 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 465 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 466 | |
| 467 | kfree(rp); |
| 468 | |
| 469 | return err; |
| 470 | } |
| 471 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 472 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 473 | void *data, u16 data_len) |
| 474 | { |
| 475 | struct mgmt_rp_read_ext_index_list *rp; |
| 476 | struct hci_dev *d; |
| 477 | size_t rp_len; |
| 478 | u16 count; |
| 479 | int err; |
| 480 | |
| 481 | BT_DBG("sock %p", sk); |
| 482 | |
| 483 | read_lock(&hci_dev_list_lock); |
| 484 | |
| 485 | count = 0; |
| 486 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 487 | if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP) |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 488 | count++; |
| 489 | } |
| 490 | |
| 491 | rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count); |
| 492 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 493 | if (!rp) { |
| 494 | read_unlock(&hci_dev_list_lock); |
| 495 | return -ENOMEM; |
| 496 | } |
| 497 | |
| 498 | count = 0; |
| 499 | list_for_each_entry(d, &hci_dev_list, list) { |
| 500 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 501 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 502 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 503 | continue; |
| 504 | |
| 505 | /* Devices marked as raw-only are neither configured |
| 506 | * nor unconfigured controllers. |
| 507 | */ |
| 508 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 509 | continue; |
| 510 | |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 511 | if (d->dev_type == HCI_PRIMARY) { |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 512 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 513 | rp->entry[count].type = 0x01; |
| 514 | else |
| 515 | rp->entry[count].type = 0x00; |
| 516 | } else if (d->dev_type == HCI_AMP) { |
| 517 | rp->entry[count].type = 0x02; |
| 518 | } else { |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | rp->entry[count].bus = d->bus; |
| 523 | rp->entry[count++].index = cpu_to_le16(d->id); |
| 524 | BT_DBG("Added hci%u", d->id); |
| 525 | } |
| 526 | |
| 527 | rp->num_controllers = cpu_to_le16(count); |
| 528 | rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count); |
| 529 | |
| 530 | read_unlock(&hci_dev_list_lock); |
| 531 | |
| 532 | /* If this command is called at least once, then all the |
| 533 | * default index and unconfigured index events are disabled |
| 534 | * and from now on only extended index events are used. |
| 535 | */ |
| 536 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 537 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 538 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 539 | |
| 540 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 541 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len); |
| 542 | |
| 543 | kfree(rp); |
| 544 | |
| 545 | return err; |
| 546 | } |
| 547 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 548 | static bool is_configured(struct hci_dev *hdev) |
| 549 | { |
| 550 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 551 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 552 | return false; |
| 553 | |
| 554 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 555 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 556 | return false; |
| 557 | |
| 558 | return true; |
| 559 | } |
| 560 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 561 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 562 | { |
| 563 | u32 options = 0; |
| 564 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 565 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 566 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 567 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 568 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 569 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 570 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 571 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 572 | |
| 573 | return cpu_to_le32(options); |
| 574 | } |
| 575 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 576 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 577 | { |
| 578 | __le32 options = get_missing_options(hdev); |
| 579 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 580 | return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 581 | sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 582 | } |
| 583 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 584 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 585 | { |
| 586 | __le32 options = get_missing_options(hdev); |
| 587 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 588 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 589 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 590 | } |
| 591 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 592 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 593 | void *data, u16 data_len) |
| 594 | { |
| 595 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 596 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 597 | |
| 598 | BT_DBG("sock %p %s", sk, hdev->name); |
| 599 | |
| 600 | hci_dev_lock(hdev); |
| 601 | |
| 602 | memset(&rp, 0, sizeof(rp)); |
| 603 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 604 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 605 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 606 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 607 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 608 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 609 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 610 | |
| 611 | rp.supported_options = cpu_to_le32(options); |
| 612 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 613 | |
| 614 | hci_dev_unlock(hdev); |
| 615 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 616 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 617 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 620 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 621 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 622 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 623 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 624 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 625 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 626 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 627 | settings |= MGMT_SETTING_CONNECTABLE; |
| 628 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 629 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 630 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 631 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 632 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 633 | settings |= MGMT_SETTING_BREDR; |
| 634 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 635 | |
| 636 | if (lmp_ssp_capable(hdev)) { |
| 637 | settings |= MGMT_SETTING_SSP; |
| 638 | settings |= MGMT_SETTING_HS; |
| 639 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 640 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 641 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 642 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 643 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 644 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 645 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 646 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 647 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 648 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 649 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 650 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 651 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 652 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 653 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 654 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 655 | settings |= MGMT_SETTING_CONFIGURATION; |
| 656 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 657 | return settings; |
| 658 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 659 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 660 | static u32 get_current_settings(struct hci_dev *hdev) |
| 661 | { |
| 662 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 663 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 664 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 665 | settings |= MGMT_SETTING_POWERED; |
| 666 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 667 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 668 | settings |= MGMT_SETTING_CONNECTABLE; |
| 669 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 670 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 671 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 672 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 673 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 674 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 675 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 676 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 677 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 678 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 679 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 680 | settings |= MGMT_SETTING_BREDR; |
| 681 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 682 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 683 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 684 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 685 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 686 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 687 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 688 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 689 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 690 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 691 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 692 | settings |= MGMT_SETTING_HS; |
| 693 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 694 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 695 | settings |= MGMT_SETTING_ADVERTISING; |
| 696 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 697 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 698 | settings |= MGMT_SETTING_SECURE_CONN; |
| 699 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 700 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 701 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 702 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 703 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 704 | settings |= MGMT_SETTING_PRIVACY; |
| 705 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 706 | /* The current setting for static address has two purposes. The |
| 707 | * first is to indicate if the static address will be used and |
| 708 | * the second is to indicate if it is actually set. |
| 709 | * |
| 710 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e9 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 711 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 712 | * address is actually used decides if the flag is set or not. |
| 713 | * |
| 714 | * For single mode LE only controllers and dual-mode controllers |
| 715 | * with BR/EDR disabled, the existence of the static address will |
| 716 | * be evaluated. |
| 717 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 718 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 719 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 720 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 721 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 722 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 723 | } |
| 724 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 725 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 728 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 729 | { |
| 730 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 731 | } |
| 732 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 733 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 734 | struct hci_dev *hdev, |
| 735 | const void *data) |
| 736 | { |
| 737 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 738 | } |
| 739 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 740 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 741 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 742 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 743 | |
| 744 | /* If there's a pending mgmt command the flags will not yet have |
| 745 | * their final values, so check for this first. |
| 746 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 747 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 748 | if (cmd) { |
| 749 | struct mgmt_mode *cp = cmd->param; |
| 750 | if (cp->val == 0x01) |
| 751 | return LE_AD_GENERAL; |
| 752 | else if (cp->val == 0x02) |
| 753 | return LE_AD_LIMITED; |
| 754 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 755 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 756 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 757 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 758 | return LE_AD_GENERAL; |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 764 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 765 | { |
| 766 | struct mgmt_pending_cmd *cmd; |
| 767 | |
| 768 | /* If there's a pending mgmt command the flag will not yet have |
| 769 | * it's final value, so check for this first. |
| 770 | */ |
| 771 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 772 | if (cmd) { |
| 773 | struct mgmt_mode *cp = cmd->param; |
| 774 | |
| 775 | return cp->val; |
| 776 | } |
| 777 | |
| 778 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 779 | } |
| 780 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 781 | static void service_cache_off(struct work_struct *work) |
| 782 | { |
| 783 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 784 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 785 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 786 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 787 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 788 | return; |
| 789 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 790 | hci_req_init(&req, hdev); |
| 791 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 792 | hci_dev_lock(hdev); |
| 793 | |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 794 | __hci_req_update_eir(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 795 | __hci_req_update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 796 | |
| 797 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 798 | |
| 799 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 800 | } |
| 801 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 802 | static void rpa_expired(struct work_struct *work) |
| 803 | { |
| 804 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 805 | rpa_expired.work); |
| 806 | struct hci_request req; |
| 807 | |
| 808 | BT_DBG(""); |
| 809 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 810 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 811 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 812 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 813 | return; |
| 814 | |
| 815 | /* The generation of a new RPA and programming it into the |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 816 | * controller happens in the hci_req_enable_advertising() |
| 817 | * function. |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 818 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 819 | hci_req_init(&req, hdev); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 820 | __hci_req_enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 821 | hci_req_run(&req, NULL); |
| 822 | } |
| 823 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 824 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 825 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 826 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 827 | return; |
| 828 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 829 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 830 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 831 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 832 | /* Non-mgmt controlled devices get this bit set |
| 833 | * implicitly so that pairing works for them, however |
| 834 | * for mgmt we require user-space to explicitly enable |
| 835 | * it |
| 836 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 837 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 838 | } |
| 839 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 840 | static int read_controller_info(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 841 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 842 | { |
| 843 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 844 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 845 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 846 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 847 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 848 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 849 | memset(&rp, 0, sizeof(rp)); |
| 850 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 851 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 852 | |
| 853 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 854 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 855 | |
| 856 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 857 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 858 | |
| 859 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 860 | |
| 861 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 862 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 863 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 864 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 865 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 866 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 867 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 868 | } |
| 869 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 870 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 871 | { |
| 872 | u16 eir_len = 0; |
| 873 | size_t name_len; |
| 874 | |
| 875 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 876 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 877 | hdev->dev_class, 3); |
| 878 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 879 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 880 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 881 | hdev->appearance); |
| 882 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 883 | name_len = strlen(hdev->dev_name); |
| 884 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 885 | hdev->dev_name, name_len); |
| 886 | |
| 887 | name_len = strlen(hdev->short_name); |
| 888 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 889 | hdev->short_name, name_len); |
| 890 | |
| 891 | return eir_len; |
| 892 | } |
| 893 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 894 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 895 | void *data, u16 data_len) |
| 896 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 897 | char buf[512]; |
| 898 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 899 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 900 | |
| 901 | BT_DBG("sock %p %s", sk, hdev->name); |
| 902 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 903 | memset(&buf, 0, sizeof(buf)); |
| 904 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 905 | hci_dev_lock(hdev); |
| 906 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 907 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 908 | |
| 909 | rp->version = hdev->hci_ver; |
| 910 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 911 | |
| 912 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 913 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 914 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 915 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 916 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 917 | rp->eir_len = cpu_to_le16(eir_len); |
| 918 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 919 | hci_dev_unlock(hdev); |
| 920 | |
| 921 | /* If this command is called at least once, then the events |
| 922 | * for class of device and local name changes are disabled |
| 923 | * and only the new extended controller information event |
| 924 | * is used. |
| 925 | */ |
| 926 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 927 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 928 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 929 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 930 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 931 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 935 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 936 | char buf[512]; |
| 937 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 938 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 939 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 940 | memset(buf, 0, sizeof(buf)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 941 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 942 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 943 | ev->eir_len = cpu_to_le16(eir_len); |
| 944 | |
| 945 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 946 | sizeof(*ev) + eir_len, |
| 947 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 948 | } |
| 949 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 950 | static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 951 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 952 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 953 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 954 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 955 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 956 | } |
| 957 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 958 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 959 | { |
| 960 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 961 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 962 | if (hci_conn_count(hdev) == 0) { |
| 963 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 964 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 965 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 966 | } |
| 967 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 968 | void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 969 | { |
| 970 | struct mgmt_ev_advertising_added ev; |
| 971 | |
| 972 | ev.instance = instance; |
| 973 | |
| 974 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 975 | } |
| 976 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 977 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 978 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 979 | { |
| 980 | struct mgmt_ev_advertising_removed ev; |
| 981 | |
| 982 | ev.instance = instance; |
| 983 | |
| 984 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 985 | } |
| 986 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 987 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 988 | { |
| 989 | if (hdev->adv_instance_timeout) { |
| 990 | hdev->adv_instance_timeout = 0; |
| 991 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 992 | } |
| 993 | } |
| 994 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 995 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 996 | { |
| 997 | struct hci_request req; |
| 998 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 999 | bool discov_stopped; |
| 1000 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1001 | |
| 1002 | hci_req_init(&req, hdev); |
| 1003 | |
| 1004 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1005 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1006 | u8 scan = 0x00; |
| 1007 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1008 | } |
| 1009 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1010 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1011 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1012 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1013 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1014 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 1015 | discov_stopped = hci_req_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1016 | |
| 1017 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 1018 | /* 0x15 == Terminated due to Power Off */ |
| 1019 | __hci_abort_conn(&req, conn, 0x15); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1020 | } |
| 1021 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1022 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1023 | if (!err && discov_stopped) |
| 1024 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1025 | |
| 1026 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1027 | } |
| 1028 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1029 | static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1030 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1031 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1032 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1033 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1034 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1035 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1036 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1037 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1038 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1039 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1040 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1041 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1042 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1043 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1044 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1045 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1046 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1047 | goto failed; |
| 1048 | } |
| 1049 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1050 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1051 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1052 | goto failed; |
| 1053 | } |
| 1054 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1055 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1056 | if (!cmd) { |
| 1057 | err = -ENOMEM; |
| 1058 | goto failed; |
| 1059 | } |
| 1060 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1061 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1062 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1063 | err = 0; |
| 1064 | } else { |
| 1065 | /* Disconnect connections, stop scans, etc */ |
| 1066 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1067 | if (!err) |
| 1068 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1069 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1070 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1071 | /* ENODATA means there were no HCI commands queued */ |
| 1072 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1073 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1074 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1075 | err = 0; |
| 1076 | } |
| 1077 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1078 | |
| 1079 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1080 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1081 | return err; |
| 1082 | } |
| 1083 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1084 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1085 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1086 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1087 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 1088 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1089 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1092 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1093 | { |
| 1094 | return new_settings(hdev, NULL); |
| 1095 | } |
| 1096 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1097 | struct cmd_lookup { |
| 1098 | struct sock *sk; |
| 1099 | struct hci_dev *hdev; |
| 1100 | u8 mgmt_status; |
| 1101 | }; |
| 1102 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1103 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1104 | { |
| 1105 | struct cmd_lookup *match = data; |
| 1106 | |
| 1107 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1108 | |
| 1109 | list_del(&cmd->list); |
| 1110 | |
| 1111 | if (match->sk == NULL) { |
| 1112 | match->sk = cmd->sk; |
| 1113 | sock_hold(match->sk); |
| 1114 | } |
| 1115 | |
| 1116 | mgmt_pending_free(cmd); |
| 1117 | } |
| 1118 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1119 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1120 | { |
| 1121 | u8 *status = data; |
| 1122 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1123 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1124 | mgmt_pending_remove(cmd); |
| 1125 | } |
| 1126 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1127 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1128 | { |
| 1129 | if (cmd->cmd_complete) { |
| 1130 | u8 *status = data; |
| 1131 | |
| 1132 | cmd->cmd_complete(cmd, *status); |
| 1133 | mgmt_pending_remove(cmd); |
| 1134 | |
| 1135 | return; |
| 1136 | } |
| 1137 | |
| 1138 | cmd_status_rsp(cmd, data); |
| 1139 | } |
| 1140 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1141 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1142 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1143 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1144 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1147 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1148 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1149 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1150 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1153 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1154 | { |
| 1155 | if (!lmp_bredr_capable(hdev)) |
| 1156 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1157 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1158 | return MGMT_STATUS_REJECTED; |
| 1159 | else |
| 1160 | return MGMT_STATUS_SUCCESS; |
| 1161 | } |
| 1162 | |
| 1163 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1164 | { |
| 1165 | if (!lmp_le_capable(hdev)) |
| 1166 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1167 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1168 | return MGMT_STATUS_REJECTED; |
| 1169 | else |
| 1170 | return MGMT_STATUS_SUCCESS; |
| 1171 | } |
| 1172 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1173 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1174 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1175 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1176 | |
| 1177 | BT_DBG("status 0x%02x", status); |
| 1178 | |
| 1179 | hci_dev_lock(hdev); |
| 1180 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1181 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1182 | if (!cmd) |
| 1183 | goto unlock; |
| 1184 | |
| 1185 | if (status) { |
| 1186 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1187 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1188 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1189 | goto remove_cmd; |
| 1190 | } |
| 1191 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1192 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1193 | hdev->discov_timeout > 0) { |
| 1194 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1195 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1196 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1197 | |
| 1198 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1199 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1200 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1201 | remove_cmd: |
| 1202 | mgmt_pending_remove(cmd); |
| 1203 | |
| 1204 | unlock: |
| 1205 | hci_dev_unlock(hdev); |
| 1206 | } |
| 1207 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1208 | static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1209 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1210 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1211 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1212 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1213 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1214 | int err; |
| 1215 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1216 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1217 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1218 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1219 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1220 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1221 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1222 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1223 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1224 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1225 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1226 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1227 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1228 | |
| 1229 | /* Disabling discoverable requires that no timeout is set, |
| 1230 | * and enabling limited discoverable requires a timeout. |
| 1231 | */ |
| 1232 | if ((cp->val == 0x00 && timeout > 0) || |
| 1233 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1234 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1235 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1236 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1237 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1238 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1239 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1240 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1241 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1242 | goto failed; |
| 1243 | } |
| 1244 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1245 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1246 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1247 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1248 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1249 | goto failed; |
| 1250 | } |
| 1251 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1252 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1253 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1254 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1255 | goto failed; |
| 1256 | } |
| 1257 | |
| 1258 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1259 | bool changed = false; |
| 1260 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1261 | /* Setting limited discoverable when powered off is |
| 1262 | * not a valid operation since it requires a timeout |
| 1263 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1264 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1265 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1266 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1267 | changed = true; |
| 1268 | } |
| 1269 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1270 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1271 | if (err < 0) |
| 1272 | goto failed; |
| 1273 | |
| 1274 | if (changed) |
| 1275 | err = new_settings(hdev, sk); |
| 1276 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1277 | goto failed; |
| 1278 | } |
| 1279 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1280 | /* If the current mode is the same, then just update the timeout |
| 1281 | * value with the new value. And if only the timeout gets updated, |
| 1282 | * then no need for any HCI transactions. |
| 1283 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1284 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1285 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1286 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1287 | cancel_delayed_work(&hdev->discov_off); |
| 1288 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1289 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1290 | if (cp->val && hdev->discov_timeout > 0) { |
| 1291 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1292 | queue_delayed_work(hdev->req_workqueue, |
| 1293 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1294 | } |
| 1295 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1296 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1297 | goto failed; |
| 1298 | } |
| 1299 | |
| 1300 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1301 | if (!cmd) { |
| 1302 | err = -ENOMEM; |
| 1303 | goto failed; |
| 1304 | } |
| 1305 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1306 | /* Cancel any potential discoverable timeout that might be |
| 1307 | * still active and store new timeout value. The arming of |
| 1308 | * the timeout happens in the complete handler. |
| 1309 | */ |
| 1310 | cancel_delayed_work(&hdev->discov_off); |
| 1311 | hdev->discov_timeout = timeout; |
| 1312 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1313 | if (cp->val) |
| 1314 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1315 | else |
| 1316 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1317 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1318 | /* Limited discoverable mode */ |
| 1319 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1320 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1321 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1322 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1323 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1324 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1325 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1326 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1327 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1328 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1329 | return err; |
| 1330 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1331 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1332 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1333 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1334 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1335 | |
| 1336 | BT_DBG("status 0x%02x", status); |
| 1337 | |
| 1338 | hci_dev_lock(hdev); |
| 1339 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1340 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1341 | if (!cmd) |
| 1342 | goto unlock; |
| 1343 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1344 | if (status) { |
| 1345 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1346 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1347 | goto remove_cmd; |
| 1348 | } |
| 1349 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1350 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1351 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1352 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1353 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1354 | mgmt_pending_remove(cmd); |
| 1355 | |
| 1356 | unlock: |
| 1357 | hci_dev_unlock(hdev); |
| 1358 | } |
| 1359 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1360 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1361 | struct sock *sk, u8 val) |
| 1362 | { |
| 1363 | bool changed = false; |
| 1364 | int err; |
| 1365 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1366 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1367 | changed = true; |
| 1368 | |
| 1369 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1370 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1371 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1372 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1373 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1377 | if (err < 0) |
| 1378 | return err; |
| 1379 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1380 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1381 | hci_req_update_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1382 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1383 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1384 | } |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1385 | |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1389 | static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1390 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1391 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1392 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1393 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1394 | int err; |
| 1395 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1396 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1397 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1398 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1399 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1400 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1401 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1402 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1403 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1404 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1405 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1406 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1407 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1408 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1409 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1410 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1411 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1412 | } |
| 1413 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1414 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1415 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1416 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1417 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1418 | goto failed; |
| 1419 | } |
| 1420 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1421 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1422 | if (!cmd) { |
| 1423 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1424 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1425 | } |
| 1426 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1427 | if (cp->val) { |
| 1428 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1429 | } else { |
| 1430 | if (hdev->discov_timeout > 0) |
| 1431 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1432 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1433 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1434 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1435 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1436 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1437 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1438 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1439 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1440 | |
| 1441 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1442 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1443 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1446 | static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1447 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1448 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1449 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1450 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1451 | int err; |
| 1452 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1453 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1454 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1455 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1456 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1457 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1458 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1459 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1460 | |
| 1461 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1462 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1463 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1464 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1465 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1466 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1467 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1468 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1469 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1470 | if (changed) { |
| 1471 | /* In limited privacy mode the change of bondable mode |
| 1472 | * may affect the local advertising address. |
| 1473 | */ |
| 1474 | if (hdev_is_powered(hdev) && |
| 1475 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 1476 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1477 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1478 | queue_work(hdev->req_workqueue, |
| 1479 | &hdev->discoverable_update); |
| 1480 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1481 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1482 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1483 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1484 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1485 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1486 | return err; |
| 1487 | } |
| 1488 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1489 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1490 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1491 | { |
| 1492 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1493 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1494 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1495 | int err; |
| 1496 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1497 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1498 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1499 | status = mgmt_bredr_support(hdev); |
| 1500 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1501 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1502 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1503 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1504 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1505 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1506 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1507 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1508 | hci_dev_lock(hdev); |
| 1509 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1510 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1511 | bool changed = false; |
| 1512 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1513 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1514 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1515 | changed = true; |
| 1516 | } |
| 1517 | |
| 1518 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1519 | if (err < 0) |
| 1520 | goto failed; |
| 1521 | |
| 1522 | if (changed) |
| 1523 | err = new_settings(hdev, sk); |
| 1524 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1525 | goto failed; |
| 1526 | } |
| 1527 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1528 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1529 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1530 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1531 | goto failed; |
| 1532 | } |
| 1533 | |
| 1534 | val = !!cp->val; |
| 1535 | |
| 1536 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1537 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1538 | goto failed; |
| 1539 | } |
| 1540 | |
| 1541 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1542 | if (!cmd) { |
| 1543 | err = -ENOMEM; |
| 1544 | goto failed; |
| 1545 | } |
| 1546 | |
| 1547 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1548 | if (err < 0) { |
| 1549 | mgmt_pending_remove(cmd); |
| 1550 | goto failed; |
| 1551 | } |
| 1552 | |
| 1553 | failed: |
| 1554 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1555 | return err; |
| 1556 | } |
| 1557 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1558 | static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1559 | { |
| 1560 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1561 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1562 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1563 | int err; |
| 1564 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1565 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1566 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1567 | status = mgmt_bredr_support(hdev); |
| 1568 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1569 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1570 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1571 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1572 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1573 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1574 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1575 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1576 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1577 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1578 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1579 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1580 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1581 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1582 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1583 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1584 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1585 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1586 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1587 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1588 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1589 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1590 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1591 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1592 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1593 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1594 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1598 | if (err < 0) |
| 1599 | goto failed; |
| 1600 | |
| 1601 | if (changed) |
| 1602 | err = new_settings(hdev, sk); |
| 1603 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1604 | goto failed; |
| 1605 | } |
| 1606 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1607 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1608 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1609 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1610 | goto failed; |
| 1611 | } |
| 1612 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1613 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1614 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1615 | goto failed; |
| 1616 | } |
| 1617 | |
| 1618 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1619 | if (!cmd) { |
| 1620 | err = -ENOMEM; |
| 1621 | goto failed; |
| 1622 | } |
| 1623 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1624 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1625 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1626 | sizeof(cp->val), &cp->val); |
| 1627 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1628 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1629 | if (err < 0) { |
| 1630 | mgmt_pending_remove(cmd); |
| 1631 | goto failed; |
| 1632 | } |
| 1633 | |
| 1634 | failed: |
| 1635 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1636 | return err; |
| 1637 | } |
| 1638 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1639 | static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1640 | { |
| 1641 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1642 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1643 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1644 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1645 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1646 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1647 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1648 | status = mgmt_bredr_support(hdev); |
| 1649 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1650 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1651 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1652 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1653 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1654 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1655 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1656 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1657 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1658 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1659 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1660 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1661 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1662 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1663 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1664 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1665 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1666 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1667 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1668 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1669 | goto unlock; |
| 1670 | } |
| 1671 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1672 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1673 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1674 | } else { |
| 1675 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1676 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1677 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1678 | goto unlock; |
| 1679 | } |
| 1680 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1681 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1682 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1683 | |
| 1684 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1685 | if (err < 0) |
| 1686 | goto unlock; |
| 1687 | |
| 1688 | if (changed) |
| 1689 | err = new_settings(hdev, sk); |
| 1690 | |
| 1691 | unlock: |
| 1692 | hci_dev_unlock(hdev); |
| 1693 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1696 | static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1697 | { |
| 1698 | struct cmd_lookup match = { NULL, hdev }; |
| 1699 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1700 | hci_dev_lock(hdev); |
| 1701 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1702 | if (status) { |
| 1703 | u8 mgmt_err = mgmt_status(status); |
| 1704 | |
| 1705 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1706 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1707 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1711 | |
| 1712 | new_settings(hdev, match.sk); |
| 1713 | |
| 1714 | if (match.sk) |
| 1715 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1716 | |
| 1717 | /* Make sure the controller has a good default for |
| 1718 | * advertising data. Restrict the update to when LE |
| 1719 | * has actually been enabled. During power on, the |
| 1720 | * update in powered_update_hci will take care of it. |
| 1721 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1722 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1723 | struct hci_request req; |
| 1724 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1725 | hci_req_init(&req, hdev); |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 1726 | __hci_req_update_adv_data(&req, 0x00); |
| 1727 | __hci_req_update_scan_rsp_data(&req, 0x00); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1728 | hci_req_run(&req, NULL); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 1729 | hci_update_background_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1730 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1731 | |
| 1732 | unlock: |
| 1733 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1734 | } |
| 1735 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1736 | static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1737 | { |
| 1738 | struct mgmt_mode *cp = data; |
| 1739 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1740 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1741 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1742 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1743 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1744 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1745 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1746 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1747 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1748 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1749 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1750 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1751 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1752 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1753 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1754 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1755 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1756 | * controllers configured as LE only devices, do not allow |
| 1757 | * switching LE off. These have either LE enabled explicitly |
| 1758 | * or BR/EDR has been previously switched off. |
| 1759 | * |
| 1760 | * When trying to enable an already enabled LE, then gracefully |
| 1761 | * send a positive response. Trying to disable it however will |
| 1762 | * result into rejection. |
| 1763 | */ |
| 1764 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1765 | if (cp->val == 0x01) |
| 1766 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1767 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1768 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1769 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1770 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1771 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1772 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1773 | |
| 1774 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1775 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1776 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1777 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1778 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1779 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1780 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1781 | bool changed = false; |
| 1782 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1783 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1784 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1785 | changed = true; |
| 1786 | } |
| 1787 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1788 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1789 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1790 | changed = true; |
| 1791 | } |
| 1792 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1793 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1794 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1795 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1796 | |
| 1797 | if (changed) |
| 1798 | err = new_settings(hdev, sk); |
| 1799 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1800 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1801 | } |
| 1802 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1803 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 1804 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1805 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1806 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1807 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1811 | if (!cmd) { |
| 1812 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1813 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1814 | } |
| 1815 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1816 | hci_req_init(&req, hdev); |
| 1817 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1818 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1819 | |
| 1820 | if (val) { |
| 1821 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 1822 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1823 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1824 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1825 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1826 | } |
| 1827 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1828 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 1829 | &hci_cp); |
| 1830 | |
| 1831 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1832 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1833 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1834 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1835 | unlock: |
| 1836 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1837 | return err; |
| 1838 | } |
| 1839 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1840 | /* This is a helper function to test for pending mgmt commands that can |
| 1841 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 1842 | * mgmt command at a time since otherwise we cannot easily track what |
| 1843 | * the current values are, will be, and based on that calculate if a new |
| 1844 | * HCI command needs to be sent and if yes with what value. |
| 1845 | */ |
| 1846 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 1847 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1848 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1849 | |
| 1850 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 1851 | switch (cmd->opcode) { |
| 1852 | case MGMT_OP_ADD_UUID: |
| 1853 | case MGMT_OP_REMOVE_UUID: |
| 1854 | case MGMT_OP_SET_DEV_CLASS: |
| 1855 | case MGMT_OP_SET_POWERED: |
| 1856 | return true; |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | return false; |
| 1861 | } |
| 1862 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 1863 | static const u8 bluetooth_base_uuid[] = { |
| 1864 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 1865 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1866 | }; |
| 1867 | |
| 1868 | static u8 get_uuid_size(const u8 *uuid) |
| 1869 | { |
| 1870 | u32 val; |
| 1871 | |
| 1872 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 1873 | return 128; |
| 1874 | |
| 1875 | val = get_unaligned_le32(&uuid[12]); |
| 1876 | if (val > 0xffff) |
| 1877 | return 32; |
| 1878 | |
| 1879 | return 16; |
| 1880 | } |
| 1881 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1882 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 1883 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1884 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1885 | |
| 1886 | hci_dev_lock(hdev); |
| 1887 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1888 | cmd = pending_find(mgmt_op, hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1889 | if (!cmd) |
| 1890 | goto unlock; |
| 1891 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1892 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 1893 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1894 | |
| 1895 | mgmt_pending_remove(cmd); |
| 1896 | |
| 1897 | unlock: |
| 1898 | hci_dev_unlock(hdev); |
| 1899 | } |
| 1900 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1901 | static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1902 | { |
| 1903 | BT_DBG("status 0x%02x", status); |
| 1904 | |
| 1905 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 1906 | } |
| 1907 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1908 | static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1909 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1910 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1911 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1912 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1913 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1914 | int err; |
| 1915 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1916 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1917 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1918 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1919 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1920 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1921 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 1922 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1923 | goto failed; |
| 1924 | } |
| 1925 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 1926 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1927 | if (!uuid) { |
| 1928 | err = -ENOMEM; |
| 1929 | goto failed; |
| 1930 | } |
| 1931 | |
| 1932 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1933 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 1934 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1935 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 1936 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1937 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1938 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1939 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 1940 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 1941 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1942 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1943 | err = hci_req_run(&req, add_uuid_complete); |
| 1944 | if (err < 0) { |
| 1945 | if (err != -ENODATA) |
| 1946 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1947 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1948 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 1949 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1950 | goto failed; |
| 1951 | } |
| 1952 | |
| 1953 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1954 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1955 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1956 | goto failed; |
| 1957 | } |
| 1958 | |
| 1959 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1960 | |
| 1961 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1962 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1963 | return err; |
| 1964 | } |
| 1965 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 1966 | static bool enable_service_cache(struct hci_dev *hdev) |
| 1967 | { |
| 1968 | if (!hdev_is_powered(hdev)) |
| 1969 | return false; |
| 1970 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1971 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 1972 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 1973 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 1974 | return true; |
| 1975 | } |
| 1976 | |
| 1977 | return false; |
| 1978 | } |
| 1979 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1980 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1981 | { |
| 1982 | BT_DBG("status 0x%02x", status); |
| 1983 | |
| 1984 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 1985 | } |
| 1986 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1987 | static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1988 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1989 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1990 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1991 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 1992 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1993 | u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1994 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1995 | int err, found; |
| 1996 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1997 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1998 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1999 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2000 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2001 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2002 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2003 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2004 | goto unlock; |
| 2005 | } |
| 2006 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2007 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2008 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2009 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2010 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2011 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2012 | MGMT_OP_REMOVE_UUID, |
| 2013 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2014 | goto unlock; |
| 2015 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2016 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2017 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | found = 0; |
| 2021 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2022 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2023 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2024 | continue; |
| 2025 | |
| 2026 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2027 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2028 | found++; |
| 2029 | } |
| 2030 | |
| 2031 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2032 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2033 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2034 | goto unlock; |
| 2035 | } |
| 2036 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2037 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2038 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2039 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2040 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2041 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2042 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2043 | err = hci_req_run(&req, remove_uuid_complete); |
| 2044 | if (err < 0) { |
| 2045 | if (err != -ENODATA) |
| 2046 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2047 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2048 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2049 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2050 | goto unlock; |
| 2051 | } |
| 2052 | |
| 2053 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2054 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2055 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2056 | goto unlock; |
| 2057 | } |
| 2058 | |
| 2059 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2060 | |
| 2061 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2062 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2063 | return err; |
| 2064 | } |
| 2065 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2066 | static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2067 | { |
| 2068 | BT_DBG("status 0x%02x", status); |
| 2069 | |
| 2070 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2071 | } |
| 2072 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2073 | static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2074 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2075 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2076 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2077 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2078 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2079 | int err; |
| 2080 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2081 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2082 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2083 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2084 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2085 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2086 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2087 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2088 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2089 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2090 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2091 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2092 | goto unlock; |
| 2093 | } |
| 2094 | |
| 2095 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2096 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2097 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2098 | goto unlock; |
| 2099 | } |
| 2100 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2101 | hdev->major_class = cp->major; |
| 2102 | hdev->minor_class = cp->minor; |
| 2103 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2104 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2105 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2106 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2107 | goto unlock; |
| 2108 | } |
| 2109 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2110 | hci_req_init(&req, hdev); |
| 2111 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2112 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2113 | hci_dev_unlock(hdev); |
| 2114 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2115 | hci_dev_lock(hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2116 | __hci_req_update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2117 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2118 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2119 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2120 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2121 | err = hci_req_run(&req, set_class_complete); |
| 2122 | if (err < 0) { |
| 2123 | if (err != -ENODATA) |
| 2124 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2125 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2126 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2127 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2128 | goto unlock; |
| 2129 | } |
| 2130 | |
| 2131 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2132 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2133 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2134 | goto unlock; |
| 2135 | } |
| 2136 | |
| 2137 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2138 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2139 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2140 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2141 | return err; |
| 2142 | } |
| 2143 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2144 | static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2145 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2146 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2147 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2148 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2149 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2150 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2151 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2152 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2153 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2154 | BT_DBG("request for %s", hdev->name); |
| 2155 | |
| 2156 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2157 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2158 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2159 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2160 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2161 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2162 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2163 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2164 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2165 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2166 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2167 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2168 | expected_len = sizeof(*cp) + key_count * |
| 2169 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2170 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2171 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2172 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2173 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2174 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2175 | } |
| 2176 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2177 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2178 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2179 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2180 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2181 | BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2182 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2183 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2184 | for (i = 0; i < key_count; i++) { |
| 2185 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2186 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2187 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2188 | return mgmt_cmd_status(sk, hdev->id, |
| 2189 | MGMT_OP_LOAD_LINK_KEYS, |
| 2190 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2191 | } |
| 2192 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2193 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2194 | |
| 2195 | hci_link_keys_clear(hdev); |
| 2196 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2197 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2198 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2199 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2200 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2201 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2202 | |
| 2203 | if (changed) |
| 2204 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2205 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2206 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2207 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2208 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2209 | /* Always ignore debug keys and require a new pairing if |
| 2210 | * the user wants to use them. |
| 2211 | */ |
| 2212 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2213 | continue; |
| 2214 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2215 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2216 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2217 | } |
| 2218 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2219 | mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0); |
Johan Hedberg | 0e5f875 | 2011-11-11 16:18:54 +0200 | [diff] [blame] | 2220 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2221 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2222 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2223 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2224 | } |
| 2225 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2226 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2227 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2228 | { |
| 2229 | struct mgmt_ev_device_unpaired ev; |
| 2230 | |
| 2231 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2232 | ev.addr.type = addr_type; |
| 2233 | |
| 2234 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2235 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2236 | } |
| 2237 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2238 | static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2239 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2240 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2241 | struct mgmt_cp_unpair_device *cp = data; |
| 2242 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2243 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2244 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2245 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2246 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2247 | int err; |
| 2248 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2249 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2250 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2251 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2252 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2253 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2254 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2255 | MGMT_STATUS_INVALID_PARAMS, |
| 2256 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2257 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2258 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2259 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2260 | MGMT_STATUS_INVALID_PARAMS, |
| 2261 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2262 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2263 | hci_dev_lock(hdev); |
| 2264 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2265 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2266 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2267 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2268 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2269 | goto unlock; |
| 2270 | } |
| 2271 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2272 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2273 | /* If disconnection is requested, then look up the |
| 2274 | * connection. If the remote device is connected, it |
| 2275 | * will be later used to terminate the link. |
| 2276 | * |
| 2277 | * Setting it to NULL explicitly will cause no |
| 2278 | * termination of the link. |
| 2279 | */ |
| 2280 | if (cp->disconnect) |
| 2281 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2282 | &cp->addr.bdaddr); |
| 2283 | else |
| 2284 | conn = NULL; |
| 2285 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2286 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2287 | if (err < 0) { |
| 2288 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2289 | MGMT_OP_UNPAIR_DEVICE, |
| 2290 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2291 | sizeof(rp)); |
| 2292 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2295 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2296 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2297 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2298 | /* LE address type */ |
| 2299 | addr_type = le_addr_type(cp->addr.type); |
| 2300 | |
| 2301 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2302 | |
| 2303 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2304 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2305 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2306 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2307 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2308 | goto unlock; |
| 2309 | } |
| 2310 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2311 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2312 | if (!conn) { |
| 2313 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2314 | goto done; |
| 2315 | } |
| 2316 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2317 | /* Abort any ongoing SMP pairing */ |
| 2318 | smp_cancel_pairing(conn); |
| 2319 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2320 | /* Defer clearing up the connection parameters until closing to |
| 2321 | * give a chance of keeping them if a repairing happens. |
| 2322 | */ |
| 2323 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2324 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2325 | /* Disable auto-connection parameters if present */ |
| 2326 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2327 | if (params) { |
| 2328 | if (params->explicit_connect) |
| 2329 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2330 | else |
| 2331 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2332 | } |
| 2333 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2334 | /* If disconnection is not requested, then clear the connection |
| 2335 | * variable so that the link is not terminated. |
| 2336 | */ |
| 2337 | if (!cp->disconnect) |
| 2338 | conn = NULL; |
| 2339 | |
| 2340 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2341 | /* If the connection variable is set, then termination of the |
| 2342 | * link is requested. |
| 2343 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2344 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2345 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2346 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2347 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2348 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2349 | } |
| 2350 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2351 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2352 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2353 | if (!cmd) { |
| 2354 | err = -ENOMEM; |
| 2355 | goto unlock; |
| 2356 | } |
| 2357 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2358 | cmd->cmd_complete = addr_cmd_complete; |
| 2359 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2360 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2361 | if (err < 0) |
| 2362 | mgmt_pending_remove(cmd); |
| 2363 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2364 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2365 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2366 | return err; |
| 2367 | } |
| 2368 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2369 | static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2370 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2371 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2372 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2373 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2374 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2375 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2376 | int err; |
| 2377 | |
| 2378 | BT_DBG(""); |
| 2379 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2380 | memset(&rp, 0, sizeof(rp)); |
| 2381 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2382 | rp.addr.type = cp->addr.type; |
| 2383 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2384 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2385 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2386 | MGMT_STATUS_INVALID_PARAMS, |
| 2387 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2388 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2389 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2390 | |
| 2391 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2392 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2393 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2394 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2395 | goto failed; |
| 2396 | } |
| 2397 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2398 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2399 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2400 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2401 | goto failed; |
| 2402 | } |
| 2403 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2404 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2405 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2406 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2407 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2408 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2409 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2410 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2411 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2412 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2413 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2414 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2415 | goto failed; |
| 2416 | } |
| 2417 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2418 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2419 | if (!cmd) { |
| 2420 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2421 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2422 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2423 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2424 | cmd->cmd_complete = generic_cmd_complete; |
| 2425 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2426 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2427 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2428 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2429 | |
| 2430 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2431 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2432 | return err; |
| 2433 | } |
| 2434 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2435 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2436 | { |
| 2437 | switch (link_type) { |
| 2438 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2439 | switch (addr_type) { |
| 2440 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2441 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2442 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2443 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2444 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2445 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2446 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2447 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2448 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2449 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2450 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2451 | } |
| 2452 | } |
| 2453 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2454 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2455 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2456 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2457 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2458 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2459 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2460 | int err; |
| 2461 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2462 | |
| 2463 | BT_DBG(""); |
| 2464 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2465 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2466 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2467 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2468 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2469 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2470 | goto unlock; |
| 2471 | } |
| 2472 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2473 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2474 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2475 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2476 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2479 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2480 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2481 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2482 | err = -ENOMEM; |
| 2483 | goto unlock; |
| 2484 | } |
| 2485 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2486 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2487 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2488 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2489 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2490 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2491 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2492 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2493 | continue; |
| 2494 | i++; |
| 2495 | } |
| 2496 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2497 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2498 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2499 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2500 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2501 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2502 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
| 2503 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2504 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2505 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2506 | |
| 2507 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2508 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2509 | return err; |
| 2510 | } |
| 2511 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2512 | static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2513 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2514 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2515 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2516 | int err; |
| 2517 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2518 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2519 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2520 | if (!cmd) |
| 2521 | return -ENOMEM; |
| 2522 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2523 | cmd->cmd_complete = addr_cmd_complete; |
| 2524 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2525 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2526 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2527 | if (err < 0) |
| 2528 | mgmt_pending_remove(cmd); |
| 2529 | |
| 2530 | return err; |
| 2531 | } |
| 2532 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2533 | static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2534 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2535 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2536 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2537 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2538 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2539 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2540 | int err; |
| 2541 | |
| 2542 | BT_DBG(""); |
| 2543 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2544 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2545 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2546 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2547 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2548 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2549 | goto failed; |
| 2550 | } |
| 2551 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2552 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2553 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2554 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2555 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2556 | goto failed; |
| 2557 | } |
| 2558 | |
| 2559 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2560 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2561 | |
| 2562 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2563 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2564 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2565 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2566 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2567 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2568 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2569 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2570 | |
| 2571 | goto failed; |
| 2572 | } |
| 2573 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2574 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2575 | if (!cmd) { |
| 2576 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2577 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2578 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2579 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2580 | cmd->cmd_complete = addr_cmd_complete; |
| 2581 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2582 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2583 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2584 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2585 | |
| 2586 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2587 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2588 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2589 | |
| 2590 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2591 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2592 | return err; |
| 2593 | } |
| 2594 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2595 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2596 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2597 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2598 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2599 | |
| 2600 | BT_DBG(""); |
| 2601 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2602 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2603 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2604 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2605 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2606 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2607 | |
| 2608 | hdev->io_capability = cp->io_capability; |
| 2609 | |
| 2610 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2611 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2612 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2613 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2614 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2615 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2616 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2617 | } |
| 2618 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2619 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2620 | { |
| 2621 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2622 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2623 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2624 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2625 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2626 | continue; |
| 2627 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2628 | if (cmd->user_data != conn) |
| 2629 | continue; |
| 2630 | |
| 2631 | return cmd; |
| 2632 | } |
| 2633 | |
| 2634 | return NULL; |
| 2635 | } |
| 2636 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2637 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2638 | { |
| 2639 | struct mgmt_rp_pair_device rp; |
| 2640 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2641 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2642 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2643 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2644 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2645 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2646 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2647 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2648 | |
| 2649 | /* So we don't get further callbacks for this connection */ |
| 2650 | conn->connect_cfm_cb = NULL; |
| 2651 | conn->security_cfm_cb = NULL; |
| 2652 | conn->disconn_cfm_cb = NULL; |
| 2653 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2654 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2655 | |
| 2656 | /* The device is paired so there is no need to remove |
| 2657 | * its connection parameters anymore. |
| 2658 | */ |
| 2659 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2660 | |
| 2661 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2662 | |
| 2663 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2664 | } |
| 2665 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2666 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2667 | { |
| 2668 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2669 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2670 | |
| 2671 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2672 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2673 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2674 | mgmt_pending_remove(cmd); |
| 2675 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2676 | } |
| 2677 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2678 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2679 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2680 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2681 | |
| 2682 | BT_DBG("status %u", status); |
| 2683 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2684 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2685 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2686 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2687 | return; |
| 2688 | } |
| 2689 | |
| 2690 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2691 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2692 | } |
| 2693 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2694 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2695 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2696 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2697 | |
| 2698 | BT_DBG("status %u", status); |
| 2699 | |
| 2700 | if (!status) |
| 2701 | return; |
| 2702 | |
| 2703 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2704 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2705 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2706 | return; |
| 2707 | } |
| 2708 | |
| 2709 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2710 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2711 | } |
| 2712 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2713 | static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2714 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2715 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2716 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2717 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2718 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2719 | u8 sec_level, auth_type; |
| 2720 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2721 | int err; |
| 2722 | |
| 2723 | BT_DBG(""); |
| 2724 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2725 | memset(&rp, 0, sizeof(rp)); |
| 2726 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2727 | rp.addr.type = cp->addr.type; |
| 2728 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2729 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2730 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2731 | MGMT_STATUS_INVALID_PARAMS, |
| 2732 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2733 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2734 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2735 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2736 | MGMT_STATUS_INVALID_PARAMS, |
| 2737 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2738 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2739 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2740 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2741 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2742 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2743 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2744 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2745 | goto unlock; |
| 2746 | } |
| 2747 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2748 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2749 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2750 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2751 | sizeof(rp)); |
| 2752 | goto unlock; |
| 2753 | } |
| 2754 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2755 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2756 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2757 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2758 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2759 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2760 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2761 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2762 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2763 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2764 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2765 | /* When pairing a new device, it is expected to remember |
| 2766 | * this device for future connections. Adding the connection |
| 2767 | * parameter information ahead of time allows tracking |
| 2768 | * of the slave preferred values and will speed up any |
| 2769 | * further connection establishment. |
| 2770 | * |
| 2771 | * If connection parameters already exist, then they |
| 2772 | * will be kept and this function does nothing. |
| 2773 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2774 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2775 | |
| 2776 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2777 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2778 | |
Jakub Pawlowski | fa14222 | 2015-08-07 20:22:56 +0200 | [diff] [blame] | 2779 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, |
| 2780 | addr_type, sec_level, |
Johan Hedberg | 0ad06aa | 2015-11-11 14:44:57 +0200 | [diff] [blame] | 2781 | HCI_LE_CONN_TIMEOUT); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2782 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2783 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2784 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2785 | int status; |
| 2786 | |
| 2787 | if (PTR_ERR(conn) == -EBUSY) |
| 2788 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2789 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2790 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2791 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2792 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2793 | else |
| 2794 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2795 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2796 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2797 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2798 | goto unlock; |
| 2799 | } |
| 2800 | |
| 2801 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2802 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2803 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2804 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2805 | goto unlock; |
| 2806 | } |
| 2807 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2808 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2809 | if (!cmd) { |
| 2810 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2811 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2812 | goto unlock; |
| 2813 | } |
| 2814 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2815 | cmd->cmd_complete = pairing_complete; |
| 2816 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2817 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2818 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2819 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2820 | conn->security_cfm_cb = pairing_complete_cb; |
| 2821 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2822 | } else { |
| 2823 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2824 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2825 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2826 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2827 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2828 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 2829 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2830 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 2831 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2832 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 2833 | cmd->cmd_complete(cmd, 0); |
| 2834 | mgmt_pending_remove(cmd); |
| 2835 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2836 | |
| 2837 | err = 0; |
| 2838 | |
| 2839 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2840 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2841 | return err; |
| 2842 | } |
| 2843 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2844 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2845 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2846 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2847 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2848 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2849 | struct hci_conn *conn; |
| 2850 | int err; |
| 2851 | |
| 2852 | BT_DBG(""); |
| 2853 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2854 | hci_dev_lock(hdev); |
| 2855 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2856 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2857 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2858 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2859 | goto unlock; |
| 2860 | } |
| 2861 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2862 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2863 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2864 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2865 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2866 | goto unlock; |
| 2867 | } |
| 2868 | |
| 2869 | conn = cmd->user_data; |
| 2870 | |
| 2871 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2872 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2873 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2874 | goto unlock; |
| 2875 | } |
| 2876 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2877 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 2878 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2879 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2880 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 2881 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2882 | unlock: |
| 2883 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2884 | return err; |
| 2885 | } |
| 2886 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2887 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2888 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2889 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2890 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2891 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2892 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2893 | int err; |
| 2894 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2895 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 2896 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2897 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2898 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 2899 | MGMT_STATUS_NOT_POWERED, addr, |
| 2900 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2901 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2902 | } |
| 2903 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2904 | if (addr->type == BDADDR_BREDR) |
| 2905 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2906 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2907 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 2908 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2909 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2910 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2911 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 2912 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 2913 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2914 | goto done; |
| 2915 | } |
| 2916 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2917 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2918 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2919 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2920 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 2921 | MGMT_STATUS_SUCCESS, addr, |
| 2922 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2923 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2924 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 2925 | MGMT_STATUS_FAILED, addr, |
| 2926 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2927 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2928 | goto done; |
| 2929 | } |
| 2930 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2931 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2932 | if (!cmd) { |
| 2933 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2934 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2935 | } |
| 2936 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2937 | cmd->cmd_complete = addr_cmd_complete; |
| 2938 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2939 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2940 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 2941 | struct hci_cp_user_passkey_reply cp; |
| 2942 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2943 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2944 | cp.passkey = passkey; |
| 2945 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 2946 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2947 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 2948 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2949 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2950 | if (err < 0) |
| 2951 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2952 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2953 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2954 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2955 | return err; |
| 2956 | } |
| 2957 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 2958 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 2959 | void *data, u16 len) |
| 2960 | { |
| 2961 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 2962 | |
| 2963 | BT_DBG(""); |
| 2964 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2965 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 2966 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 2967 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 2968 | } |
| 2969 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2970 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2971 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2972 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2973 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2974 | |
| 2975 | BT_DBG(""); |
| 2976 | |
| 2977 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2978 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 2979 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2980 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2981 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2982 | MGMT_OP_USER_CONFIRM_REPLY, |
| 2983 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2984 | } |
| 2985 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2986 | static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2987 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2988 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 2989 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2990 | |
| 2991 | BT_DBG(""); |
| 2992 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2993 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2994 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 2995 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2996 | } |
| 2997 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2998 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2999 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3000 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3001 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3002 | |
| 3003 | BT_DBG(""); |
| 3004 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3005 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3006 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3007 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3008 | } |
| 3009 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3010 | static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3011 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3012 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3013 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3014 | |
| 3015 | BT_DBG(""); |
| 3016 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3017 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3018 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3019 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3020 | } |
| 3021 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3022 | static void adv_expire(struct hci_dev *hdev, u32 flags) |
| 3023 | { |
| 3024 | struct adv_info *adv_instance; |
| 3025 | struct hci_request req; |
| 3026 | int err; |
| 3027 | |
| 3028 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3029 | if (!adv_instance) |
| 3030 | return; |
| 3031 | |
| 3032 | /* stop if current instance doesn't need to be changed */ |
| 3033 | if (!(adv_instance->flags & flags)) |
| 3034 | return; |
| 3035 | |
| 3036 | cancel_adv_timeout(hdev); |
| 3037 | |
| 3038 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3039 | if (!adv_instance) |
| 3040 | return; |
| 3041 | |
| 3042 | hci_req_init(&req, hdev); |
| 3043 | err = __hci_req_schedule_adv_instance(&req, adv_instance->instance, |
| 3044 | true); |
| 3045 | if (err) |
| 3046 | return; |
| 3047 | |
| 3048 | hci_req_run(&req, NULL); |
| 3049 | } |
| 3050 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3051 | static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3052 | { |
| 3053 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3054 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3055 | |
| 3056 | BT_DBG("status 0x%02x", status); |
| 3057 | |
| 3058 | hci_dev_lock(hdev); |
| 3059 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3060 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3061 | if (!cmd) |
| 3062 | goto unlock; |
| 3063 | |
| 3064 | cp = cmd->param; |
| 3065 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3066 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3067 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3068 | mgmt_status(status)); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3069 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3070 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3071 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3072 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3073 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3074 | adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
| 3075 | } |
| 3076 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3077 | mgmt_pending_remove(cmd); |
| 3078 | |
| 3079 | unlock: |
| 3080 | hci_dev_unlock(hdev); |
| 3081 | } |
| 3082 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3083 | static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3084 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3085 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3086 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3087 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3088 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3089 | int err; |
| 3090 | |
| 3091 | BT_DBG(""); |
| 3092 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3093 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3094 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3095 | /* If the old values are the same as the new ones just return a |
| 3096 | * direct command complete event. |
| 3097 | */ |
| 3098 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3099 | !memcmp(hdev->short_name, cp->short_name, |
| 3100 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3101 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3102 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3103 | goto failed; |
| 3104 | } |
| 3105 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3106 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3107 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3108 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3109 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3110 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3111 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3112 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3113 | if (err < 0) |
| 3114 | goto failed; |
| 3115 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3116 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3117 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3118 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3119 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3120 | goto failed; |
| 3121 | } |
| 3122 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3123 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3124 | if (!cmd) { |
| 3125 | err = -ENOMEM; |
| 3126 | goto failed; |
| 3127 | } |
| 3128 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3129 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3130 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3131 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3132 | |
| 3133 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 3134 | __hci_req_update_name(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3135 | __hci_req_update_eir(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3136 | } |
| 3137 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3138 | /* The name is stored in the scan response data and so |
| 3139 | * no need to udpate the advertising data here. |
| 3140 | */ |
MichaĆ Narajowski | 7dc6f16 | 2016-09-22 16:01:39 +0200 | [diff] [blame] | 3141 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3142 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3143 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3144 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3145 | if (err < 0) |
| 3146 | mgmt_pending_remove(cmd); |
| 3147 | |
| 3148 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3149 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3150 | return err; |
| 3151 | } |
| 3152 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3153 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3154 | u16 len) |
| 3155 | { |
| 3156 | struct mgmt_cp_set_appearance *cp = data; |
| 3157 | u16 apperance; |
| 3158 | int err; |
| 3159 | |
| 3160 | BT_DBG(""); |
| 3161 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3162 | if (!lmp_le_capable(hdev)) |
| 3163 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3164 | MGMT_STATUS_NOT_SUPPORTED); |
| 3165 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3166 | apperance = le16_to_cpu(cp->appearance); |
| 3167 | |
| 3168 | hci_dev_lock(hdev); |
| 3169 | |
| 3170 | if (hdev->appearance != apperance) { |
| 3171 | hdev->appearance = apperance; |
| 3172 | |
| 3173 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3174 | adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3175 | |
| 3176 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3180 | 0); |
| 3181 | |
| 3182 | hci_dev_unlock(hdev); |
| 3183 | |
| 3184 | return err; |
| 3185 | } |
| 3186 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3187 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 3188 | u16 opcode, struct sk_buff *skb) |
| 3189 | { |
| 3190 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 3191 | size_t rp_size = sizeof(mgmt_rp); |
| 3192 | struct mgmt_pending_cmd *cmd; |
| 3193 | |
| 3194 | BT_DBG("%s status %u", hdev->name, status); |
| 3195 | |
| 3196 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 3197 | if (!cmd) |
| 3198 | return; |
| 3199 | |
| 3200 | if (status || !skb) { |
| 3201 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3202 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 3203 | goto remove; |
| 3204 | } |
| 3205 | |
| 3206 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 3207 | |
| 3208 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 3209 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 3210 | |
| 3211 | if (skb->len < sizeof(*rp)) { |
| 3212 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3213 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3214 | MGMT_STATUS_FAILED); |
| 3215 | goto remove; |
| 3216 | } |
| 3217 | |
| 3218 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 3219 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 3220 | |
| 3221 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 3222 | } else { |
| 3223 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 3224 | |
| 3225 | if (skb->len < sizeof(*rp)) { |
| 3226 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3227 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3228 | MGMT_STATUS_FAILED); |
| 3229 | goto remove; |
| 3230 | } |
| 3231 | |
| 3232 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 3233 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 3234 | |
| 3235 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 3236 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 3237 | } |
| 3238 | |
| 3239 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3240 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 3241 | |
| 3242 | remove: |
| 3243 | mgmt_pending_remove(cmd); |
| 3244 | } |
| 3245 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3246 | static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3247 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3248 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3249 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3250 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3251 | int err; |
| 3252 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3253 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3254 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3255 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3256 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3257 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3258 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3259 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3260 | goto unlock; |
| 3261 | } |
| 3262 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3263 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3264 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3265 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3266 | goto unlock; |
| 3267 | } |
| 3268 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3269 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3270 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3271 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3272 | goto unlock; |
| 3273 | } |
| 3274 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3275 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3276 | if (!cmd) { |
| 3277 | err = -ENOMEM; |
| 3278 | goto unlock; |
| 3279 | } |
| 3280 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3281 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3282 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3283 | if (bredr_sc_enabled(hdev)) |
| 3284 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 3285 | else |
| 3286 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3287 | |
| 3288 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3289 | if (err < 0) |
| 3290 | mgmt_pending_remove(cmd); |
| 3291 | |
| 3292 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3293 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3294 | return err; |
| 3295 | } |
| 3296 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3297 | static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3298 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3299 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3300 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3301 | int err; |
| 3302 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3303 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3304 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3305 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3306 | return mgmt_cmd_complete(sk, hdev->id, |
| 3307 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3308 | MGMT_STATUS_INVALID_PARAMS, |
| 3309 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3310 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3311 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3312 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3313 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3314 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3315 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3316 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3317 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3318 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3319 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3320 | MGMT_STATUS_INVALID_PARAMS, |
| 3321 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3322 | goto unlock; |
| 3323 | } |
| 3324 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3325 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3326 | cp->addr.type, cp->hash, |
| 3327 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3328 | if (err < 0) |
| 3329 | status = MGMT_STATUS_FAILED; |
| 3330 | else |
| 3331 | status = MGMT_STATUS_SUCCESS; |
| 3332 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3333 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3334 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 3335 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3336 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3337 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3338 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3339 | u8 status; |
| 3340 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3341 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3342 | /* Enforce zero-valued 192-bit parameters as |
| 3343 | * long as legacy SMP OOB isn't implemented. |
| 3344 | */ |
| 3345 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3346 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3347 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3348 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3349 | MGMT_STATUS_INVALID_PARAMS, |
| 3350 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3351 | goto unlock; |
| 3352 | } |
| 3353 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3354 | rand192 = NULL; |
| 3355 | hash192 = NULL; |
| 3356 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3357 | /* In case one of the P-192 values is set to zero, |
| 3358 | * then just disable OOB data for P-192. |
| 3359 | */ |
| 3360 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3361 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 3362 | rand192 = NULL; |
| 3363 | hash192 = NULL; |
| 3364 | } else { |
| 3365 | rand192 = cp->rand192; |
| 3366 | hash192 = cp->hash192; |
| 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | /* In case one of the P-256 values is set to zero, then just |
| 3371 | * disable OOB data for P-256. |
| 3372 | */ |
| 3373 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 3374 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 3375 | rand256 = NULL; |
| 3376 | hash256 = NULL; |
| 3377 | } else { |
| 3378 | rand256 = cp->rand256; |
| 3379 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3380 | } |
| 3381 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 3382 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3383 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3384 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3385 | if (err < 0) |
| 3386 | status = MGMT_STATUS_FAILED; |
| 3387 | else |
| 3388 | status = MGMT_STATUS_SUCCESS; |
| 3389 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3390 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3391 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3392 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3393 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 3394 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 3395 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3396 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3397 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3398 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3399 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3400 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3401 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3402 | return err; |
| 3403 | } |
| 3404 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3405 | static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3406 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3407 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3408 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3409 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3410 | int err; |
| 3411 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3412 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3413 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3414 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3415 | return mgmt_cmd_complete(sk, hdev->id, |
| 3416 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3417 | MGMT_STATUS_INVALID_PARAMS, |
| 3418 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3419 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3420 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3421 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3422 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 3423 | hci_remote_oob_data_clear(hdev); |
| 3424 | status = MGMT_STATUS_SUCCESS; |
| 3425 | goto done; |
| 3426 | } |
| 3427 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3428 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3429 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3430 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3431 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3432 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3433 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3434 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3435 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3436 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3437 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3438 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3439 | return err; |
| 3440 | } |
| 3441 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3442 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3443 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3444 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3445 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3446 | BT_DBG("status %d", status); |
| 3447 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3448 | hci_dev_lock(hdev); |
| 3449 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3450 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3451 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3452 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3453 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3454 | if (!cmd) |
| 3455 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 3456 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3457 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3458 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3459 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3460 | } |
| 3461 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3462 | hci_dev_unlock(hdev); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3463 | } |
| 3464 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3465 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 3466 | uint8_t *mgmt_status) |
| 3467 | { |
| 3468 | switch (type) { |
| 3469 | case DISCOV_TYPE_LE: |
| 3470 | *mgmt_status = mgmt_le_support(hdev); |
| 3471 | if (*mgmt_status) |
| 3472 | return false; |
| 3473 | break; |
| 3474 | case DISCOV_TYPE_INTERLEAVED: |
| 3475 | *mgmt_status = mgmt_le_support(hdev); |
| 3476 | if (*mgmt_status) |
| 3477 | return false; |
| 3478 | /* Intentional fall-through */ |
| 3479 | case DISCOV_TYPE_BREDR: |
| 3480 | *mgmt_status = mgmt_bredr_support(hdev); |
| 3481 | if (*mgmt_status) |
| 3482 | return false; |
| 3483 | break; |
| 3484 | default: |
| 3485 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 3486 | return false; |
| 3487 | } |
| 3488 | |
| 3489 | return true; |
| 3490 | } |
| 3491 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3492 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 3493 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3494 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3495 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3496 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3497 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3498 | int err; |
| 3499 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3500 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3501 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3502 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3503 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3504 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3505 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3506 | MGMT_STATUS_NOT_POWERED, |
| 3507 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3508 | goto failed; |
| 3509 | } |
| 3510 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3511 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3512 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3513 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 3514 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3515 | goto failed; |
| 3516 | } |
| 3517 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3518 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3519 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 3520 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3521 | goto failed; |
| 3522 | } |
| 3523 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 3524 | /* Clear the discovery filter first to free any previously |
| 3525 | * allocated memory for the UUID list. |
| 3526 | */ |
| 3527 | hci_discovery_filter_clear(hdev); |
| 3528 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3529 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 3530 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3531 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 3532 | hdev->discovery.limited = true; |
| 3533 | else |
| 3534 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3535 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3536 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3537 | if (!cmd) { |
| 3538 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3539 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3540 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3541 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3542 | cmd->cmd_complete = generic_cmd_complete; |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3543 | |
| 3544 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3545 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 3546 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3547 | |
| 3548 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3549 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3550 | return err; |
| 3551 | } |
| 3552 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3553 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 3554 | void *data, u16 len) |
| 3555 | { |
| 3556 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 3557 | data, len); |
| 3558 | } |
| 3559 | |
| 3560 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 3561 | void *data, u16 len) |
| 3562 | { |
| 3563 | return start_discovery_internal(sk, hdev, |
| 3564 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 3565 | data, len); |
| 3566 | } |
| 3567 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3568 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 3569 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3570 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3571 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 3572 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3573 | } |
| 3574 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3575 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 3576 | void *data, u16 len) |
| 3577 | { |
| 3578 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3579 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3580 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 3581 | u16 uuid_count, expected_len; |
| 3582 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3583 | int err; |
| 3584 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3585 | BT_DBG("%s", hdev->name); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3586 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3587 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3588 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3589 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3590 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3591 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3592 | MGMT_STATUS_NOT_POWERED, |
| 3593 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3594 | goto failed; |
| 3595 | } |
| 3596 | |
| 3597 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3598 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3599 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3600 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3601 | MGMT_STATUS_BUSY, &cp->type, |
| 3602 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3603 | goto failed; |
| 3604 | } |
| 3605 | |
| 3606 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 3607 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 3608 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 3609 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3610 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3611 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3612 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 3613 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3614 | goto failed; |
| 3615 | } |
| 3616 | |
| 3617 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 3618 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 3619 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 3620 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3621 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3622 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3623 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 3624 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3625 | goto failed; |
| 3626 | } |
| 3627 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3628 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 3629 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3630 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3631 | status, &cp->type, sizeof(cp->type)); |
| 3632 | goto failed; |
| 3633 | } |
| 3634 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3635 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3636 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3637 | if (!cmd) { |
| 3638 | err = -ENOMEM; |
| 3639 | goto failed; |
| 3640 | } |
| 3641 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3642 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 3643 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 3644 | /* Clear the discovery filter first to free any previously |
| 3645 | * allocated memory for the UUID list. |
| 3646 | */ |
| 3647 | hci_discovery_filter_clear(hdev); |
| 3648 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 3649 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3650 | hdev->discovery.type = cp->type; |
| 3651 | hdev->discovery.rssi = cp->rssi; |
| 3652 | hdev->discovery.uuid_count = uuid_count; |
| 3653 | |
| 3654 | if (uuid_count > 0) { |
| 3655 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 3656 | GFP_KERNEL); |
| 3657 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3658 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3659 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3660 | MGMT_STATUS_FAILED, |
| 3661 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3662 | mgmt_pending_remove(cmd); |
| 3663 | goto failed; |
| 3664 | } |
| 3665 | } |
| 3666 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3667 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3668 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 3669 | err = 0; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3670 | |
| 3671 | failed: |
| 3672 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3673 | return err; |
| 3674 | } |
| 3675 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 3676 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3677 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3678 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3679 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3680 | BT_DBG("status %d", status); |
| 3681 | |
| 3682 | hci_dev_lock(hdev); |
| 3683 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3684 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3685 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3686 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3687 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3688 | } |
| 3689 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3690 | hci_dev_unlock(hdev); |
| 3691 | } |
| 3692 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3693 | static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3694 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3695 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3696 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3697 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3698 | int err; |
| 3699 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3700 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3701 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3702 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3703 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3704 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3705 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 3706 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3707 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3708 | goto unlock; |
| 3709 | } |
| 3710 | |
| 3711 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3712 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 3713 | MGMT_STATUS_INVALID_PARAMS, |
| 3714 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3715 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3716 | } |
| 3717 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3718 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3719 | if (!cmd) { |
| 3720 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3721 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3722 | } |
| 3723 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3724 | cmd->cmd_complete = generic_cmd_complete; |
| 3725 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 3726 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 3727 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 3728 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3729 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3730 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3731 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3732 | return err; |
| 3733 | } |
| 3734 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3735 | static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3736 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3737 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3738 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3739 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3740 | int err; |
| 3741 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3742 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3743 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3744 | hci_dev_lock(hdev); |
| 3745 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3746 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3747 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3748 | MGMT_STATUS_FAILED, &cp->addr, |
| 3749 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3750 | goto failed; |
| 3751 | } |
| 3752 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3753 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3754 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3755 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3756 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3757 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3758 | goto failed; |
| 3759 | } |
| 3760 | |
| 3761 | if (cp->name_known) { |
| 3762 | e->name_state = NAME_KNOWN; |
| 3763 | list_del(&e->list); |
| 3764 | } else { |
| 3765 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3766 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3767 | } |
| 3768 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3769 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 3770 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3771 | |
| 3772 | failed: |
| 3773 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3774 | return err; |
| 3775 | } |
| 3776 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3777 | static int block_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3778 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3779 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3780 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3781 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3782 | int err; |
| 3783 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3784 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3785 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3786 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3787 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3788 | MGMT_STATUS_INVALID_PARAMS, |
| 3789 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3790 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3791 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3792 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 3793 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 3794 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3795 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3796 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3797 | goto done; |
| 3798 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3799 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3800 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3801 | sk); |
| 3802 | status = MGMT_STATUS_SUCCESS; |
| 3803 | |
| 3804 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3805 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 3806 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3807 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3808 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3809 | |
| 3810 | return err; |
| 3811 | } |
| 3812 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3813 | static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3814 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3815 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3816 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3817 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3818 | int err; |
| 3819 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3820 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3821 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3822 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3823 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3824 | MGMT_STATUS_INVALID_PARAMS, |
| 3825 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3826 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3827 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3828 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 3829 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 3830 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3831 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3832 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3833 | goto done; |
| 3834 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3835 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3836 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3837 | sk); |
| 3838 | status = MGMT_STATUS_SUCCESS; |
| 3839 | |
| 3840 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3841 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 3842 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3843 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3844 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3845 | |
| 3846 | return err; |
| 3847 | } |
| 3848 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3849 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3850 | u16 len) |
| 3851 | { |
| 3852 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3853 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3854 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3855 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3856 | |
| 3857 | BT_DBG("%s", hdev->name); |
| 3858 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3859 | source = __le16_to_cpu(cp->source); |
| 3860 | |
| 3861 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3862 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3863 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3864 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3865 | hci_dev_lock(hdev); |
| 3866 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3867 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3868 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3869 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3870 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3871 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3872 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 3873 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3874 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3875 | hci_req_init(&req, hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3876 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3877 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3878 | |
| 3879 | hci_dev_unlock(hdev); |
| 3880 | |
| 3881 | return err; |
| 3882 | } |
| 3883 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3884 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 3885 | u16 opcode) |
| 3886 | { |
| 3887 | BT_DBG("status %d", status); |
| 3888 | } |
| 3889 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3890 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 3891 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3892 | { |
| 3893 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3894 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3895 | u8 instance; |
| 3896 | struct adv_info *adv_instance; |
| 3897 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3898 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 3899 | hci_dev_lock(hdev); |
| 3900 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3901 | if (status) { |
| 3902 | u8 mgmt_err = mgmt_status(status); |
| 3903 | |
| 3904 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3905 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 3906 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3907 | } |
| 3908 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3909 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 3910 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 3911 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 3912 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 3913 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3914 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3915 | &match); |
| 3916 | |
| 3917 | new_settings(hdev, match.sk); |
| 3918 | |
| 3919 | if (match.sk) |
| 3920 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 3921 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3922 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3923 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3924 | */ |
| 3925 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3926 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3927 | goto unlock; |
| 3928 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3929 | instance = hdev->cur_adv_instance; |
| 3930 | if (!instance) { |
| 3931 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 3932 | struct adv_info, list); |
| 3933 | if (!adv_instance) |
| 3934 | goto unlock; |
| 3935 | |
| 3936 | instance = adv_instance->instance; |
| 3937 | } |
| 3938 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3939 | hci_req_init(&req, hdev); |
| 3940 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 3941 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3942 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 3943 | if (!err) |
| 3944 | err = hci_req_run(&req, enable_advertising_instance); |
| 3945 | |
| 3946 | if (err) |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 3947 | bt_dev_err(hdev, "failed to re-configure advertising"); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 3948 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 3949 | unlock: |
| 3950 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3951 | } |
| 3952 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3953 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3954 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3955 | { |
| 3956 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3957 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3958 | struct hci_request req; |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 3959 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3960 | int err; |
| 3961 | |
| 3962 | BT_DBG("request for %s", hdev->name); |
| 3963 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3964 | status = mgmt_le_support(hdev); |
| 3965 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3966 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3967 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3968 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 3969 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3970 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3971 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3972 | |
| 3973 | hci_dev_lock(hdev); |
| 3974 | |
| 3975 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3976 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3977 | /* The following conditions are ones which mean that we should |
| 3978 | * not do any HCI communication but directly send a mgmt |
| 3979 | * response to user space (after toggling the flag if |
| 3980 | * necessary). |
| 3981 | */ |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 3982 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3983 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 3984 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 3985 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3986 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 3987 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 3988 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3989 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 3990 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3991 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 3992 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 3993 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 3994 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 3995 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 3996 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 3997 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 3998 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 3999 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4003 | if (err < 0) |
| 4004 | goto unlock; |
| 4005 | |
| 4006 | if (changed) |
| 4007 | err = new_settings(hdev, sk); |
| 4008 | |
| 4009 | goto unlock; |
| 4010 | } |
| 4011 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4012 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4013 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4014 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4015 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4016 | goto unlock; |
| 4017 | } |
| 4018 | |
| 4019 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4020 | if (!cmd) { |
| 4021 | err = -ENOMEM; |
| 4022 | goto unlock; |
| 4023 | } |
| 4024 | |
| 4025 | hci_req_init(&req, hdev); |
| 4026 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4027 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4028 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4029 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4030 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4031 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4032 | cancel_adv_timeout(hdev); |
| 4033 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4034 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4035 | /* Switch to instance "0" for the Set Advertising setting. |
| 4036 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 4037 | * HCI_ADVERTISING flag is not yet set. |
| 4038 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 4039 | hdev->cur_adv_instance = 0x00; |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4040 | __hci_req_update_adv_data(&req, 0x00); |
| 4041 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 4042 | __hci_req_enable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4043 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4044 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4045 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4046 | |
| 4047 | err = hci_req_run(&req, set_advertising_complete); |
| 4048 | if (err < 0) |
| 4049 | mgmt_pending_remove(cmd); |
| 4050 | |
| 4051 | unlock: |
| 4052 | hci_dev_unlock(hdev); |
| 4053 | return err; |
| 4054 | } |
| 4055 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4056 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4057 | void *data, u16 len) |
| 4058 | { |
| 4059 | struct mgmt_cp_set_static_address *cp = data; |
| 4060 | int err; |
| 4061 | |
| 4062 | BT_DBG("%s", hdev->name); |
| 4063 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4064 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4065 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4066 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4067 | |
| 4068 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4069 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4070 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4071 | |
| 4072 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4073 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4074 | return mgmt_cmd_status(sk, hdev->id, |
| 4075 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4076 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4077 | |
| 4078 | /* Two most significant bits shall be set */ |
| 4079 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4080 | return mgmt_cmd_status(sk, hdev->id, |
| 4081 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4082 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | hci_dev_lock(hdev); |
| 4086 | |
| 4087 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4088 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4089 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 4090 | if (err < 0) |
| 4091 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4092 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4093 | err = new_settings(hdev, sk); |
| 4094 | |
| 4095 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4096 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4097 | return err; |
| 4098 | } |
| 4099 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4100 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4101 | void *data, u16 len) |
| 4102 | { |
| 4103 | struct mgmt_cp_set_scan_params *cp = data; |
| 4104 | __u16 interval, window; |
| 4105 | int err; |
| 4106 | |
| 4107 | BT_DBG("%s", hdev->name); |
| 4108 | |
| 4109 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4110 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4111 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4112 | |
| 4113 | interval = __le16_to_cpu(cp->interval); |
| 4114 | |
| 4115 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4116 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4117 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4118 | |
| 4119 | window = __le16_to_cpu(cp->window); |
| 4120 | |
| 4121 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4122 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4123 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4124 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4125 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4126 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4127 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4128 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4129 | hci_dev_lock(hdev); |
| 4130 | |
| 4131 | hdev->le_scan_interval = interval; |
| 4132 | hdev->le_scan_window = window; |
| 4133 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4134 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 4135 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4136 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4137 | /* If background scan is running, restart it so new parameters are |
| 4138 | * loaded. |
| 4139 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4140 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4141 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4142 | struct hci_request req; |
| 4143 | |
| 4144 | hci_req_init(&req, hdev); |
| 4145 | |
| 4146 | hci_req_add_le_scan_disable(&req); |
| 4147 | hci_req_add_le_passive_scan(&req); |
| 4148 | |
| 4149 | hci_req_run(&req, NULL); |
| 4150 | } |
| 4151 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4152 | hci_dev_unlock(hdev); |
| 4153 | |
| 4154 | return err; |
| 4155 | } |
| 4156 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4157 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 4158 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4159 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4160 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4161 | |
| 4162 | BT_DBG("status 0x%02x", status); |
| 4163 | |
| 4164 | hci_dev_lock(hdev); |
| 4165 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4166 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4167 | if (!cmd) |
| 4168 | goto unlock; |
| 4169 | |
| 4170 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4171 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4172 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4173 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4174 | struct mgmt_mode *cp = cmd->param; |
| 4175 | |
| 4176 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4177 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4178 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4179 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4180 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4181 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4182 | new_settings(hdev, cmd->sk); |
| 4183 | } |
| 4184 | |
| 4185 | mgmt_pending_remove(cmd); |
| 4186 | |
| 4187 | unlock: |
| 4188 | hci_dev_unlock(hdev); |
| 4189 | } |
| 4190 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4191 | static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4192 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4193 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4194 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4195 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4196 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4197 | int err; |
| 4198 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4199 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4200 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4201 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4202 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4203 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4204 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4205 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4206 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4207 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4208 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4209 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4210 | hci_dev_lock(hdev); |
| 4211 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4212 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4213 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4214 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4215 | goto unlock; |
| 4216 | } |
| 4217 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4218 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4219 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4220 | hdev); |
| 4221 | goto unlock; |
| 4222 | } |
| 4223 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4224 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4225 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4226 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4227 | hdev); |
| 4228 | new_settings(hdev, sk); |
| 4229 | goto unlock; |
| 4230 | } |
| 4231 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4232 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4233 | data, len); |
| 4234 | if (!cmd) { |
| 4235 | err = -ENOMEM; |
| 4236 | goto unlock; |
| 4237 | } |
| 4238 | |
| 4239 | hci_req_init(&req, hdev); |
| 4240 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 4241 | __hci_req_write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4242 | |
| 4243 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4244 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4245 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4246 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4247 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4248 | } |
| 4249 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4250 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4251 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4252 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4253 | return err; |
| 4254 | } |
| 4255 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4256 | static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4257 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4258 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4259 | |
| 4260 | BT_DBG("status 0x%02x", status); |
| 4261 | |
| 4262 | hci_dev_lock(hdev); |
| 4263 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4264 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4265 | if (!cmd) |
| 4266 | goto unlock; |
| 4267 | |
| 4268 | if (status) { |
| 4269 | u8 mgmt_err = mgmt_status(status); |
| 4270 | |
| 4271 | /* We need to restore the flag if related HCI commands |
| 4272 | * failed. |
| 4273 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4274 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4275 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4276 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4277 | } else { |
| 4278 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4279 | new_settings(hdev, cmd->sk); |
| 4280 | } |
| 4281 | |
| 4282 | mgmt_pending_remove(cmd); |
| 4283 | |
| 4284 | unlock: |
| 4285 | hci_dev_unlock(hdev); |
| 4286 | } |
| 4287 | |
| 4288 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4289 | { |
| 4290 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4291 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4292 | struct hci_request req; |
| 4293 | int err; |
| 4294 | |
| 4295 | BT_DBG("request for %s", hdev->name); |
| 4296 | |
| 4297 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4298 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4299 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4300 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4301 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4302 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4303 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4304 | |
| 4305 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4306 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4307 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4308 | |
| 4309 | hci_dev_lock(hdev); |
| 4310 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4311 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4312 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4313 | goto unlock; |
| 4314 | } |
| 4315 | |
| 4316 | if (!hdev_is_powered(hdev)) { |
| 4317 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4318 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 4319 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 4320 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 4321 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 4322 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4323 | } |
| 4324 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4325 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4326 | |
| 4327 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4328 | if (err < 0) |
| 4329 | goto unlock; |
| 4330 | |
| 4331 | err = new_settings(hdev, sk); |
| 4332 | goto unlock; |
| 4333 | } |
| 4334 | |
| 4335 | /* Reject disabling when powered on */ |
| 4336 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4337 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4338 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4339 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4340 | } else { |
| 4341 | /* When configuring a dual-mode controller to operate |
| 4342 | * with LE only and using a static address, then switching |
| 4343 | * BR/EDR back on is not allowed. |
| 4344 | * |
| 4345 | * Dual-mode controllers shall operate with the public |
| 4346 | * address as its identity address for BR/EDR and LE. So |
| 4347 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4348 | * |
| 4349 | * The same restrictions applies when secure connections |
| 4350 | * has been enabled. For BR/EDR this is a controller feature |
| 4351 | * while for LE it is a host stack feature. This means that |
| 4352 | * switching BR/EDR back on when secure connections has been |
| 4353 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4354 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4355 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4356 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4357 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4358 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4359 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4360 | goto unlock; |
| 4361 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4362 | } |
| 4363 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4364 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4365 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4366 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4367 | goto unlock; |
| 4368 | } |
| 4369 | |
| 4370 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4371 | if (!cmd) { |
| 4372 | err = -ENOMEM; |
| 4373 | goto unlock; |
| 4374 | } |
| 4375 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4376 | /* We need to flip the bit already here so that |
| 4377 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4378 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4379 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4380 | |
| 4381 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4382 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 4383 | __hci_req_write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 4384 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4385 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4386 | /* Since only the advertising data flags will change, there |
| 4387 | * is no need to update the scan response data. |
| 4388 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 4389 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4390 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4391 | err = hci_req_run(&req, set_bredr_complete); |
| 4392 | if (err < 0) |
| 4393 | mgmt_pending_remove(cmd); |
| 4394 | |
| 4395 | unlock: |
| 4396 | hci_dev_unlock(hdev); |
| 4397 | return err; |
| 4398 | } |
| 4399 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4400 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 4401 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4402 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4403 | struct mgmt_mode *cp; |
| 4404 | |
| 4405 | BT_DBG("%s status %u", hdev->name, status); |
| 4406 | |
| 4407 | hci_dev_lock(hdev); |
| 4408 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4409 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4410 | if (!cmd) |
| 4411 | goto unlock; |
| 4412 | |
| 4413 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4414 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 4415 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4416 | goto remove; |
| 4417 | } |
| 4418 | |
| 4419 | cp = cmd->param; |
| 4420 | |
| 4421 | switch (cp->val) { |
| 4422 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4423 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 4424 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4425 | break; |
| 4426 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4427 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4428 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4429 | break; |
| 4430 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4431 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 4432 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4433 | break; |
| 4434 | } |
| 4435 | |
| 4436 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4437 | new_settings(hdev, cmd->sk); |
| 4438 | |
| 4439 | remove: |
| 4440 | mgmt_pending_remove(cmd); |
| 4441 | unlock: |
| 4442 | hci_dev_unlock(hdev); |
| 4443 | } |
| 4444 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4445 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4446 | void *data, u16 len) |
| 4447 | { |
| 4448 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4449 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4450 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4451 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4452 | int err; |
| 4453 | |
| 4454 | BT_DBG("request for %s", hdev->name); |
| 4455 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4456 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4457 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4458 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4459 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4460 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4461 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 4462 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4463 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4464 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4465 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 4466 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4467 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4468 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4469 | MGMT_STATUS_INVALID_PARAMS); |
| 4470 | |
| 4471 | hci_dev_lock(hdev); |
| 4472 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4473 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4474 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4475 | bool changed; |
| 4476 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4477 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4478 | changed = !hci_dev_test_and_set_flag(hdev, |
| 4479 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4480 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4481 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4482 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4483 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4484 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4485 | changed = hci_dev_test_and_clear_flag(hdev, |
| 4486 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4487 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4488 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4489 | |
| 4490 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4491 | if (err < 0) |
| 4492 | goto failed; |
| 4493 | |
| 4494 | if (changed) |
| 4495 | err = new_settings(hdev, sk); |
| 4496 | |
| 4497 | goto failed; |
| 4498 | } |
| 4499 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4500 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4501 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4502 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4503 | goto failed; |
| 4504 | } |
| 4505 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4506 | val = !!cp->val; |
| 4507 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4508 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 4509 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4510 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4511 | goto failed; |
| 4512 | } |
| 4513 | |
| 4514 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4515 | if (!cmd) { |
| 4516 | err = -ENOMEM; |
| 4517 | goto failed; |
| 4518 | } |
| 4519 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4520 | hci_req_init(&req, hdev); |
| 4521 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 4522 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4523 | if (err < 0) { |
| 4524 | mgmt_pending_remove(cmd); |
| 4525 | goto failed; |
| 4526 | } |
| 4527 | |
| 4528 | failed: |
| 4529 | hci_dev_unlock(hdev); |
| 4530 | return err; |
| 4531 | } |
| 4532 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4533 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4534 | void *data, u16 len) |
| 4535 | { |
| 4536 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4537 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4538 | int err; |
| 4539 | |
| 4540 | BT_DBG("request for %s", hdev->name); |
| 4541 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4542 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4543 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4544 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4545 | |
| 4546 | hci_dev_lock(hdev); |
| 4547 | |
| 4548 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4549 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4550 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4551 | changed = hci_dev_test_and_clear_flag(hdev, |
| 4552 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4553 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4554 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4555 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 4556 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4557 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4558 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 4559 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4560 | |
| 4561 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4562 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4563 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4564 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4565 | sizeof(mode), &mode); |
| 4566 | } |
| 4567 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4568 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4569 | if (err < 0) |
| 4570 | goto unlock; |
| 4571 | |
| 4572 | if (changed) |
| 4573 | err = new_settings(hdev, sk); |
| 4574 | |
| 4575 | unlock: |
| 4576 | hci_dev_unlock(hdev); |
| 4577 | return err; |
| 4578 | } |
| 4579 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4580 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4581 | u16 len) |
| 4582 | { |
| 4583 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4584 | bool changed; |
| 4585 | int err; |
| 4586 | |
| 4587 | BT_DBG("request for %s", hdev->name); |
| 4588 | |
| 4589 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4590 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4591 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4592 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 4593 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4594 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4595 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4596 | |
| 4597 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4598 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4599 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4600 | |
| 4601 | hci_dev_lock(hdev); |
| 4602 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4603 | /* If user space supports this command it is also expected to |
| 4604 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4605 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4606 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4607 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4608 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4609 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4610 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4611 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 4612 | if (cp->privacy == 0x02) |
| 4613 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 4614 | else |
| 4615 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4616 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4617 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4618 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4619 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 4620 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4624 | if (err < 0) |
| 4625 | goto unlock; |
| 4626 | |
| 4627 | if (changed) |
| 4628 | err = new_settings(hdev, sk); |
| 4629 | |
| 4630 | unlock: |
| 4631 | hci_dev_unlock(hdev); |
| 4632 | return err; |
| 4633 | } |
| 4634 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4635 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4636 | { |
| 4637 | switch (irk->addr.type) { |
| 4638 | case BDADDR_LE_PUBLIC: |
| 4639 | return true; |
| 4640 | |
| 4641 | case BDADDR_LE_RANDOM: |
| 4642 | /* Two most significant bits shall be set */ |
| 4643 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4644 | return false; |
| 4645 | return true; |
| 4646 | } |
| 4647 | |
| 4648 | return false; |
| 4649 | } |
| 4650 | |
| 4651 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4652 | u16 len) |
| 4653 | { |
| 4654 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4655 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4656 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4657 | u16 irk_count, expected_len; |
| 4658 | int i, err; |
| 4659 | |
| 4660 | BT_DBG("request for %s", hdev->name); |
| 4661 | |
| 4662 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4663 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4664 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4665 | |
| 4666 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4667 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4668 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 4669 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4670 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4671 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4672 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4673 | |
| 4674 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4675 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4676 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 4677 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4678 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4679 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4683 | |
| 4684 | for (i = 0; i < irk_count; i++) { |
| 4685 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4686 | |
| 4687 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4688 | return mgmt_cmd_status(sk, hdev->id, |
| 4689 | MGMT_OP_LOAD_IRKS, |
| 4690 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | hci_dev_lock(hdev); |
| 4694 | |
| 4695 | hci_smp_irks_clear(hdev); |
| 4696 | |
| 4697 | for (i = 0; i < irk_count; i++) { |
| 4698 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4699 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 4700 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 4701 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4702 | BDADDR_ANY); |
| 4703 | } |
| 4704 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4705 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4706 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4707 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4708 | |
| 4709 | hci_dev_unlock(hdev); |
| 4710 | |
| 4711 | return err; |
| 4712 | } |
| 4713 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4714 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4715 | { |
| 4716 | if (key->master != 0x00 && key->master != 0x01) |
| 4717 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4718 | |
| 4719 | switch (key->addr.type) { |
| 4720 | case BDADDR_LE_PUBLIC: |
| 4721 | return true; |
| 4722 | |
| 4723 | case BDADDR_LE_RANDOM: |
| 4724 | /* Two most significant bits shall be set */ |
| 4725 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4726 | return false; |
| 4727 | return true; |
| 4728 | } |
| 4729 | |
| 4730 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4731 | } |
| 4732 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4733 | static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4734 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4735 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4736 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4737 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4738 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4739 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4740 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4741 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4742 | BT_DBG("request for %s", hdev->name); |
| 4743 | |
| 4744 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4745 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4746 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4747 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4748 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4749 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4750 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 4751 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4752 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4753 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4754 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4755 | |
| 4756 | expected_len = sizeof(*cp) + key_count * |
| 4757 | sizeof(struct mgmt_ltk_info); |
| 4758 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4759 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 4760 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4761 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4762 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4763 | } |
| 4764 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4765 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4766 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4767 | for (i = 0; i < key_count; i++) { |
| 4768 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4769 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4770 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4771 | return mgmt_cmd_status(sk, hdev->id, |
| 4772 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4773 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4774 | } |
| 4775 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4776 | hci_dev_lock(hdev); |
| 4777 | |
| 4778 | hci_smp_ltks_clear(hdev); |
| 4779 | |
| 4780 | for (i = 0; i < key_count; i++) { |
| 4781 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 4782 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4783 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4784 | switch (key->type) { |
| 4785 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4786 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 4787 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4788 | break; |
| 4789 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4790 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 4791 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4792 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 4793 | case MGMT_LTK_P256_UNAUTH: |
| 4794 | authenticated = 0x00; |
| 4795 | type = SMP_LTK_P256; |
| 4796 | break; |
| 4797 | case MGMT_LTK_P256_AUTH: |
| 4798 | authenticated = 0x01; |
| 4799 | type = SMP_LTK_P256; |
| 4800 | break; |
| 4801 | case MGMT_LTK_P256_DEBUG: |
| 4802 | authenticated = 0x00; |
| 4803 | type = SMP_LTK_P256_DEBUG; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4804 | default: |
| 4805 | continue; |
| 4806 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4807 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 4808 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 4809 | le_addr_type(key->addr.type), type, authenticated, |
| 4810 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4811 | } |
| 4812 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4813 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4814 | NULL, 0); |
| 4815 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4816 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4817 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4818 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4819 | } |
| 4820 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4821 | static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4822 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4823 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4824 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 4825 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4826 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4827 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4828 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4829 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4830 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4831 | rp.tx_power = conn->tx_power; |
| 4832 | rp.max_tx_power = conn->max_tx_power; |
| 4833 | } else { |
| 4834 | rp.rssi = HCI_RSSI_INVALID; |
| 4835 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 4836 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4837 | } |
| 4838 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4839 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4840 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4841 | |
| 4842 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 4843 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 4844 | |
| 4845 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4846 | } |
| 4847 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4848 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 4849 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4850 | { |
| 4851 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4852 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4853 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4854 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4855 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4856 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4857 | BT_DBG("status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4858 | |
| 4859 | hci_dev_lock(hdev); |
| 4860 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4861 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4862 | * Level so we check which one was last sent to retrieve connection |
| 4863 | * handle. Both commands have handle as first parameter so it's safe to |
| 4864 | * cast data on the same command struct. |
| 4865 | * |
| 4866 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4867 | * In other case we simply override error to indicate success as we |
| 4868 | * already remembered if TX power value is actually valid. |
| 4869 | */ |
| 4870 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4871 | if (!cp) { |
| 4872 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4873 | status = MGMT_STATUS_SUCCESS; |
| 4874 | } else { |
| 4875 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | if (!cp) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4879 | bt_dev_err(hdev, "invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4880 | goto unlock; |
| 4881 | } |
| 4882 | |
| 4883 | handle = __le16_to_cpu(cp->handle); |
| 4884 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4885 | if (!conn) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4886 | bt_dev_err(hdev, "unknown handle (%d) in conn_info response", |
| 4887 | handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4888 | goto unlock; |
| 4889 | } |
| 4890 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4891 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4892 | if (!cmd) |
| 4893 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4894 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4895 | cmd->cmd_complete(cmd, status); |
| 4896 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4897 | |
| 4898 | unlock: |
| 4899 | hci_dev_unlock(hdev); |
| 4900 | } |
| 4901 | |
| 4902 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4903 | u16 len) |
| 4904 | { |
| 4905 | struct mgmt_cp_get_conn_info *cp = data; |
| 4906 | struct mgmt_rp_get_conn_info rp; |
| 4907 | struct hci_conn *conn; |
| 4908 | unsigned long conn_info_age; |
| 4909 | int err = 0; |
| 4910 | |
| 4911 | BT_DBG("%s", hdev->name); |
| 4912 | |
| 4913 | memset(&rp, 0, sizeof(rp)); |
| 4914 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4915 | rp.addr.type = cp->addr.type; |
| 4916 | |
| 4917 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4918 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4919 | MGMT_STATUS_INVALID_PARAMS, |
| 4920 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4921 | |
| 4922 | hci_dev_lock(hdev); |
| 4923 | |
| 4924 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4925 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4926 | MGMT_STATUS_NOT_POWERED, &rp, |
| 4927 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4928 | goto unlock; |
| 4929 | } |
| 4930 | |
| 4931 | if (cp->addr.type == BDADDR_BREDR) |
| 4932 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4933 | &cp->addr.bdaddr); |
| 4934 | else |
| 4935 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4936 | |
| 4937 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4938 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4939 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 4940 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4941 | goto unlock; |
| 4942 | } |
| 4943 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4944 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4945 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4946 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 4947 | goto unlock; |
| 4948 | } |
| 4949 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4950 | /* To avoid client trying to guess when to poll again for information we |
| 4951 | * calculate conn info age as random value between min/max set in hdev. |
| 4952 | */ |
| 4953 | conn_info_age = hdev->conn_info_min_age + |
| 4954 | prandom_u32_max(hdev->conn_info_max_age - |
| 4955 | hdev->conn_info_min_age); |
| 4956 | |
| 4957 | /* Query controller to refresh cached values if they are too old or were |
| 4958 | * never read. |
| 4959 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4960 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4961 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4962 | !conn->conn_info_timestamp) { |
| 4963 | struct hci_request req; |
| 4964 | struct hci_cp_read_tx_power req_txp_cp; |
| 4965 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4966 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4967 | |
| 4968 | hci_req_init(&req, hdev); |
| 4969 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4970 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4971 | &req_rssi_cp); |
| 4972 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4973 | /* For LE links TX power does not change thus we don't need to |
| 4974 | * query for it once value is known. |
| 4975 | */ |
| 4976 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4977 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4978 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4979 | req_txp_cp.type = 0x00; |
| 4980 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4981 | sizeof(req_txp_cp), &req_txp_cp); |
| 4982 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4983 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4984 | /* Max TX power needs to be read only once per connection */ |
| 4985 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4986 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4987 | req_txp_cp.type = 0x01; |
| 4988 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4989 | sizeof(req_txp_cp), &req_txp_cp); |
| 4990 | } |
| 4991 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4992 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4993 | if (err < 0) |
| 4994 | goto unlock; |
| 4995 | |
| 4996 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4997 | data, len); |
| 4998 | if (!cmd) { |
| 4999 | err = -ENOMEM; |
| 5000 | goto unlock; |
| 5001 | } |
| 5002 | |
| 5003 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5004 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5005 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5006 | |
| 5007 | conn->conn_info_timestamp = jiffies; |
| 5008 | } else { |
| 5009 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5010 | rp.rssi = conn->rssi; |
| 5011 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5012 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5013 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5014 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5015 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5016 | } |
| 5017 | |
| 5018 | unlock: |
| 5019 | hci_dev_unlock(hdev); |
| 5020 | return err; |
| 5021 | } |
| 5022 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5023 | static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5024 | { |
| 5025 | struct hci_conn *conn = cmd->user_data; |
| 5026 | struct mgmt_rp_get_clock_info rp; |
| 5027 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5028 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5029 | |
| 5030 | memset(&rp, 0, sizeof(rp)); |
Marcel Holtmann | 56f787c | 2016-08-29 06:19:47 +0200 | [diff] [blame] | 5031 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5032 | |
| 5033 | if (status) |
| 5034 | goto complete; |
| 5035 | |
| 5036 | hdev = hci_dev_get(cmd->index); |
| 5037 | if (hdev) { |
| 5038 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5039 | hci_dev_put(hdev); |
| 5040 | } |
| 5041 | |
| 5042 | if (conn) { |
| 5043 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5044 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5045 | } |
| 5046 | |
| 5047 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5048 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 5049 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5050 | |
| 5051 | if (conn) { |
| 5052 | hci_conn_drop(conn); |
| 5053 | hci_conn_put(conn); |
| 5054 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5055 | |
| 5056 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5057 | } |
| 5058 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5059 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5060 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5061 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5062 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5063 | struct hci_conn *conn; |
| 5064 | |
| 5065 | BT_DBG("%s status %u", hdev->name, status); |
| 5066 | |
| 5067 | hci_dev_lock(hdev); |
| 5068 | |
| 5069 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5070 | if (!hci_cp) |
| 5071 | goto unlock; |
| 5072 | |
| 5073 | if (hci_cp->which) { |
| 5074 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5075 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5076 | } else { |
| 5077 | conn = NULL; |
| 5078 | } |
| 5079 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5080 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5081 | if (!cmd) |
| 5082 | goto unlock; |
| 5083 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5084 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5085 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5086 | |
| 5087 | unlock: |
| 5088 | hci_dev_unlock(hdev); |
| 5089 | } |
| 5090 | |
| 5091 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5092 | u16 len) |
| 5093 | { |
| 5094 | struct mgmt_cp_get_clock_info *cp = data; |
| 5095 | struct mgmt_rp_get_clock_info rp; |
| 5096 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5097 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5098 | struct hci_request req; |
| 5099 | struct hci_conn *conn; |
| 5100 | int err; |
| 5101 | |
| 5102 | BT_DBG("%s", hdev->name); |
| 5103 | |
| 5104 | memset(&rp, 0, sizeof(rp)); |
| 5105 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5106 | rp.addr.type = cp->addr.type; |
| 5107 | |
| 5108 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5109 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5110 | MGMT_STATUS_INVALID_PARAMS, |
| 5111 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5112 | |
| 5113 | hci_dev_lock(hdev); |
| 5114 | |
| 5115 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5116 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5117 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5118 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5119 | goto unlock; |
| 5120 | } |
| 5121 | |
| 5122 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5123 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5124 | &cp->addr.bdaddr); |
| 5125 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5126 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5127 | MGMT_OP_GET_CLOCK_INFO, |
| 5128 | MGMT_STATUS_NOT_CONNECTED, |
| 5129 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5130 | goto unlock; |
| 5131 | } |
| 5132 | } else { |
| 5133 | conn = NULL; |
| 5134 | } |
| 5135 | |
| 5136 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5137 | if (!cmd) { |
| 5138 | err = -ENOMEM; |
| 5139 | goto unlock; |
| 5140 | } |
| 5141 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5142 | cmd->cmd_complete = clock_info_cmd_complete; |
| 5143 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5144 | hci_req_init(&req, hdev); |
| 5145 | |
| 5146 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5147 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5148 | |
| 5149 | if (conn) { |
| 5150 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5151 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5152 | |
| 5153 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5154 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5155 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5156 | } |
| 5157 | |
| 5158 | err = hci_req_run(&req, get_clock_info_complete); |
| 5159 | if (err < 0) |
| 5160 | mgmt_pending_remove(cmd); |
| 5161 | |
| 5162 | unlock: |
| 5163 | hci_dev_unlock(hdev); |
| 5164 | return err; |
| 5165 | } |
| 5166 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5167 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 5168 | { |
| 5169 | struct hci_conn *conn; |
| 5170 | |
| 5171 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 5172 | if (!conn) |
| 5173 | return false; |
| 5174 | |
| 5175 | if (conn->dst_type != type) |
| 5176 | return false; |
| 5177 | |
| 5178 | if (conn->state != BT_CONNECTED) |
| 5179 | return false; |
| 5180 | |
| 5181 | return true; |
| 5182 | } |
| 5183 | |
| 5184 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5185 | static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr, |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5186 | u8 addr_type, u8 auto_connect) |
| 5187 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5188 | struct hci_conn_params *params; |
| 5189 | |
| 5190 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 5191 | if (!params) |
| 5192 | return -EIO; |
| 5193 | |
| 5194 | if (params->auto_connect == auto_connect) |
| 5195 | return 0; |
| 5196 | |
| 5197 | list_del_init(¶ms->action); |
| 5198 | |
| 5199 | switch (auto_connect) { |
| 5200 | case HCI_AUTO_CONN_DISABLED: |
| 5201 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 5202 | /* If auto connect is being disabled when we're trying to |
| 5203 | * connect to device, keep connecting. |
| 5204 | */ |
| 5205 | if (params->explicit_connect) |
| 5206 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5207 | break; |
| 5208 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 5209 | if (params->explicit_connect) |
| 5210 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 5211 | else |
| 5212 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5213 | break; |
| 5214 | case HCI_AUTO_CONN_DIRECT: |
| 5215 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5216 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5217 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5218 | break; |
| 5219 | } |
| 5220 | |
| 5221 | params->auto_connect = auto_connect; |
| 5222 | |
| 5223 | BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, |
| 5224 | auto_connect); |
| 5225 | |
| 5226 | return 0; |
| 5227 | } |
| 5228 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5229 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5230 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5231 | { |
| 5232 | struct mgmt_ev_device_added ev; |
| 5233 | |
| 5234 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5235 | ev.addr.type = type; |
| 5236 | ev.action = action; |
| 5237 | |
| 5238 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5239 | } |
| 5240 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5241 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5242 | void *data, u16 len) |
| 5243 | { |
| 5244 | struct mgmt_cp_add_device *cp = data; |
| 5245 | u8 auto_conn, addr_type; |
| 5246 | int err; |
| 5247 | |
| 5248 | BT_DBG("%s", hdev->name); |
| 5249 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5250 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5251 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5252 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5253 | MGMT_STATUS_INVALID_PARAMS, |
| 5254 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5255 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5256 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5257 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5258 | MGMT_STATUS_INVALID_PARAMS, |
| 5259 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5260 | |
| 5261 | hci_dev_lock(hdev); |
| 5262 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5263 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5264 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5265 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5266 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5267 | MGMT_OP_ADD_DEVICE, |
| 5268 | MGMT_STATUS_INVALID_PARAMS, |
| 5269 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5270 | goto unlock; |
| 5271 | } |
| 5272 | |
| 5273 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5274 | cp->addr.type); |
| 5275 | if (err) |
| 5276 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5277 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5278 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5279 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5280 | goto added; |
| 5281 | } |
| 5282 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5283 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5284 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5285 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5286 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5287 | else if (cp->action == 0x01) |
| 5288 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5289 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5290 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5291 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5292 | /* Kernel internally uses conn_params with resolvable private |
| 5293 | * address, but Add Device allows only identity addresses. |
| 5294 | * Make sure it is enforced before calling |
| 5295 | * hci_conn_params_lookup. |
| 5296 | */ |
| 5297 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5298 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5299 | MGMT_STATUS_INVALID_PARAMS, |
| 5300 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5301 | goto unlock; |
| 5302 | } |
| 5303 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5304 | /* If the connection parameters don't exist for this device, |
| 5305 | * they will be created and configured with defaults. |
| 5306 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5307 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5308 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5309 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5310 | MGMT_STATUS_FAILED, &cp->addr, |
| 5311 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5312 | goto unlock; |
| 5313 | } |
| 5314 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5315 | hci_update_background_scan(hdev); |
| 5316 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5317 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5318 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5319 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5320 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5321 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 5322 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5323 | |
| 5324 | unlock: |
| 5325 | hci_dev_unlock(hdev); |
| 5326 | return err; |
| 5327 | } |
| 5328 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5329 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5330 | bdaddr_t *bdaddr, u8 type) |
| 5331 | { |
| 5332 | struct mgmt_ev_device_removed ev; |
| 5333 | |
| 5334 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5335 | ev.addr.type = type; |
| 5336 | |
| 5337 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5338 | } |
| 5339 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5340 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5341 | void *data, u16 len) |
| 5342 | { |
| 5343 | struct mgmt_cp_remove_device *cp = data; |
| 5344 | int err; |
| 5345 | |
| 5346 | BT_DBG("%s", hdev->name); |
| 5347 | |
| 5348 | hci_dev_lock(hdev); |
| 5349 | |
| 5350 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5351 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5352 | u8 addr_type; |
| 5353 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5354 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5355 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5356 | MGMT_OP_REMOVE_DEVICE, |
| 5357 | MGMT_STATUS_INVALID_PARAMS, |
| 5358 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5359 | goto unlock; |
| 5360 | } |
| 5361 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5362 | if (cp->addr.type == BDADDR_BREDR) { |
| 5363 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5364 | &cp->addr.bdaddr, |
| 5365 | cp->addr.type); |
| 5366 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5367 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5368 | MGMT_OP_REMOVE_DEVICE, |
| 5369 | MGMT_STATUS_INVALID_PARAMS, |
| 5370 | &cp->addr, |
| 5371 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5372 | goto unlock; |
| 5373 | } |
| 5374 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5375 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5376 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5377 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5378 | cp->addr.type); |
| 5379 | goto complete; |
| 5380 | } |
| 5381 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5382 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5383 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5384 | /* Kernel internally uses conn_params with resolvable private |
| 5385 | * address, but Remove Device allows only identity addresses. |
| 5386 | * Make sure it is enforced before calling |
| 5387 | * hci_conn_params_lookup. |
| 5388 | */ |
| 5389 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5390 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5391 | MGMT_OP_REMOVE_DEVICE, |
| 5392 | MGMT_STATUS_INVALID_PARAMS, |
| 5393 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5394 | goto unlock; |
| 5395 | } |
| 5396 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5397 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5398 | addr_type); |
| 5399 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5400 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5401 | MGMT_OP_REMOVE_DEVICE, |
| 5402 | MGMT_STATUS_INVALID_PARAMS, |
| 5403 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5404 | goto unlock; |
| 5405 | } |
| 5406 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 5407 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 5408 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5409 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5410 | MGMT_OP_REMOVE_DEVICE, |
| 5411 | MGMT_STATUS_INVALID_PARAMS, |
| 5412 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5413 | goto unlock; |
| 5414 | } |
| 5415 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5416 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5417 | list_del(¶ms->list); |
| 5418 | kfree(params); |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5419 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5420 | |
| 5421 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5422 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5423 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5424 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5425 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5426 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5427 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5428 | MGMT_OP_REMOVE_DEVICE, |
| 5429 | MGMT_STATUS_INVALID_PARAMS, |
| 5430 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5431 | goto unlock; |
| 5432 | } |
| 5433 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5434 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 5435 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 5436 | list_del(&b->list); |
| 5437 | kfree(b); |
| 5438 | } |
| 5439 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5440 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5441 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5442 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5443 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5444 | continue; |
| 5445 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 5446 | if (p->explicit_connect) { |
| 5447 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 5448 | continue; |
| 5449 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5450 | list_del(&p->action); |
| 5451 | list_del(&p->list); |
| 5452 | kfree(p); |
| 5453 | } |
| 5454 | |
| 5455 | BT_DBG("All LE connection parameters were removed"); |
| 5456 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5457 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5458 | } |
| 5459 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5460 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5461 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5462 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 5463 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5464 | unlock: |
| 5465 | hci_dev_unlock(hdev); |
| 5466 | return err; |
| 5467 | } |
| 5468 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5469 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5470 | u16 len) |
| 5471 | { |
| 5472 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5473 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5474 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5475 | u16 param_count, expected_len; |
| 5476 | int i; |
| 5477 | |
| 5478 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5479 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5480 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5481 | |
| 5482 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5483 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5484 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 5485 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5486 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5487 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5488 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5489 | |
| 5490 | expected_len = sizeof(*cp) + param_count * |
| 5491 | sizeof(struct mgmt_conn_param); |
| 5492 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5493 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 5494 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5495 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5496 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5497 | } |
| 5498 | |
| 5499 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5500 | |
| 5501 | hci_dev_lock(hdev); |
| 5502 | |
| 5503 | hci_conn_params_clear_disabled(hdev); |
| 5504 | |
| 5505 | for (i = 0; i < param_count; i++) { |
| 5506 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5507 | struct hci_conn_params *hci_param; |
| 5508 | u16 min, max, latency, timeout; |
| 5509 | u8 addr_type; |
| 5510 | |
| 5511 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5512 | param->addr.type); |
| 5513 | |
| 5514 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5515 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5516 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5517 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5518 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5519 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5520 | continue; |
| 5521 | } |
| 5522 | |
| 5523 | min = le16_to_cpu(param->min_interval); |
| 5524 | max = le16_to_cpu(param->max_interval); |
| 5525 | latency = le16_to_cpu(param->latency); |
| 5526 | timeout = le16_to_cpu(param->timeout); |
| 5527 | |
| 5528 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5529 | min, max, latency, timeout); |
| 5530 | |
| 5531 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5532 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5533 | continue; |
| 5534 | } |
| 5535 | |
| 5536 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5537 | addr_type); |
| 5538 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5539 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5540 | continue; |
| 5541 | } |
| 5542 | |
| 5543 | hci_param->conn_min_interval = min; |
| 5544 | hci_param->conn_max_interval = max; |
| 5545 | hci_param->conn_latency = latency; |
| 5546 | hci_param->supervision_timeout = timeout; |
| 5547 | } |
| 5548 | |
| 5549 | hci_dev_unlock(hdev); |
| 5550 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5551 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 5552 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5553 | } |
| 5554 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5555 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5556 | void *data, u16 len) |
| 5557 | { |
| 5558 | struct mgmt_cp_set_external_config *cp = data; |
| 5559 | bool changed; |
| 5560 | int err; |
| 5561 | |
| 5562 | BT_DBG("%s", hdev->name); |
| 5563 | |
| 5564 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5565 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5566 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5567 | |
| 5568 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5569 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5570 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5571 | |
| 5572 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5573 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5574 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5575 | |
| 5576 | hci_dev_lock(hdev); |
| 5577 | |
| 5578 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5579 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5580 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5581 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5582 | |
| 5583 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5584 | if (err < 0) |
| 5585 | goto unlock; |
| 5586 | |
| 5587 | if (!changed) |
| 5588 | goto unlock; |
| 5589 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 5590 | err = new_options(hdev, sk); |
| 5591 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5592 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5593 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5594 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 5595 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5596 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 5597 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5598 | |
| 5599 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5600 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 5601 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5602 | mgmt_index_added(hdev); |
| 5603 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | unlock: |
| 5607 | hci_dev_unlock(hdev); |
| 5608 | return err; |
| 5609 | } |
| 5610 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5611 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 5612 | void *data, u16 len) |
| 5613 | { |
| 5614 | struct mgmt_cp_set_public_address *cp = data; |
| 5615 | bool changed; |
| 5616 | int err; |
| 5617 | |
| 5618 | BT_DBG("%s", hdev->name); |
| 5619 | |
| 5620 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5621 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5622 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5623 | |
| 5624 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5625 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5626 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5627 | |
| 5628 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5629 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5630 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5631 | |
| 5632 | hci_dev_lock(hdev); |
| 5633 | |
| 5634 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 5635 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 5636 | |
| 5637 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 5638 | if (err < 0) |
| 5639 | goto unlock; |
| 5640 | |
| 5641 | if (!changed) |
| 5642 | goto unlock; |
| 5643 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5644 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5645 | err = new_options(hdev, sk); |
| 5646 | |
| 5647 | if (is_configured(hdev)) { |
| 5648 | mgmt_index_removed(hdev); |
| 5649 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5650 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5651 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5652 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 5653 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5654 | |
| 5655 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5656 | } |
| 5657 | |
| 5658 | unlock: |
| 5659 | hci_dev_unlock(hdev); |
| 5660 | return err; |
| 5661 | } |
| 5662 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 5663 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 5664 | u16 opcode, struct sk_buff *skb) |
| 5665 | { |
| 5666 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 5667 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 5668 | u8 *h192, *r192, *h256, *r256; |
| 5669 | struct mgmt_pending_cmd *cmd; |
| 5670 | u16 eir_len; |
| 5671 | int err; |
| 5672 | |
| 5673 | BT_DBG("%s status %u", hdev->name, status); |
| 5674 | |
| 5675 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 5676 | if (!cmd) |
| 5677 | return; |
| 5678 | |
| 5679 | mgmt_cp = cmd->param; |
| 5680 | |
| 5681 | if (status) { |
| 5682 | status = mgmt_status(status); |
| 5683 | eir_len = 0; |
| 5684 | |
| 5685 | h192 = NULL; |
| 5686 | r192 = NULL; |
| 5687 | h256 = NULL; |
| 5688 | r256 = NULL; |
| 5689 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 5690 | struct hci_rp_read_local_oob_data *rp; |
| 5691 | |
| 5692 | if (skb->len != sizeof(*rp)) { |
| 5693 | status = MGMT_STATUS_FAILED; |
| 5694 | eir_len = 0; |
| 5695 | } else { |
| 5696 | status = MGMT_STATUS_SUCCESS; |
| 5697 | rp = (void *)skb->data; |
| 5698 | |
| 5699 | eir_len = 5 + 18 + 18; |
| 5700 | h192 = rp->hash; |
| 5701 | r192 = rp->rand; |
| 5702 | h256 = NULL; |
| 5703 | r256 = NULL; |
| 5704 | } |
| 5705 | } else { |
| 5706 | struct hci_rp_read_local_oob_ext_data *rp; |
| 5707 | |
| 5708 | if (skb->len != sizeof(*rp)) { |
| 5709 | status = MGMT_STATUS_FAILED; |
| 5710 | eir_len = 0; |
| 5711 | } else { |
| 5712 | status = MGMT_STATUS_SUCCESS; |
| 5713 | rp = (void *)skb->data; |
| 5714 | |
| 5715 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 5716 | eir_len = 5 + 18 + 18; |
| 5717 | h192 = NULL; |
| 5718 | r192 = NULL; |
| 5719 | } else { |
| 5720 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 5721 | h192 = rp->hash192; |
| 5722 | r192 = rp->rand192; |
| 5723 | } |
| 5724 | |
| 5725 | h256 = rp->hash256; |
| 5726 | r256 = rp->rand256; |
| 5727 | } |
| 5728 | } |
| 5729 | |
| 5730 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 5731 | if (!mgmt_rp) |
| 5732 | goto done; |
| 5733 | |
| 5734 | if (status) |
| 5735 | goto send_rsp; |
| 5736 | |
| 5737 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 5738 | hdev->dev_class, 3); |
| 5739 | |
| 5740 | if (h192 && r192) { |
| 5741 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 5742 | EIR_SSP_HASH_C192, h192, 16); |
| 5743 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 5744 | EIR_SSP_RAND_R192, r192, 16); |
| 5745 | } |
| 5746 | |
| 5747 | if (h256 && r256) { |
| 5748 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 5749 | EIR_SSP_HASH_C256, h256, 16); |
| 5750 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 5751 | EIR_SSP_RAND_R256, r256, 16); |
| 5752 | } |
| 5753 | |
| 5754 | send_rsp: |
| 5755 | mgmt_rp->type = mgmt_cp->type; |
| 5756 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 5757 | |
| 5758 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 5759 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 5760 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 5761 | if (err < 0 || status) |
| 5762 | goto done; |
| 5763 | |
| 5764 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 5765 | |
| 5766 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 5767 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 5768 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 5769 | done: |
| 5770 | kfree(mgmt_rp); |
| 5771 | mgmt_pending_remove(cmd); |
| 5772 | } |
| 5773 | |
| 5774 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 5775 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 5776 | { |
| 5777 | struct mgmt_pending_cmd *cmd; |
| 5778 | struct hci_request req; |
| 5779 | int err; |
| 5780 | |
| 5781 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 5782 | cp, sizeof(*cp)); |
| 5783 | if (!cmd) |
| 5784 | return -ENOMEM; |
| 5785 | |
| 5786 | hci_req_init(&req, hdev); |
| 5787 | |
| 5788 | if (bredr_sc_enabled(hdev)) |
| 5789 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 5790 | else |
| 5791 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 5792 | |
| 5793 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 5794 | if (err < 0) { |
| 5795 | mgmt_pending_remove(cmd); |
| 5796 | return err; |
| 5797 | } |
| 5798 | |
| 5799 | return 0; |
| 5800 | } |
| 5801 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5802 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 5803 | void *data, u16 data_len) |
| 5804 | { |
| 5805 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 5806 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 5807 | size_t rp_len; |
| 5808 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5809 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5810 | int err; |
| 5811 | |
| 5812 | BT_DBG("%s", hdev->name); |
| 5813 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 5814 | if (hdev_is_powered(hdev)) { |
| 5815 | switch (cp->type) { |
| 5816 | case BIT(BDADDR_BREDR): |
| 5817 | status = mgmt_bredr_support(hdev); |
| 5818 | if (status) |
| 5819 | eir_len = 0; |
| 5820 | else |
| 5821 | eir_len = 5; |
| 5822 | break; |
| 5823 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 5824 | status = mgmt_le_support(hdev); |
| 5825 | if (status) |
| 5826 | eir_len = 0; |
| 5827 | else |
| 5828 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 5829 | break; |
| 5830 | default: |
| 5831 | status = MGMT_STATUS_INVALID_PARAMS; |
| 5832 | eir_len = 0; |
| 5833 | break; |
| 5834 | } |
| 5835 | } else { |
| 5836 | status = MGMT_STATUS_NOT_POWERED; |
| 5837 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5838 | } |
| 5839 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5840 | rp_len = sizeof(*rp) + eir_len; |
| 5841 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 5842 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5843 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 5844 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 5845 | if (status) |
| 5846 | goto complete; |
| 5847 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 5848 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5849 | |
| 5850 | eir_len = 0; |
| 5851 | switch (cp->type) { |
| 5852 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 5853 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 5854 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 5855 | hci_dev_unlock(hdev); |
| 5856 | if (!err) |
| 5857 | goto done; |
| 5858 | |
| 5859 | status = MGMT_STATUS_FAILED; |
| 5860 | goto complete; |
| 5861 | } else { |
| 5862 | eir_len = eir_append_data(rp->eir, eir_len, |
| 5863 | EIR_CLASS_OF_DEV, |
| 5864 | hdev->dev_class, 3); |
| 5865 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5866 | break; |
| 5867 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 5868 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 5869 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5870 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 5871 | status = MGMT_STATUS_FAILED; |
| 5872 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5873 | } |
| 5874 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 5875 | /* This should return the active RPA, but since the RPA |
| 5876 | * is only programmed on demand, it is really hard to fill |
| 5877 | * this in at the moment. For now disallow retrieving |
| 5878 | * local out-of-band data when privacy is in use. |
| 5879 | * |
| 5880 | * Returning the identity address will not help here since |
| 5881 | * pairing happens before the identity resolving key is |
| 5882 | * known and thus the connection establishment happens |
| 5883 | * based on the RPA and not the identity address. |
| 5884 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5885 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 5886 | hci_dev_unlock(hdev); |
| 5887 | status = MGMT_STATUS_REJECTED; |
| 5888 | goto complete; |
| 5889 | } |
| 5890 | |
| 5891 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 5892 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 5893 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 5894 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5895 | memcpy(addr, &hdev->static_addr, 6); |
| 5896 | addr[6] = 0x01; |
| 5897 | } else { |
| 5898 | memcpy(addr, &hdev->bdaddr, 6); |
| 5899 | addr[6] = 0x00; |
| 5900 | } |
| 5901 | |
| 5902 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 5903 | addr, sizeof(addr)); |
| 5904 | |
| 5905 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 5906 | role = 0x02; |
| 5907 | else |
| 5908 | role = 0x01; |
| 5909 | |
| 5910 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 5911 | &role, sizeof(role)); |
| 5912 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 5913 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 5914 | eir_len = eir_append_data(rp->eir, eir_len, |
| 5915 | EIR_LE_SC_CONFIRM, |
| 5916 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5917 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 5918 | eir_len = eir_append_data(rp->eir, eir_len, |
| 5919 | EIR_LE_SC_RANDOM, |
| 5920 | rand, sizeof(rand)); |
| 5921 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5922 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 5923 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5924 | |
| 5925 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 5926 | flags |= LE_AD_NO_BREDR; |
| 5927 | |
| 5928 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 5929 | &flags, sizeof(flags)); |
| 5930 | break; |
| 5931 | } |
| 5932 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5933 | hci_dev_unlock(hdev); |
| 5934 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 5935 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 5936 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 5937 | status = MGMT_STATUS_SUCCESS; |
| 5938 | |
| 5939 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 5940 | rp->type = cp->type; |
| 5941 | rp->eir_len = cpu_to_le16(eir_len); |
| 5942 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5943 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 5944 | status, rp, sizeof(*rp) + eir_len); |
| 5945 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 5946 | goto done; |
| 5947 | |
| 5948 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 5949 | rp, sizeof(*rp) + eir_len, |
| 5950 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5951 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 5952 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 5953 | kfree(rp); |
| 5954 | |
| 5955 | return err; |
| 5956 | } |
| 5957 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 5958 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 5959 | { |
| 5960 | u32 flags = 0; |
| 5961 | |
| 5962 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 5963 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 5964 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 5965 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 5966 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 5967 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 5968 | |
| 5969 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) |
| 5970 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 5971 | |
| 5972 | return flags; |
| 5973 | } |
| 5974 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 5975 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 5976 | void *data, u16 data_len) |
| 5977 | { |
| 5978 | struct mgmt_rp_read_adv_features *rp; |
| 5979 | size_t rp_len; |
Johan Hedberg | 02c04af | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 5980 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 5981 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 5982 | u32 supported_flags; |
Johan Hedberg | 02c04af | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 5983 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 5984 | |
| 5985 | BT_DBG("%s", hdev->name); |
| 5986 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 5987 | if (!lmp_le_capable(hdev)) |
| 5988 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 5989 | MGMT_STATUS_REJECTED); |
| 5990 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 5991 | hci_dev_lock(hdev); |
| 5992 | |
Johan Hedberg | 02c04af | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 5993 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 5994 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 5995 | if (!rp) { |
| 5996 | hci_dev_unlock(hdev); |
| 5997 | return -ENOMEM; |
| 5998 | } |
| 5999 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6000 | supported_flags = get_supported_adv_flags(hdev); |
| 6001 | |
| 6002 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 6003 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 6004 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 6005 | rp->max_instances = HCI_MAX_ADV_INSTANCES; |
Johan Hedberg | 02c04af | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6006 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6007 | |
Johan Hedberg | 02c04af | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6008 | instance = rp->instance; |
| 6009 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 6010 | *instance = adv_instance->instance; |
| 6011 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6012 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6013 | |
| 6014 | hci_dev_unlock(hdev); |
| 6015 | |
| 6016 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 6017 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 6018 | |
| 6019 | kfree(rp); |
| 6020 | |
| 6021 | return err; |
| 6022 | } |
| 6023 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6024 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 6025 | { |
| 6026 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 6027 | |
| 6028 | return append_local_name(hdev, buf, 0); |
| 6029 | } |
| 6030 | |
| 6031 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 6032 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6033 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6034 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6035 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6036 | if (is_adv_data) { |
| 6037 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 6038 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6039 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6040 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6041 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6042 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6043 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 6044 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 6045 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6046 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6047 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6048 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6049 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 6050 | } |
| 6051 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6052 | return max_len; |
| 6053 | } |
| 6054 | |
| 6055 | static bool flags_managed(u32 adv_flags) |
| 6056 | { |
| 6057 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 6058 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 6059 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 6060 | } |
| 6061 | |
| 6062 | static bool tx_power_managed(u32 adv_flags) |
| 6063 | { |
| 6064 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 6065 | } |
| 6066 | |
| 6067 | static bool name_managed(u32 adv_flags) |
| 6068 | { |
| 6069 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 6070 | } |
| 6071 | |
| 6072 | static bool appearance_managed(u32 adv_flags) |
| 6073 | { |
| 6074 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 6075 | } |
| 6076 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6077 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 6078 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6079 | { |
| 6080 | int i, cur_len; |
| 6081 | u8 max_len; |
| 6082 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6083 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6084 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6085 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6086 | return false; |
| 6087 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6088 | /* Make sure that the data is correctly formatted. */ |
| 6089 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 6090 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6091 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 6092 | if (data[i + 1] == EIR_FLAGS && |
| 6093 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6094 | return false; |
| 6095 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6096 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 6097 | return false; |
| 6098 | |
| 6099 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 6100 | return false; |
| 6101 | |
| 6102 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 6103 | return false; |
| 6104 | |
| 6105 | if (data[i + 1] == EIR_APPEARANCE && |
| 6106 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 6107 | return false; |
| 6108 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6109 | /* If the current field length would exceed the total data |
| 6110 | * length, then it's invalid. |
| 6111 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6112 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6113 | return false; |
| 6114 | } |
| 6115 | |
| 6116 | return true; |
| 6117 | } |
| 6118 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6119 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
| 6120 | u16 opcode) |
| 6121 | { |
| 6122 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6123 | struct mgmt_cp_add_advertising *cp; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6124 | struct mgmt_rp_add_advertising rp; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6125 | struct adv_info *adv_instance, *n; |
| 6126 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6127 | |
| 6128 | BT_DBG("status %d", status); |
| 6129 | |
| 6130 | hci_dev_lock(hdev); |
| 6131 | |
| 6132 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); |
| 6133 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6134 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 6135 | if (!adv_instance->pending) |
| 6136 | continue; |
| 6137 | |
| 6138 | if (!status) { |
| 6139 | adv_instance->pending = false; |
| 6140 | continue; |
| 6141 | } |
| 6142 | |
| 6143 | instance = adv_instance->instance; |
| 6144 | |
| 6145 | if (hdev->cur_adv_instance == instance) |
| 6146 | cancel_adv_timeout(hdev); |
| 6147 | |
| 6148 | hci_remove_adv_instance(hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6149 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6150 | } |
| 6151 | |
| 6152 | if (!cmd) |
| 6153 | goto unlock; |
| 6154 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6155 | cp = cmd->param; |
| 6156 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6157 | |
| 6158 | if (status) |
| 6159 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 6160 | mgmt_status(status)); |
| 6161 | else |
| 6162 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 6163 | mgmt_status(status), &rp, sizeof(rp)); |
| 6164 | |
| 6165 | mgmt_pending_remove(cmd); |
| 6166 | |
| 6167 | unlock: |
| 6168 | hci_dev_unlock(hdev); |
| 6169 | } |
| 6170 | |
| 6171 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 6172 | void *data, u16 data_len) |
| 6173 | { |
| 6174 | struct mgmt_cp_add_advertising *cp = data; |
| 6175 | struct mgmt_rp_add_advertising rp; |
| 6176 | u32 flags; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6177 | u32 supported_flags; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6178 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6179 | u16 timeout, duration; |
| 6180 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 6181 | u8 schedule_instance = 0; |
| 6182 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6183 | int err; |
| 6184 | struct mgmt_pending_cmd *cmd; |
| 6185 | struct hci_request req; |
| 6186 | |
| 6187 | BT_DBG("%s", hdev->name); |
| 6188 | |
| 6189 | status = mgmt_le_support(hdev); |
| 6190 | if (status) |
| 6191 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6192 | status); |
| 6193 | |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 6194 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 6195 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6196 | MGMT_STATUS_INVALID_PARAMS); |
| 6197 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 6198 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 6199 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6200 | MGMT_STATUS_INVALID_PARAMS); |
| 6201 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6202 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6203 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6204 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6205 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6206 | /* The current implementation only supports a subset of the specified |
| 6207 | * flags. |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6208 | */ |
| 6209 | supported_flags = get_supported_adv_flags(hdev); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6210 | if (flags & ~supported_flags) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6211 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6212 | MGMT_STATUS_INVALID_PARAMS); |
| 6213 | |
| 6214 | hci_dev_lock(hdev); |
| 6215 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6216 | if (timeout && !hdev_is_powered(hdev)) { |
| 6217 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6218 | MGMT_STATUS_REJECTED); |
| 6219 | goto unlock; |
| 6220 | } |
| 6221 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6222 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6223 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6224 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 6225 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6226 | MGMT_STATUS_BUSY); |
| 6227 | goto unlock; |
| 6228 | } |
| 6229 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6230 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 6231 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6232 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6233 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6234 | MGMT_STATUS_INVALID_PARAMS); |
| 6235 | goto unlock; |
| 6236 | } |
| 6237 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6238 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 6239 | cp->adv_data_len, cp->data, |
| 6240 | cp->scan_rsp_len, |
| 6241 | cp->data + cp->adv_data_len, |
| 6242 | timeout, duration); |
| 6243 | if (err < 0) { |
| 6244 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6245 | MGMT_STATUS_FAILED); |
| 6246 | goto unlock; |
| 6247 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6248 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6249 | /* Only trigger an advertising added event if a new instance was |
| 6250 | * actually added. |
| 6251 | */ |
| 6252 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6253 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6254 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6255 | if (hdev->cur_adv_instance == cp->instance) { |
| 6256 | /* If the currently advertised instance is being changed then |
| 6257 | * cancel the current advertising and schedule the next |
| 6258 | * instance. If there is only one instance then the overridden |
| 6259 | * advertising data will be visible right away. |
| 6260 | */ |
| 6261 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6262 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6263 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 6264 | if (next_instance) |
| 6265 | schedule_instance = next_instance->instance; |
| 6266 | } else if (!hdev->adv_instance_timeout) { |
| 6267 | /* Immediately advertise the new instance if no other |
| 6268 | * instance is currently being advertised. |
| 6269 | */ |
| 6270 | schedule_instance = cp->instance; |
| 6271 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6272 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6273 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 6274 | * there is no instance to be advertised then we have no HCI |
| 6275 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6276 | */ |
| 6277 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6278 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 6279 | !schedule_instance) { |
| 6280 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6281 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6282 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6283 | goto unlock; |
| 6284 | } |
| 6285 | |
| 6286 | /* We're good to go, update advertising data, parameters, and start |
| 6287 | * advertising. |
| 6288 | */ |
| 6289 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
| 6290 | data_len); |
| 6291 | if (!cmd) { |
| 6292 | err = -ENOMEM; |
| 6293 | goto unlock; |
| 6294 | } |
| 6295 | |
| 6296 | hci_req_init(&req, hdev); |
| 6297 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6298 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6299 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6300 | if (!err) |
| 6301 | err = hci_req_run(&req, add_advertising_complete); |
| 6302 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6303 | if (err < 0) |
| 6304 | mgmt_pending_remove(cmd); |
| 6305 | |
| 6306 | unlock: |
| 6307 | hci_dev_unlock(hdev); |
| 6308 | |
| 6309 | return err; |
| 6310 | } |
| 6311 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6312 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
| 6313 | u16 opcode) |
| 6314 | { |
| 6315 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6316 | struct mgmt_cp_remove_advertising *cp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6317 | struct mgmt_rp_remove_advertising rp; |
| 6318 | |
| 6319 | BT_DBG("status %d", status); |
| 6320 | |
| 6321 | hci_dev_lock(hdev); |
| 6322 | |
| 6323 | /* A failure status here only means that we failed to disable |
| 6324 | * advertising. Otherwise, the advertising instance has been removed, |
| 6325 | * so report success. |
| 6326 | */ |
| 6327 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); |
| 6328 | if (!cmd) |
| 6329 | goto unlock; |
| 6330 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6331 | cp = cmd->param; |
| 6332 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6333 | |
| 6334 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, |
| 6335 | &rp, sizeof(rp)); |
| 6336 | mgmt_pending_remove(cmd); |
| 6337 | |
| 6338 | unlock: |
| 6339 | hci_dev_unlock(hdev); |
| 6340 | } |
| 6341 | |
| 6342 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 6343 | void *data, u16 data_len) |
| 6344 | { |
| 6345 | struct mgmt_cp_remove_advertising *cp = data; |
| 6346 | struct mgmt_rp_remove_advertising rp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6347 | struct mgmt_pending_cmd *cmd; |
| 6348 | struct hci_request req; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 6349 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6350 | |
| 6351 | BT_DBG("%s", hdev->name); |
| 6352 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6353 | hci_dev_lock(hdev); |
| 6354 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 6355 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6356 | err = mgmt_cmd_status(sk, hdev->id, |
| 6357 | MGMT_OP_REMOVE_ADVERTISING, |
| 6358 | MGMT_STATUS_INVALID_PARAMS); |
| 6359 | goto unlock; |
| 6360 | } |
| 6361 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6362 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 6363 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 6364 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 6365 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 6366 | MGMT_STATUS_BUSY); |
| 6367 | goto unlock; |
| 6368 | } |
| 6369 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 6370 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6371 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 6372 | MGMT_STATUS_INVALID_PARAMS); |
| 6373 | goto unlock; |
| 6374 | } |
| 6375 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6376 | hci_req_init(&req, hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6377 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 6378 | hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6379 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6380 | if (list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6381 | __hci_req_disable_advertising(&req); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6382 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6383 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
| 6384 | * flag is set or the device isn't powered then we have no HCI |
| 6385 | * communication to make. Simply return. |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6386 | */ |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6387 | if (skb_queue_empty(&req.cmd_q) || |
| 6388 | !hdev_is_powered(hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6389 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Jaganath Kanakkassery | f17d858 | 2017-10-25 10:58:48 +0530 | [diff] [blame] | 6390 | hci_req_purge(&req); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6391 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6392 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6393 | MGMT_OP_REMOVE_ADVERTISING, |
| 6394 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6395 | goto unlock; |
| 6396 | } |
| 6397 | |
| 6398 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
| 6399 | data_len); |
| 6400 | if (!cmd) { |
| 6401 | err = -ENOMEM; |
| 6402 | goto unlock; |
| 6403 | } |
| 6404 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6405 | err = hci_req_run(&req, remove_advertising_complete); |
| 6406 | if (err < 0) |
| 6407 | mgmt_pending_remove(cmd); |
| 6408 | |
| 6409 | unlock: |
| 6410 | hci_dev_unlock(hdev); |
| 6411 | |
| 6412 | return err; |
| 6413 | } |
| 6414 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6415 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 6416 | void *data, u16 data_len) |
| 6417 | { |
| 6418 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 6419 | struct mgmt_rp_get_adv_size_info rp; |
| 6420 | u32 flags, supported_flags; |
| 6421 | int err; |
| 6422 | |
| 6423 | BT_DBG("%s", hdev->name); |
| 6424 | |
| 6425 | if (!lmp_le_capable(hdev)) |
| 6426 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6427 | MGMT_STATUS_REJECTED); |
| 6428 | |
| 6429 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 6430 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6431 | MGMT_STATUS_INVALID_PARAMS); |
| 6432 | |
| 6433 | flags = __le32_to_cpu(cp->flags); |
| 6434 | |
| 6435 | /* The current implementation only supports a subset of the specified |
| 6436 | * flags. |
| 6437 | */ |
| 6438 | supported_flags = get_supported_adv_flags(hdev); |
| 6439 | if (flags & ~supported_flags) |
| 6440 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6441 | MGMT_STATUS_INVALID_PARAMS); |
| 6442 | |
| 6443 | rp.instance = cp->instance; |
| 6444 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6445 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 6446 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6447 | |
| 6448 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6449 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6450 | |
| 6451 | return err; |
| 6452 | } |
| 6453 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6454 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6455 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6456 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6457 | HCI_MGMT_NO_HDEV | |
| 6458 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6459 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6460 | HCI_MGMT_NO_HDEV | |
| 6461 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6462 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6463 | HCI_MGMT_NO_HDEV | |
| 6464 | HCI_MGMT_UNTRUSTED }, |
| 6465 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 6466 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6467 | { set_powered, MGMT_SETTING_SIZE }, |
| 6468 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 6469 | { set_connectable, MGMT_SETTING_SIZE }, |
| 6470 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 6471 | { set_bondable, MGMT_SETTING_SIZE }, |
| 6472 | { set_link_security, MGMT_SETTING_SIZE }, |
| 6473 | { set_ssp, MGMT_SETTING_SIZE }, |
| 6474 | { set_hs, MGMT_SETTING_SIZE }, |
| 6475 | { set_le, MGMT_SETTING_SIZE }, |
| 6476 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 6477 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 6478 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 6479 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6480 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 6481 | HCI_MGMT_VAR_LEN }, |
| 6482 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 6483 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6484 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 6485 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 6486 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 6487 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 6488 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 6489 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 6490 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 6491 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 6492 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 6493 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 6494 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 6495 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6496 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 6497 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 6498 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6499 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 6500 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 6501 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 6502 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 6503 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 6504 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 6505 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 6506 | { set_advertising, MGMT_SETTING_SIZE }, |
| 6507 | { set_bredr, MGMT_SETTING_SIZE }, |
| 6508 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 6509 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 6510 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 6511 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 6512 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6513 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 6514 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6515 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 6516 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 6517 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 6518 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6519 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 6520 | HCI_MGMT_VAR_LEN }, |
| 6521 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6522 | HCI_MGMT_NO_HDEV | |
| 6523 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6524 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6525 | HCI_MGMT_UNCONFIGURED | |
| 6526 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6527 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 6528 | HCI_MGMT_UNCONFIGURED }, |
| 6529 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 6530 | HCI_MGMT_UNCONFIGURED }, |
| 6531 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 6532 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6533 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 6534 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6535 | HCI_MGMT_NO_HDEV | |
| 6536 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6537 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6538 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 6539 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6540 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6541 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 6542 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 6543 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 6544 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6545 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6546 | }; |
| 6547 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6548 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6549 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6550 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 6551 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6552 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6553 | return; |
| 6554 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6555 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 6556 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6557 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6558 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 6559 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6560 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6561 | } else { |
| 6562 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 6563 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6564 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6565 | } |
| 6566 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6567 | case HCI_AMP: |
| 6568 | ev.type = 0x02; |
| 6569 | break; |
| 6570 | default: |
| 6571 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6572 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6573 | |
| 6574 | ev.bus = hdev->bus; |
| 6575 | |
| 6576 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 6577 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6578 | } |
| 6579 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6580 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6581 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6582 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 6583 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6584 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6585 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6586 | return; |
| 6587 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6588 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 6589 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6590 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6591 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6592 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6593 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 6594 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6595 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6596 | } else { |
| 6597 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 6598 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6599 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6600 | } |
| 6601 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6602 | case HCI_AMP: |
| 6603 | ev.type = 0x02; |
| 6604 | break; |
| 6605 | default: |
| 6606 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6607 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6608 | |
| 6609 | ev.bus = hdev->bus; |
| 6610 | |
| 6611 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 6612 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6613 | } |
| 6614 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6615 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 6616 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6617 | { |
| 6618 | struct hci_conn_params *p; |
| 6619 | |
| 6620 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6621 | /* Needed for AUTO_OFF case where might not "really" |
| 6622 | * have been powered off. |
| 6623 | */ |
| 6624 | list_del_init(&p->action); |
| 6625 | |
| 6626 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6627 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6628 | case HCI_AUTO_CONN_ALWAYS: |
| 6629 | list_add(&p->action, &hdev->pend_le_conns); |
| 6630 | break; |
| 6631 | case HCI_AUTO_CONN_REPORT: |
| 6632 | list_add(&p->action, &hdev->pend_le_reports); |
| 6633 | break; |
| 6634 | default: |
| 6635 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6636 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6637 | } |
| 6638 | } |
| 6639 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 6640 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6641 | { |
| 6642 | struct cmd_lookup match = { NULL, hdev }; |
| 6643 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 6644 | BT_DBG("err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6645 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 6646 | hci_dev_lock(hdev); |
| 6647 | |
| 6648 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 6649 | restart_le_actions(hdev); |
| 6650 | hci_update_background_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 6651 | } |
| 6652 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6653 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 6654 | |
| 6655 | new_settings(hdev, match.sk); |
| 6656 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6657 | if (match.sk) |
| 6658 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 6659 | |
| 6660 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6661 | } |
| 6662 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 6663 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6664 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 6665 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6666 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6667 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6668 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6669 | |
| 6670 | /* If the power off is because of hdev unregistration let |
| 6671 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 6672 | * NOT_POWERED. We cover both scenarios here since later in |
| 6673 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 6674 | * been triggered, potentially causing misleading DISCONNECTED |
| 6675 | * status responses. |
| 6676 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6677 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6678 | status = MGMT_STATUS_INVALID_INDEX; |
| 6679 | else |
| 6680 | status = MGMT_STATUS_NOT_POWERED; |
| 6681 | |
| 6682 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6683 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 6684 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 6685 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 6686 | zero_cod, sizeof(zero_cod), |
| 6687 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 6688 | ext_info_changed(hdev, NULL); |
| 6689 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6690 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 6691 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6692 | |
| 6693 | if (match.sk) |
| 6694 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6695 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6696 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6697 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6698 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6699 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6700 | u8 status; |
| 6701 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6702 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6703 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6704 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6705 | |
| 6706 | if (err == -ERFKILL) |
| 6707 | status = MGMT_STATUS_RFKILLED; |
| 6708 | else |
| 6709 | status = MGMT_STATUS_FAILED; |
| 6710 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6711 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6712 | |
| 6713 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6714 | } |
| 6715 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6716 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 6717 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6718 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 6719 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6720 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6721 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6722 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6723 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 6724 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6725 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6726 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 6727 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6728 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6729 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6730 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6731 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6732 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6733 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 6734 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6735 | switch (ltk->type) { |
| 6736 | case SMP_LTK: |
| 6737 | case SMP_LTK_SLAVE: |
| 6738 | if (ltk->authenticated) |
| 6739 | return MGMT_LTK_AUTHENTICATED; |
| 6740 | return MGMT_LTK_UNAUTHENTICATED; |
| 6741 | case SMP_LTK_P256: |
| 6742 | if (ltk->authenticated) |
| 6743 | return MGMT_LTK_P256_AUTH; |
| 6744 | return MGMT_LTK_P256_UNAUTH; |
| 6745 | case SMP_LTK_P256_DEBUG: |
| 6746 | return MGMT_LTK_P256_DEBUG; |
| 6747 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6748 | |
| 6749 | return MGMT_LTK_UNAUTHENTICATED; |
| 6750 | } |
| 6751 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6752 | void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6753 | { |
| 6754 | struct mgmt_ev_new_long_term_key ev; |
| 6755 | |
| 6756 | memset(&ev, 0, sizeof(ev)); |
| 6757 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6758 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 6759 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6760 | * to store long term keys. Their addresses will change the |
| 6761 | * next time around. |
| 6762 | * |
| 6763 | * Only when a remote device provides an identity address |
| 6764 | * make sure the long term key is stored. If the remote |
| 6765 | * identity is known, the long term keys are internally |
| 6766 | * mapped to the identity address. So allow static random |
| 6767 | * and public addresses here. |
| 6768 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6769 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6770 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6771 | ev.store_hint = 0x00; |
| 6772 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6773 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6774 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6775 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6776 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6777 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6778 | ev.key.enc_size = key->enc_size; |
| 6779 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 6780 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6781 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 6782 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6783 | ev.key.master = 1; |
| 6784 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 6785 | /* Make sure we copy only the significant bytes based on the |
| 6786 | * encryption key size, and set the rest of the value to zeroes. |
| 6787 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 6788 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 6789 | memset(ev.key.val + key->enc_size, 0, |
| 6790 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6791 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 6792 | mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6793 | } |
| 6794 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 6795 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent) |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6796 | { |
| 6797 | struct mgmt_ev_new_irk ev; |
| 6798 | |
| 6799 | memset(&ev, 0, sizeof(ev)); |
| 6800 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 6801 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6802 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6803 | bacpy(&ev.rpa, &irk->rpa); |
| 6804 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6805 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6806 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6807 | |
| 6808 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6809 | } |
| 6810 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6811 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6812 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6813 | { |
| 6814 | struct mgmt_ev_new_csrk ev; |
| 6815 | |
| 6816 | memset(&ev, 0, sizeof(ev)); |
| 6817 | |
| 6818 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 6819 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6820 | * to store signature resolving keys. Their addresses will change |
| 6821 | * the next time around. |
| 6822 | * |
| 6823 | * Only when a remote device provides an identity address |
| 6824 | * make sure the signature resolving key is stored. So allow |
| 6825 | * static random and public addresses here. |
| 6826 | */ |
| 6827 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6828 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6829 | ev.store_hint = 0x00; |
| 6830 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6831 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6832 | |
| 6833 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6834 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 6835 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6836 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6837 | |
| 6838 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6839 | } |
| 6840 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6841 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6842 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6843 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6844 | { |
| 6845 | struct mgmt_ev_new_conn_param ev; |
| 6846 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6847 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6848 | return; |
| 6849 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6850 | memset(&ev, 0, sizeof(ev)); |
| 6851 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6852 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6853 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6854 | ev.min_interval = cpu_to_le16(min_interval); |
| 6855 | ev.max_interval = cpu_to_le16(max_interval); |
| 6856 | ev.latency = cpu_to_le16(latency); |
| 6857 | ev.timeout = cpu_to_le16(timeout); |
| 6858 | |
| 6859 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6860 | } |
| 6861 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6862 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 6863 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6864 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6865 | char buf[512]; |
| 6866 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6867 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6868 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6869 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 6870 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6871 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6872 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6873 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6874 | /* We must ensure that the EIR Data fields are ordered and |
| 6875 | * unique. Keep it simple for now and avoid the problem by not |
| 6876 | * adding any BR/EDR data to the LE adv. |
| 6877 | */ |
| 6878 | if (conn->le_adv_data_len > 0) { |
| 6879 | memcpy(&ev->eir[eir_len], |
| 6880 | conn->le_adv_data, conn->le_adv_data_len); |
| 6881 | eir_len = conn->le_adv_data_len; |
| 6882 | } else { |
| 6883 | if (name_len > 0) |
| 6884 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 6885 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6886 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 6887 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6888 | eir_len = eir_append_data(ev->eir, eir_len, |
| 6889 | EIR_CLASS_OF_DEV, |
| 6890 | conn->dev_class, 3); |
| 6891 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6892 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6893 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6894 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6895 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6896 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6897 | } |
| 6898 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6899 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6900 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6901 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6902 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 6903 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6904 | |
| 6905 | *sk = cmd->sk; |
| 6906 | sock_hold(*sk); |
| 6907 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6908 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6909 | } |
| 6910 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6911 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6912 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6913 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6914 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6915 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6916 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6917 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 6918 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6919 | mgmt_pending_remove(cmd); |
| 6920 | } |
| 6921 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6922 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 6923 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6924 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6925 | struct mgmt_mode *cp; |
| 6926 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6927 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6928 | if (!cmd) |
| 6929 | return false; |
| 6930 | |
| 6931 | cp = cmd->param; |
| 6932 | if (!cp->val) |
| 6933 | return true; |
| 6934 | |
| 6935 | return false; |
| 6936 | } |
| 6937 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6938 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6939 | u8 link_type, u8 addr_type, u8 reason, |
| 6940 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6941 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6942 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6943 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6944 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6945 | /* The connection is still in hci_conn_hash so test for 1 |
| 6946 | * instead of 0 to know if this is the last one. |
| 6947 | */ |
| 6948 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 6949 | cancel_delayed_work(&hdev->power_off); |
| 6950 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6951 | } |
| 6952 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6953 | if (!mgmt_connected) |
| 6954 | return; |
| 6955 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6956 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6957 | return; |
| 6958 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6959 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6960 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6961 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6962 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6963 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6964 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6965 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6966 | |
| 6967 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6968 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6969 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6970 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6971 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6972 | } |
| 6973 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6974 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6975 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6976 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6977 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6978 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6979 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6980 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6981 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6982 | hdev); |
| 6983 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 6984 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6985 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6986 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6987 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6988 | cp = cmd->param; |
| 6989 | |
| 6990 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6991 | return; |
| 6992 | |
| 6993 | if (cp->addr.type != bdaddr_type) |
| 6994 | return; |
| 6995 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 6996 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6997 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6998 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6999 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7000 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7001 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7002 | { |
| 7003 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7004 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7005 | /* The connection is still in hci_conn_hash so test for 1 |
| 7006 | * instead of 0 to know if this is the last one. |
| 7007 | */ |
| 7008 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7009 | cancel_delayed_work(&hdev->power_off); |
| 7010 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7011 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7012 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 7013 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7014 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 7015 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7016 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7017 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7018 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7019 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7020 | void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7021 | { |
| 7022 | struct mgmt_ev_pin_code_request ev; |
| 7023 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 7024 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 7025 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 7026 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7027 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7028 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7029 | } |
| 7030 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7031 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7032 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7033 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7034 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7035 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7036 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7037 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7038 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7039 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7040 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7041 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7042 | } |
| 7043 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7044 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7045 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7046 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7047 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7048 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7049 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7050 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7051 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7052 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7053 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7054 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7055 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7056 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7057 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7058 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7059 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7060 | { |
| 7061 | struct mgmt_ev_user_confirm_request ev; |
| 7062 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7063 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7064 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7065 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7066 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 7067 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7068 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7069 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7070 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7071 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7072 | } |
| 7073 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7074 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 7075 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7076 | { |
| 7077 | struct mgmt_ev_user_passkey_request ev; |
| 7078 | |
| 7079 | BT_DBG("%s", hdev->name); |
| 7080 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7081 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7082 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7083 | |
| 7084 | return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7085 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7086 | } |
| 7087 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 7088 | static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 7089 | u8 link_type, u8 addr_type, u8 status, |
| 7090 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7091 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7092 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7093 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7094 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7095 | if (!cmd) |
| 7096 | return -ENOENT; |
| 7097 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7098 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7099 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7100 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7101 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7102 | } |
| 7103 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7104 | int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7105 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7106 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7107 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7108 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7109 | } |
| 7110 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7111 | int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7112 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7113 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7114 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7115 | status, |
| 7116 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7117 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7118 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7119 | int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7120 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7121 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7122 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7123 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7124 | } |
| 7125 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7126 | int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7127 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7128 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7129 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7130 | status, |
| 7131 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7132 | } |
| 7133 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 7134 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7135 | u8 link_type, u8 addr_type, u32 passkey, |
| 7136 | u8 entered) |
| 7137 | { |
| 7138 | struct mgmt_ev_passkey_notify ev; |
| 7139 | |
| 7140 | BT_DBG("%s", hdev->name); |
| 7141 | |
| 7142 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7143 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7144 | ev.passkey = __cpu_to_le32(passkey); |
| 7145 | ev.entered = entered; |
| 7146 | |
| 7147 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 7148 | } |
| 7149 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7150 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7151 | { |
| 7152 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7153 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7154 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7155 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7156 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 7157 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 7158 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7159 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7160 | cmd = find_pairing(conn); |
| 7161 | |
| 7162 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 7163 | cmd ? cmd->sk : NULL); |
| 7164 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 7165 | if (cmd) { |
| 7166 | cmd->cmd_complete(cmd, status); |
| 7167 | mgmt_pending_remove(cmd); |
| 7168 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7169 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7170 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7171 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7172 | { |
| 7173 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7174 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7175 | |
| 7176 | if (status) { |
| 7177 | u8 mgmt_err = mgmt_status(status); |
| 7178 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7179 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7180 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7181 | } |
| 7182 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7183 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7184 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7185 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7186 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7187 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7188 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7189 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7190 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7191 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7192 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7193 | |
| 7194 | if (match.sk) |
| 7195 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7196 | } |
| 7197 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7198 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7199 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7200 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7201 | struct hci_cp_write_eir cp; |
| 7202 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 7203 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7204 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7205 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 7206 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 7207 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7208 | memset(&cp, 0, sizeof(cp)); |
| 7209 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7210 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7211 | } |
| 7212 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7213 | void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7214 | { |
| 7215 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7216 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7217 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7218 | |
| 7219 | if (status) { |
| 7220 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7221 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7222 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 7223 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7224 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7225 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7226 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7227 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7228 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 7229 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7230 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7231 | } |
| 7232 | |
| 7233 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7234 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7235 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7236 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7237 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7238 | changed = hci_dev_test_and_clear_flag(hdev, |
| 7239 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7240 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7241 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7242 | } |
| 7243 | |
| 7244 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 7245 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7246 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7247 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7248 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 7249 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7250 | sock_put(match.sk); |
| 7251 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7252 | hci_req_init(&req, hdev); |
| 7253 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7254 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7255 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7256 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 7257 | sizeof(enable), &enable); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 7258 | __hci_req_update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7259 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7260 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7261 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7262 | |
| 7263 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7264 | } |
| 7265 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7266 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7267 | { |
| 7268 | struct cmd_lookup *match = data; |
| 7269 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7270 | if (match->sk == NULL) { |
| 7271 | match->sk = cmd->sk; |
| 7272 | sock_hold(match->sk); |
| 7273 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7274 | } |
| 7275 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 7276 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 7277 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7278 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7279 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7280 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 7281 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 7282 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 7283 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7284 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7285 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7286 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 7287 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7288 | ext_info_changed(hdev, NULL); |
| 7289 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7290 | |
| 7291 | if (match.sk) |
| 7292 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7293 | } |
| 7294 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7295 | void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7296 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7297 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7298 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7299 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7300 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7301 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7302 | |
| 7303 | memset(&ev, 0, sizeof(ev)); |
| 7304 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7305 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7306 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7307 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7308 | if (!cmd) { |
| 7309 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7310 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7311 | /* If this is a HCI command related to powering on the |
| 7312 | * HCI dev don't send any mgmt signals. |
| 7313 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7314 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7315 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7316 | } |
| 7317 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7318 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 7319 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7320 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7321 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7322 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7323 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 7324 | { |
| 7325 | int i; |
| 7326 | |
| 7327 | for (i = 0; i < uuid_count; i++) { |
| 7328 | if (!memcmp(uuid, uuids[i], 16)) |
| 7329 | return true; |
| 7330 | } |
| 7331 | |
| 7332 | return false; |
| 7333 | } |
| 7334 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7335 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 7336 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7337 | u16 parsed = 0; |
| 7338 | |
| 7339 | while (parsed < eir_len) { |
| 7340 | u8 field_len = eir[0]; |
| 7341 | u8 uuid[16]; |
| 7342 | int i; |
| 7343 | |
| 7344 | if (field_len == 0) |
| 7345 | break; |
| 7346 | |
| 7347 | if (eir_len - parsed < field_len + 1) |
| 7348 | break; |
| 7349 | |
| 7350 | switch (eir[1]) { |
| 7351 | case EIR_UUID16_ALL: |
| 7352 | case EIR_UUID16_SOME: |
| 7353 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7354 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7355 | uuid[13] = eir[i + 3]; |
| 7356 | uuid[12] = eir[i + 2]; |
| 7357 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7358 | return true; |
| 7359 | } |
| 7360 | break; |
| 7361 | case EIR_UUID32_ALL: |
| 7362 | case EIR_UUID32_SOME: |
| 7363 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7364 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7365 | uuid[15] = eir[i + 5]; |
| 7366 | uuid[14] = eir[i + 4]; |
| 7367 | uuid[13] = eir[i + 3]; |
| 7368 | uuid[12] = eir[i + 2]; |
| 7369 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7370 | return true; |
| 7371 | } |
| 7372 | break; |
| 7373 | case EIR_UUID128_ALL: |
| 7374 | case EIR_UUID128_SOME: |
| 7375 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 7376 | memcpy(uuid, eir + i + 2, 16); |
| 7377 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7378 | return true; |
| 7379 | } |
| 7380 | break; |
| 7381 | } |
| 7382 | |
| 7383 | parsed += field_len + 1; |
| 7384 | eir += field_len + 1; |
| 7385 | } |
| 7386 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7387 | return false; |
| 7388 | } |
| 7389 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7390 | static void restart_le_scan(struct hci_dev *hdev) |
| 7391 | { |
| 7392 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7393 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7394 | return; |
| 7395 | |
| 7396 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 7397 | hdev->discovery.scan_start + |
| 7398 | hdev->discovery.scan_duration)) |
| 7399 | return; |
| 7400 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 7401 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7402 | DISCOV_LE_RESTART_DELAY); |
| 7403 | } |
| 7404 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7405 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 7406 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 7407 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7408 | /* If a RSSI threshold has been specified, and |
| 7409 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 7410 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 7411 | * is set, let it through for further processing, as we might need to |
| 7412 | * restart the scan. |
| 7413 | * |
| 7414 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 7415 | * the results are also dropped. |
| 7416 | */ |
| 7417 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7418 | (rssi == HCI_RSSI_INVALID || |
| 7419 | (rssi < hdev->discovery.rssi && |
| 7420 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 7421 | return false; |
| 7422 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7423 | if (hdev->discovery.uuid_count != 0) { |
| 7424 | /* If a list of UUIDs is provided in filter, results with no |
| 7425 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7426 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7427 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 7428 | hdev->discovery.uuids) && |
| 7429 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 7430 | hdev->discovery.uuid_count, |
| 7431 | hdev->discovery.uuids)) |
| 7432 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7433 | } |
| 7434 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7435 | /* If duplicate filtering does not report RSSI changes, then restart |
| 7436 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7437 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7438 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 7439 | restart_le_scan(hdev); |
| 7440 | |
| 7441 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 7442 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7443 | rssi < hdev->discovery.rssi) |
| 7444 | return false; |
| 7445 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7446 | |
| 7447 | return true; |
| 7448 | } |
| 7449 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7450 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 7451 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 7452 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7453 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7454 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7455 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7456 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7457 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7458 | /* Don't send events for a non-kernel initiated discovery. With |
| 7459 | * LE one exception is if we have pend_le_reports > 0 in which |
| 7460 | * case we're doing passive scanning and want these events. |
| 7461 | */ |
| 7462 | if (!hci_discovery_active(hdev)) { |
| 7463 | if (link_type == ACL_LINK) |
| 7464 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 7465 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7466 | return; |
| 7467 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 7468 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 7469 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7470 | /* We are using service discovery */ |
| 7471 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 7472 | scan_rsp_len)) |
| 7473 | return; |
| 7474 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 7475 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 7476 | if (hdev->discovery.limited) { |
| 7477 | /* Check for limited discoverable bit */ |
| 7478 | if (dev_class) { |
| 7479 | if (!(dev_class[1] & 0x20)) |
| 7480 | return; |
| 7481 | } else { |
| 7482 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 7483 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 7484 | return; |
| 7485 | } |
| 7486 | } |
| 7487 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7488 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 7489 | * are for the potential CoD field. |
| 7490 | */ |
| 7491 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7492 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 7493 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7494 | memset(buf, 0, sizeof(buf)); |
| 7495 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 7496 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 7497 | * RSSI value was reported as 0 when not available. This behavior |
| 7498 | * is kept when using device discovery. This is required for full |
| 7499 | * backwards compatibility with the API. |
| 7500 | * |
| 7501 | * However when using service discovery, the value 127 will be |
| 7502 | * returned when the RSSI is not available. |
| 7503 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 7504 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 7505 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 7506 | rssi = 0; |
| 7507 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 7508 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 7509 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7510 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 7511 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7512 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7513 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7514 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7515 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7516 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 7517 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 7518 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7519 | eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7520 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7521 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7522 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7523 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7524 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7525 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7526 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 7527 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 7528 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7529 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7530 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7531 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7532 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7533 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7534 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7535 | struct mgmt_ev_device_found *ev; |
| 7536 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 7537 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7538 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7539 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7540 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7541 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7542 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7543 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7544 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7545 | ev->rssi = rssi; |
| 7546 | |
| 7547 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7548 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7549 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7550 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7551 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7552 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7553 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7554 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7555 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7556 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7557 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 7558 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 7559 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 7560 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7561 | memset(&ev, 0, sizeof(ev)); |
| 7562 | ev.type = hdev->discovery.type; |
| 7563 | ev.discovering = discovering; |
| 7564 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7565 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7566 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 7567 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7568 | static struct hci_mgmt_chan chan = { |
| 7569 | .channel = HCI_CHANNEL_CONTROL, |
| 7570 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 7571 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 7572 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7573 | }; |
| 7574 | |
| 7575 | int mgmt_init(void) |
| 7576 | { |
| 7577 | return hci_mgmt_chan_register(&chan); |
| 7578 | } |
| 7579 | |
| 7580 | void mgmt_exit(void) |
| 7581 | { |
| 7582 | hci_mgmt_chan_unregister(&chan); |
| 7583 | } |