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 | edd3896b | 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 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 620 | static u32 get_supported_phys(struct hci_dev *hdev) |
| 621 | { |
| 622 | u32 supported_phys = 0; |
| 623 | |
| 624 | if (lmp_bredr_capable(hdev)) { |
| 625 | supported_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 626 | |
| 627 | if (hdev->features[0][0] & LMP_3SLOT) |
| 628 | supported_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 629 | |
| 630 | if (hdev->features[0][0] & LMP_5SLOT) |
| 631 | supported_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 632 | |
| 633 | if (lmp_edr_2m_capable(hdev)) { |
| 634 | supported_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 635 | |
| 636 | if (lmp_edr_3slot_capable(hdev)) |
| 637 | supported_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 638 | |
| 639 | if (lmp_edr_5slot_capable(hdev)) |
| 640 | supported_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 641 | |
| 642 | if (lmp_edr_3m_capable(hdev)) { |
| 643 | supported_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 644 | |
| 645 | if (lmp_edr_3slot_capable(hdev)) |
| 646 | supported_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 647 | |
| 648 | if (lmp_edr_5slot_capable(hdev)) |
| 649 | supported_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (lmp_le_capable(hdev)) { |
| 655 | supported_phys |= MGMT_PHY_LE_1M_TX; |
| 656 | supported_phys |= MGMT_PHY_LE_1M_RX; |
| 657 | |
| 658 | if (hdev->le_features[1] & HCI_LE_PHY_2M) { |
| 659 | supported_phys |= MGMT_PHY_LE_2M_TX; |
| 660 | supported_phys |= MGMT_PHY_LE_2M_RX; |
| 661 | } |
| 662 | |
| 663 | if (hdev->le_features[1] & HCI_LE_PHY_CODED) { |
| 664 | supported_phys |= MGMT_PHY_LE_CODED_TX; |
| 665 | supported_phys |= MGMT_PHY_LE_CODED_RX; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | return supported_phys; |
| 670 | } |
| 671 | |
| 672 | static u32 get_selected_phys(struct hci_dev *hdev) |
| 673 | { |
| 674 | u32 selected_phys = 0; |
| 675 | |
| 676 | if (lmp_bredr_capable(hdev)) { |
| 677 | selected_phys |= MGMT_PHY_BR_1M_1SLOT; |
| 678 | |
| 679 | if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) |
| 680 | selected_phys |= MGMT_PHY_BR_1M_3SLOT; |
| 681 | |
| 682 | if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) |
| 683 | selected_phys |= MGMT_PHY_BR_1M_5SLOT; |
| 684 | |
| 685 | if (lmp_edr_2m_capable(hdev)) { |
| 686 | if (!(hdev->pkt_type & HCI_2DH1)) |
| 687 | selected_phys |= MGMT_PHY_EDR_2M_1SLOT; |
| 688 | |
| 689 | if (lmp_edr_3slot_capable(hdev) && |
| 690 | !(hdev->pkt_type & HCI_2DH3)) |
| 691 | selected_phys |= MGMT_PHY_EDR_2M_3SLOT; |
| 692 | |
| 693 | if (lmp_edr_5slot_capable(hdev) && |
| 694 | !(hdev->pkt_type & HCI_2DH5)) |
| 695 | selected_phys |= MGMT_PHY_EDR_2M_5SLOT; |
| 696 | |
| 697 | if (lmp_edr_3m_capable(hdev)) { |
| 698 | if (!(hdev->pkt_type & HCI_3DH1)) |
| 699 | selected_phys |= MGMT_PHY_EDR_3M_1SLOT; |
| 700 | |
| 701 | if (lmp_edr_3slot_capable(hdev) && |
| 702 | !(hdev->pkt_type & HCI_3DH3)) |
| 703 | selected_phys |= MGMT_PHY_EDR_3M_3SLOT; |
| 704 | |
| 705 | if (lmp_edr_5slot_capable(hdev) && |
| 706 | !(hdev->pkt_type & HCI_3DH5)) |
| 707 | selected_phys |= MGMT_PHY_EDR_3M_5SLOT; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | if (lmp_le_capable(hdev)) { |
| 713 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) |
| 714 | selected_phys |= MGMT_PHY_LE_1M_TX; |
| 715 | |
| 716 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) |
| 717 | selected_phys |= MGMT_PHY_LE_1M_RX; |
| 718 | |
| 719 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) |
| 720 | selected_phys |= MGMT_PHY_LE_2M_TX; |
| 721 | |
| 722 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) |
| 723 | selected_phys |= MGMT_PHY_LE_2M_RX; |
| 724 | |
| 725 | if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) |
| 726 | selected_phys |= MGMT_PHY_LE_CODED_TX; |
| 727 | |
| 728 | if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) |
| 729 | selected_phys |= MGMT_PHY_LE_CODED_RX; |
| 730 | } |
| 731 | |
| 732 | return selected_phys; |
| 733 | } |
| 734 | |
| 735 | static u32 get_configurable_phys(struct hci_dev *hdev) |
| 736 | { |
| 737 | return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & |
| 738 | ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); |
| 739 | } |
| 740 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 741 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 742 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 743 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 744 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 745 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 746 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 747 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 748 | settings |= MGMT_SETTING_CONNECTABLE; |
| 749 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 750 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 751 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 752 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 753 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 754 | settings |= MGMT_SETTING_BREDR; |
| 755 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 756 | |
| 757 | if (lmp_ssp_capable(hdev)) { |
| 758 | settings |= MGMT_SETTING_SSP; |
| 759 | settings |= MGMT_SETTING_HS; |
| 760 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 761 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 762 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 763 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 764 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 765 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 766 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 767 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 768 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 769 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 770 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 771 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 772 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 773 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 774 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 775 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 776 | settings |= MGMT_SETTING_CONFIGURATION; |
| 777 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 778 | settings |= MGMT_SETTING_PHY_CONFIGURATION; |
| 779 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 780 | return settings; |
| 781 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 782 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 783 | static u32 get_current_settings(struct hci_dev *hdev) |
| 784 | { |
| 785 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 786 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 787 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 788 | settings |= MGMT_SETTING_POWERED; |
| 789 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 790 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 791 | settings |= MGMT_SETTING_CONNECTABLE; |
| 792 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 793 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 794 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 795 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 796 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 797 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 798 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 799 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 800 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 801 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 802 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 803 | settings |= MGMT_SETTING_BREDR; |
| 804 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 805 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 806 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 807 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 808 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 809 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 810 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 811 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 812 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 813 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 814 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 815 | settings |= MGMT_SETTING_HS; |
| 816 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 817 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 818 | settings |= MGMT_SETTING_ADVERTISING; |
| 819 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 820 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 821 | settings |= MGMT_SETTING_SECURE_CONN; |
| 822 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 823 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 824 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 825 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 826 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 827 | settings |= MGMT_SETTING_PRIVACY; |
| 828 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 829 | /* The current setting for static address has two purposes. The |
| 830 | * first is to indicate if the static address will be used and |
| 831 | * the second is to indicate if it is actually set. |
| 832 | * |
| 833 | * This means if the static address is not configured, this flag |
Marcel Holtmann | 08dc0e98 | 2015-03-25 18:32:13 -0700 | [diff] [blame] | 834 | * will never be set. If the address is configured, then if the |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 835 | * address is actually used decides if the flag is set or not. |
| 836 | * |
| 837 | * For single mode LE only controllers and dual-mode controllers |
| 838 | * with BR/EDR disabled, the existence of the static address will |
| 839 | * be evaluated. |
| 840 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 841 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 842 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 843 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 844 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 845 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 846 | } |
| 847 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 848 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 849 | } |
| 850 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 851 | static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) |
| 852 | { |
| 853 | return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); |
| 854 | } |
| 855 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 856 | static struct mgmt_pending_cmd *pending_find_data(u16 opcode, |
| 857 | struct hci_dev *hdev, |
| 858 | const void *data) |
| 859 | { |
| 860 | return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data); |
| 861 | } |
| 862 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 863 | u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 864 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 865 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 866 | |
| 867 | /* If there's a pending mgmt command the flags will not yet have |
| 868 | * their final values, so check for this first. |
| 869 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 870 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 871 | if (cmd) { |
| 872 | struct mgmt_mode *cp = cmd->param; |
| 873 | if (cp->val == 0x01) |
| 874 | return LE_AD_GENERAL; |
| 875 | else if (cp->val == 0x02) |
| 876 | return LE_AD_LIMITED; |
| 877 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 878 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 879 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 880 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 881 | return LE_AD_GENERAL; |
| 882 | } |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 887 | bool mgmt_get_connectable(struct hci_dev *hdev) |
Arman Uguray | e7a685d | 2015-03-25 18:53:40 -0700 | [diff] [blame] | 888 | { |
| 889 | struct mgmt_pending_cmd *cmd; |
| 890 | |
| 891 | /* If there's a pending mgmt command the flag will not yet have |
| 892 | * it's final value, so check for this first. |
| 893 | */ |
| 894 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 895 | if (cmd) { |
| 896 | struct mgmt_mode *cp = cmd->param; |
| 897 | |
| 898 | return cp->val; |
| 899 | } |
| 900 | |
| 901 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
| 902 | } |
| 903 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 904 | static void service_cache_off(struct work_struct *work) |
| 905 | { |
| 906 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 907 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 908 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 909 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 910 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 911 | return; |
| 912 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 913 | hci_req_init(&req, hdev); |
| 914 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 915 | hci_dev_lock(hdev); |
| 916 | |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 917 | __hci_req_update_eir(&req); |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 918 | __hci_req_update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 919 | |
| 920 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 921 | |
| 922 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 923 | } |
| 924 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 925 | static void rpa_expired(struct work_struct *work) |
| 926 | { |
| 927 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 928 | rpa_expired.work); |
| 929 | struct hci_request req; |
| 930 | |
| 931 | BT_DBG(""); |
| 932 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 933 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 934 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 935 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 936 | return; |
| 937 | |
| 938 | /* The generation of a new RPA and programming it into the |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 939 | * controller happens in the hci_req_enable_advertising() |
| 940 | * function. |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 941 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 942 | hci_req_init(&req, hdev); |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame^] | 943 | if (ext_adv_capable(hdev)) |
| 944 | __hci_req_start_ext_adv(&req, hdev->cur_adv_instance); |
| 945 | else |
| 946 | __hci_req_enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 947 | hci_req_run(&req, NULL); |
| 948 | } |
| 949 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 950 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 951 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 952 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 953 | return; |
| 954 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 955 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 956 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 957 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 958 | /* Non-mgmt controlled devices get this bit set |
| 959 | * implicitly so that pairing works for them, however |
| 960 | * for mgmt we require user-space to explicitly enable |
| 961 | * it |
| 962 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 963 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 964 | } |
| 965 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 966 | 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] | 967 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 968 | { |
| 969 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 970 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 971 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 972 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 973 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 974 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 975 | memset(&rp, 0, sizeof(rp)); |
| 976 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 977 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 978 | |
| 979 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 980 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 981 | |
| 982 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 983 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 984 | |
| 985 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 986 | |
| 987 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 988 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 989 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 990 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 991 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 992 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 993 | sizeof(rp)); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 994 | } |
| 995 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 996 | static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) |
| 997 | { |
| 998 | u16 eir_len = 0; |
| 999 | size_t name_len; |
| 1000 | |
| 1001 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 1002 | eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, |
| 1003 | hdev->dev_class, 3); |
| 1004 | |
Szymon Janc | 6a9e90b | 2016-09-19 20:25:54 +0200 | [diff] [blame] | 1005 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
| 1006 | eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, |
| 1007 | hdev->appearance); |
| 1008 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1009 | name_len = strlen(hdev->dev_name); |
| 1010 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, |
| 1011 | hdev->dev_name, name_len); |
| 1012 | |
| 1013 | name_len = strlen(hdev->short_name); |
| 1014 | eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, |
| 1015 | hdev->short_name, name_len); |
| 1016 | |
| 1017 | return eir_len; |
| 1018 | } |
| 1019 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1020 | static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, |
| 1021 | void *data, u16 data_len) |
| 1022 | { |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1023 | char buf[512]; |
| 1024 | struct mgmt_rp_read_ext_info *rp = (void *)buf; |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1025 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1026 | |
| 1027 | BT_DBG("sock %p %s", sk, hdev->name); |
| 1028 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1029 | memset(&buf, 0, sizeof(buf)); |
| 1030 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1031 | hci_dev_lock(hdev); |
| 1032 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1033 | bacpy(&rp->bdaddr, &hdev->bdaddr); |
| 1034 | |
| 1035 | rp->version = hdev->hci_ver; |
| 1036 | rp->manufacturer = cpu_to_le16(hdev->manufacturer); |
| 1037 | |
| 1038 | rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1039 | rp->current_settings = cpu_to_le32(get_current_settings(hdev)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1040 | |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1041 | |
MichaĆ Narajowski | cde7a86 | 2016-09-19 20:25:53 +0200 | [diff] [blame] | 1042 | eir_len = append_eir_data_to_buf(hdev, rp->eir); |
Szymon Janc | 7d5c11d | 2016-09-19 20:25:52 +0200 | [diff] [blame] | 1043 | rp->eir_len = cpu_to_le16(eir_len); |
| 1044 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1045 | hci_dev_unlock(hdev); |
| 1046 | |
| 1047 | /* If this command is called at least once, then the events |
| 1048 | * for class of device and local name changes are disabled |
| 1049 | * and only the new extended controller information event |
| 1050 | * is used. |
| 1051 | */ |
| 1052 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); |
| 1053 | hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); |
| 1054 | hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); |
| 1055 | |
MichaĆ Narajowski | 8a0c9f4 | 2016-09-01 16:46:24 +0200 | [diff] [blame] | 1056 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, |
| 1057 | sizeof(*rp) + eir_len); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) |
| 1061 | { |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1062 | char buf[512]; |
| 1063 | struct mgmt_ev_ext_info_changed *ev = (void *)buf; |
| 1064 | u16 eir_len; |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1065 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1066 | memset(buf, 0, sizeof(buf)); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1067 | |
MichaĆ Narajowski | 5e9fae4 | 2016-09-19 20:25:55 +0200 | [diff] [blame] | 1068 | eir_len = append_eir_data_to_buf(hdev, ev->eir); |
| 1069 | ev->eir_len = cpu_to_le16(eir_len); |
| 1070 | |
| 1071 | return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, |
| 1072 | sizeof(*ev) + eir_len, |
| 1073 | HCI_MGMT_EXT_INFO_EVENTS, skip); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1076 | 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] | 1077 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1078 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1079 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1080 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1081 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1084 | 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] | 1085 | { |
| 1086 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1087 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1088 | if (hci_conn_count(hdev) == 0) { |
| 1089 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1090 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1091 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1094 | 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] | 1095 | { |
| 1096 | struct mgmt_ev_advertising_added ev; |
| 1097 | |
| 1098 | ev.instance = instance; |
| 1099 | |
| 1100 | mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); |
| 1101 | } |
| 1102 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1103 | void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, |
| 1104 | u8 instance) |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1105 | { |
| 1106 | struct mgmt_ev_advertising_removed ev; |
| 1107 | |
| 1108 | ev.instance = instance; |
| 1109 | |
| 1110 | mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 1111 | } |
| 1112 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 1113 | static void cancel_adv_timeout(struct hci_dev *hdev) |
| 1114 | { |
| 1115 | if (hdev->adv_instance_timeout) { |
| 1116 | hdev->adv_instance_timeout = 0; |
| 1117 | cancel_delayed_work(&hdev->adv_instance_expire); |
| 1118 | } |
| 1119 | } |
| 1120 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1121 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1122 | { |
| 1123 | struct hci_request req; |
| 1124 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1125 | bool discov_stopped; |
| 1126 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1127 | |
| 1128 | hci_req_init(&req, hdev); |
| 1129 | |
| 1130 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1131 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1132 | u8 scan = 0x00; |
| 1133 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1134 | } |
| 1135 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1136 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 1137 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1138 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1139 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1140 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 1141 | discov_stopped = hci_req_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1142 | |
| 1143 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 1144 | /* 0x15 == Terminated due to Power Off */ |
| 1145 | __hci_abort_conn(&req, conn, 0x15); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1148 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1149 | if (!err && discov_stopped) |
| 1150 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1151 | |
| 1152 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1153 | } |
| 1154 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1155 | 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] | 1156 | u16 len) |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1157 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1158 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1159 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1160 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1161 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1162 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1163 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1164 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1165 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1166 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1167 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1168 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1169 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1170 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1171 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1172 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1173 | goto failed; |
| 1174 | } |
| 1175 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1176 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1177 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1178 | goto failed; |
| 1179 | } |
| 1180 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1181 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1182 | if (!cmd) { |
| 1183 | err = -ENOMEM; |
| 1184 | goto failed; |
| 1185 | } |
| 1186 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1187 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1188 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1189 | err = 0; |
| 1190 | } else { |
| 1191 | /* Disconnect connections, stop scans, etc */ |
| 1192 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1193 | if (!err) |
| 1194 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1195 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1196 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1197 | /* ENODATA means there were no HCI commands queued */ |
| 1198 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1199 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1200 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1201 | err = 0; |
| 1202 | } |
| 1203 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1204 | |
| 1205 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1206 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1207 | return err; |
| 1208 | } |
| 1209 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1210 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1211 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1212 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1213 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 1214 | return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1215 | sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1218 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1219 | { |
| 1220 | return new_settings(hdev, NULL); |
| 1221 | } |
| 1222 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1223 | struct cmd_lookup { |
| 1224 | struct sock *sk; |
| 1225 | struct hci_dev *hdev; |
| 1226 | u8 mgmt_status; |
| 1227 | }; |
| 1228 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1229 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1230 | { |
| 1231 | struct cmd_lookup *match = data; |
| 1232 | |
| 1233 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1234 | |
| 1235 | list_del(&cmd->list); |
| 1236 | |
| 1237 | if (match->sk == NULL) { |
| 1238 | match->sk = cmd->sk; |
| 1239 | sock_hold(match->sk); |
| 1240 | } |
| 1241 | |
| 1242 | mgmt_pending_free(cmd); |
| 1243 | } |
| 1244 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1245 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1246 | { |
| 1247 | u8 *status = data; |
| 1248 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1249 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1250 | mgmt_pending_remove(cmd); |
| 1251 | } |
| 1252 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1253 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1254 | { |
| 1255 | if (cmd->cmd_complete) { |
| 1256 | u8 *status = data; |
| 1257 | |
| 1258 | cmd->cmd_complete(cmd, *status); |
| 1259 | mgmt_pending_remove(cmd); |
| 1260 | |
| 1261 | return; |
| 1262 | } |
| 1263 | |
| 1264 | cmd_status_rsp(cmd, data); |
| 1265 | } |
| 1266 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1267 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1268 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1269 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1270 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1271 | } |
| 1272 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1273 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1274 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1275 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1276 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1277 | } |
| 1278 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1279 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1280 | { |
| 1281 | if (!lmp_bredr_capable(hdev)) |
| 1282 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1283 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1284 | return MGMT_STATUS_REJECTED; |
| 1285 | else |
| 1286 | return MGMT_STATUS_SUCCESS; |
| 1287 | } |
| 1288 | |
| 1289 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1290 | { |
| 1291 | if (!lmp_le_capable(hdev)) |
| 1292 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1293 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1294 | return MGMT_STATUS_REJECTED; |
| 1295 | else |
| 1296 | return MGMT_STATUS_SUCCESS; |
| 1297 | } |
| 1298 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1299 | void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1300 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1301 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1302 | |
| 1303 | BT_DBG("status 0x%02x", status); |
| 1304 | |
| 1305 | hci_dev_lock(hdev); |
| 1306 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1307 | cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1308 | if (!cmd) |
| 1309 | goto unlock; |
| 1310 | |
| 1311 | if (status) { |
| 1312 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1313 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1314 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1315 | goto remove_cmd; |
| 1316 | } |
| 1317 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1318 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1319 | hdev->discov_timeout > 0) { |
| 1320 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1321 | queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1322 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1323 | |
| 1324 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1325 | new_settings(hdev, cmd->sk); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1326 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1327 | remove_cmd: |
| 1328 | mgmt_pending_remove(cmd); |
| 1329 | |
| 1330 | unlock: |
| 1331 | hci_dev_unlock(hdev); |
| 1332 | } |
| 1333 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1334 | 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] | 1335 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1336 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1337 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1338 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1339 | u16 timeout; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1340 | int err; |
| 1341 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1342 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1343 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1344 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1345 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1346 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1347 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1348 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1349 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1350 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1351 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1352 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1353 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1354 | |
| 1355 | /* Disabling discoverable requires that no timeout is set, |
| 1356 | * and enabling limited discoverable requires a timeout. |
| 1357 | */ |
| 1358 | if ((cp->val == 0x00 && timeout > 0) || |
| 1359 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1360 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1361 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1362 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1363 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1364 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1365 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1366 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1367 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1368 | goto failed; |
| 1369 | } |
| 1370 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1371 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1372 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1373 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1374 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1375 | goto failed; |
| 1376 | } |
| 1377 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1378 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1379 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1380 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1381 | goto failed; |
| 1382 | } |
| 1383 | |
| 1384 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1385 | bool changed = false; |
| 1386 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1387 | /* Setting limited discoverable when powered off is |
| 1388 | * not a valid operation since it requires a timeout |
| 1389 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1390 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1391 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1392 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1393 | changed = true; |
| 1394 | } |
| 1395 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1396 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1397 | if (err < 0) |
| 1398 | goto failed; |
| 1399 | |
| 1400 | if (changed) |
| 1401 | err = new_settings(hdev, sk); |
| 1402 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1403 | goto failed; |
| 1404 | } |
| 1405 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1406 | /* If the current mode is the same, then just update the timeout |
| 1407 | * value with the new value. And if only the timeout gets updated, |
| 1408 | * then no need for any HCI transactions. |
| 1409 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1410 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1411 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1412 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1413 | cancel_delayed_work(&hdev->discov_off); |
| 1414 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1415 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1416 | if (cp->val && hdev->discov_timeout > 0) { |
| 1417 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Johan Hedberg | c366f55 | 2015-11-23 15:43:06 +0200 | [diff] [blame] | 1418 | queue_delayed_work(hdev->req_workqueue, |
| 1419 | &hdev->discov_off, to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1420 | } |
| 1421 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1422 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1423 | goto failed; |
| 1424 | } |
| 1425 | |
| 1426 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1427 | if (!cmd) { |
| 1428 | err = -ENOMEM; |
| 1429 | goto failed; |
| 1430 | } |
| 1431 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1432 | /* Cancel any potential discoverable timeout that might be |
| 1433 | * still active and store new timeout value. The arming of |
| 1434 | * the timeout happens in the complete handler. |
| 1435 | */ |
| 1436 | cancel_delayed_work(&hdev->discov_off); |
| 1437 | hdev->discov_timeout = timeout; |
| 1438 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1439 | if (cp->val) |
| 1440 | hci_dev_set_flag(hdev, HCI_DISCOVERABLE); |
| 1441 | else |
| 1442 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1443 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1444 | /* Limited discoverable mode */ |
| 1445 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1446 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1447 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1448 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1449 | |
Johan Hedberg | aed1a88 | 2015-11-22 17:24:44 +0300 | [diff] [blame] | 1450 | queue_work(hdev->req_workqueue, &hdev->discoverable_update); |
| 1451 | err = 0; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1452 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1453 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1454 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1455 | return err; |
| 1456 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1457 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1458 | void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1459 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1460 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1461 | |
| 1462 | BT_DBG("status 0x%02x", status); |
| 1463 | |
| 1464 | hci_dev_lock(hdev); |
| 1465 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1466 | cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1467 | if (!cmd) |
| 1468 | goto unlock; |
| 1469 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1470 | if (status) { |
| 1471 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1472 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1473 | goto remove_cmd; |
| 1474 | } |
| 1475 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1476 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1477 | new_settings(hdev, cmd->sk); |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1478 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1479 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1480 | mgmt_pending_remove(cmd); |
| 1481 | |
| 1482 | unlock: |
| 1483 | hci_dev_unlock(hdev); |
| 1484 | } |
| 1485 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1486 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1487 | struct sock *sk, u8 val) |
| 1488 | { |
| 1489 | bool changed = false; |
| 1490 | int err; |
| 1491 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1492 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1493 | changed = true; |
| 1494 | |
| 1495 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1496 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1497 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1498 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1499 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1503 | if (err < 0) |
| 1504 | return err; |
| 1505 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1506 | if (changed) { |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 1507 | hci_req_update_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1508 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1509 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1510 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1511 | |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1515 | 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] | 1516 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1517 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1518 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1519 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1520 | int err; |
| 1521 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1522 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1523 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1524 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1525 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1526 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1527 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1528 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1529 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1530 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1531 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1532 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1533 | hci_dev_lock(hdev); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1534 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1535 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1536 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1537 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1538 | } |
| 1539 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1540 | if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
| 1541 | pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1542 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1543 | MGMT_STATUS_BUSY); |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1544 | goto failed; |
| 1545 | } |
| 1546 | |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1547 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1548 | if (!cmd) { |
| 1549 | err = -ENOMEM; |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1550 | goto failed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1551 | } |
| 1552 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1553 | if (cp->val) { |
| 1554 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
| 1555 | } else { |
| 1556 | if (hdev->discov_timeout > 0) |
| 1557 | cancel_delayed_work(&hdev->discov_off); |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1558 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1559 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 1560 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 1561 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1562 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1563 | |
Johan Hedberg | 53c0ba7 | 2015-11-22 16:43:43 +0300 | [diff] [blame] | 1564 | queue_work(hdev->req_workqueue, &hdev->connectable_update); |
| 1565 | err = 0; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1566 | |
| 1567 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1568 | hci_dev_unlock(hdev); |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1569 | return err; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1570 | } |
| 1571 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1572 | 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] | 1573 | u16 len) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1574 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1575 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1576 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1577 | int err; |
| 1578 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1579 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 1580 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1581 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1582 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 1583 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1584 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1585 | hci_dev_lock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1586 | |
| 1587 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1588 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1589 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1590 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1591 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1592 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1593 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1594 | goto unlock; |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1595 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1596 | if (changed) { |
| 1597 | /* In limited privacy mode the change of bondable mode |
| 1598 | * may affect the local advertising address. |
| 1599 | */ |
| 1600 | if (hdev_is_powered(hdev) && |
| 1601 | hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 1602 | hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1603 | hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY)) |
| 1604 | queue_work(hdev->req_workqueue, |
| 1605 | &hdev->discoverable_update); |
| 1606 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1607 | err = new_settings(hdev, sk); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 1608 | } |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1609 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1610 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1611 | hci_dev_unlock(hdev); |
Johan Hedberg | 053f021 | 2011-01-26 13:07:10 +0200 | [diff] [blame] | 1612 | return err; |
| 1613 | } |
| 1614 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1615 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1616 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1617 | { |
| 1618 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1619 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1620 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1621 | int err; |
| 1622 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1623 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1624 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1625 | status = mgmt_bredr_support(hdev); |
| 1626 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1627 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1628 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1629 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1630 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1631 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1632 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1633 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1634 | hci_dev_lock(hdev); |
| 1635 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1636 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1637 | bool changed = false; |
| 1638 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1639 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1640 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1641 | changed = true; |
| 1642 | } |
| 1643 | |
| 1644 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1645 | if (err < 0) |
| 1646 | goto failed; |
| 1647 | |
| 1648 | if (changed) |
| 1649 | err = new_settings(hdev, sk); |
| 1650 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1651 | goto failed; |
| 1652 | } |
| 1653 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1654 | if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1655 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1656 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1657 | goto failed; |
| 1658 | } |
| 1659 | |
| 1660 | val = !!cp->val; |
| 1661 | |
| 1662 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1663 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1664 | goto failed; |
| 1665 | } |
| 1666 | |
| 1667 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1668 | if (!cmd) { |
| 1669 | err = -ENOMEM; |
| 1670 | goto failed; |
| 1671 | } |
| 1672 | |
| 1673 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1674 | if (err < 0) { |
| 1675 | mgmt_pending_remove(cmd); |
| 1676 | goto failed; |
| 1677 | } |
| 1678 | |
| 1679 | failed: |
| 1680 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1681 | return err; |
| 1682 | } |
| 1683 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1684 | 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] | 1685 | { |
| 1686 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1687 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1688 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1689 | int err; |
| 1690 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1691 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1692 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1693 | status = mgmt_bredr_support(hdev); |
| 1694 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1695 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1696 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1697 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1698 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1699 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1700 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1701 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1702 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1703 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1704 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1705 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1706 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1707 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1708 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1709 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1710 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1711 | changed = !hci_dev_test_and_set_flag(hdev, |
| 1712 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1713 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1714 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1715 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1716 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1717 | changed = hci_dev_test_and_clear_flag(hdev, |
| 1718 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1719 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1720 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1724 | if (err < 0) |
| 1725 | goto failed; |
| 1726 | |
| 1727 | if (changed) |
| 1728 | err = new_settings(hdev, sk); |
| 1729 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1730 | goto failed; |
| 1731 | } |
| 1732 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1733 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1734 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1735 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1736 | goto failed; |
| 1737 | } |
| 1738 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1739 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1740 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1741 | goto failed; |
| 1742 | } |
| 1743 | |
| 1744 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1745 | if (!cmd) { |
| 1746 | err = -ENOMEM; |
| 1747 | goto failed; |
| 1748 | } |
| 1749 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1750 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1751 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1752 | sizeof(cp->val), &cp->val); |
| 1753 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1754 | 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] | 1755 | if (err < 0) { |
| 1756 | mgmt_pending_remove(cmd); |
| 1757 | goto failed; |
| 1758 | } |
| 1759 | |
| 1760 | failed: |
| 1761 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1762 | return err; |
| 1763 | } |
| 1764 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1765 | 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] | 1766 | { |
| 1767 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1768 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1769 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1770 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1771 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1772 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1773 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1774 | status = mgmt_bredr_support(hdev); |
| 1775 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1776 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1777 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1778 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1779 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1780 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1781 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1782 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1783 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1784 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1785 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1786 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1787 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1788 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1789 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1790 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1791 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1792 | if (pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1793 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1794 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 1795 | goto unlock; |
| 1796 | } |
| 1797 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1798 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1799 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1800 | } else { |
| 1801 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1802 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1803 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1804 | goto unlock; |
| 1805 | } |
| 1806 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1807 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1808 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1809 | |
| 1810 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1811 | if (err < 0) |
| 1812 | goto unlock; |
| 1813 | |
| 1814 | if (changed) |
| 1815 | err = new_settings(hdev, sk); |
| 1816 | |
| 1817 | unlock: |
| 1818 | hci_dev_unlock(hdev); |
| 1819 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1820 | } |
| 1821 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1822 | 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] | 1823 | { |
| 1824 | struct cmd_lookup match = { NULL, hdev }; |
| 1825 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1826 | hci_dev_lock(hdev); |
| 1827 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1828 | if (status) { |
| 1829 | u8 mgmt_err = mgmt_status(status); |
| 1830 | |
| 1831 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1832 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1833 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1837 | |
| 1838 | new_settings(hdev, match.sk); |
| 1839 | |
| 1840 | if (match.sk) |
| 1841 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1842 | |
| 1843 | /* Make sure the controller has a good default for |
| 1844 | * advertising data. Restrict the update to when LE |
| 1845 | * has actually been enabled. During power on, the |
| 1846 | * update in powered_update_hci will take care of it. |
| 1847 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1848 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1849 | struct hci_request req; |
| 1850 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1851 | hci_req_init(&req, hdev); |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 1852 | __hci_req_update_adv_data(&req, 0x00); |
| 1853 | __hci_req_update_scan_rsp_data(&req, 0x00); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1854 | hci_req_run(&req, NULL); |
Johan Hedberg | 2e93e53 | 2015-11-11 08:11:17 +0200 | [diff] [blame] | 1855 | hci_update_background_scan(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1856 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 1857 | |
| 1858 | unlock: |
| 1859 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1860 | } |
| 1861 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1862 | 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] | 1863 | { |
| 1864 | struct mgmt_mode *cp = data; |
| 1865 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1866 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1867 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1868 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1869 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1870 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1871 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1872 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1873 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1874 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1875 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1876 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1877 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1878 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1879 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1880 | |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1881 | /* Bluetooth single mode LE only controllers or dual-mode |
| 1882 | * controllers configured as LE only devices, do not allow |
| 1883 | * switching LE off. These have either LE enabled explicitly |
| 1884 | * or BR/EDR has been previously switched off. |
| 1885 | * |
| 1886 | * When trying to enable an already enabled LE, then gracefully |
| 1887 | * send a positive response. Trying to disable it however will |
| 1888 | * result into rejection. |
| 1889 | */ |
| 1890 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
| 1891 | if (cp->val == 0x01) |
| 1892 | return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1893 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1894 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1895 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | e7844ee | 2015-03-18 16:15:07 -0700 | [diff] [blame] | 1896 | } |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1897 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1898 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1899 | |
| 1900 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1901 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1902 | |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1903 | if (!val) |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 1904 | hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true); |
Florian Grandel | 847818d | 2015-06-18 03:16:46 +0200 | [diff] [blame] | 1905 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1906 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1907 | bool changed = false; |
| 1908 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1909 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1910 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1911 | changed = true; |
| 1912 | } |
| 1913 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1914 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1915 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1916 | changed = true; |
| 1917 | } |
| 1918 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1919 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1920 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1921 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1922 | |
| 1923 | if (changed) |
| 1924 | err = new_settings(hdev, sk); |
| 1925 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1926 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1927 | } |
| 1928 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 1929 | if (pending_find(MGMT_OP_SET_LE, hdev) || |
| 1930 | pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1931 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1932 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1933 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1937 | if (!cmd) { |
| 1938 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1939 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1940 | } |
| 1941 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1942 | hci_req_init(&req, hdev); |
| 1943 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1944 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1945 | |
| 1946 | if (val) { |
| 1947 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 1948 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1949 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1950 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 1951 | __hci_req_disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1952 | } |
| 1953 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1954 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 1955 | &hci_cp); |
| 1956 | |
| 1957 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1958 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1959 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1960 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1961 | unlock: |
| 1962 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1963 | return err; |
| 1964 | } |
| 1965 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1966 | /* This is a helper function to test for pending mgmt commands that can |
| 1967 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 1968 | * mgmt command at a time since otherwise we cannot easily track what |
| 1969 | * the current values are, will be, and based on that calculate if a new |
| 1970 | * HCI command needs to be sent and if yes with what value. |
| 1971 | */ |
| 1972 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 1973 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1974 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1975 | |
| 1976 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 1977 | switch (cmd->opcode) { |
| 1978 | case MGMT_OP_ADD_UUID: |
| 1979 | case MGMT_OP_REMOVE_UUID: |
| 1980 | case MGMT_OP_SET_DEV_CLASS: |
| 1981 | case MGMT_OP_SET_POWERED: |
| 1982 | return true; |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | return false; |
| 1987 | } |
| 1988 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 1989 | static const u8 bluetooth_base_uuid[] = { |
| 1990 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 1991 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1992 | }; |
| 1993 | |
| 1994 | static u8 get_uuid_size(const u8 *uuid) |
| 1995 | { |
| 1996 | u32 val; |
| 1997 | |
| 1998 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 1999 | return 128; |
| 2000 | |
| 2001 | val = get_unaligned_le32(&uuid[12]); |
| 2002 | if (val > 0xffff) |
| 2003 | return 32; |
| 2004 | |
| 2005 | return 16; |
| 2006 | } |
| 2007 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2008 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2009 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2010 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2011 | |
| 2012 | hci_dev_lock(hdev); |
| 2013 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2014 | cmd = pending_find(mgmt_op, hdev); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2015 | if (!cmd) |
| 2016 | goto unlock; |
| 2017 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2018 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2019 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2020 | |
| 2021 | mgmt_pending_remove(cmd); |
| 2022 | |
| 2023 | unlock: |
| 2024 | hci_dev_unlock(hdev); |
| 2025 | } |
| 2026 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2027 | 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] | 2028 | { |
| 2029 | BT_DBG("status 0x%02x", status); |
| 2030 | |
| 2031 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2032 | } |
| 2033 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2034 | 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] | 2035 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2036 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2037 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2038 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2039 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2040 | int err; |
| 2041 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2042 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2043 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2044 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2045 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2046 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2047 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2048 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2049 | goto failed; |
| 2050 | } |
| 2051 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2052 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2053 | if (!uuid) { |
| 2054 | err = -ENOMEM; |
| 2055 | goto failed; |
| 2056 | } |
| 2057 | |
| 2058 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2059 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2060 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2061 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2062 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2063 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2064 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2065 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2066 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2067 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2068 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2069 | err = hci_req_run(&req, add_uuid_complete); |
| 2070 | if (err < 0) { |
| 2071 | if (err != -ENODATA) |
| 2072 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2073 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2074 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2075 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2076 | goto failed; |
| 2077 | } |
| 2078 | |
| 2079 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2080 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2081 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2082 | goto failed; |
| 2083 | } |
| 2084 | |
| 2085 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2086 | |
| 2087 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2088 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2089 | return err; |
| 2090 | } |
| 2091 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2092 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2093 | { |
| 2094 | if (!hdev_is_powered(hdev)) |
| 2095 | return false; |
| 2096 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2097 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2098 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2099 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2100 | return true; |
| 2101 | } |
| 2102 | |
| 2103 | return false; |
| 2104 | } |
| 2105 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2106 | 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] | 2107 | { |
| 2108 | BT_DBG("status 0x%02x", status); |
| 2109 | |
| 2110 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2111 | } |
| 2112 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2113 | 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] | 2114 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2115 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2116 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2117 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2118 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2119 | 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] | 2120 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2121 | int err, found; |
| 2122 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2123 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2124 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2125 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2126 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2127 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2128 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2129 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2130 | goto unlock; |
| 2131 | } |
| 2132 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2133 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2134 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2135 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2136 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2137 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2138 | MGMT_OP_REMOVE_UUID, |
| 2139 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2140 | goto unlock; |
| 2141 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2142 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2143 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | found = 0; |
| 2147 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2148 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2149 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2150 | continue; |
| 2151 | |
| 2152 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2153 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2154 | found++; |
| 2155 | } |
| 2156 | |
| 2157 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2158 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2159 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2160 | goto unlock; |
| 2161 | } |
| 2162 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2163 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2164 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2165 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2166 | __hci_req_update_class(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2167 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2168 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2169 | err = hci_req_run(&req, remove_uuid_complete); |
| 2170 | if (err < 0) { |
| 2171 | if (err != -ENODATA) |
| 2172 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2173 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2174 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2175 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2176 | goto unlock; |
| 2177 | } |
| 2178 | |
| 2179 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2180 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2181 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2182 | goto unlock; |
| 2183 | } |
| 2184 | |
| 2185 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2186 | |
| 2187 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2188 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2189 | return err; |
| 2190 | } |
| 2191 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2192 | 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] | 2193 | { |
| 2194 | BT_DBG("status 0x%02x", status); |
| 2195 | |
| 2196 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2197 | } |
| 2198 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2199 | 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] | 2200 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2201 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2202 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2203 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2204 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2205 | int err; |
| 2206 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2207 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2208 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2209 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2210 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2211 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2212 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2213 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2214 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2215 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2216 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2217 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2218 | goto unlock; |
| 2219 | } |
| 2220 | |
| 2221 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2222 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2223 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2224 | goto unlock; |
| 2225 | } |
| 2226 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2227 | hdev->major_class = cp->major; |
| 2228 | hdev->minor_class = cp->minor; |
| 2229 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2230 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2231 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2232 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2233 | goto unlock; |
| 2234 | } |
| 2235 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2236 | hci_req_init(&req, hdev); |
| 2237 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2238 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2239 | hci_dev_unlock(hdev); |
| 2240 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2241 | hci_dev_lock(hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 2242 | __hci_req_update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2243 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2244 | |
Johan Hedberg | 14bf5ea | 2015-11-22 19:00:22 +0200 | [diff] [blame] | 2245 | __hci_req_update_class(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2246 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2247 | err = hci_req_run(&req, set_class_complete); |
| 2248 | if (err < 0) { |
| 2249 | if (err != -ENODATA) |
| 2250 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2251 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2252 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2253 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2254 | goto unlock; |
| 2255 | } |
| 2256 | |
| 2257 | 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] | 2258 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2259 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2260 | goto unlock; |
| 2261 | } |
| 2262 | |
| 2263 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2264 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2265 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2266 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2267 | return err; |
| 2268 | } |
| 2269 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2270 | 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] | 2271 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2272 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2273 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2274 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2275 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2276 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2277 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2278 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2279 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2280 | BT_DBG("request for %s", hdev->name); |
| 2281 | |
| 2282 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2283 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2284 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2285 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2286 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2287 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2288 | bt_dev_err(hdev, "load_link_keys: too big key_count value %u", |
| 2289 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2290 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2291 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2292 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2293 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2294 | expected_len = sizeof(*cp) + key_count * |
| 2295 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2296 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2297 | bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", |
| 2298 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2299 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2300 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2301 | } |
| 2302 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2303 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2304 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2305 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2306 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2307 | 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] | 2308 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2309 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2310 | for (i = 0; i < key_count; i++) { |
| 2311 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2312 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2313 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2314 | return mgmt_cmd_status(sk, hdev->id, |
| 2315 | MGMT_OP_LOAD_LINK_KEYS, |
| 2316 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2317 | } |
| 2318 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2319 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2320 | |
| 2321 | hci_link_keys_clear(hdev); |
| 2322 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2323 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2324 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2325 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2326 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2327 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2328 | |
| 2329 | if (changed) |
| 2330 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2331 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2332 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2333 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2334 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2335 | /* Always ignore debug keys and require a new pairing if |
| 2336 | * the user wants to use them. |
| 2337 | */ |
| 2338 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2339 | continue; |
| 2340 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2341 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2342 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2343 | } |
| 2344 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2345 | 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] | 2346 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2347 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2348 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2349 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2350 | } |
| 2351 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2352 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2353 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2354 | { |
| 2355 | struct mgmt_ev_device_unpaired ev; |
| 2356 | |
| 2357 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2358 | ev.addr.type = addr_type; |
| 2359 | |
| 2360 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2361 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2364 | 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] | 2365 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2366 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2367 | struct mgmt_cp_unpair_device *cp = data; |
| 2368 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2369 | struct hci_conn_params *params; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2370 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2371 | struct hci_conn *conn; |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2372 | u8 addr_type; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2373 | int err; |
| 2374 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2375 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2376 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2377 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2378 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2379 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2380 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2381 | MGMT_STATUS_INVALID_PARAMS, |
| 2382 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2383 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2384 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2385 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2386 | MGMT_STATUS_INVALID_PARAMS, |
| 2387 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2388 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2389 | hci_dev_lock(hdev); |
| 2390 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2391 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2392 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2393 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2394 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2395 | goto unlock; |
| 2396 | } |
| 2397 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2398 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2399 | /* If disconnection is requested, then look up the |
| 2400 | * connection. If the remote device is connected, it |
| 2401 | * will be later used to terminate the link. |
| 2402 | * |
| 2403 | * Setting it to NULL explicitly will cause no |
| 2404 | * termination of the link. |
| 2405 | */ |
| 2406 | if (cp->disconnect) |
| 2407 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2408 | &cp->addr.bdaddr); |
| 2409 | else |
| 2410 | conn = NULL; |
| 2411 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2412 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2413 | if (err < 0) { |
| 2414 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2415 | MGMT_OP_UNPAIR_DEVICE, |
| 2416 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2417 | sizeof(rp)); |
| 2418 | goto unlock; |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2421 | goto done; |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2422 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2423 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2424 | /* LE address type */ |
| 2425 | addr_type = le_addr_type(cp->addr.type); |
| 2426 | |
| 2427 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2428 | |
| 2429 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2430 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2431 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2432 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2433 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2434 | goto unlock; |
| 2435 | } |
| 2436 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2437 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); |
| 2438 | if (!conn) { |
| 2439 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2440 | goto done; |
| 2441 | } |
| 2442 | |
Johan Hedberg | c81d555 | 2015-10-22 09:38:35 +0300 | [diff] [blame] | 2443 | /* Abort any ongoing SMP pairing */ |
| 2444 | smp_cancel_pairing(conn); |
| 2445 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2446 | /* Defer clearing up the connection parameters until closing to |
| 2447 | * give a chance of keeping them if a repairing happens. |
| 2448 | */ |
| 2449 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2450 | |
Johan Hedberg | fc64361 | 2015-10-22 09:38:31 +0300 | [diff] [blame] | 2451 | /* Disable auto-connection parameters if present */ |
| 2452 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); |
| 2453 | if (params) { |
| 2454 | if (params->explicit_connect) |
| 2455 | params->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 2456 | else |
| 2457 | params->auto_connect = HCI_AUTO_CONN_DISABLED; |
| 2458 | } |
| 2459 | |
Johan Hedberg | ec182f0 | 2015-10-21 18:03:03 +0300 | [diff] [blame] | 2460 | /* If disconnection is not requested, then clear the connection |
| 2461 | * variable so that the link is not terminated. |
| 2462 | */ |
| 2463 | if (!cp->disconnect) |
| 2464 | conn = NULL; |
| 2465 | |
| 2466 | done: |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2467 | /* If the connection variable is set, then termination of the |
| 2468 | * link is requested. |
| 2469 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2470 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2471 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2472 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2473 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2474 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2475 | } |
| 2476 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2477 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2478 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2479 | if (!cmd) { |
| 2480 | err = -ENOMEM; |
| 2481 | goto unlock; |
| 2482 | } |
| 2483 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2484 | cmd->cmd_complete = addr_cmd_complete; |
| 2485 | |
Johan Hedberg | 89e0ccc | 2015-10-22 10:49:38 +0300 | [diff] [blame] | 2486 | err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2487 | if (err < 0) |
| 2488 | mgmt_pending_remove(cmd); |
| 2489 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2490 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2491 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2492 | return err; |
| 2493 | } |
| 2494 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2495 | 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] | 2496 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2497 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2498 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2499 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2500 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2501 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2502 | int err; |
| 2503 | |
| 2504 | BT_DBG(""); |
| 2505 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2506 | memset(&rp, 0, sizeof(rp)); |
| 2507 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2508 | rp.addr.type = cp->addr.type; |
| 2509 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2510 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2511 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2512 | MGMT_STATUS_INVALID_PARAMS, |
| 2513 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2514 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2515 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2516 | |
| 2517 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2518 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2519 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2520 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2521 | goto failed; |
| 2522 | } |
| 2523 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2524 | if (pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2525 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2526 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2527 | goto failed; |
| 2528 | } |
| 2529 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2530 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2531 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2532 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2533 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 2534 | conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, |
| 2535 | le_addr_type(cp->addr.type)); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 2536 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2537 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2538 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2539 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 2540 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2541 | goto failed; |
| 2542 | } |
| 2543 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2544 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2545 | if (!cmd) { |
| 2546 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2547 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2548 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2549 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 2550 | cmd->cmd_complete = generic_cmd_complete; |
| 2551 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2552 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2553 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2554 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2555 | |
| 2556 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2557 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2558 | return err; |
| 2559 | } |
| 2560 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2561 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2562 | { |
| 2563 | switch (link_type) { |
| 2564 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2565 | switch (addr_type) { |
| 2566 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2567 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2568 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2569 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2570 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2571 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2572 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2573 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2574 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2575 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2576 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2577 | } |
| 2578 | } |
| 2579 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2580 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2581 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2582 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2583 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2584 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2585 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2586 | int err; |
| 2587 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2588 | |
| 2589 | BT_DBG(""); |
| 2590 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2591 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2592 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2593 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2594 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 2595 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2596 | goto unlock; |
| 2597 | } |
| 2598 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2599 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2600 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2601 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2602 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2603 | } |
| 2604 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2605 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2606 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2607 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2608 | err = -ENOMEM; |
| 2609 | goto unlock; |
| 2610 | } |
| 2611 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2612 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2613 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2614 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2615 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2616 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2617 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2618 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2619 | continue; |
| 2620 | i++; |
| 2621 | } |
| 2622 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2623 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2624 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2625 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2626 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2627 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2628 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
| 2629 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2630 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2631 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2632 | |
| 2633 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2634 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2635 | return err; |
| 2636 | } |
| 2637 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2638 | 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] | 2639 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2640 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2641 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2642 | int err; |
| 2643 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2644 | 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] | 2645 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2646 | if (!cmd) |
| 2647 | return -ENOMEM; |
| 2648 | |
Arek Lichwa | dd7e39b | 2016-09-22 14:08:05 +0200 | [diff] [blame] | 2649 | cmd->cmd_complete = addr_cmd_complete; |
| 2650 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2651 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2652 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2653 | if (err < 0) |
| 2654 | mgmt_pending_remove(cmd); |
| 2655 | |
| 2656 | return err; |
| 2657 | } |
| 2658 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2659 | 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] | 2660 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2661 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2662 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2663 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2664 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2665 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2666 | int err; |
| 2667 | |
| 2668 | BT_DBG(""); |
| 2669 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2670 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2671 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2672 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2673 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2674 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2675 | goto failed; |
| 2676 | } |
| 2677 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2678 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2679 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2680 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2681 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2682 | goto failed; |
| 2683 | } |
| 2684 | |
| 2685 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2686 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2687 | |
| 2688 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2689 | |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 2690 | bt_dev_err(hdev, "PIN code is not 16 bytes long"); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2691 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2692 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2693 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2694 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 2695 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2696 | |
| 2697 | goto failed; |
| 2698 | } |
| 2699 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2700 | 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] | 2701 | if (!cmd) { |
| 2702 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2703 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2704 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2705 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 2706 | cmd->cmd_complete = addr_cmd_complete; |
| 2707 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2708 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2709 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2710 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2711 | |
| 2712 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2713 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2714 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2715 | |
| 2716 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2717 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2718 | return err; |
| 2719 | } |
| 2720 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2721 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2722 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2723 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2724 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2725 | |
| 2726 | BT_DBG(""); |
| 2727 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2728 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Marcel Holtmann | 9db5c62 | 2016-08-29 06:31:57 +0200 | [diff] [blame] | 2729 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2730 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2731 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2732 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2733 | |
| 2734 | hdev->io_capability = cp->io_capability; |
| 2735 | |
| 2736 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2737 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2738 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2739 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2740 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2741 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 2742 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2743 | } |
| 2744 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2745 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2746 | { |
| 2747 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2748 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2749 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2750 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2751 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2752 | continue; |
| 2753 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2754 | if (cmd->user_data != conn) |
| 2755 | continue; |
| 2756 | |
| 2757 | return cmd; |
| 2758 | } |
| 2759 | |
| 2760 | return NULL; |
| 2761 | } |
| 2762 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2763 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2764 | { |
| 2765 | struct mgmt_rp_pair_device rp; |
| 2766 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2767 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2768 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2769 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2770 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2771 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2772 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 2773 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2774 | |
| 2775 | /* So we don't get further callbacks for this connection */ |
| 2776 | conn->connect_cfm_cb = NULL; |
| 2777 | conn->security_cfm_cb = NULL; |
| 2778 | conn->disconn_cfm_cb = NULL; |
| 2779 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2780 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2781 | |
| 2782 | /* The device is paired so there is no need to remove |
| 2783 | * its connection parameters anymore. |
| 2784 | */ |
| 2785 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 2786 | |
| 2787 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 2788 | |
| 2789 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2790 | } |
| 2791 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2792 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2793 | { |
| 2794 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2795 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2796 | |
| 2797 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2798 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2799 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2800 | mgmt_pending_remove(cmd); |
| 2801 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2802 | } |
| 2803 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2804 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2805 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2806 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2807 | |
| 2808 | BT_DBG("status %u", status); |
| 2809 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2810 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2811 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2812 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2813 | return; |
| 2814 | } |
| 2815 | |
| 2816 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2817 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2818 | } |
| 2819 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2820 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2821 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2822 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2823 | |
| 2824 | BT_DBG("status %u", status); |
| 2825 | |
| 2826 | if (!status) |
| 2827 | return; |
| 2828 | |
| 2829 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2830 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2831 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2832 | return; |
| 2833 | } |
| 2834 | |
| 2835 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 2836 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2837 | } |
| 2838 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2839 | 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] | 2840 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2841 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2842 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2843 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2844 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2845 | u8 sec_level, auth_type; |
| 2846 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2847 | int err; |
| 2848 | |
| 2849 | BT_DBG(""); |
| 2850 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2851 | memset(&rp, 0, sizeof(rp)); |
| 2852 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2853 | rp.addr.type = cp->addr.type; |
| 2854 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2855 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2856 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2857 | MGMT_STATUS_INVALID_PARAMS, |
| 2858 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2859 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2860 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2861 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2862 | MGMT_STATUS_INVALID_PARAMS, |
| 2863 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2864 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2865 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2866 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2867 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2868 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2869 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2870 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2871 | goto unlock; |
| 2872 | } |
| 2873 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 2874 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 2875 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2876 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 2877 | sizeof(rp)); |
| 2878 | goto unlock; |
| 2879 | } |
| 2880 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2881 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2882 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2883 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2884 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2885 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2886 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2887 | } else { |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 2888 | u8 addr_type = le_addr_type(cp->addr.type); |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2889 | struct hci_conn_params *p; |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2890 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2891 | /* When pairing a new device, it is expected to remember |
| 2892 | * this device for future connections. Adding the connection |
| 2893 | * parameter information ahead of time allows tracking |
| 2894 | * of the slave preferred values and will speed up any |
| 2895 | * further connection establishment. |
| 2896 | * |
| 2897 | * If connection parameters already exist, then they |
| 2898 | * will be kept and this function does nothing. |
| 2899 | */ |
Jakub Pawlowski | 5157b8a | 2015-10-16 10:07:54 +0300 | [diff] [blame] | 2900 | p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2901 | |
| 2902 | if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) |
| 2903 | p->auto_connect = HCI_AUTO_CONN_DISABLED; |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2904 | |
Jakub Pawlowski | fa14222 | 2015-08-07 20:22:56 +0200 | [diff] [blame] | 2905 | conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, |
| 2906 | addr_type, sec_level, |
Johan Hedberg | 0ad06aa | 2015-11-11 14:44:57 +0200 | [diff] [blame] | 2907 | HCI_LE_CONN_TIMEOUT); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2908 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2909 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2910 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2911 | int status; |
| 2912 | |
| 2913 | if (PTR_ERR(conn) == -EBUSY) |
| 2914 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 2915 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 2916 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 2917 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 2918 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2919 | else |
| 2920 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2921 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2922 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2923 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2924 | goto unlock; |
| 2925 | } |
| 2926 | |
| 2927 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2928 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2929 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2930 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2931 | goto unlock; |
| 2932 | } |
| 2933 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2934 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2935 | if (!cmd) { |
| 2936 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2937 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2938 | goto unlock; |
| 2939 | } |
| 2940 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 2941 | cmd->cmd_complete = pairing_complete; |
| 2942 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2943 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2944 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2945 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2946 | conn->security_cfm_cb = pairing_complete_cb; |
| 2947 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2948 | } else { |
| 2949 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2950 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2951 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2952 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2953 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2954 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 2955 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2956 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 2957 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 2958 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 2959 | cmd->cmd_complete(cmd, 0); |
| 2960 | mgmt_pending_remove(cmd); |
| 2961 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2962 | |
| 2963 | err = 0; |
| 2964 | |
| 2965 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2966 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2967 | return err; |
| 2968 | } |
| 2969 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2970 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2971 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2972 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2973 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2974 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2975 | struct hci_conn *conn; |
| 2976 | int err; |
| 2977 | |
| 2978 | BT_DBG(""); |
| 2979 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2980 | hci_dev_lock(hdev); |
| 2981 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2982 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2983 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2984 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2985 | goto unlock; |
| 2986 | } |
| 2987 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 2988 | cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2989 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2990 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2991 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2992 | goto unlock; |
| 2993 | } |
| 2994 | |
| 2995 | conn = cmd->user_data; |
| 2996 | |
| 2997 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2998 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 2999 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3000 | goto unlock; |
| 3001 | } |
| 3002 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3003 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3004 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3005 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3006 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3007 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3008 | unlock: |
| 3009 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3010 | return err; |
| 3011 | } |
| 3012 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3013 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3014 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3015 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3016 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3017 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3018 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3019 | int err; |
| 3020 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3021 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3022 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3023 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3024 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3025 | MGMT_STATUS_NOT_POWERED, addr, |
| 3026 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3027 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3028 | } |
| 3029 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3030 | if (addr->type == BDADDR_BREDR) |
| 3031 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3032 | else |
Johan Hedberg | 9d4c1cc | 2015-10-21 18:03:01 +0300 | [diff] [blame] | 3033 | conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, |
| 3034 | le_addr_type(addr->type)); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3035 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3036 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3037 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3038 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3039 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3040 | goto done; |
| 3041 | } |
| 3042 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3043 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3044 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3045 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3046 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3047 | MGMT_STATUS_SUCCESS, addr, |
| 3048 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3049 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3050 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3051 | MGMT_STATUS_FAILED, addr, |
| 3052 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3053 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3054 | goto done; |
| 3055 | } |
| 3056 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3057 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3058 | if (!cmd) { |
| 3059 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3060 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3061 | } |
| 3062 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3063 | cmd->cmd_complete = addr_cmd_complete; |
| 3064 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3065 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3066 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3067 | struct hci_cp_user_passkey_reply cp; |
| 3068 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3069 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3070 | cp.passkey = passkey; |
| 3071 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3072 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3073 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3074 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3075 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3076 | if (err < 0) |
| 3077 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3078 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3079 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3080 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3081 | return err; |
| 3082 | } |
| 3083 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3084 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3085 | void *data, u16 len) |
| 3086 | { |
| 3087 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3088 | |
| 3089 | BT_DBG(""); |
| 3090 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3091 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3092 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3093 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3094 | } |
| 3095 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3096 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3097 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3098 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3099 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3100 | |
| 3101 | BT_DBG(""); |
| 3102 | |
| 3103 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3104 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3105 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3106 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3107 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3108 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3109 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3110 | } |
| 3111 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3112 | 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] | 3113 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3114 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3115 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3116 | |
| 3117 | BT_DBG(""); |
| 3118 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3119 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3120 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3121 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3122 | } |
| 3123 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3124 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3125 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3126 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3127 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3128 | |
| 3129 | BT_DBG(""); |
| 3130 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3131 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3132 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3133 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3134 | } |
| 3135 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3136 | 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] | 3137 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3138 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3139 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3140 | |
| 3141 | BT_DBG(""); |
| 3142 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3143 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3144 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3145 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3146 | } |
| 3147 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3148 | static void adv_expire(struct hci_dev *hdev, u32 flags) |
| 3149 | { |
| 3150 | struct adv_info *adv_instance; |
| 3151 | struct hci_request req; |
| 3152 | int err; |
| 3153 | |
| 3154 | adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); |
| 3155 | if (!adv_instance) |
| 3156 | return; |
| 3157 | |
| 3158 | /* stop if current instance doesn't need to be changed */ |
| 3159 | if (!(adv_instance->flags & flags)) |
| 3160 | return; |
| 3161 | |
| 3162 | cancel_adv_timeout(hdev); |
| 3163 | |
| 3164 | adv_instance = hci_get_next_instance(hdev, adv_instance->instance); |
| 3165 | if (!adv_instance) |
| 3166 | return; |
| 3167 | |
| 3168 | hci_req_init(&req, hdev); |
| 3169 | err = __hci_req_schedule_adv_instance(&req, adv_instance->instance, |
| 3170 | true); |
| 3171 | if (err) |
| 3172 | return; |
| 3173 | |
| 3174 | hci_req_run(&req, NULL); |
| 3175 | } |
| 3176 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3177 | 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] | 3178 | { |
| 3179 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3180 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3181 | |
| 3182 | BT_DBG("status 0x%02x", status); |
| 3183 | |
| 3184 | hci_dev_lock(hdev); |
| 3185 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3186 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3187 | if (!cmd) |
| 3188 | goto unlock; |
| 3189 | |
| 3190 | cp = cmd->param; |
| 3191 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3192 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3193 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3194 | mgmt_status(status)); |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3195 | } else { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3196 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3197 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3198 | |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 3199 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3200 | adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME); |
| 3201 | } |
| 3202 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3203 | mgmt_pending_remove(cmd); |
| 3204 | |
| 3205 | unlock: |
| 3206 | hci_dev_unlock(hdev); |
| 3207 | } |
| 3208 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3209 | 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] | 3210 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3211 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3212 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3213 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3214 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3215 | int err; |
| 3216 | |
| 3217 | BT_DBG(""); |
| 3218 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3219 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3220 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3221 | /* If the old values are the same as the new ones just return a |
| 3222 | * direct command complete event. |
| 3223 | */ |
| 3224 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3225 | !memcmp(hdev->short_name, cp->short_name, |
| 3226 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3227 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3228 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3229 | goto failed; |
| 3230 | } |
| 3231 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3232 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3233 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3234 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3235 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3236 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3237 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3238 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3239 | if (err < 0) |
| 3240 | goto failed; |
| 3241 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 3242 | err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, |
| 3243 | len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 3244 | ext_info_changed(hdev, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3245 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3246 | goto failed; |
| 3247 | } |
| 3248 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3249 | 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] | 3250 | if (!cmd) { |
| 3251 | err = -ENOMEM; |
| 3252 | goto failed; |
| 3253 | } |
| 3254 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3255 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3256 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3257 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3258 | |
| 3259 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 00cf504 | 2015-11-25 16:15:41 +0200 | [diff] [blame] | 3260 | __hci_req_update_name(&req); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 3261 | __hci_req_update_eir(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3262 | } |
| 3263 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3264 | /* The name is stored in the scan response data and so |
| 3265 | * no need to udpate the advertising data here. |
| 3266 | */ |
MichaĆ Narajowski | 7dc6f16 | 2016-09-22 16:01:39 +0200 | [diff] [blame] | 3267 | if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 3268 | __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3269 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3270 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3271 | if (err < 0) |
| 3272 | mgmt_pending_remove(cmd); |
| 3273 | |
| 3274 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3275 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3276 | return err; |
| 3277 | } |
| 3278 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3279 | static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3280 | u16 len) |
| 3281 | { |
| 3282 | struct mgmt_cp_set_appearance *cp = data; |
| 3283 | u16 apperance; |
| 3284 | int err; |
| 3285 | |
| 3286 | BT_DBG(""); |
| 3287 | |
MichaĆ Narajowski | af4168c | 2016-09-19 14:33:33 +0200 | [diff] [blame] | 3288 | if (!lmp_le_capable(hdev)) |
| 3289 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, |
| 3290 | MGMT_STATUS_NOT_SUPPORTED); |
| 3291 | |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3292 | apperance = le16_to_cpu(cp->appearance); |
| 3293 | |
| 3294 | hci_dev_lock(hdev); |
| 3295 | |
| 3296 | if (hdev->appearance != apperance) { |
| 3297 | hdev->appearance = apperance; |
| 3298 | |
| 3299 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
| 3300 | adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE); |
MichaĆ Narajowski | e74317f | 2016-09-19 20:25:56 +0200 | [diff] [blame] | 3301 | |
| 3302 | ext_info_changed(hdev, sk); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, |
| 3306 | 0); |
| 3307 | |
| 3308 | hci_dev_unlock(hdev); |
| 3309 | |
| 3310 | return err; |
| 3311 | } |
| 3312 | |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 3313 | static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3314 | void *data, u16 len) |
| 3315 | { |
| 3316 | struct mgmt_rp_get_phy_confguration rp; |
| 3317 | |
| 3318 | BT_DBG("sock %p %s", sk, hdev->name); |
| 3319 | |
| 3320 | hci_dev_lock(hdev); |
| 3321 | |
| 3322 | memset(&rp, 0, sizeof(rp)); |
| 3323 | |
| 3324 | rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); |
| 3325 | rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3326 | rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); |
| 3327 | |
| 3328 | hci_dev_unlock(hdev); |
| 3329 | |
| 3330 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, |
| 3331 | &rp, sizeof(rp)); |
| 3332 | } |
| 3333 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3334 | int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) |
| 3335 | { |
| 3336 | struct mgmt_ev_phy_configuration_changed ev; |
| 3337 | |
| 3338 | memset(&ev, 0, sizeof(ev)); |
| 3339 | |
| 3340 | ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); |
| 3341 | |
| 3342 | return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, |
| 3343 | sizeof(ev), skip); |
| 3344 | } |
| 3345 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3346 | static void set_default_phy_complete(struct hci_dev *hdev, u8 status, |
| 3347 | u16 opcode, struct sk_buff *skb) |
| 3348 | { |
| 3349 | struct mgmt_cp_set_phy_confguration *cp; |
| 3350 | struct mgmt_pending_cmd *cmd; |
| 3351 | |
| 3352 | BT_DBG("status 0x%02x", status); |
| 3353 | |
| 3354 | hci_dev_lock(hdev); |
| 3355 | |
| 3356 | cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev); |
| 3357 | if (!cmd) |
| 3358 | goto unlock; |
| 3359 | |
| 3360 | cp = cmd->param; |
| 3361 | |
| 3362 | if (status) { |
| 3363 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3364 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3365 | mgmt_status(status)); |
| 3366 | } else { |
| 3367 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 3368 | MGMT_OP_SET_PHY_CONFIGURATION, 0, |
| 3369 | NULL, 0); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3370 | |
| 3371 | mgmt_phy_configuration_changed(hdev, cmd->sk); |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | mgmt_pending_remove(cmd); |
| 3375 | |
| 3376 | unlock: |
| 3377 | hci_dev_unlock(hdev); |
| 3378 | } |
| 3379 | |
| 3380 | static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, |
| 3381 | void *data, u16 len) |
| 3382 | { |
| 3383 | struct mgmt_cp_set_phy_confguration *cp = data; |
| 3384 | struct hci_cp_le_set_default_phy cp_phy; |
| 3385 | struct mgmt_pending_cmd *cmd; |
| 3386 | struct hci_request req; |
| 3387 | u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; |
| 3388 | u16 pkt_type = (HCI_DH1 | HCI_DM1); |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3389 | bool changed = false; |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3390 | int err; |
| 3391 | |
| 3392 | BT_DBG("sock %p %s", sk, hdev->name); |
| 3393 | |
| 3394 | configurable_phys = get_configurable_phys(hdev); |
| 3395 | supported_phys = get_supported_phys(hdev); |
| 3396 | selected_phys = __le32_to_cpu(cp->selected_phys); |
| 3397 | |
| 3398 | if (selected_phys & ~supported_phys) |
| 3399 | return mgmt_cmd_status(sk, hdev->id, |
| 3400 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3401 | MGMT_STATUS_INVALID_PARAMS); |
| 3402 | |
| 3403 | unconfigure_phys = supported_phys & ~configurable_phys; |
| 3404 | |
| 3405 | if ((selected_phys & unconfigure_phys) != unconfigure_phys) |
| 3406 | return mgmt_cmd_status(sk, hdev->id, |
| 3407 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3408 | MGMT_STATUS_INVALID_PARAMS); |
| 3409 | |
| 3410 | if (selected_phys == get_selected_phys(hdev)) |
| 3411 | return mgmt_cmd_complete(sk, hdev->id, |
| 3412 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3413 | 0, NULL, 0); |
| 3414 | |
| 3415 | hci_dev_lock(hdev); |
| 3416 | |
| 3417 | if (!hdev_is_powered(hdev)) { |
| 3418 | err = mgmt_cmd_status(sk, hdev->id, |
| 3419 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3420 | MGMT_STATUS_REJECTED); |
| 3421 | goto unlock; |
| 3422 | } |
| 3423 | |
| 3424 | if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { |
| 3425 | err = mgmt_cmd_status(sk, hdev->id, |
| 3426 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3427 | MGMT_STATUS_BUSY); |
| 3428 | goto unlock; |
| 3429 | } |
| 3430 | |
| 3431 | if (selected_phys & MGMT_PHY_BR_1M_3SLOT) |
| 3432 | pkt_type |= (HCI_DH3 | HCI_DM3); |
| 3433 | else |
| 3434 | pkt_type &= ~(HCI_DH3 | HCI_DM3); |
| 3435 | |
| 3436 | if (selected_phys & MGMT_PHY_BR_1M_5SLOT) |
| 3437 | pkt_type |= (HCI_DH5 | HCI_DM5); |
| 3438 | else |
| 3439 | pkt_type &= ~(HCI_DH5 | HCI_DM5); |
| 3440 | |
| 3441 | if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) |
| 3442 | pkt_type &= ~HCI_2DH1; |
| 3443 | else |
| 3444 | pkt_type |= HCI_2DH1; |
| 3445 | |
| 3446 | if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) |
| 3447 | pkt_type &= ~HCI_2DH3; |
| 3448 | else |
| 3449 | pkt_type |= HCI_2DH3; |
| 3450 | |
| 3451 | if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) |
| 3452 | pkt_type &= ~HCI_2DH5; |
| 3453 | else |
| 3454 | pkt_type |= HCI_2DH5; |
| 3455 | |
| 3456 | if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) |
| 3457 | pkt_type &= ~HCI_3DH1; |
| 3458 | else |
| 3459 | pkt_type |= HCI_3DH1; |
| 3460 | |
| 3461 | if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) |
| 3462 | pkt_type &= ~HCI_3DH3; |
| 3463 | else |
| 3464 | pkt_type |= HCI_3DH3; |
| 3465 | |
| 3466 | if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) |
| 3467 | pkt_type &= ~HCI_3DH5; |
| 3468 | else |
| 3469 | pkt_type |= HCI_3DH5; |
| 3470 | |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3471 | if (pkt_type != hdev->pkt_type) { |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3472 | hdev->pkt_type = pkt_type; |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3473 | changed = true; |
| 3474 | } |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3475 | |
| 3476 | if ((selected_phys & MGMT_PHY_LE_MASK) == |
| 3477 | (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { |
Jaganath Kanakkassery | b7c23df | 2018-07-19 17:09:36 +0530 | [diff] [blame] | 3478 | if (changed) |
| 3479 | mgmt_phy_configuration_changed(hdev, sk); |
| 3480 | |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 3481 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3482 | MGMT_OP_SET_PHY_CONFIGURATION, |
| 3483 | 0, NULL, 0); |
| 3484 | |
| 3485 | goto unlock; |
| 3486 | } |
| 3487 | |
| 3488 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, |
| 3489 | len); |
| 3490 | if (!cmd) { |
| 3491 | err = -ENOMEM; |
| 3492 | goto unlock; |
| 3493 | } |
| 3494 | |
| 3495 | hci_req_init(&req, hdev); |
| 3496 | |
| 3497 | memset(&cp_phy, 0, sizeof(cp_phy)); |
| 3498 | |
| 3499 | if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) |
| 3500 | cp_phy.all_phys |= 0x01; |
| 3501 | |
| 3502 | if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) |
| 3503 | cp_phy.all_phys |= 0x02; |
| 3504 | |
| 3505 | if (selected_phys & MGMT_PHY_LE_1M_TX) |
| 3506 | cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; |
| 3507 | |
| 3508 | if (selected_phys & MGMT_PHY_LE_2M_TX) |
| 3509 | cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; |
| 3510 | |
| 3511 | if (selected_phys & MGMT_PHY_LE_CODED_TX) |
| 3512 | cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; |
| 3513 | |
| 3514 | if (selected_phys & MGMT_PHY_LE_1M_RX) |
| 3515 | cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; |
| 3516 | |
| 3517 | if (selected_phys & MGMT_PHY_LE_2M_RX) |
| 3518 | cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; |
| 3519 | |
| 3520 | if (selected_phys & MGMT_PHY_LE_CODED_RX) |
| 3521 | cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; |
| 3522 | |
| 3523 | hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy); |
| 3524 | |
| 3525 | err = hci_req_run_skb(&req, set_default_phy_complete); |
| 3526 | if (err < 0) |
| 3527 | mgmt_pending_remove(cmd); |
| 3528 | |
| 3529 | unlock: |
| 3530 | hci_dev_unlock(hdev); |
| 3531 | |
| 3532 | return err; |
| 3533 | } |
| 3534 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3535 | static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status, |
| 3536 | u16 opcode, struct sk_buff *skb) |
| 3537 | { |
| 3538 | struct mgmt_rp_read_local_oob_data mgmt_rp; |
| 3539 | size_t rp_size = sizeof(mgmt_rp); |
| 3540 | struct mgmt_pending_cmd *cmd; |
| 3541 | |
| 3542 | BT_DBG("%s status %u", hdev->name, status); |
| 3543 | |
| 3544 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
| 3545 | if (!cmd) |
| 3546 | return; |
| 3547 | |
| 3548 | if (status || !skb) { |
| 3549 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3550 | status ? mgmt_status(status) : MGMT_STATUS_FAILED); |
| 3551 | goto remove; |
| 3552 | } |
| 3553 | |
| 3554 | memset(&mgmt_rp, 0, sizeof(mgmt_rp)); |
| 3555 | |
| 3556 | if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 3557 | struct hci_rp_read_local_oob_data *rp = (void *) skb->data; |
| 3558 | |
| 3559 | if (skb->len < sizeof(*rp)) { |
| 3560 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3561 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3562 | MGMT_STATUS_FAILED); |
| 3563 | goto remove; |
| 3564 | } |
| 3565 | |
| 3566 | memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); |
| 3567 | memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); |
| 3568 | |
| 3569 | rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); |
| 3570 | } else { |
| 3571 | struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; |
| 3572 | |
| 3573 | if (skb->len < sizeof(*rp)) { |
| 3574 | mgmt_cmd_status(cmd->sk, hdev->id, |
| 3575 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3576 | MGMT_STATUS_FAILED); |
| 3577 | goto remove; |
| 3578 | } |
| 3579 | |
| 3580 | memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); |
| 3581 | memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); |
| 3582 | |
| 3583 | memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); |
| 3584 | memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); |
| 3585 | } |
| 3586 | |
| 3587 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3588 | MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); |
| 3589 | |
| 3590 | remove: |
| 3591 | mgmt_pending_remove(cmd); |
| 3592 | } |
| 3593 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3594 | 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] | 3595 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3596 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3597 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3598 | struct hci_request req; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3599 | int err; |
| 3600 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3601 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3602 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3603 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3604 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3605 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3606 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3607 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3608 | goto unlock; |
| 3609 | } |
| 3610 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3611 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3612 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3613 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3614 | goto unlock; |
| 3615 | } |
| 3616 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3617 | if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3618 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3619 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3620 | goto unlock; |
| 3621 | } |
| 3622 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3623 | 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] | 3624 | if (!cmd) { |
| 3625 | err = -ENOMEM; |
| 3626 | goto unlock; |
| 3627 | } |
| 3628 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3629 | hci_req_init(&req, hdev); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3630 | |
Johan Hedberg | 1b9441f | 2015-04-02 13:41:13 +0300 | [diff] [blame] | 3631 | if (bredr_sc_enabled(hdev)) |
| 3632 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 3633 | else |
| 3634 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3635 | |
| 3636 | err = hci_req_run_skb(&req, read_local_oob_data_complete); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3637 | if (err < 0) |
| 3638 | mgmt_pending_remove(cmd); |
| 3639 | |
| 3640 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3641 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3642 | return err; |
| 3643 | } |
| 3644 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3645 | 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] | 3646 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3647 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3648 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3649 | int err; |
| 3650 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3651 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3652 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3653 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3654 | return mgmt_cmd_complete(sk, hdev->id, |
| 3655 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3656 | MGMT_STATUS_INVALID_PARAMS, |
| 3657 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3658 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3659 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3660 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3661 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3662 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3663 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3664 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3665 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3666 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3667 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3668 | MGMT_STATUS_INVALID_PARAMS, |
| 3669 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3670 | goto unlock; |
| 3671 | } |
| 3672 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3673 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3674 | cp->addr.type, cp->hash, |
| 3675 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3676 | if (err < 0) |
| 3677 | status = MGMT_STATUS_FAILED; |
| 3678 | else |
| 3679 | status = MGMT_STATUS_SUCCESS; |
| 3680 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3681 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3682 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 3683 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3684 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3685 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3686 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3687 | u8 status; |
| 3688 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3689 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3690 | /* Enforce zero-valued 192-bit parameters as |
| 3691 | * long as legacy SMP OOB isn't implemented. |
| 3692 | */ |
| 3693 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3694 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3695 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3696 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3697 | MGMT_STATUS_INVALID_PARAMS, |
| 3698 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3699 | goto unlock; |
| 3700 | } |
| 3701 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3702 | rand192 = NULL; |
| 3703 | hash192 = NULL; |
| 3704 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3705 | /* In case one of the P-192 values is set to zero, |
| 3706 | * then just disable OOB data for P-192. |
| 3707 | */ |
| 3708 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3709 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 3710 | rand192 = NULL; |
| 3711 | hash192 = NULL; |
| 3712 | } else { |
| 3713 | rand192 = cp->rand192; |
| 3714 | hash192 = cp->hash192; |
| 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | /* In case one of the P-256 values is set to zero, then just |
| 3719 | * disable OOB data for P-256. |
| 3720 | */ |
| 3721 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 3722 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 3723 | rand256 = NULL; |
| 3724 | hash256 = NULL; |
| 3725 | } else { |
| 3726 | rand256 = cp->rand256; |
| 3727 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3728 | } |
| 3729 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 3730 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3731 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3732 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3733 | if (err < 0) |
| 3734 | status = MGMT_STATUS_FAILED; |
| 3735 | else |
| 3736 | status = MGMT_STATUS_SUCCESS; |
| 3737 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3738 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3739 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3740 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3741 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 3742 | bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", |
| 3743 | len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3744 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3745 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3746 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3747 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3748 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3749 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3750 | return err; |
| 3751 | } |
| 3752 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3753 | 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] | 3754 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3755 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3756 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3757 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3758 | int err; |
| 3759 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3760 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3761 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3762 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3763 | return mgmt_cmd_complete(sk, hdev->id, |
| 3764 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3765 | MGMT_STATUS_INVALID_PARAMS, |
| 3766 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3767 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3768 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3769 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3770 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 3771 | hci_remote_oob_data_clear(hdev); |
| 3772 | status = MGMT_STATUS_SUCCESS; |
| 3773 | goto done; |
| 3774 | } |
| 3775 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3776 | 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] | 3777 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3778 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3779 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3780 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3781 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3782 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3783 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3784 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3785 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3786 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3787 | return err; |
| 3788 | } |
| 3789 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3790 | void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3791 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3792 | struct mgmt_pending_cmd *cmd; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3793 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3794 | BT_DBG("status %d", status); |
| 3795 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3796 | hci_dev_lock(hdev); |
| 3797 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3798 | cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3799 | if (!cmd) |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 3800 | cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3801 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3802 | if (!cmd) |
| 3803 | cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev); |
| 3804 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3805 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3806 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3807 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3808 | } |
| 3809 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3810 | hci_dev_unlock(hdev); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3811 | } |
| 3812 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3813 | static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, |
| 3814 | uint8_t *mgmt_status) |
| 3815 | { |
| 3816 | switch (type) { |
| 3817 | case DISCOV_TYPE_LE: |
| 3818 | *mgmt_status = mgmt_le_support(hdev); |
| 3819 | if (*mgmt_status) |
| 3820 | return false; |
| 3821 | break; |
| 3822 | case DISCOV_TYPE_INTERLEAVED: |
| 3823 | *mgmt_status = mgmt_le_support(hdev); |
| 3824 | if (*mgmt_status) |
| 3825 | return false; |
| 3826 | /* Intentional fall-through */ |
| 3827 | case DISCOV_TYPE_BREDR: |
| 3828 | *mgmt_status = mgmt_bredr_support(hdev); |
| 3829 | if (*mgmt_status) |
| 3830 | return false; |
| 3831 | break; |
| 3832 | default: |
| 3833 | *mgmt_status = MGMT_STATUS_INVALID_PARAMS; |
| 3834 | return false; |
| 3835 | } |
| 3836 | |
| 3837 | return true; |
| 3838 | } |
| 3839 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3840 | static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, |
| 3841 | u16 op, void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3842 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3843 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3844 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3845 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3846 | int err; |
| 3847 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3848 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3849 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3850 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3851 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3852 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3853 | err = mgmt_cmd_complete(sk, hdev->id, op, |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3854 | MGMT_STATUS_NOT_POWERED, |
| 3855 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3856 | goto failed; |
| 3857 | } |
| 3858 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3859 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3860 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3861 | err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, |
| 3862 | &cp->type, sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3863 | goto failed; |
| 3864 | } |
| 3865 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3866 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3867 | err = mgmt_cmd_complete(sk, hdev->id, op, status, |
| 3868 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3869 | goto failed; |
| 3870 | } |
| 3871 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 3872 | /* Clear the discovery filter first to free any previously |
| 3873 | * allocated memory for the UUID list. |
| 3874 | */ |
| 3875 | hci_discovery_filter_clear(hdev); |
| 3876 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3877 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 3878 | hdev->discovery.report_invalid_rssi = false; |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3879 | if (op == MGMT_OP_START_LIMITED_DISCOVERY) |
| 3880 | hdev->discovery.limited = true; |
| 3881 | else |
| 3882 | hdev->discovery.limited = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3883 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3884 | cmd = mgmt_pending_add(sk, op, hdev, data, len); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3885 | if (!cmd) { |
| 3886 | err = -ENOMEM; |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3887 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3888 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3889 | |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3890 | cmd->cmd_complete = generic_cmd_complete; |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3891 | |
| 3892 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 3893 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 3894 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3895 | |
| 3896 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3897 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3898 | return err; |
| 3899 | } |
| 3900 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 3901 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
| 3902 | void *data, u16 len) |
| 3903 | { |
| 3904 | return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, |
| 3905 | data, len); |
| 3906 | } |
| 3907 | |
| 3908 | static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, |
| 3909 | void *data, u16 len) |
| 3910 | { |
| 3911 | return start_discovery_internal(sk, hdev, |
| 3912 | MGMT_OP_START_LIMITED_DISCOVERY, |
| 3913 | data, len); |
| 3914 | } |
| 3915 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3916 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 3917 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3918 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3919 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 3920 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3921 | } |
| 3922 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3923 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 3924 | void *data, u16 len) |
| 3925 | { |
| 3926 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3927 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3928 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 3929 | u16 uuid_count, expected_len; |
| 3930 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3931 | int err; |
| 3932 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3933 | BT_DBG("%s", hdev->name); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3934 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3935 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3936 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3937 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3938 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3939 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3940 | MGMT_STATUS_NOT_POWERED, |
| 3941 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3942 | goto failed; |
| 3943 | } |
| 3944 | |
| 3945 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3946 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3947 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3948 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3949 | MGMT_STATUS_BUSY, &cp->type, |
| 3950 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3951 | goto failed; |
| 3952 | } |
| 3953 | |
| 3954 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 3955 | if (uuid_count > max_uuid_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 3956 | bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", |
| 3957 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3958 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3959 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3960 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 3961 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3962 | goto failed; |
| 3963 | } |
| 3964 | |
| 3965 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 3966 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 3967 | bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", |
| 3968 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3969 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3970 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3971 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 3972 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3973 | goto failed; |
| 3974 | } |
| 3975 | |
Johan Hedberg | 591752a | 2015-11-11 08:11:24 +0200 | [diff] [blame] | 3976 | if (!discovery_type_is_valid(hdev, cp->type, &status)) { |
| 3977 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3978 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3979 | status, &cp->type, sizeof(cp->type)); |
| 3980 | goto failed; |
| 3981 | } |
| 3982 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3983 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3984 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3985 | if (!cmd) { |
| 3986 | err = -ENOMEM; |
| 3987 | goto failed; |
| 3988 | } |
| 3989 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 3990 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 3991 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 3992 | /* Clear the discovery filter first to free any previously |
| 3993 | * allocated memory for the UUID list. |
| 3994 | */ |
| 3995 | hci_discovery_filter_clear(hdev); |
| 3996 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 3997 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3998 | hdev->discovery.type = cp->type; |
| 3999 | hdev->discovery.rssi = cp->rssi; |
| 4000 | hdev->discovery.uuid_count = uuid_count; |
| 4001 | |
| 4002 | if (uuid_count > 0) { |
| 4003 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 4004 | GFP_KERNEL); |
| 4005 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4006 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4007 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4008 | MGMT_STATUS_FAILED, |
| 4009 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4010 | mgmt_pending_remove(cmd); |
| 4011 | goto failed; |
| 4012 | } |
| 4013 | } |
| 4014 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4015 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | e68f072 | 2015-11-11 08:30:30 +0200 | [diff] [blame] | 4016 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4017 | err = 0; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4018 | |
| 4019 | failed: |
| 4020 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4021 | return err; |
| 4022 | } |
| 4023 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4024 | void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4025 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4026 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4027 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4028 | BT_DBG("status %d", status); |
| 4029 | |
| 4030 | hci_dev_lock(hdev); |
| 4031 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4032 | cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4033 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4034 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4035 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4036 | } |
| 4037 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4038 | hci_dev_unlock(hdev); |
| 4039 | } |
| 4040 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4041 | 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] | 4042 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4043 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4044 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4045 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4046 | int err; |
| 4047 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4048 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4049 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4050 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4051 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4052 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4053 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4054 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 4055 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4056 | goto unlock; |
| 4057 | } |
| 4058 | |
| 4059 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4060 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4061 | MGMT_STATUS_INVALID_PARAMS, |
| 4062 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4063 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 4064 | } |
| 4065 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4066 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4067 | if (!cmd) { |
| 4068 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4069 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4070 | } |
| 4071 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4072 | cmd->cmd_complete = generic_cmd_complete; |
| 4073 | |
Johan Hedberg | 2154d3f | 2015-11-11 08:30:45 +0200 | [diff] [blame] | 4074 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 4075 | queue_work(hdev->req_workqueue, &hdev->discov_update); |
| 4076 | err = 0; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4077 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4078 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4079 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4080 | return err; |
| 4081 | } |
| 4082 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4083 | 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] | 4084 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4085 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4086 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4087 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4088 | int err; |
| 4089 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4090 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4091 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4092 | hci_dev_lock(hdev); |
| 4093 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4094 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4095 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4096 | MGMT_STATUS_FAILED, &cp->addr, |
| 4097 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4098 | goto failed; |
| 4099 | } |
| 4100 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 4101 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4102 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4103 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4104 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 4105 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4106 | goto failed; |
| 4107 | } |
| 4108 | |
| 4109 | if (cp->name_known) { |
| 4110 | e->name_state = NAME_KNOWN; |
| 4111 | list_del(&e->list); |
| 4112 | } else { |
| 4113 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 4114 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4115 | } |
| 4116 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4117 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 4118 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4119 | |
| 4120 | failed: |
| 4121 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4122 | return err; |
| 4123 | } |
| 4124 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4125 | 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] | 4126 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4127 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4128 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4129 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4130 | int err; |
| 4131 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4132 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4133 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4134 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4135 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 4136 | MGMT_STATUS_INVALID_PARAMS, |
| 4137 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4138 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4139 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4140 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4141 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 4142 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4143 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4144 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4145 | goto done; |
| 4146 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4147 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4148 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4149 | sk); |
| 4150 | status = MGMT_STATUS_SUCCESS; |
| 4151 | |
| 4152 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4153 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 4154 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4155 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4156 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4157 | |
| 4158 | return err; |
| 4159 | } |
| 4160 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4161 | 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] | 4162 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4163 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4164 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4165 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4166 | int err; |
| 4167 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4168 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4169 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4170 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4171 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 4172 | MGMT_STATUS_INVALID_PARAMS, |
| 4173 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4174 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4175 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4176 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4177 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 4178 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4179 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4180 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4181 | goto done; |
| 4182 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4183 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4184 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4185 | sk); |
| 4186 | status = MGMT_STATUS_SUCCESS; |
| 4187 | |
| 4188 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4189 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 4190 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4191 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4192 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4193 | |
| 4194 | return err; |
| 4195 | } |
| 4196 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4197 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4198 | u16 len) |
| 4199 | { |
| 4200 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4201 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4202 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4203 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4204 | |
| 4205 | BT_DBG("%s", hdev->name); |
| 4206 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4207 | source = __le16_to_cpu(cp->source); |
| 4208 | |
| 4209 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4210 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4211 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4212 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4213 | hci_dev_lock(hdev); |
| 4214 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4215 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4216 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4217 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4218 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4219 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4220 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 4221 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4222 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4223 | hci_req_init(&req, hdev); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 4224 | __hci_req_update_eir(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4225 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4226 | |
| 4227 | hci_dev_unlock(hdev); |
| 4228 | |
| 4229 | return err; |
| 4230 | } |
| 4231 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4232 | static void enable_advertising_instance(struct hci_dev *hdev, u8 status, |
| 4233 | u16 opcode) |
| 4234 | { |
| 4235 | BT_DBG("status %d", status); |
| 4236 | } |
| 4237 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4238 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 4239 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4240 | { |
| 4241 | struct cmd_lookup match = { NULL, hdev }; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4242 | struct hci_request req; |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4243 | u8 instance; |
| 4244 | struct adv_info *adv_instance; |
| 4245 | int err; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4246 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4247 | hci_dev_lock(hdev); |
| 4248 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4249 | if (status) { |
| 4250 | u8 mgmt_err = mgmt_status(status); |
| 4251 | |
| 4252 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4253 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4254 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4255 | } |
| 4256 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4257 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4258 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4259 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4260 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4261 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4262 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4263 | &match); |
| 4264 | |
| 4265 | new_settings(hdev, match.sk); |
| 4266 | |
| 4267 | if (match.sk) |
| 4268 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4269 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4270 | /* If "Set Advertising" was just disabled and instance advertising was |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4271 | * set up earlier, then re-enable multi-instance advertising. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4272 | */ |
| 4273 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4274 | list_empty(&hdev->adv_instances)) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4275 | goto unlock; |
| 4276 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4277 | instance = hdev->cur_adv_instance; |
| 4278 | if (!instance) { |
| 4279 | adv_instance = list_first_entry_or_null(&hdev->adv_instances, |
| 4280 | struct adv_info, list); |
| 4281 | if (!adv_instance) |
| 4282 | goto unlock; |
| 4283 | |
| 4284 | instance = adv_instance->instance; |
| 4285 | } |
| 4286 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4287 | hci_req_init(&req, hdev); |
| 4288 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4289 | err = __hci_req_schedule_adv_instance(&req, instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4290 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4291 | if (!err) |
| 4292 | err = hci_req_run(&req, enable_advertising_instance); |
| 4293 | |
| 4294 | if (err) |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 4295 | bt_dev_err(hdev, "failed to re-configure advertising"); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4296 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4297 | unlock: |
| 4298 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4299 | } |
| 4300 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4301 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4302 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4303 | { |
| 4304 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4305 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4306 | struct hci_request req; |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4307 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4308 | int err; |
| 4309 | |
| 4310 | BT_DBG("request for %s", hdev->name); |
| 4311 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4312 | status = mgmt_le_support(hdev); |
| 4313 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4314 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4315 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4316 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4317 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4318 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4319 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4320 | |
| 4321 | hci_dev_lock(hdev); |
| 4322 | |
| 4323 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4324 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4325 | /* The following conditions are ones which mean that we should |
| 4326 | * not do any HCI communication but directly send a mgmt |
| 4327 | * response to user space (after toggling the flag if |
| 4328 | * necessary). |
| 4329 | */ |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4330 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4331 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 4332 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4333 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4334 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4335 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4336 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4337 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4338 | if (cp->val) { |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 4339 | hdev->cur_adv_instance = 0x00; |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4340 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4341 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4342 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4343 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4344 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4345 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4346 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4347 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4351 | if (err < 0) |
| 4352 | goto unlock; |
| 4353 | |
| 4354 | if (changed) |
| 4355 | err = new_settings(hdev, sk); |
| 4356 | |
| 4357 | goto unlock; |
| 4358 | } |
| 4359 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4360 | if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4361 | pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4362 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4363 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4364 | goto unlock; |
| 4365 | } |
| 4366 | |
| 4367 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4368 | if (!cmd) { |
| 4369 | err = -ENOMEM; |
| 4370 | goto unlock; |
| 4371 | } |
| 4372 | |
| 4373 | hci_req_init(&req, hdev); |
| 4374 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4375 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4376 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4377 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4378 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4379 | |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4380 | cancel_adv_timeout(hdev); |
| 4381 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4382 | if (val) { |
Florian Grandel | 7816b82 | 2015-06-18 03:16:45 +0200 | [diff] [blame] | 4383 | /* Switch to instance "0" for the Set Advertising setting. |
| 4384 | * We cannot use update_[adv|scan_rsp]_data() here as the |
| 4385 | * HCI_ADVERTISING flag is not yet set. |
| 4386 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 4387 | hdev->cur_adv_instance = 0x00; |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame^] | 4388 | |
| 4389 | if (ext_adv_capable(hdev)) { |
| 4390 | __hci_req_start_ext_adv(&req, 0x00); |
| 4391 | } else { |
| 4392 | __hci_req_update_adv_data(&req, 0x00); |
| 4393 | __hci_req_update_scan_rsp_data(&req, 0x00); |
| 4394 | __hci_req_enable_advertising(&req); |
| 4395 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4396 | } else { |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4397 | __hci_req_disable_advertising(&req); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 4398 | } |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4399 | |
| 4400 | err = hci_req_run(&req, set_advertising_complete); |
| 4401 | if (err < 0) |
| 4402 | mgmt_pending_remove(cmd); |
| 4403 | |
| 4404 | unlock: |
| 4405 | hci_dev_unlock(hdev); |
| 4406 | return err; |
| 4407 | } |
| 4408 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4409 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4410 | void *data, u16 len) |
| 4411 | { |
| 4412 | struct mgmt_cp_set_static_address *cp = data; |
| 4413 | int err; |
| 4414 | |
| 4415 | BT_DBG("%s", hdev->name); |
| 4416 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4417 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4418 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4419 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4420 | |
| 4421 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4422 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4423 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4424 | |
| 4425 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4426 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4427 | return mgmt_cmd_status(sk, hdev->id, |
| 4428 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4429 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4430 | |
| 4431 | /* Two most significant bits shall be set */ |
| 4432 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4433 | return mgmt_cmd_status(sk, hdev->id, |
| 4434 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4435 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | hci_dev_lock(hdev); |
| 4439 | |
| 4440 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4441 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4442 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 4443 | if (err < 0) |
| 4444 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4445 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4446 | err = new_settings(hdev, sk); |
| 4447 | |
| 4448 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4449 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4450 | return err; |
| 4451 | } |
| 4452 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4453 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4454 | void *data, u16 len) |
| 4455 | { |
| 4456 | struct mgmt_cp_set_scan_params *cp = data; |
| 4457 | __u16 interval, window; |
| 4458 | int err; |
| 4459 | |
| 4460 | BT_DBG("%s", hdev->name); |
| 4461 | |
| 4462 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4463 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4464 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4465 | |
| 4466 | interval = __le16_to_cpu(cp->interval); |
| 4467 | |
| 4468 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4469 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4470 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4471 | |
| 4472 | window = __le16_to_cpu(cp->window); |
| 4473 | |
| 4474 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4475 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4476 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4477 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4478 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4479 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4480 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4481 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4482 | hci_dev_lock(hdev); |
| 4483 | |
| 4484 | hdev->le_scan_interval = interval; |
| 4485 | hdev->le_scan_window = window; |
| 4486 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4487 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 4488 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4489 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4490 | /* If background scan is running, restart it so new parameters are |
| 4491 | * loaded. |
| 4492 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4493 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4494 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4495 | struct hci_request req; |
| 4496 | |
| 4497 | hci_req_init(&req, hdev); |
| 4498 | |
| 4499 | hci_req_add_le_scan_disable(&req); |
| 4500 | hci_req_add_le_passive_scan(&req); |
| 4501 | |
| 4502 | hci_req_run(&req, NULL); |
| 4503 | } |
| 4504 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4505 | hci_dev_unlock(hdev); |
| 4506 | |
| 4507 | return err; |
| 4508 | } |
| 4509 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4510 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 4511 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4512 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4513 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4514 | |
| 4515 | BT_DBG("status 0x%02x", status); |
| 4516 | |
| 4517 | hci_dev_lock(hdev); |
| 4518 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4519 | cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4520 | if (!cmd) |
| 4521 | goto unlock; |
| 4522 | |
| 4523 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4524 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4525 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4526 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4527 | struct mgmt_mode *cp = cmd->param; |
| 4528 | |
| 4529 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4530 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4531 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4532 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4533 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4534 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4535 | new_settings(hdev, cmd->sk); |
| 4536 | } |
| 4537 | |
| 4538 | mgmt_pending_remove(cmd); |
| 4539 | |
| 4540 | unlock: |
| 4541 | hci_dev_unlock(hdev); |
| 4542 | } |
| 4543 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4544 | 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] | 4545 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4546 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4547 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4548 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4549 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4550 | int err; |
| 4551 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4552 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4553 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4554 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4555 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4556 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4557 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4558 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4559 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4560 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4561 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4562 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4563 | hci_dev_lock(hdev); |
| 4564 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4565 | if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4566 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4567 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4568 | goto unlock; |
| 4569 | } |
| 4570 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4571 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4572 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4573 | hdev); |
| 4574 | goto unlock; |
| 4575 | } |
| 4576 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4577 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4578 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4579 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4580 | hdev); |
| 4581 | new_settings(hdev, sk); |
| 4582 | goto unlock; |
| 4583 | } |
| 4584 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4585 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4586 | data, len); |
| 4587 | if (!cmd) { |
| 4588 | err = -ENOMEM; |
| 4589 | goto unlock; |
| 4590 | } |
| 4591 | |
| 4592 | hci_req_init(&req, hdev); |
| 4593 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 4594 | __hci_req_write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4595 | |
| 4596 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4597 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4598 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4599 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4600 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4601 | } |
| 4602 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4603 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4604 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4605 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4606 | return err; |
| 4607 | } |
| 4608 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4609 | 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] | 4610 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4611 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4612 | |
| 4613 | BT_DBG("status 0x%02x", status); |
| 4614 | |
| 4615 | hci_dev_lock(hdev); |
| 4616 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4617 | cmd = pending_find(MGMT_OP_SET_BREDR, hdev); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4618 | if (!cmd) |
| 4619 | goto unlock; |
| 4620 | |
| 4621 | if (status) { |
| 4622 | u8 mgmt_err = mgmt_status(status); |
| 4623 | |
| 4624 | /* We need to restore the flag if related HCI commands |
| 4625 | * failed. |
| 4626 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4627 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4628 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4629 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4630 | } else { |
| 4631 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4632 | new_settings(hdev, cmd->sk); |
| 4633 | } |
| 4634 | |
| 4635 | mgmt_pending_remove(cmd); |
| 4636 | |
| 4637 | unlock: |
| 4638 | hci_dev_unlock(hdev); |
| 4639 | } |
| 4640 | |
| 4641 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4642 | { |
| 4643 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4644 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4645 | struct hci_request req; |
| 4646 | int err; |
| 4647 | |
| 4648 | BT_DBG("request for %s", hdev->name); |
| 4649 | |
| 4650 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4651 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4652 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4653 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4654 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4655 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4656 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4657 | |
| 4658 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4659 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4660 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4661 | |
| 4662 | hci_dev_lock(hdev); |
| 4663 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4664 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4665 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4666 | goto unlock; |
| 4667 | } |
| 4668 | |
| 4669 | if (!hdev_is_powered(hdev)) { |
| 4670 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4671 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 4672 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 4673 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 4674 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 4675 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4676 | } |
| 4677 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4678 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4679 | |
| 4680 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4681 | if (err < 0) |
| 4682 | goto unlock; |
| 4683 | |
| 4684 | err = new_settings(hdev, sk); |
| 4685 | goto unlock; |
| 4686 | } |
| 4687 | |
| 4688 | /* Reject disabling when powered on */ |
| 4689 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4690 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4691 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4692 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4693 | } else { |
| 4694 | /* When configuring a dual-mode controller to operate |
| 4695 | * with LE only and using a static address, then switching |
| 4696 | * BR/EDR back on is not allowed. |
| 4697 | * |
| 4698 | * Dual-mode controllers shall operate with the public |
| 4699 | * address as its identity address for BR/EDR and LE. So |
| 4700 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4701 | * |
| 4702 | * The same restrictions applies when secure connections |
| 4703 | * has been enabled. For BR/EDR this is a controller feature |
| 4704 | * while for LE it is a host stack feature. This means that |
| 4705 | * switching BR/EDR back on when secure connections has been |
| 4706 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4707 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4708 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4709 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4710 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4711 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4712 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4713 | goto unlock; |
| 4714 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4715 | } |
| 4716 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4717 | if (pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4718 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4719 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4720 | goto unlock; |
| 4721 | } |
| 4722 | |
| 4723 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4724 | if (!cmd) { |
| 4725 | err = -ENOMEM; |
| 4726 | goto unlock; |
| 4727 | } |
| 4728 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 4729 | /* We need to flip the bit already here so that |
| 4730 | * hci_req_update_adv_data generates the correct flags. |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4731 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4732 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4733 | |
| 4734 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4735 | |
Johan Hedberg | bf943cb | 2015-11-25 16:15:43 +0200 | [diff] [blame] | 4736 | __hci_req_write_fast_connectable(&req, false); |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 4737 | __hci_req_update_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4738 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4739 | /* Since only the advertising data flags will change, there |
| 4740 | * is no need to update the scan response data. |
| 4741 | */ |
Johan Hedberg | cab054a | 2015-11-30 11:21:45 +0200 | [diff] [blame] | 4742 | __hci_req_update_adv_data(&req, hdev->cur_adv_instance); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4743 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4744 | err = hci_req_run(&req, set_bredr_complete); |
| 4745 | if (err < 0) |
| 4746 | mgmt_pending_remove(cmd); |
| 4747 | |
| 4748 | unlock: |
| 4749 | hci_dev_unlock(hdev); |
| 4750 | return err; |
| 4751 | } |
| 4752 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4753 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 4754 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4755 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4756 | struct mgmt_mode *cp; |
| 4757 | |
| 4758 | BT_DBG("%s status %u", hdev->name, status); |
| 4759 | |
| 4760 | hci_dev_lock(hdev); |
| 4761 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4762 | cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4763 | if (!cmd) |
| 4764 | goto unlock; |
| 4765 | |
| 4766 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4767 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 4768 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4769 | goto remove; |
| 4770 | } |
| 4771 | |
| 4772 | cp = cmd->param; |
| 4773 | |
| 4774 | switch (cp->val) { |
| 4775 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4776 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 4777 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4778 | break; |
| 4779 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4780 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4781 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4782 | break; |
| 4783 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4784 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 4785 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4786 | break; |
| 4787 | } |
| 4788 | |
| 4789 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4790 | new_settings(hdev, cmd->sk); |
| 4791 | |
| 4792 | remove: |
| 4793 | mgmt_pending_remove(cmd); |
| 4794 | unlock: |
| 4795 | hci_dev_unlock(hdev); |
| 4796 | } |
| 4797 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4798 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4799 | void *data, u16 len) |
| 4800 | { |
| 4801 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4802 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4803 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4804 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4805 | int err; |
| 4806 | |
| 4807 | BT_DBG("request for %s", hdev->name); |
| 4808 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4809 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4810 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4811 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4812 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4813 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4814 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 4815 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4816 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4817 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4818 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 4819 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4820 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4821 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4822 | MGMT_STATUS_INVALID_PARAMS); |
| 4823 | |
| 4824 | hci_dev_lock(hdev); |
| 4825 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 4826 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4827 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4828 | bool changed; |
| 4829 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4830 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4831 | changed = !hci_dev_test_and_set_flag(hdev, |
| 4832 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4833 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4834 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4835 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4836 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4837 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4838 | changed = hci_dev_test_and_clear_flag(hdev, |
| 4839 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4840 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4841 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4842 | |
| 4843 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4844 | if (err < 0) |
| 4845 | goto failed; |
| 4846 | |
| 4847 | if (changed) |
| 4848 | err = new_settings(hdev, sk); |
| 4849 | |
| 4850 | goto failed; |
| 4851 | } |
| 4852 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 4853 | if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4854 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4855 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4856 | goto failed; |
| 4857 | } |
| 4858 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4859 | val = !!cp->val; |
| 4860 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4861 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 4862 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4863 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4864 | goto failed; |
| 4865 | } |
| 4866 | |
| 4867 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4868 | if (!cmd) { |
| 4869 | err = -ENOMEM; |
| 4870 | goto failed; |
| 4871 | } |
| 4872 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4873 | hci_req_init(&req, hdev); |
| 4874 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 4875 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4876 | if (err < 0) { |
| 4877 | mgmt_pending_remove(cmd); |
| 4878 | goto failed; |
| 4879 | } |
| 4880 | |
| 4881 | failed: |
| 4882 | hci_dev_unlock(hdev); |
| 4883 | return err; |
| 4884 | } |
| 4885 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4886 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4887 | void *data, u16 len) |
| 4888 | { |
| 4889 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4890 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4891 | int err; |
| 4892 | |
| 4893 | BT_DBG("request for %s", hdev->name); |
| 4894 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4895 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4896 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4897 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4898 | |
| 4899 | hci_dev_lock(hdev); |
| 4900 | |
| 4901 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4902 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4903 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4904 | changed = hci_dev_test_and_clear_flag(hdev, |
| 4905 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4906 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4907 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4908 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 4909 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4910 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4911 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 4912 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4913 | |
| 4914 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4915 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4916 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4917 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4918 | sizeof(mode), &mode); |
| 4919 | } |
| 4920 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4921 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4922 | if (err < 0) |
| 4923 | goto unlock; |
| 4924 | |
| 4925 | if (changed) |
| 4926 | err = new_settings(hdev, sk); |
| 4927 | |
| 4928 | unlock: |
| 4929 | hci_dev_unlock(hdev); |
| 4930 | return err; |
| 4931 | } |
| 4932 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4933 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4934 | u16 len) |
| 4935 | { |
| 4936 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4937 | bool changed; |
| 4938 | int err; |
| 4939 | |
| 4940 | BT_DBG("request for %s", hdev->name); |
| 4941 | |
| 4942 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4943 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4944 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4945 | |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 4946 | if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4947 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4948 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4949 | |
| 4950 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4951 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4952 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4953 | |
| 4954 | hci_dev_lock(hdev); |
| 4955 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4956 | /* If user space supports this command it is also expected to |
| 4957 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4958 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4959 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4960 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4961 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4962 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4963 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4964 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 4965 | if (cp->privacy == 0x02) |
| 4966 | hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); |
| 4967 | else |
| 4968 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4969 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4970 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4971 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4972 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 82a37ad | 2016-03-09 17:30:34 +0200 | [diff] [blame] | 4973 | hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4977 | if (err < 0) |
| 4978 | goto unlock; |
| 4979 | |
| 4980 | if (changed) |
| 4981 | err = new_settings(hdev, sk); |
| 4982 | |
| 4983 | unlock: |
| 4984 | hci_dev_unlock(hdev); |
| 4985 | return err; |
| 4986 | } |
| 4987 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4988 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4989 | { |
| 4990 | switch (irk->addr.type) { |
| 4991 | case BDADDR_LE_PUBLIC: |
| 4992 | return true; |
| 4993 | |
| 4994 | case BDADDR_LE_RANDOM: |
| 4995 | /* Two most significant bits shall be set */ |
| 4996 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4997 | return false; |
| 4998 | return true; |
| 4999 | } |
| 5000 | |
| 5001 | return false; |
| 5002 | } |
| 5003 | |
| 5004 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5005 | u16 len) |
| 5006 | { |
| 5007 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5008 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 5009 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5010 | u16 irk_count, expected_len; |
| 5011 | int i, err; |
| 5012 | |
| 5013 | BT_DBG("request for %s", hdev->name); |
| 5014 | |
| 5015 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5016 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5017 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5018 | |
| 5019 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5020 | if (irk_count > max_irk_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5021 | bt_dev_err(hdev, "load_irks: too big irk_count value %u", |
| 5022 | irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5023 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5024 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5025 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5026 | |
| 5027 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 5028 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5029 | bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", |
| 5030 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5031 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5032 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 5036 | |
| 5037 | for (i = 0; i < irk_count; i++) { |
| 5038 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 5039 | |
| 5040 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5041 | return mgmt_cmd_status(sk, hdev->id, |
| 5042 | MGMT_OP_LOAD_IRKS, |
| 5043 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5044 | } |
| 5045 | |
| 5046 | hci_dev_lock(hdev); |
| 5047 | |
| 5048 | hci_smp_irks_clear(hdev); |
| 5049 | |
| 5050 | for (i = 0; i < irk_count; i++) { |
| 5051 | struct mgmt_irk_info *irk = &cp->irks[i]; |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5052 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5053 | hci_add_irk(hdev, &irk->addr.bdaddr, |
| 5054 | le_addr_type(irk->addr.type), irk->val, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5055 | BDADDR_ANY); |
| 5056 | } |
| 5057 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5058 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5059 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5060 | 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] | 5061 | |
| 5062 | hci_dev_unlock(hdev); |
| 5063 | |
| 5064 | return err; |
| 5065 | } |
| 5066 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5067 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 5068 | { |
| 5069 | if (key->master != 0x00 && key->master != 0x01) |
| 5070 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 5071 | |
| 5072 | switch (key->addr.type) { |
| 5073 | case BDADDR_LE_PUBLIC: |
| 5074 | return true; |
| 5075 | |
| 5076 | case BDADDR_LE_RANDOM: |
| 5077 | /* Two most significant bits shall be set */ |
| 5078 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5079 | return false; |
| 5080 | return true; |
| 5081 | } |
| 5082 | |
| 5083 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5084 | } |
| 5085 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5086 | 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] | 5087 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5088 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5089 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5090 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 5091 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5092 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5093 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5094 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5095 | BT_DBG("request for %s", hdev->name); |
| 5096 | |
| 5097 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5098 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5099 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5100 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5101 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5102 | if (key_count > max_key_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5103 | bt_dev_err(hdev, "load_ltks: too big key_count value %u", |
| 5104 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5105 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5106 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5107 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5108 | |
| 5109 | expected_len = sizeof(*cp) + key_count * |
| 5110 | sizeof(struct mgmt_ltk_info); |
| 5111 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5112 | bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", |
| 5113 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5114 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5115 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5116 | } |
| 5117 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5118 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5119 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5120 | for (i = 0; i < key_count; i++) { |
| 5121 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 5122 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5123 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5124 | return mgmt_cmd_status(sk, hdev->id, |
| 5125 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5126 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5127 | } |
| 5128 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5129 | hci_dev_lock(hdev); |
| 5130 | |
| 5131 | hci_smp_ltks_clear(hdev); |
| 5132 | |
| 5133 | for (i = 0; i < key_count; i++) { |
| 5134 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5135 | u8 type, authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5136 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5137 | switch (key->type) { |
| 5138 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5139 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5140 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5141 | break; |
| 5142 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5143 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5144 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5145 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5146 | case MGMT_LTK_P256_UNAUTH: |
| 5147 | authenticated = 0x00; |
| 5148 | type = SMP_LTK_P256; |
| 5149 | break; |
| 5150 | case MGMT_LTK_P256_AUTH: |
| 5151 | authenticated = 0x01; |
| 5152 | type = SMP_LTK_P256; |
| 5153 | break; |
| 5154 | case MGMT_LTK_P256_DEBUG: |
| 5155 | authenticated = 0x00; |
| 5156 | type = SMP_LTK_P256_DEBUG; |
Gustavo A. R. Silva | 9ea47132 | 2018-03-30 16:05:06 -0500 | [diff] [blame] | 5157 | /* fall through */ |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5158 | default: |
| 5159 | continue; |
| 5160 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5161 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5162 | hci_add_ltk(hdev, &key->addr.bdaddr, |
| 5163 | le_addr_type(key->addr.type), type, authenticated, |
| 5164 | key->val, key->enc_size, key->ediv, key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5165 | } |
| 5166 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5167 | 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] | 5168 | NULL, 0); |
| 5169 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5170 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5171 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5172 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5173 | } |
| 5174 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5175 | 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] | 5176 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5177 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5178 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5179 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5180 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5181 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5182 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5183 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5184 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5185 | rp.tx_power = conn->tx_power; |
| 5186 | rp.max_tx_power = conn->max_tx_power; |
| 5187 | } else { |
| 5188 | rp.rssi = HCI_RSSI_INVALID; |
| 5189 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 5190 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5191 | } |
| 5192 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5193 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 5194 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5195 | |
| 5196 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5197 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5198 | |
| 5199 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5200 | } |
| 5201 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5202 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 5203 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5204 | { |
| 5205 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5206 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5207 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5208 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5209 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5210 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5211 | BT_DBG("status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5212 | |
| 5213 | hci_dev_lock(hdev); |
| 5214 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5215 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 5216 | * Level so we check which one was last sent to retrieve connection |
| 5217 | * handle. Both commands have handle as first parameter so it's safe to |
| 5218 | * cast data on the same command struct. |
| 5219 | * |
| 5220 | * First command sent is always Read RSSI and we fail only if it fails. |
| 5221 | * In other case we simply override error to indicate success as we |
| 5222 | * already remembered if TX power value is actually valid. |
| 5223 | */ |
| 5224 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 5225 | if (!cp) { |
| 5226 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5227 | status = MGMT_STATUS_SUCCESS; |
| 5228 | } else { |
| 5229 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5230 | } |
| 5231 | |
| 5232 | if (!cp) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5233 | bt_dev_err(hdev, "invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5234 | goto unlock; |
| 5235 | } |
| 5236 | |
| 5237 | handle = __le16_to_cpu(cp->handle); |
| 5238 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5239 | if (!conn) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5240 | bt_dev_err(hdev, "unknown handle (%d) in conn_info response", |
| 5241 | handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5242 | goto unlock; |
| 5243 | } |
| 5244 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5245 | cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5246 | if (!cmd) |
| 5247 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5248 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5249 | cmd->cmd_complete(cmd, status); |
| 5250 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5251 | |
| 5252 | unlock: |
| 5253 | hci_dev_unlock(hdev); |
| 5254 | } |
| 5255 | |
| 5256 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5257 | u16 len) |
| 5258 | { |
| 5259 | struct mgmt_cp_get_conn_info *cp = data; |
| 5260 | struct mgmt_rp_get_conn_info rp; |
| 5261 | struct hci_conn *conn; |
| 5262 | unsigned long conn_info_age; |
| 5263 | int err = 0; |
| 5264 | |
| 5265 | BT_DBG("%s", hdev->name); |
| 5266 | |
| 5267 | memset(&rp, 0, sizeof(rp)); |
| 5268 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5269 | rp.addr.type = cp->addr.type; |
| 5270 | |
| 5271 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5272 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5273 | MGMT_STATUS_INVALID_PARAMS, |
| 5274 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5275 | |
| 5276 | hci_dev_lock(hdev); |
| 5277 | |
| 5278 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5279 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5280 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5281 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5282 | goto unlock; |
| 5283 | } |
| 5284 | |
| 5285 | if (cp->addr.type == BDADDR_BREDR) |
| 5286 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5287 | &cp->addr.bdaddr); |
| 5288 | else |
| 5289 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 5290 | |
| 5291 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5292 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5293 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 5294 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5295 | goto unlock; |
| 5296 | } |
| 5297 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5298 | if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5299 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5300 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5301 | goto unlock; |
| 5302 | } |
| 5303 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5304 | /* To avoid client trying to guess when to poll again for information we |
| 5305 | * calculate conn info age as random value between min/max set in hdev. |
| 5306 | */ |
| 5307 | conn_info_age = hdev->conn_info_min_age + |
| 5308 | prandom_u32_max(hdev->conn_info_max_age - |
| 5309 | hdev->conn_info_min_age); |
| 5310 | |
| 5311 | /* Query controller to refresh cached values if they are too old or were |
| 5312 | * never read. |
| 5313 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5314 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5315 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5316 | !conn->conn_info_timestamp) { |
| 5317 | struct hci_request req; |
| 5318 | struct hci_cp_read_tx_power req_txp_cp; |
| 5319 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5320 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5321 | |
| 5322 | hci_req_init(&req, hdev); |
| 5323 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5324 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5325 | &req_rssi_cp); |
| 5326 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5327 | /* For LE links TX power does not change thus we don't need to |
| 5328 | * query for it once value is known. |
| 5329 | */ |
| 5330 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5331 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5332 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5333 | req_txp_cp.type = 0x00; |
| 5334 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5335 | sizeof(req_txp_cp), &req_txp_cp); |
| 5336 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5337 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5338 | /* Max TX power needs to be read only once per connection */ |
| 5339 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5340 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5341 | req_txp_cp.type = 0x01; |
| 5342 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5343 | sizeof(req_txp_cp), &req_txp_cp); |
| 5344 | } |
| 5345 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5346 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5347 | if (err < 0) |
| 5348 | goto unlock; |
| 5349 | |
| 5350 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5351 | data, len); |
| 5352 | if (!cmd) { |
| 5353 | err = -ENOMEM; |
| 5354 | goto unlock; |
| 5355 | } |
| 5356 | |
| 5357 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5358 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5359 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5360 | |
| 5361 | conn->conn_info_timestamp = jiffies; |
| 5362 | } else { |
| 5363 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5364 | rp.rssi = conn->rssi; |
| 5365 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5366 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5367 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5368 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5369 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5370 | } |
| 5371 | |
| 5372 | unlock: |
| 5373 | hci_dev_unlock(hdev); |
| 5374 | return err; |
| 5375 | } |
| 5376 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5377 | 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] | 5378 | { |
| 5379 | struct hci_conn *conn = cmd->user_data; |
| 5380 | struct mgmt_rp_get_clock_info rp; |
| 5381 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5382 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5383 | |
| 5384 | memset(&rp, 0, sizeof(rp)); |
Marcel Holtmann | 56f787c | 2016-08-29 06:19:47 +0200 | [diff] [blame] | 5385 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5386 | |
| 5387 | if (status) |
| 5388 | goto complete; |
| 5389 | |
| 5390 | hdev = hci_dev_get(cmd->index); |
| 5391 | if (hdev) { |
| 5392 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5393 | hci_dev_put(hdev); |
| 5394 | } |
| 5395 | |
| 5396 | if (conn) { |
| 5397 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5398 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5399 | } |
| 5400 | |
| 5401 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5402 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 5403 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5404 | |
| 5405 | if (conn) { |
| 5406 | hci_conn_drop(conn); |
| 5407 | hci_conn_put(conn); |
| 5408 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5409 | |
| 5410 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5411 | } |
| 5412 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5413 | 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] | 5414 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5415 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5416 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5417 | struct hci_conn *conn; |
| 5418 | |
| 5419 | BT_DBG("%s status %u", hdev->name, status); |
| 5420 | |
| 5421 | hci_dev_lock(hdev); |
| 5422 | |
| 5423 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5424 | if (!hci_cp) |
| 5425 | goto unlock; |
| 5426 | |
| 5427 | if (hci_cp->which) { |
| 5428 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5429 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5430 | } else { |
| 5431 | conn = NULL; |
| 5432 | } |
| 5433 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 5434 | cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5435 | if (!cmd) |
| 5436 | goto unlock; |
| 5437 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5438 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5439 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5440 | |
| 5441 | unlock: |
| 5442 | hci_dev_unlock(hdev); |
| 5443 | } |
| 5444 | |
| 5445 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5446 | u16 len) |
| 5447 | { |
| 5448 | struct mgmt_cp_get_clock_info *cp = data; |
| 5449 | struct mgmt_rp_get_clock_info rp; |
| 5450 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5451 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5452 | struct hci_request req; |
| 5453 | struct hci_conn *conn; |
| 5454 | int err; |
| 5455 | |
| 5456 | BT_DBG("%s", hdev->name); |
| 5457 | |
| 5458 | memset(&rp, 0, sizeof(rp)); |
| 5459 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5460 | rp.addr.type = cp->addr.type; |
| 5461 | |
| 5462 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5463 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5464 | MGMT_STATUS_INVALID_PARAMS, |
| 5465 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5466 | |
| 5467 | hci_dev_lock(hdev); |
| 5468 | |
| 5469 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5470 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5471 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5472 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5473 | goto unlock; |
| 5474 | } |
| 5475 | |
| 5476 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5477 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5478 | &cp->addr.bdaddr); |
| 5479 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5480 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5481 | MGMT_OP_GET_CLOCK_INFO, |
| 5482 | MGMT_STATUS_NOT_CONNECTED, |
| 5483 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5484 | goto unlock; |
| 5485 | } |
| 5486 | } else { |
| 5487 | conn = NULL; |
| 5488 | } |
| 5489 | |
| 5490 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5491 | if (!cmd) { |
| 5492 | err = -ENOMEM; |
| 5493 | goto unlock; |
| 5494 | } |
| 5495 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5496 | cmd->cmd_complete = clock_info_cmd_complete; |
| 5497 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5498 | hci_req_init(&req, hdev); |
| 5499 | |
| 5500 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5501 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5502 | |
| 5503 | if (conn) { |
| 5504 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5505 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5506 | |
| 5507 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5508 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5509 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5510 | } |
| 5511 | |
| 5512 | err = hci_req_run(&req, get_clock_info_complete); |
| 5513 | if (err < 0) |
| 5514 | mgmt_pending_remove(cmd); |
| 5515 | |
| 5516 | unlock: |
| 5517 | hci_dev_unlock(hdev); |
| 5518 | return err; |
| 5519 | } |
| 5520 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5521 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 5522 | { |
| 5523 | struct hci_conn *conn; |
| 5524 | |
| 5525 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 5526 | if (!conn) |
| 5527 | return false; |
| 5528 | |
| 5529 | if (conn->dst_type != type) |
| 5530 | return false; |
| 5531 | |
| 5532 | if (conn->state != BT_CONNECTED) |
| 5533 | return false; |
| 5534 | |
| 5535 | return true; |
| 5536 | } |
| 5537 | |
| 5538 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5539 | 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] | 5540 | u8 addr_type, u8 auto_connect) |
| 5541 | { |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5542 | struct hci_conn_params *params; |
| 5543 | |
| 5544 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 5545 | if (!params) |
| 5546 | return -EIO; |
| 5547 | |
| 5548 | if (params->auto_connect == auto_connect) |
| 5549 | return 0; |
| 5550 | |
| 5551 | list_del_init(¶ms->action); |
| 5552 | |
| 5553 | switch (auto_connect) { |
| 5554 | case HCI_AUTO_CONN_DISABLED: |
| 5555 | case HCI_AUTO_CONN_LINK_LOSS: |
Jakub Pawlowski | 28a667c | 2015-08-07 20:22:54 +0200 | [diff] [blame] | 5556 | /* If auto connect is being disabled when we're trying to |
| 5557 | * connect to device, keep connecting. |
| 5558 | */ |
| 5559 | if (params->explicit_connect) |
| 5560 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5561 | break; |
| 5562 | case HCI_AUTO_CONN_REPORT: |
Johan Hedberg | 49c5092 | 2015-10-16 10:07:51 +0300 | [diff] [blame] | 5563 | if (params->explicit_connect) |
| 5564 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 5565 | else |
| 5566 | list_add(¶ms->action, &hdev->pend_le_reports); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5567 | break; |
| 5568 | case HCI_AUTO_CONN_DIRECT: |
| 5569 | case HCI_AUTO_CONN_ALWAYS: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5570 | if (!is_connected(hdev, addr, addr_type)) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5571 | list_add(¶ms->action, &hdev->pend_le_conns); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5572 | break; |
| 5573 | } |
| 5574 | |
| 5575 | params->auto_connect = auto_connect; |
| 5576 | |
| 5577 | BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, |
| 5578 | auto_connect); |
| 5579 | |
| 5580 | return 0; |
| 5581 | } |
| 5582 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5583 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5584 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5585 | { |
| 5586 | struct mgmt_ev_device_added ev; |
| 5587 | |
| 5588 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5589 | ev.addr.type = type; |
| 5590 | ev.action = action; |
| 5591 | |
| 5592 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5593 | } |
| 5594 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5595 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5596 | void *data, u16 len) |
| 5597 | { |
| 5598 | struct mgmt_cp_add_device *cp = data; |
| 5599 | u8 auto_conn, addr_type; |
| 5600 | int err; |
| 5601 | |
| 5602 | BT_DBG("%s", hdev->name); |
| 5603 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5604 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5605 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5606 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5607 | MGMT_STATUS_INVALID_PARAMS, |
| 5608 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5609 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5610 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5611 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5612 | MGMT_STATUS_INVALID_PARAMS, |
| 5613 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5614 | |
| 5615 | hci_dev_lock(hdev); |
| 5616 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5617 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5618 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5619 | if (cp->action != 0x01) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5620 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5621 | MGMT_OP_ADD_DEVICE, |
| 5622 | MGMT_STATUS_INVALID_PARAMS, |
| 5623 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5624 | goto unlock; |
| 5625 | } |
| 5626 | |
| 5627 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5628 | cp->addr.type); |
| 5629 | if (err) |
| 5630 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5631 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5632 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5633 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5634 | goto added; |
| 5635 | } |
| 5636 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5637 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5638 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5639 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5640 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5641 | else if (cp->action == 0x01) |
| 5642 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5643 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5644 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5645 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5646 | /* Kernel internally uses conn_params with resolvable private |
| 5647 | * address, but Add Device allows only identity addresses. |
| 5648 | * Make sure it is enforced before calling |
| 5649 | * hci_conn_params_lookup. |
| 5650 | */ |
| 5651 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5652 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5653 | MGMT_STATUS_INVALID_PARAMS, |
| 5654 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5655 | goto unlock; |
| 5656 | } |
| 5657 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5658 | /* If the connection parameters don't exist for this device, |
| 5659 | * they will be created and configured with defaults. |
| 5660 | */ |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5661 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5662 | auto_conn) < 0) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5663 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5664 | MGMT_STATUS_FAILED, &cp->addr, |
| 5665 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5666 | goto unlock; |
| 5667 | } |
| 5668 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5669 | hci_update_background_scan(hdev); |
| 5670 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5671 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5672 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5673 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5674 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5675 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 5676 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5677 | |
| 5678 | unlock: |
| 5679 | hci_dev_unlock(hdev); |
| 5680 | return err; |
| 5681 | } |
| 5682 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5683 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5684 | bdaddr_t *bdaddr, u8 type) |
| 5685 | { |
| 5686 | struct mgmt_ev_device_removed ev; |
| 5687 | |
| 5688 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5689 | ev.addr.type = type; |
| 5690 | |
| 5691 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5692 | } |
| 5693 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5694 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5695 | void *data, u16 len) |
| 5696 | { |
| 5697 | struct mgmt_cp_remove_device *cp = data; |
| 5698 | int err; |
| 5699 | |
| 5700 | BT_DBG("%s", hdev->name); |
| 5701 | |
| 5702 | hci_dev_lock(hdev); |
| 5703 | |
| 5704 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5705 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5706 | u8 addr_type; |
| 5707 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5708 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5709 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5710 | MGMT_OP_REMOVE_DEVICE, |
| 5711 | MGMT_STATUS_INVALID_PARAMS, |
| 5712 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5713 | goto unlock; |
| 5714 | } |
| 5715 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5716 | if (cp->addr.type == BDADDR_BREDR) { |
| 5717 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5718 | &cp->addr.bdaddr, |
| 5719 | cp->addr.type); |
| 5720 | if (err) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5721 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5722 | MGMT_OP_REMOVE_DEVICE, |
| 5723 | MGMT_STATUS_INVALID_PARAMS, |
| 5724 | &cp->addr, |
| 5725 | sizeof(cp->addr)); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5726 | goto unlock; |
| 5727 | } |
| 5728 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5729 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5730 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5731 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5732 | cp->addr.type); |
| 5733 | goto complete; |
| 5734 | } |
| 5735 | |
Johan Hedberg | 85813a7 | 2015-10-21 18:02:59 +0300 | [diff] [blame] | 5736 | addr_type = le_addr_type(cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5737 | |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5738 | /* Kernel internally uses conn_params with resolvable private |
| 5739 | * address, but Remove Device allows only identity addresses. |
| 5740 | * Make sure it is enforced before calling |
| 5741 | * hci_conn_params_lookup. |
| 5742 | */ |
| 5743 | if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5744 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5745 | MGMT_OP_REMOVE_DEVICE, |
| 5746 | MGMT_STATUS_INVALID_PARAMS, |
| 5747 | &cp->addr, sizeof(cp->addr)); |
Jakub Pawlowski | 9a0a8a8 | 2015-07-20 13:12:49 +0200 | [diff] [blame] | 5748 | goto unlock; |
| 5749 | } |
| 5750 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5751 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5752 | addr_type); |
| 5753 | if (!params) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5754 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5755 | MGMT_OP_REMOVE_DEVICE, |
| 5756 | MGMT_STATUS_INVALID_PARAMS, |
| 5757 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5758 | goto unlock; |
| 5759 | } |
| 5760 | |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 5761 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED || |
| 5762 | params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5763 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5764 | MGMT_OP_REMOVE_DEVICE, |
| 5765 | MGMT_STATUS_INVALID_PARAMS, |
| 5766 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5767 | goto unlock; |
| 5768 | } |
| 5769 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5770 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5771 | list_del(¶ms->list); |
| 5772 | kfree(params); |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5773 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5774 | |
| 5775 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5776 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5777 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5778 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5779 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5780 | if (cp->addr.type) { |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5781 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5782 | MGMT_OP_REMOVE_DEVICE, |
| 5783 | MGMT_STATUS_INVALID_PARAMS, |
| 5784 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5785 | goto unlock; |
| 5786 | } |
| 5787 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5788 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 5789 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 5790 | list_del(&b->list); |
| 5791 | kfree(b); |
| 5792 | } |
| 5793 | |
Johan Hedberg | 01b1cb8 | 2015-11-16 12:52:21 +0200 | [diff] [blame] | 5794 | hci_req_update_scan(hdev); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5795 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5796 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5797 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5798 | continue; |
| 5799 | device_removed(sk, hdev, &p->addr, p->addr_type); |
Johan Hedberg | 679d2b6 | 2015-10-16 10:07:52 +0300 | [diff] [blame] | 5800 | if (p->explicit_connect) { |
| 5801 | p->auto_connect = HCI_AUTO_CONN_EXPLICIT; |
| 5802 | continue; |
| 5803 | } |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5804 | list_del(&p->action); |
| 5805 | list_del(&p->list); |
| 5806 | kfree(p); |
| 5807 | } |
| 5808 | |
| 5809 | BT_DBG("All LE connection parameters were removed"); |
| 5810 | |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5811 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5812 | } |
| 5813 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5814 | complete: |
Johan Hedberg | 51d7a94 | 2015-11-11 08:11:18 +0200 | [diff] [blame] | 5815 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5816 | MGMT_STATUS_SUCCESS, &cp->addr, |
| 5817 | sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5818 | unlock: |
| 5819 | hci_dev_unlock(hdev); |
| 5820 | return err; |
| 5821 | } |
| 5822 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5823 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5824 | u16 len) |
| 5825 | { |
| 5826 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5827 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5828 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5829 | u16 param_count, expected_len; |
| 5830 | int i; |
| 5831 | |
| 5832 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5833 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5834 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5835 | |
| 5836 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5837 | if (param_count > max_param_count) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5838 | bt_dev_err(hdev, "load_conn_param: too big param_count value %u", |
| 5839 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5840 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5841 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5842 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5843 | |
| 5844 | expected_len = sizeof(*cp) + param_count * |
| 5845 | sizeof(struct mgmt_conn_param); |
| 5846 | if (expected_len != len) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5847 | bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", |
| 5848 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5849 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5850 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5854 | |
| 5855 | hci_dev_lock(hdev); |
| 5856 | |
| 5857 | hci_conn_params_clear_disabled(hdev); |
| 5858 | |
| 5859 | for (i = 0; i < param_count; i++) { |
| 5860 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5861 | struct hci_conn_params *hci_param; |
| 5862 | u16 min, max, latency, timeout; |
| 5863 | u8 addr_type; |
| 5864 | |
| 5865 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5866 | param->addr.type); |
| 5867 | |
| 5868 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5869 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5870 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5871 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5872 | } else { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5873 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5874 | continue; |
| 5875 | } |
| 5876 | |
| 5877 | min = le16_to_cpu(param->min_interval); |
| 5878 | max = le16_to_cpu(param->max_interval); |
| 5879 | latency = le16_to_cpu(param->latency); |
| 5880 | timeout = le16_to_cpu(param->timeout); |
| 5881 | |
| 5882 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5883 | min, max, latency, timeout); |
| 5884 | |
| 5885 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5886 | bt_dev_err(hdev, "ignoring invalid connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5887 | continue; |
| 5888 | } |
| 5889 | |
| 5890 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5891 | addr_type); |
| 5892 | if (!hci_param) { |
Marcel Holtmann | 2064ee3 | 2017-10-30 10:42:59 +0100 | [diff] [blame] | 5893 | bt_dev_err(hdev, "failed to add connection parameters"); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5894 | continue; |
| 5895 | } |
| 5896 | |
| 5897 | hci_param->conn_min_interval = min; |
| 5898 | hci_param->conn_max_interval = max; |
| 5899 | hci_param->conn_latency = latency; |
| 5900 | hci_param->supervision_timeout = timeout; |
| 5901 | } |
| 5902 | |
| 5903 | hci_dev_unlock(hdev); |
| 5904 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5905 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 5906 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5907 | } |
| 5908 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5909 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5910 | void *data, u16 len) |
| 5911 | { |
| 5912 | struct mgmt_cp_set_external_config *cp = data; |
| 5913 | bool changed; |
| 5914 | int err; |
| 5915 | |
| 5916 | BT_DBG("%s", hdev->name); |
| 5917 | |
| 5918 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5919 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5920 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5921 | |
| 5922 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5923 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5924 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5925 | |
| 5926 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5927 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5928 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5929 | |
| 5930 | hci_dev_lock(hdev); |
| 5931 | |
| 5932 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5933 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5934 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5935 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5936 | |
| 5937 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5938 | if (err < 0) |
| 5939 | goto unlock; |
| 5940 | |
| 5941 | if (!changed) |
| 5942 | goto unlock; |
| 5943 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 5944 | err = new_options(hdev, sk); |
| 5945 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5946 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5947 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5948 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 5949 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5950 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 5951 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5952 | |
| 5953 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5954 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 5955 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5956 | mgmt_index_added(hdev); |
| 5957 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5958 | } |
| 5959 | |
| 5960 | unlock: |
| 5961 | hci_dev_unlock(hdev); |
| 5962 | return err; |
| 5963 | } |
| 5964 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5965 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 5966 | void *data, u16 len) |
| 5967 | { |
| 5968 | struct mgmt_cp_set_public_address *cp = data; |
| 5969 | bool changed; |
| 5970 | int err; |
| 5971 | |
| 5972 | BT_DBG("%s", hdev->name); |
| 5973 | |
| 5974 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5975 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5976 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5977 | |
| 5978 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5979 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5980 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5981 | |
| 5982 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5983 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5984 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5985 | |
| 5986 | hci_dev_lock(hdev); |
| 5987 | |
| 5988 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 5989 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 5990 | |
| 5991 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 5992 | if (err < 0) |
| 5993 | goto unlock; |
| 5994 | |
| 5995 | if (!changed) |
| 5996 | goto unlock; |
| 5997 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5998 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5999 | err = new_options(hdev, sk); |
| 6000 | |
| 6001 | if (is_configured(hdev)) { |
| 6002 | mgmt_index_removed(hdev); |
| 6003 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6004 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6005 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6006 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6007 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6008 | |
| 6009 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6010 | } |
| 6011 | |
| 6012 | unlock: |
| 6013 | hci_dev_unlock(hdev); |
| 6014 | return err; |
| 6015 | } |
| 6016 | |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6017 | static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status, |
| 6018 | u16 opcode, struct sk_buff *skb) |
| 6019 | { |
| 6020 | const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; |
| 6021 | struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; |
| 6022 | u8 *h192, *r192, *h256, *r256; |
| 6023 | struct mgmt_pending_cmd *cmd; |
| 6024 | u16 eir_len; |
| 6025 | int err; |
| 6026 | |
| 6027 | BT_DBG("%s status %u", hdev->name, status); |
| 6028 | |
| 6029 | cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev); |
| 6030 | if (!cmd) |
| 6031 | return; |
| 6032 | |
| 6033 | mgmt_cp = cmd->param; |
| 6034 | |
| 6035 | if (status) { |
| 6036 | status = mgmt_status(status); |
| 6037 | eir_len = 0; |
| 6038 | |
| 6039 | h192 = NULL; |
| 6040 | r192 = NULL; |
| 6041 | h256 = NULL; |
| 6042 | r256 = NULL; |
| 6043 | } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) { |
| 6044 | struct hci_rp_read_local_oob_data *rp; |
| 6045 | |
| 6046 | if (skb->len != sizeof(*rp)) { |
| 6047 | status = MGMT_STATUS_FAILED; |
| 6048 | eir_len = 0; |
| 6049 | } else { |
| 6050 | status = MGMT_STATUS_SUCCESS; |
| 6051 | rp = (void *)skb->data; |
| 6052 | |
| 6053 | eir_len = 5 + 18 + 18; |
| 6054 | h192 = rp->hash; |
| 6055 | r192 = rp->rand; |
| 6056 | h256 = NULL; |
| 6057 | r256 = NULL; |
| 6058 | } |
| 6059 | } else { |
| 6060 | struct hci_rp_read_local_oob_ext_data *rp; |
| 6061 | |
| 6062 | if (skb->len != sizeof(*rp)) { |
| 6063 | status = MGMT_STATUS_FAILED; |
| 6064 | eir_len = 0; |
| 6065 | } else { |
| 6066 | status = MGMT_STATUS_SUCCESS; |
| 6067 | rp = (void *)skb->data; |
| 6068 | |
| 6069 | if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
| 6070 | eir_len = 5 + 18 + 18; |
| 6071 | h192 = NULL; |
| 6072 | r192 = NULL; |
| 6073 | } else { |
| 6074 | eir_len = 5 + 18 + 18 + 18 + 18; |
| 6075 | h192 = rp->hash192; |
| 6076 | r192 = rp->rand192; |
| 6077 | } |
| 6078 | |
| 6079 | h256 = rp->hash256; |
| 6080 | r256 = rp->rand256; |
| 6081 | } |
| 6082 | } |
| 6083 | |
| 6084 | mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); |
| 6085 | if (!mgmt_rp) |
| 6086 | goto done; |
| 6087 | |
| 6088 | if (status) |
| 6089 | goto send_rsp; |
| 6090 | |
| 6091 | eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, |
| 6092 | hdev->dev_class, 3); |
| 6093 | |
| 6094 | if (h192 && r192) { |
| 6095 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6096 | EIR_SSP_HASH_C192, h192, 16); |
| 6097 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6098 | EIR_SSP_RAND_R192, r192, 16); |
| 6099 | } |
| 6100 | |
| 6101 | if (h256 && r256) { |
| 6102 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6103 | EIR_SSP_HASH_C256, h256, 16); |
| 6104 | eir_len = eir_append_data(mgmt_rp->eir, eir_len, |
| 6105 | EIR_SSP_RAND_R256, r256, 16); |
| 6106 | } |
| 6107 | |
| 6108 | send_rsp: |
| 6109 | mgmt_rp->type = mgmt_cp->type; |
| 6110 | mgmt_rp->eir_len = cpu_to_le16(eir_len); |
| 6111 | |
| 6112 | err = mgmt_cmd_complete(cmd->sk, hdev->id, |
| 6113 | MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, |
| 6114 | mgmt_rp, sizeof(*mgmt_rp) + eir_len); |
| 6115 | if (err < 0 || status) |
| 6116 | goto done; |
| 6117 | |
| 6118 | hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 6119 | |
| 6120 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 6121 | mgmt_rp, sizeof(*mgmt_rp) + eir_len, |
| 6122 | HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); |
| 6123 | done: |
| 6124 | kfree(mgmt_rp); |
| 6125 | mgmt_pending_remove(cmd); |
| 6126 | } |
| 6127 | |
| 6128 | static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, |
| 6129 | struct mgmt_cp_read_local_oob_ext_data *cp) |
| 6130 | { |
| 6131 | struct mgmt_pending_cmd *cmd; |
| 6132 | struct hci_request req; |
| 6133 | int err; |
| 6134 | |
| 6135 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, |
| 6136 | cp, sizeof(*cp)); |
| 6137 | if (!cmd) |
| 6138 | return -ENOMEM; |
| 6139 | |
| 6140 | hci_req_init(&req, hdev); |
| 6141 | |
| 6142 | if (bredr_sc_enabled(hdev)) |
| 6143 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL); |
| 6144 | else |
| 6145 | hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 6146 | |
| 6147 | err = hci_req_run_skb(&req, read_local_oob_ext_data_complete); |
| 6148 | if (err < 0) { |
| 6149 | mgmt_pending_remove(cmd); |
| 6150 | return err; |
| 6151 | } |
| 6152 | |
| 6153 | return 0; |
| 6154 | } |
| 6155 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6156 | static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, |
| 6157 | void *data, u16 data_len) |
| 6158 | { |
| 6159 | struct mgmt_cp_read_local_oob_ext_data *cp = data; |
| 6160 | struct mgmt_rp_read_local_oob_ext_data *rp; |
| 6161 | size_t rp_len; |
| 6162 | u16 eir_len; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6163 | u8 status, flags, role, addr[7], hash[16], rand[16]; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6164 | int err; |
| 6165 | |
| 6166 | BT_DBG("%s", hdev->name); |
| 6167 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6168 | if (hdev_is_powered(hdev)) { |
| 6169 | switch (cp->type) { |
| 6170 | case BIT(BDADDR_BREDR): |
| 6171 | status = mgmt_bredr_support(hdev); |
| 6172 | if (status) |
| 6173 | eir_len = 0; |
| 6174 | else |
| 6175 | eir_len = 5; |
| 6176 | break; |
| 6177 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
| 6178 | status = mgmt_le_support(hdev); |
| 6179 | if (status) |
| 6180 | eir_len = 0; |
| 6181 | else |
| 6182 | eir_len = 9 + 3 + 18 + 18 + 3; |
| 6183 | break; |
| 6184 | default: |
| 6185 | status = MGMT_STATUS_INVALID_PARAMS; |
| 6186 | eir_len = 0; |
| 6187 | break; |
| 6188 | } |
| 6189 | } else { |
| 6190 | status = MGMT_STATUS_NOT_POWERED; |
| 6191 | eir_len = 0; |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6192 | } |
| 6193 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6194 | rp_len = sizeof(*rp) + eir_len; |
| 6195 | rp = kmalloc(rp_len, GFP_ATOMIC); |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6196 | if (!rp) |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6197 | return -ENOMEM; |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6198 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6199 | if (status) |
| 6200 | goto complete; |
| 6201 | |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6202 | hci_dev_lock(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6203 | |
| 6204 | eir_len = 0; |
| 6205 | switch (cp->type) { |
| 6206 | case BIT(BDADDR_BREDR): |
Johan Hedberg | 40f66c0 | 2015-04-07 21:52:22 +0300 | [diff] [blame] | 6207 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 6208 | err = read_local_ssp_oob_req(hdev, sk, cp); |
| 6209 | hci_dev_unlock(hdev); |
| 6210 | if (!err) |
| 6211 | goto done; |
| 6212 | |
| 6213 | status = MGMT_STATUS_FAILED; |
| 6214 | goto complete; |
| 6215 | } else { |
| 6216 | eir_len = eir_append_data(rp->eir, eir_len, |
| 6217 | EIR_CLASS_OF_DEV, |
| 6218 | hdev->dev_class, 3); |
| 6219 | } |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6220 | break; |
| 6221 | case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 6222 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 6223 | smp_generate_oob(hdev, hash, rand) < 0) { |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6224 | hci_dev_unlock(hdev); |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6225 | status = MGMT_STATUS_FAILED; |
| 6226 | goto complete; |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6227 | } |
| 6228 | |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 6229 | /* This should return the active RPA, but since the RPA |
| 6230 | * is only programmed on demand, it is really hard to fill |
| 6231 | * this in at the moment. For now disallow retrieving |
| 6232 | * local out-of-band data when privacy is in use. |
| 6233 | * |
| 6234 | * Returning the identity address will not help here since |
| 6235 | * pairing happens before the identity resolving key is |
| 6236 | * known and thus the connection establishment happens |
| 6237 | * based on the RPA and not the identity address. |
| 6238 | */ |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6239 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Marcel Holtmann | e213568 | 2015-04-02 12:00:58 -0700 | [diff] [blame] | 6240 | hci_dev_unlock(hdev); |
| 6241 | status = MGMT_STATUS_REJECTED; |
| 6242 | goto complete; |
| 6243 | } |
| 6244 | |
| 6245 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
| 6246 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
| 6247 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
| 6248 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6249 | memcpy(addr, &hdev->static_addr, 6); |
| 6250 | addr[6] = 0x01; |
| 6251 | } else { |
| 6252 | memcpy(addr, &hdev->bdaddr, 6); |
| 6253 | addr[6] = 0x00; |
| 6254 | } |
| 6255 | |
| 6256 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, |
| 6257 | addr, sizeof(addr)); |
| 6258 | |
| 6259 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
| 6260 | role = 0x02; |
| 6261 | else |
| 6262 | role = 0x01; |
| 6263 | |
| 6264 | eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, |
| 6265 | &role, sizeof(role)); |
| 6266 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 6267 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { |
| 6268 | eir_len = eir_append_data(rp->eir, eir_len, |
| 6269 | EIR_LE_SC_CONFIRM, |
| 6270 | hash, sizeof(hash)); |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6271 | |
Marcel Holtmann | 5082a59 | 2015-03-16 12:39:00 -0700 | [diff] [blame] | 6272 | eir_len = eir_append_data(rp->eir, eir_len, |
| 6273 | EIR_LE_SC_RANDOM, |
| 6274 | rand, sizeof(rand)); |
| 6275 | } |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6276 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6277 | flags = mgmt_get_adv_discov_flags(hdev); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6278 | |
| 6279 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
| 6280 | flags |= LE_AD_NO_BREDR; |
| 6281 | |
| 6282 | eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, |
| 6283 | &flags, sizeof(flags)); |
| 6284 | break; |
| 6285 | } |
| 6286 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6287 | hci_dev_unlock(hdev); |
| 6288 | |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 6289 | hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); |
| 6290 | |
Marcel Holtmann | 57b0d3e | 2015-03-28 15:18:59 -0700 | [diff] [blame] | 6291 | status = MGMT_STATUS_SUCCESS; |
| 6292 | |
| 6293 | complete: |
Marcel Holtmann | efcd8c9 | 2015-03-28 15:18:58 -0700 | [diff] [blame] | 6294 | rp->type = cp->type; |
| 6295 | rp->eir_len = cpu_to_le16(eir_len); |
| 6296 | |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6297 | 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] | 6298 | status, rp, sizeof(*rp) + eir_len); |
| 6299 | if (err < 0 || status) |
Marcel Holtmann | 72000df | 2015-03-16 16:11:21 -0700 | [diff] [blame] | 6300 | goto done; |
| 6301 | |
| 6302 | err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, |
| 6303 | rp, sizeof(*rp) + eir_len, |
| 6304 | HCI_MGMT_OOB_DATA_EVENTS, sk); |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6305 | |
Marcel Holtmann | 0821a2c | 2015-03-16 01:10:23 -0700 | [diff] [blame] | 6306 | done: |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6307 | kfree(rp); |
| 6308 | |
| 6309 | return err; |
| 6310 | } |
| 6311 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6312 | static u32 get_supported_adv_flags(struct hci_dev *hdev) |
| 6313 | { |
| 6314 | u32 flags = 0; |
| 6315 | |
| 6316 | flags |= MGMT_ADV_FLAG_CONNECTABLE; |
| 6317 | flags |= MGMT_ADV_FLAG_DISCOV; |
| 6318 | flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; |
| 6319 | flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6320 | flags |= MGMT_ADV_FLAG_APPEARANCE; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 6321 | flags |= MGMT_ADV_FLAG_LOCAL_NAME; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6322 | |
Jaganath Kanakkassery | de181e8 | 2018-07-19 17:09:41 +0530 | [diff] [blame^] | 6323 | /* In extended adv TX_POWER returned from Set Adv Param |
| 6324 | * will be always valid. |
| 6325 | */ |
| 6326 | if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) || |
| 6327 | ext_adv_capable(hdev)) |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6328 | flags |= MGMT_ADV_FLAG_TX_POWER; |
| 6329 | |
| 6330 | return flags; |
| 6331 | } |
| 6332 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6333 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 6334 | void *data, u16 data_len) |
| 6335 | { |
| 6336 | struct mgmt_rp_read_adv_features *rp; |
| 6337 | size_t rp_len; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6338 | int err; |
Florian Grandel | 286e0c8 | 2015-06-18 03:16:38 +0200 | [diff] [blame] | 6339 | struct adv_info *adv_instance; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6340 | u32 supported_flags; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6341 | u8 *instance; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6342 | |
| 6343 | BT_DBG("%s", hdev->name); |
| 6344 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6345 | if (!lmp_le_capable(hdev)) |
| 6346 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 6347 | MGMT_STATUS_REJECTED); |
| 6348 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6349 | hci_dev_lock(hdev); |
| 6350 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6351 | rp_len = sizeof(*rp) + hdev->adv_instance_cnt; |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6352 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 6353 | if (!rp) { |
| 6354 | hci_dev_unlock(hdev); |
| 6355 | return -ENOMEM; |
| 6356 | } |
| 6357 | |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6358 | supported_flags = get_supported_adv_flags(hdev); |
| 6359 | |
| 6360 | rp->supported_flags = cpu_to_le32(supported_flags); |
Marcel Holtmann | dc5d82a | 2015-03-19 17:22:25 -0700 | [diff] [blame] | 6361 | rp->max_adv_data_len = HCI_MAX_AD_LENGTH; |
| 6362 | rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH; |
Florian Grandel | d2609b3 | 2015-06-18 03:16:34 +0200 | [diff] [blame] | 6363 | rp->max_instances = HCI_MAX_ADV_INSTANCES; |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6364 | rp->num_instances = hdev->adv_instance_cnt; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6365 | |
Johan Hedberg | 02c04afe | 2015-11-26 12:15:58 +0200 | [diff] [blame] | 6366 | instance = rp->instance; |
| 6367 | list_for_each_entry(adv_instance, &hdev->adv_instances, list) { |
| 6368 | *instance = adv_instance->instance; |
| 6369 | instance++; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6370 | } |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6371 | |
| 6372 | hci_dev_unlock(hdev); |
| 6373 | |
| 6374 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 6375 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 6376 | |
| 6377 | kfree(rp); |
| 6378 | |
| 6379 | return err; |
| 6380 | } |
| 6381 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6382 | static u8 calculate_name_len(struct hci_dev *hdev) |
| 6383 | { |
| 6384 | u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3]; |
| 6385 | |
| 6386 | return append_local_name(hdev, buf, 0); |
| 6387 | } |
| 6388 | |
| 6389 | static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, |
| 6390 | bool is_adv_data) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6391 | { |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6392 | u8 max_len = HCI_MAX_AD_LENGTH; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6393 | |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6394 | if (is_adv_data) { |
| 6395 | if (adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 6396 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6397 | MGMT_ADV_FLAG_MANAGED_FLAGS)) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6398 | max_len -= 3; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6399 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6400 | if (adv_flags & MGMT_ADV_FLAG_TX_POWER) |
Marcel Holtmann | 31a3248 | 2015-11-19 16:16:42 +0100 | [diff] [blame] | 6401 | max_len -= 3; |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 6402 | } else { |
MichaĆ Narajowski | 7c295c4 | 2016-09-18 12:50:02 +0200 | [diff] [blame] | 6403 | if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6404 | max_len -= calculate_name_len(hdev); |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6405 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6406 | if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6407 | max_len -= 4; |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 6408 | } |
| 6409 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6410 | return max_len; |
| 6411 | } |
| 6412 | |
| 6413 | static bool flags_managed(u32 adv_flags) |
| 6414 | { |
| 6415 | return adv_flags & (MGMT_ADV_FLAG_DISCOV | |
| 6416 | MGMT_ADV_FLAG_LIMITED_DISCOV | |
| 6417 | MGMT_ADV_FLAG_MANAGED_FLAGS); |
| 6418 | } |
| 6419 | |
| 6420 | static bool tx_power_managed(u32 adv_flags) |
| 6421 | { |
| 6422 | return adv_flags & MGMT_ADV_FLAG_TX_POWER; |
| 6423 | } |
| 6424 | |
| 6425 | static bool name_managed(u32 adv_flags) |
| 6426 | { |
| 6427 | return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; |
| 6428 | } |
| 6429 | |
| 6430 | static bool appearance_managed(u32 adv_flags) |
| 6431 | { |
| 6432 | return adv_flags & MGMT_ADV_FLAG_APPEARANCE; |
| 6433 | } |
| 6434 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6435 | static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, |
| 6436 | u8 len, bool is_adv_data) |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6437 | { |
| 6438 | int i, cur_len; |
| 6439 | u8 max_len; |
| 6440 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6441 | max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6442 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6443 | if (len > max_len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6444 | return false; |
| 6445 | |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6446 | /* Make sure that the data is correctly formatted. */ |
| 6447 | for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) { |
| 6448 | cur_len = data[i]; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6449 | |
Szymon Janc | 9c9db78 | 2016-09-18 12:50:06 +0200 | [diff] [blame] | 6450 | if (data[i + 1] == EIR_FLAGS && |
| 6451 | (!is_adv_data || flags_managed(adv_flags))) |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6452 | return false; |
| 6453 | |
Szymon Janc | 2bb36870 | 2016-09-18 12:50:05 +0200 | [diff] [blame] | 6454 | if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) |
| 6455 | return false; |
| 6456 | |
| 6457 | if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) |
| 6458 | return false; |
| 6459 | |
| 6460 | if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) |
| 6461 | return false; |
| 6462 | |
| 6463 | if (data[i + 1] == EIR_APPEARANCE && |
| 6464 | appearance_managed(adv_flags)) |
Arman Uguray | 5507e35 | 2015-03-25 18:53:44 -0700 | [diff] [blame] | 6465 | return false; |
| 6466 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6467 | /* If the current field length would exceed the total data |
| 6468 | * length, then it's invalid. |
| 6469 | */ |
Arman Uguray | 4117ed7 | 2015-03-23 15:57:14 -0700 | [diff] [blame] | 6470 | if (i + cur_len >= len) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6471 | return false; |
| 6472 | } |
| 6473 | |
| 6474 | return true; |
| 6475 | } |
| 6476 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6477 | static void add_advertising_complete(struct hci_dev *hdev, u8 status, |
| 6478 | u16 opcode) |
| 6479 | { |
| 6480 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6481 | struct mgmt_cp_add_advertising *cp; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6482 | struct mgmt_rp_add_advertising rp; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6483 | struct adv_info *adv_instance, *n; |
| 6484 | u8 instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6485 | |
| 6486 | BT_DBG("status %d", status); |
| 6487 | |
| 6488 | hci_dev_lock(hdev); |
| 6489 | |
| 6490 | cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev); |
| 6491 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6492 | list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) { |
| 6493 | if (!adv_instance->pending) |
| 6494 | continue; |
| 6495 | |
| 6496 | if (!status) { |
| 6497 | adv_instance->pending = false; |
| 6498 | continue; |
| 6499 | } |
| 6500 | |
| 6501 | instance = adv_instance->instance; |
| 6502 | |
| 6503 | if (hdev->cur_adv_instance == instance) |
| 6504 | cancel_adv_timeout(hdev); |
| 6505 | |
| 6506 | hci_remove_adv_instance(hdev, instance); |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6507 | mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6508 | } |
| 6509 | |
| 6510 | if (!cmd) |
| 6511 | goto unlock; |
| 6512 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6513 | cp = cmd->param; |
| 6514 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6515 | |
| 6516 | if (status) |
| 6517 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 6518 | mgmt_status(status)); |
| 6519 | else |
| 6520 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 6521 | mgmt_status(status), &rp, sizeof(rp)); |
| 6522 | |
| 6523 | mgmt_pending_remove(cmd); |
| 6524 | |
| 6525 | unlock: |
| 6526 | hci_dev_unlock(hdev); |
| 6527 | } |
| 6528 | |
| 6529 | static int add_advertising(struct sock *sk, struct hci_dev *hdev, |
| 6530 | void *data, u16 data_len) |
| 6531 | { |
| 6532 | struct mgmt_cp_add_advertising *cp = data; |
| 6533 | struct mgmt_rp_add_advertising rp; |
| 6534 | u32 flags; |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6535 | u32 supported_flags; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6536 | u8 status; |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6537 | u16 timeout, duration; |
| 6538 | unsigned int prev_instance_cnt = hdev->adv_instance_cnt; |
| 6539 | u8 schedule_instance = 0; |
| 6540 | struct adv_info *next_instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6541 | int err; |
| 6542 | struct mgmt_pending_cmd *cmd; |
| 6543 | struct hci_request req; |
| 6544 | |
| 6545 | BT_DBG("%s", hdev->name); |
| 6546 | |
| 6547 | status = mgmt_le_support(hdev); |
| 6548 | if (status) |
| 6549 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6550 | status); |
| 6551 | |
Marcel Holtmann | ceff86a | 2015-11-19 16:16:41 +0100 | [diff] [blame] | 6552 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 6553 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6554 | MGMT_STATUS_INVALID_PARAMS); |
| 6555 | |
Johan Hedberg | 6a0e780 | 2016-03-11 09:56:33 +0200 | [diff] [blame] | 6556 | if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) |
| 6557 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6558 | MGMT_STATUS_INVALID_PARAMS); |
| 6559 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6560 | flags = __le32_to_cpu(cp->flags); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6561 | timeout = __le16_to_cpu(cp->timeout); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6562 | duration = __le16_to_cpu(cp->duration); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6563 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6564 | /* The current implementation only supports a subset of the specified |
| 6565 | * flags. |
Arman Uguray | 089fa8c | 2015-03-25 18:53:45 -0700 | [diff] [blame] | 6566 | */ |
| 6567 | supported_flags = get_supported_adv_flags(hdev); |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6568 | if (flags & ~supported_flags) |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6569 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6570 | MGMT_STATUS_INVALID_PARAMS); |
| 6571 | |
| 6572 | hci_dev_lock(hdev); |
| 6573 | |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6574 | if (timeout && !hdev_is_powered(hdev)) { |
| 6575 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6576 | MGMT_STATUS_REJECTED); |
| 6577 | goto unlock; |
| 6578 | } |
| 6579 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6580 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6581 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6582 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 6583 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6584 | MGMT_STATUS_BUSY); |
| 6585 | goto unlock; |
| 6586 | } |
| 6587 | |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6588 | if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || |
| 6589 | !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, |
Arman Uguray | b44133f | 2015-03-25 18:53:41 -0700 | [diff] [blame] | 6590 | cp->scan_rsp_len, false)) { |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6591 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6592 | MGMT_STATUS_INVALID_PARAMS); |
| 6593 | goto unlock; |
| 6594 | } |
| 6595 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6596 | err = hci_add_adv_instance(hdev, cp->instance, flags, |
| 6597 | cp->adv_data_len, cp->data, |
| 6598 | cp->scan_rsp_len, |
| 6599 | cp->data + cp->adv_data_len, |
| 6600 | timeout, duration); |
| 6601 | if (err < 0) { |
| 6602 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6603 | MGMT_STATUS_FAILED); |
| 6604 | goto unlock; |
| 6605 | } |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6606 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6607 | /* Only trigger an advertising added event if a new instance was |
| 6608 | * actually added. |
| 6609 | */ |
| 6610 | if (hdev->adv_instance_cnt > prev_instance_cnt) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6611 | mgmt_advertising_added(sk, hdev, cp->instance); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6612 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6613 | if (hdev->cur_adv_instance == cp->instance) { |
| 6614 | /* If the currently advertised instance is being changed then |
| 6615 | * cancel the current advertising and schedule the next |
| 6616 | * instance. If there is only one instance then the overridden |
| 6617 | * advertising data will be visible right away. |
| 6618 | */ |
| 6619 | cancel_adv_timeout(hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6620 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6621 | next_instance = hci_get_next_instance(hdev, cp->instance); |
| 6622 | if (next_instance) |
| 6623 | schedule_instance = next_instance->instance; |
| 6624 | } else if (!hdev->adv_instance_timeout) { |
| 6625 | /* Immediately advertise the new instance if no other |
| 6626 | * instance is currently being advertised. |
| 6627 | */ |
| 6628 | schedule_instance = cp->instance; |
| 6629 | } |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6630 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6631 | /* If the HCI_ADVERTISING flag is set or the device isn't powered or |
| 6632 | * there is no instance to be advertised then we have no HCI |
| 6633 | * communication to make. Simply return. |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6634 | */ |
| 6635 | if (!hdev_is_powered(hdev) || |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6636 | hci_dev_test_flag(hdev, HCI_ADVERTISING) || |
| 6637 | !schedule_instance) { |
| 6638 | rp.instance = cp->instance; |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6639 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, |
| 6640 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6641 | goto unlock; |
| 6642 | } |
| 6643 | |
| 6644 | /* We're good to go, update advertising data, parameters, and start |
| 6645 | * advertising. |
| 6646 | */ |
| 6647 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, |
| 6648 | data_len); |
| 6649 | if (!cmd) { |
| 6650 | err = -ENOMEM; |
| 6651 | goto unlock; |
| 6652 | } |
| 6653 | |
| 6654 | hci_req_init(&req, hdev); |
| 6655 | |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6656 | err = __hci_req_schedule_adv_instance(&req, schedule_instance, true); |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6657 | |
Florian Grandel | fffd38b | 2015-06-18 03:16:47 +0200 | [diff] [blame] | 6658 | if (!err) |
| 6659 | err = hci_req_run(&req, add_advertising_complete); |
| 6660 | |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6661 | if (err < 0) |
| 6662 | mgmt_pending_remove(cmd); |
| 6663 | |
| 6664 | unlock: |
| 6665 | hci_dev_unlock(hdev); |
| 6666 | |
| 6667 | return err; |
| 6668 | } |
| 6669 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6670 | static void remove_advertising_complete(struct hci_dev *hdev, u8 status, |
| 6671 | u16 opcode) |
| 6672 | { |
| 6673 | struct mgmt_pending_cmd *cmd; |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6674 | struct mgmt_cp_remove_advertising *cp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6675 | struct mgmt_rp_remove_advertising rp; |
| 6676 | |
| 6677 | BT_DBG("status %d", status); |
| 6678 | |
| 6679 | hci_dev_lock(hdev); |
| 6680 | |
| 6681 | /* A failure status here only means that we failed to disable |
| 6682 | * advertising. Otherwise, the advertising instance has been removed, |
| 6683 | * so report success. |
| 6684 | */ |
| 6685 | cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev); |
| 6686 | if (!cmd) |
| 6687 | goto unlock; |
| 6688 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6689 | cp = cmd->param; |
| 6690 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6691 | |
| 6692 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS, |
| 6693 | &rp, sizeof(rp)); |
| 6694 | mgmt_pending_remove(cmd); |
| 6695 | |
| 6696 | unlock: |
| 6697 | hci_dev_unlock(hdev); |
| 6698 | } |
| 6699 | |
| 6700 | static int remove_advertising(struct sock *sk, struct hci_dev *hdev, |
| 6701 | void *data, u16 data_len) |
| 6702 | { |
| 6703 | struct mgmt_cp_remove_advertising *cp = data; |
| 6704 | struct mgmt_rp_remove_advertising rp; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6705 | struct mgmt_pending_cmd *cmd; |
| 6706 | struct hci_request req; |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 6707 | int err; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6708 | |
| 6709 | BT_DBG("%s", hdev->name); |
| 6710 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6711 | hci_dev_lock(hdev); |
| 6712 | |
Johan Hedberg | 952497b | 2015-06-18 21:05:31 +0300 | [diff] [blame] | 6713 | if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6714 | err = mgmt_cmd_status(sk, hdev->id, |
| 6715 | MGMT_OP_REMOVE_ADVERTISING, |
| 6716 | MGMT_STATUS_INVALID_PARAMS); |
| 6717 | goto unlock; |
| 6718 | } |
| 6719 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6720 | if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) || |
| 6721 | pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) || |
| 6722 | pending_find(MGMT_OP_SET_LE, hdev)) { |
| 6723 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 6724 | MGMT_STATUS_BUSY); |
| 6725 | goto unlock; |
| 6726 | } |
| 6727 | |
Johan Hedberg | 17fd08f | 2015-11-26 12:15:59 +0200 | [diff] [blame] | 6728 | if (list_empty(&hdev->adv_instances)) { |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6729 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, |
| 6730 | MGMT_STATUS_INVALID_PARAMS); |
| 6731 | goto unlock; |
| 6732 | } |
| 6733 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6734 | hci_req_init(&req, hdev); |
Arman Uguray | 912098a | 2015-03-23 15:57:15 -0700 | [diff] [blame] | 6735 | |
Johan Hedberg | 37d3a1f | 2016-08-28 20:53:34 +0300 | [diff] [blame] | 6736 | hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6737 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6738 | if (list_empty(&hdev->adv_instances)) |
Johan Hedberg | f225257 | 2015-11-18 12:49:20 +0200 | [diff] [blame] | 6739 | __hci_req_disable_advertising(&req); |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6740 | |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6741 | /* If no HCI commands have been collected so far or the HCI_ADVERTISING |
| 6742 | * flag is set or the device isn't powered then we have no HCI |
| 6743 | * communication to make. Simply return. |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6744 | */ |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6745 | if (skb_queue_empty(&req.cmd_q) || |
| 6746 | !hdev_is_powered(hdev) || |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6747 | hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Jaganath Kanakkassery | f17d858 | 2017-10-25 10:58:48 +0530 | [diff] [blame] | 6748 | hci_req_purge(&req); |
Florian Grandel | 0194833 | 2015-06-18 03:16:48 +0200 | [diff] [blame] | 6749 | rp.instance = cp->instance; |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6750 | err = mgmt_cmd_complete(sk, hdev->id, |
| 6751 | MGMT_OP_REMOVE_ADVERTISING, |
| 6752 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6753 | goto unlock; |
| 6754 | } |
| 6755 | |
| 6756 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, |
| 6757 | data_len); |
| 6758 | if (!cmd) { |
| 6759 | err = -ENOMEM; |
| 6760 | goto unlock; |
| 6761 | } |
| 6762 | |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6763 | err = hci_req_run(&req, remove_advertising_complete); |
| 6764 | if (err < 0) |
| 6765 | mgmt_pending_remove(cmd); |
| 6766 | |
| 6767 | unlock: |
| 6768 | hci_dev_unlock(hdev); |
| 6769 | |
| 6770 | return err; |
| 6771 | } |
| 6772 | |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6773 | static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, |
| 6774 | void *data, u16 data_len) |
| 6775 | { |
| 6776 | struct mgmt_cp_get_adv_size_info *cp = data; |
| 6777 | struct mgmt_rp_get_adv_size_info rp; |
| 6778 | u32 flags, supported_flags; |
| 6779 | int err; |
| 6780 | |
| 6781 | BT_DBG("%s", hdev->name); |
| 6782 | |
| 6783 | if (!lmp_le_capable(hdev)) |
| 6784 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6785 | MGMT_STATUS_REJECTED); |
| 6786 | |
| 6787 | if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES) |
| 6788 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6789 | MGMT_STATUS_INVALID_PARAMS); |
| 6790 | |
| 6791 | flags = __le32_to_cpu(cp->flags); |
| 6792 | |
| 6793 | /* The current implementation only supports a subset of the specified |
| 6794 | * flags. |
| 6795 | */ |
| 6796 | supported_flags = get_supported_adv_flags(hdev); |
| 6797 | if (flags & ~supported_flags) |
| 6798 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6799 | MGMT_STATUS_INVALID_PARAMS); |
| 6800 | |
| 6801 | rp.instance = cp->instance; |
| 6802 | rp.flags = cp->flags; |
MichaĆ Narajowski | f61851f | 2016-10-19 10:20:27 +0200 | [diff] [blame] | 6803 | rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); |
| 6804 | rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6805 | |
| 6806 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, |
| 6807 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 6808 | |
| 6809 | return err; |
| 6810 | } |
| 6811 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6812 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6813 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6814 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6815 | HCI_MGMT_NO_HDEV | |
| 6816 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6817 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6818 | HCI_MGMT_NO_HDEV | |
| 6819 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6820 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6821 | HCI_MGMT_NO_HDEV | |
| 6822 | HCI_MGMT_UNTRUSTED }, |
| 6823 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 6824 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6825 | { set_powered, MGMT_SETTING_SIZE }, |
| 6826 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 6827 | { set_connectable, MGMT_SETTING_SIZE }, |
| 6828 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 6829 | { set_bondable, MGMT_SETTING_SIZE }, |
| 6830 | { set_link_security, MGMT_SETTING_SIZE }, |
| 6831 | { set_ssp, MGMT_SETTING_SIZE }, |
| 6832 | { set_hs, MGMT_SETTING_SIZE }, |
| 6833 | { set_le, MGMT_SETTING_SIZE }, |
| 6834 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 6835 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 6836 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 6837 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6838 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 6839 | HCI_MGMT_VAR_LEN }, |
| 6840 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 6841 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6842 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 6843 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 6844 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 6845 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 6846 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 6847 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 6848 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 6849 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 6850 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 6851 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 6852 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 6853 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6854 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 6855 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 6856 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6857 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 6858 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 6859 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 6860 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 6861 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 6862 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 6863 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 6864 | { set_advertising, MGMT_SETTING_SIZE }, |
| 6865 | { set_bredr, MGMT_SETTING_SIZE }, |
| 6866 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 6867 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 6868 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 6869 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 6870 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6871 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 6872 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6873 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 6874 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 6875 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 6876 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6877 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 6878 | HCI_MGMT_VAR_LEN }, |
| 6879 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6880 | HCI_MGMT_NO_HDEV | |
| 6881 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6882 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6883 | HCI_MGMT_UNCONFIGURED | |
| 6884 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6885 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 6886 | HCI_MGMT_UNCONFIGURED }, |
| 6887 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 6888 | HCI_MGMT_UNCONFIGURED }, |
| 6889 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 6890 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 4f0f155 | 2015-03-14 22:43:19 -0700 | [diff] [blame] | 6891 | { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 6892 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6893 | HCI_MGMT_NO_HDEV | |
| 6894 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6895 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Arman Uguray | 24b4f38 | 2015-03-23 15:57:12 -0700 | [diff] [blame] | 6896 | { add_advertising, MGMT_ADD_ADVERTISING_SIZE, |
| 6897 | HCI_MGMT_VAR_LEN }, |
Arman Uguray | da929335 | 2015-03-23 15:57:13 -0700 | [diff] [blame] | 6898 | { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, |
Marcel Holtmann | 40b25fe | 2015-11-19 16:16:43 +0100 | [diff] [blame] | 6899 | { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 6900 | { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 6901 | { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, |
| 6902 | HCI_MGMT_UNTRUSTED }, |
MichaĆ Narajowski | c4960ec | 2016-09-18 12:50:03 +0200 | [diff] [blame] | 6903 | { set_appearance, MGMT_SET_APPEARANCE_SIZE }, |
Jaganath Kanakkassery | 6244691 | 2018-07-19 17:09:34 +0530 | [diff] [blame] | 6904 | { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, |
Jaganath Kanakkassery | 0314f28 | 2018-07-19 17:09:35 +0530 | [diff] [blame] | 6905 | { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6906 | }; |
| 6907 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6908 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6909 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6910 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 6911 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6912 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6913 | return; |
| 6914 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6915 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 6916 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6917 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6918 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 6919 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6920 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6921 | } else { |
| 6922 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 6923 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6924 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6925 | } |
| 6926 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6927 | case HCI_AMP: |
| 6928 | ev.type = 0x02; |
| 6929 | break; |
| 6930 | default: |
| 6931 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6932 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6933 | |
| 6934 | ev.bus = hdev->bus; |
| 6935 | |
| 6936 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 6937 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6938 | } |
| 6939 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6940 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6941 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6942 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 6943 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6944 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6945 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6946 | return; |
| 6947 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6948 | switch (hdev->dev_type) { |
Marcel Holtmann | ca8bee5 | 2016-07-05 14:30:14 +0200 | [diff] [blame] | 6949 | case HCI_PRIMARY: |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6950 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6951 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6952 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6953 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 6954 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6955 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6956 | } else { |
| 6957 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 6958 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6959 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6960 | } |
| 6961 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6962 | case HCI_AMP: |
| 6963 | ev.type = 0x02; |
| 6964 | break; |
| 6965 | default: |
| 6966 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6967 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6968 | |
| 6969 | ev.bus = hdev->bus; |
| 6970 | |
| 6971 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 6972 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6973 | } |
| 6974 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6975 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 6976 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6977 | { |
| 6978 | struct hci_conn_params *p; |
| 6979 | |
| 6980 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6981 | /* Needed for AUTO_OFF case where might not "really" |
| 6982 | * have been powered off. |
| 6983 | */ |
| 6984 | list_del_init(&p->action); |
| 6985 | |
| 6986 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6987 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6988 | case HCI_AUTO_CONN_ALWAYS: |
| 6989 | list_add(&p->action, &hdev->pend_le_conns); |
| 6990 | break; |
| 6991 | case HCI_AUTO_CONN_REPORT: |
| 6992 | list_add(&p->action, &hdev->pend_le_reports); |
| 6993 | break; |
| 6994 | default: |
| 6995 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6996 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6997 | } |
| 6998 | } |
| 6999 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7000 | void mgmt_power_on(struct hci_dev *hdev, int err) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7001 | { |
| 7002 | struct cmd_lookup match = { NULL, hdev }; |
| 7003 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7004 | BT_DBG("err %d", err); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7005 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7006 | hci_dev_lock(hdev); |
| 7007 | |
| 7008 | if (!err) { |
Johan Hedberg | af02dd4 | 2015-11-11 08:11:21 +0200 | [diff] [blame] | 7009 | restart_le_actions(hdev); |
| 7010 | hci_update_background_scan(hdev); |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 7011 | } |
| 7012 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7013 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 7014 | |
| 7015 | new_settings(hdev, match.sk); |
| 7016 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7017 | if (match.sk) |
| 7018 | sock_put(match.sk); |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7019 | |
| 7020 | hci_dev_unlock(hdev); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7021 | } |
| 7022 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7023 | void __mgmt_power_off(struct hci_dev *hdev) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 7024 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 7025 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7026 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 7027 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7028 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7029 | |
| 7030 | /* If the power off is because of hdev unregistration let |
| 7031 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 7032 | * NOT_POWERED. We cover both scenarios here since later in |
| 7033 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 7034 | * been triggered, potentially causing misleading DISCONNECTED |
| 7035 | * status responses. |
| 7036 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7037 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 7038 | status = MGMT_STATUS_INVALID_INDEX; |
| 7039 | else |
| 7040 | status = MGMT_STATUS_NOT_POWERED; |
| 7041 | |
| 7042 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7043 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7044 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7045 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 7046 | zero_cod, sizeof(zero_cod), |
| 7047 | HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7048 | ext_info_changed(hdev, NULL); |
| 7049 | } |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 7050 | |
Johan Hedberg | 2ff1389 | 2015-11-25 16:15:44 +0200 | [diff] [blame] | 7051 | new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 7052 | |
| 7053 | if (match.sk) |
| 7054 | sock_put(match.sk); |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 7055 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 7056 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 7057 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7058 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7059 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7060 | u8 status; |
| 7061 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7062 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7063 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 7064 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7065 | |
| 7066 | if (err == -ERFKILL) |
| 7067 | status = MGMT_STATUS_RFKILLED; |
| 7068 | else |
| 7069 | status = MGMT_STATUS_FAILED; |
| 7070 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7071 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7072 | |
| 7073 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 7074 | } |
| 7075 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 7076 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 7077 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7078 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 7079 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7080 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7081 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7082 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7083 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 7084 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 7085 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7086 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 7087 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 7088 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7089 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 7090 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 7091 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7092 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 7093 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 7094 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 7095 | switch (ltk->type) { |
| 7096 | case SMP_LTK: |
| 7097 | case SMP_LTK_SLAVE: |
| 7098 | if (ltk->authenticated) |
| 7099 | return MGMT_LTK_AUTHENTICATED; |
| 7100 | return MGMT_LTK_UNAUTHENTICATED; |
| 7101 | case SMP_LTK_P256: |
| 7102 | if (ltk->authenticated) |
| 7103 | return MGMT_LTK_P256_AUTH; |
| 7104 | return MGMT_LTK_P256_UNAUTH; |
| 7105 | case SMP_LTK_P256_DEBUG: |
| 7106 | return MGMT_LTK_P256_DEBUG; |
| 7107 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 7108 | |
| 7109 | return MGMT_LTK_UNAUTHENTICATED; |
| 7110 | } |
| 7111 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7112 | 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] | 7113 | { |
| 7114 | struct mgmt_ev_new_long_term_key ev; |
| 7115 | |
| 7116 | memset(&ev, 0, sizeof(ev)); |
| 7117 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 7118 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 7119 | * without providing an identity resolving key don't require |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 7120 | * to store long term keys. Their addresses will change the |
| 7121 | * next time around. |
| 7122 | * |
| 7123 | * Only when a remote device provides an identity address |
| 7124 | * make sure the long term key is stored. If the remote |
| 7125 | * identity is known, the long term keys are internally |
| 7126 | * mapped to the identity address. So allow static random |
| 7127 | * and public addresses here. |
| 7128 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 7129 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 7130 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 7131 | ev.store_hint = 0x00; |
| 7132 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7133 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 7134 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7135 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7136 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 7137 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7138 | ev.key.enc_size = key->enc_size; |
| 7139 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 7140 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7141 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 7142 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7143 | ev.key.master = 1; |
| 7144 | |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 7145 | /* Make sure we copy only the significant bytes based on the |
| 7146 | * encryption key size, and set the rest of the value to zeroes. |
| 7147 | */ |
Jakub Pawlowski | cb92205 | 2015-08-05 23:16:29 +0200 | [diff] [blame] | 7148 | memcpy(ev.key.val, key->val, key->enc_size); |
Johan Hedberg | 1fc62c5 | 2015-06-10 11:11:20 +0300 | [diff] [blame] | 7149 | memset(ev.key.val + key->enc_size, 0, |
| 7150 | sizeof(ev.key.val) - key->enc_size); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 7151 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 7152 | 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] | 7153 | } |
| 7154 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 7155 | 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] | 7156 | { |
| 7157 | struct mgmt_ev_new_irk ev; |
| 7158 | |
| 7159 | memset(&ev, 0, sizeof(ev)); |
| 7160 | |
Johan Hedberg | cad20c2 | 2015-10-12 13:36:19 +0200 | [diff] [blame] | 7161 | ev.store_hint = persistent; |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 7162 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 7163 | bacpy(&ev.rpa, &irk->rpa); |
| 7164 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 7165 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 7166 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 7167 | |
| 7168 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 7169 | } |
| 7170 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7171 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 7172 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7173 | { |
| 7174 | struct mgmt_ev_new_csrk ev; |
| 7175 | |
| 7176 | memset(&ev, 0, sizeof(ev)); |
| 7177 | |
| 7178 | /* Devices using resolvable or non-resolvable random addresses |
Florian Grandel | f72186d | 2015-05-26 03:31:09 +0200 | [diff] [blame] | 7179 | * without providing an identity resolving key don't require |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7180 | * to store signature resolving keys. Their addresses will change |
| 7181 | * the next time around. |
| 7182 | * |
| 7183 | * Only when a remote device provides an identity address |
| 7184 | * make sure the signature resolving key is stored. So allow |
| 7185 | * static random and public addresses here. |
| 7186 | */ |
| 7187 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 7188 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 7189 | ev.store_hint = 0x00; |
| 7190 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 7191 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7192 | |
| 7193 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 7194 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 7195 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 7196 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 7197 | |
| 7198 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 7199 | } |
| 7200 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7201 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 7202 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 7203 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7204 | { |
| 7205 | struct mgmt_ev_new_conn_param ev; |
| 7206 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 7207 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 7208 | return; |
| 7209 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7210 | memset(&ev, 0, sizeof(ev)); |
| 7211 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7212 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 7213 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 7214 | ev.min_interval = cpu_to_le16(min_interval); |
| 7215 | ev.max_interval = cpu_to_le16(max_interval); |
| 7216 | ev.latency = cpu_to_le16(latency); |
| 7217 | ev.timeout = cpu_to_le16(timeout); |
| 7218 | |
| 7219 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 7220 | } |
| 7221 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 7222 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 7223 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7224 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7225 | char buf[512]; |
| 7226 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 7227 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7228 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 7229 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 7230 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7231 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 7232 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 7233 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 7234 | /* We must ensure that the EIR Data fields are ordered and |
| 7235 | * unique. Keep it simple for now and avoid the problem by not |
| 7236 | * adding any BR/EDR data to the LE adv. |
| 7237 | */ |
| 7238 | if (conn->le_adv_data_len > 0) { |
| 7239 | memcpy(&ev->eir[eir_len], |
| 7240 | conn->le_adv_data, conn->le_adv_data_len); |
| 7241 | eir_len = conn->le_adv_data_len; |
| 7242 | } else { |
| 7243 | if (name_len > 0) |
| 7244 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 7245 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7246 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 7247 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 7248 | eir_len = eir_append_data(ev->eir, eir_len, |
| 7249 | EIR_CLASS_OF_DEV, |
| 7250 | conn->dev_class, 3); |
| 7251 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7252 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7253 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7254 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 7255 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 7256 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7257 | } |
| 7258 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7259 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7260 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7261 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7262 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 7263 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7264 | |
| 7265 | *sk = cmd->sk; |
| 7266 | sock_hold(*sk); |
| 7267 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7268 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7269 | } |
| 7270 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7271 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7272 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 7273 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 7274 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7275 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 7276 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 7277 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 7278 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7279 | mgmt_pending_remove(cmd); |
| 7280 | } |
| 7281 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7282 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 7283 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7284 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7285 | struct mgmt_mode *cp; |
| 7286 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7287 | cmd = pending_find(MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7288 | if (!cmd) |
| 7289 | return false; |
| 7290 | |
| 7291 | cp = cmd->param; |
| 7292 | if (!cp->val) |
| 7293 | return true; |
| 7294 | |
| 7295 | return false; |
| 7296 | } |
| 7297 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 7298 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7299 | u8 link_type, u8 addr_type, u8 reason, |
| 7300 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7301 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 7302 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7303 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7304 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7305 | /* The connection is still in hci_conn_hash so test for 1 |
| 7306 | * instead of 0 to know if this is the last one. |
| 7307 | */ |
| 7308 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7309 | cancel_delayed_work(&hdev->power_off); |
| 7310 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 7311 | } |
| 7312 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7313 | if (!mgmt_connected) |
| 7314 | return; |
| 7315 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 7316 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 7317 | return; |
| 7318 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7319 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7320 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 7321 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7322 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7323 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7324 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 7325 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7326 | |
| 7327 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 7328 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7329 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 7330 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7331 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7332 | } |
| 7333 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 7334 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7335 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7336 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 7337 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 7338 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7339 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7340 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 7341 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 7342 | hdev); |
| 7343 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7344 | cmd = pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7345 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 7346 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7347 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 7348 | cp = cmd->param; |
| 7349 | |
| 7350 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 7351 | return; |
| 7352 | |
| 7353 | if (cp->addr.type != bdaddr_type) |
| 7354 | return; |
| 7355 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 7356 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7357 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7358 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7359 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7360 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7361 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7362 | { |
| 7363 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7364 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7365 | /* The connection is still in hci_conn_hash so test for 1 |
| 7366 | * instead of 0 to know if this is the last one. |
| 7367 | */ |
| 7368 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7369 | cancel_delayed_work(&hdev->power_off); |
| 7370 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7371 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7372 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 7373 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7374 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 7375 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7376 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7377 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7378 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7379 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7380 | 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] | 7381 | { |
| 7382 | struct mgmt_ev_pin_code_request ev; |
| 7383 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 7384 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 7385 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 7386 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7387 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7388 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7389 | } |
| 7390 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7391 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7392 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7393 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7394 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7395 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7396 | cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7397 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7398 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7399 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7400 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7401 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7402 | } |
| 7403 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7404 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7405 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7406 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7407 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7408 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7409 | cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7410 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7411 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7412 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7413 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7414 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7415 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7416 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7417 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7418 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7419 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7420 | { |
| 7421 | struct mgmt_ev_user_confirm_request ev; |
| 7422 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7423 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7424 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7425 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7426 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 7427 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7428 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7429 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7430 | 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] | 7431 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7432 | } |
| 7433 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7434 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 7435 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7436 | { |
| 7437 | struct mgmt_ev_user_passkey_request ev; |
| 7438 | |
| 7439 | BT_DBG("%s", hdev->name); |
| 7440 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7441 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7442 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7443 | |
| 7444 | 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] | 7445 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7446 | } |
| 7447 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 7448 | 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] | 7449 | u8 link_type, u8 addr_type, u8 status, |
| 7450 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7451 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7452 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7453 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7454 | cmd = pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7455 | if (!cmd) |
| 7456 | return -ENOENT; |
| 7457 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7458 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7459 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7460 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7461 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7462 | } |
| 7463 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7464 | 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] | 7465 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7466 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7467 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7468 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7469 | } |
| 7470 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7471 | 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] | 7472 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7473 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7474 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7475 | status, |
| 7476 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7477 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7478 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7479 | 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] | 7480 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7481 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7482 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7483 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7484 | } |
| 7485 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7486 | 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] | 7487 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7488 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7489 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7490 | status, |
| 7491 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7492 | } |
| 7493 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 7494 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7495 | u8 link_type, u8 addr_type, u32 passkey, |
| 7496 | u8 entered) |
| 7497 | { |
| 7498 | struct mgmt_ev_passkey_notify ev; |
| 7499 | |
| 7500 | BT_DBG("%s", hdev->name); |
| 7501 | |
| 7502 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7503 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7504 | ev.passkey = __cpu_to_le32(passkey); |
| 7505 | ev.entered = entered; |
| 7506 | |
| 7507 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 7508 | } |
| 7509 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7510 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7511 | { |
| 7512 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7513 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7514 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7515 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7516 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 7517 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 7518 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7519 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7520 | cmd = find_pairing(conn); |
| 7521 | |
| 7522 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 7523 | cmd ? cmd->sk : NULL); |
| 7524 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 7525 | if (cmd) { |
| 7526 | cmd->cmd_complete(cmd, status); |
| 7527 | mgmt_pending_remove(cmd); |
| 7528 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7529 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7530 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7531 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7532 | { |
| 7533 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7534 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7535 | |
| 7536 | if (status) { |
| 7537 | u8 mgmt_err = mgmt_status(status); |
| 7538 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7539 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7540 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7541 | } |
| 7542 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7543 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7544 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7545 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7546 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7547 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7548 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7549 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7550 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7551 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7552 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7553 | |
| 7554 | if (match.sk) |
| 7555 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7556 | } |
| 7557 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7558 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7559 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7560 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7561 | struct hci_cp_write_eir cp; |
| 7562 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 7563 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7564 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7565 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 7566 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 7567 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7568 | memset(&cp, 0, sizeof(cp)); |
| 7569 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7570 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7571 | } |
| 7572 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7573 | 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] | 7574 | { |
| 7575 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7576 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7577 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7578 | |
| 7579 | if (status) { |
| 7580 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7581 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7582 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 7583 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7584 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7585 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7586 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7587 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7588 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 7589 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7590 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7591 | } |
| 7592 | |
| 7593 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7594 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7595 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7596 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7597 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7598 | changed = hci_dev_test_and_clear_flag(hdev, |
| 7599 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7600 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7601 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7602 | } |
| 7603 | |
| 7604 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 7605 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7606 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7607 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7608 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 7609 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7610 | sock_put(match.sk); |
| 7611 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7612 | hci_req_init(&req, hdev); |
| 7613 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7614 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7615 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7616 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 7617 | sizeof(enable), &enable); |
Johan Hedberg | b1a8917 | 2015-11-25 16:15:42 +0200 | [diff] [blame] | 7618 | __hci_req_update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7619 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7620 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7621 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7622 | |
| 7623 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7624 | } |
| 7625 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7626 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7627 | { |
| 7628 | struct cmd_lookup *match = data; |
| 7629 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7630 | if (match->sk == NULL) { |
| 7631 | match->sk = cmd->sk; |
| 7632 | sock_hold(match->sk); |
| 7633 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7634 | } |
| 7635 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 7636 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 7637 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7638 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7639 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7640 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 7641 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 7642 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 7643 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7644 | |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7645 | if (!status) { |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7646 | mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, |
| 7647 | 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7648 | ext_info_changed(hdev, NULL); |
| 7649 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7650 | |
| 7651 | if (match.sk) |
| 7652 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7653 | } |
| 7654 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7655 | 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] | 7656 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7657 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7658 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7659 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7660 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7661 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7662 | |
| 7663 | memset(&ev, 0, sizeof(ev)); |
| 7664 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7665 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7666 | |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7667 | cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7668 | if (!cmd) { |
| 7669 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7670 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7671 | /* If this is a HCI command related to powering on the |
| 7672 | * HCI dev don't send any mgmt signals. |
| 7673 | */ |
Johan Hedberg | 333ae95 | 2015-03-17 13:48:47 +0200 | [diff] [blame] | 7674 | if (pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7675 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7676 | } |
| 7677 | |
Marcel Holtmann | 5504c3a | 2016-08-29 06:19:46 +0200 | [diff] [blame] | 7678 | mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 7679 | HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); |
Marcel Holtmann | 321c6fe | 2016-09-01 16:46:23 +0200 | [diff] [blame] | 7680 | ext_info_changed(hdev, cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7681 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7682 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7683 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 7684 | { |
| 7685 | int i; |
| 7686 | |
| 7687 | for (i = 0; i < uuid_count; i++) { |
| 7688 | if (!memcmp(uuid, uuids[i], 16)) |
| 7689 | return true; |
| 7690 | } |
| 7691 | |
| 7692 | return false; |
| 7693 | } |
| 7694 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7695 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 7696 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7697 | u16 parsed = 0; |
| 7698 | |
| 7699 | while (parsed < eir_len) { |
| 7700 | u8 field_len = eir[0]; |
| 7701 | u8 uuid[16]; |
| 7702 | int i; |
| 7703 | |
| 7704 | if (field_len == 0) |
| 7705 | break; |
| 7706 | |
| 7707 | if (eir_len - parsed < field_len + 1) |
| 7708 | break; |
| 7709 | |
| 7710 | switch (eir[1]) { |
| 7711 | case EIR_UUID16_ALL: |
| 7712 | case EIR_UUID16_SOME: |
| 7713 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7714 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7715 | uuid[13] = eir[i + 3]; |
| 7716 | uuid[12] = eir[i + 2]; |
| 7717 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7718 | return true; |
| 7719 | } |
| 7720 | break; |
| 7721 | case EIR_UUID32_ALL: |
| 7722 | case EIR_UUID32_SOME: |
| 7723 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7724 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7725 | uuid[15] = eir[i + 5]; |
| 7726 | uuid[14] = eir[i + 4]; |
| 7727 | uuid[13] = eir[i + 3]; |
| 7728 | uuid[12] = eir[i + 2]; |
| 7729 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7730 | return true; |
| 7731 | } |
| 7732 | break; |
| 7733 | case EIR_UUID128_ALL: |
| 7734 | case EIR_UUID128_SOME: |
| 7735 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 7736 | memcpy(uuid, eir + i + 2, 16); |
| 7737 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7738 | return true; |
| 7739 | } |
| 7740 | break; |
| 7741 | } |
| 7742 | |
| 7743 | parsed += field_len + 1; |
| 7744 | eir += field_len + 1; |
| 7745 | } |
| 7746 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7747 | return false; |
| 7748 | } |
| 7749 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7750 | static void restart_le_scan(struct hci_dev *hdev) |
| 7751 | { |
| 7752 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7753 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7754 | return; |
| 7755 | |
| 7756 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 7757 | hdev->discovery.scan_start + |
| 7758 | hdev->discovery.scan_duration)) |
| 7759 | return; |
| 7760 | |
Johan Hedberg | 7c1fbed | 2015-11-11 08:11:23 +0200 | [diff] [blame] | 7761 | queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart, |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7762 | DISCOV_LE_RESTART_DELAY); |
| 7763 | } |
| 7764 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7765 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 7766 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 7767 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7768 | /* If a RSSI threshold has been specified, and |
| 7769 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 7770 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 7771 | * is set, let it through for further processing, as we might need to |
| 7772 | * restart the scan. |
| 7773 | * |
| 7774 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 7775 | * the results are also dropped. |
| 7776 | */ |
| 7777 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7778 | (rssi == HCI_RSSI_INVALID || |
| 7779 | (rssi < hdev->discovery.rssi && |
| 7780 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 7781 | return false; |
| 7782 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7783 | if (hdev->discovery.uuid_count != 0) { |
| 7784 | /* If a list of UUIDs is provided in filter, results with no |
| 7785 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7786 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7787 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 7788 | hdev->discovery.uuids) && |
| 7789 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 7790 | hdev->discovery.uuid_count, |
| 7791 | hdev->discovery.uuids)) |
| 7792 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7793 | } |
| 7794 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7795 | /* If duplicate filtering does not report RSSI changes, then restart |
| 7796 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7797 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7798 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 7799 | restart_le_scan(hdev); |
| 7800 | |
| 7801 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 7802 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7803 | rssi < hdev->discovery.rssi) |
| 7804 | return false; |
| 7805 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7806 | |
| 7807 | return true; |
| 7808 | } |
| 7809 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7810 | 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] | 7811 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 7812 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7813 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7814 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7815 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7816 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7817 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7818 | /* Don't send events for a non-kernel initiated discovery. With |
| 7819 | * LE one exception is if we have pend_le_reports > 0 in which |
| 7820 | * case we're doing passive scanning and want these events. |
| 7821 | */ |
| 7822 | if (!hci_discovery_active(hdev)) { |
| 7823 | if (link_type == ACL_LINK) |
| 7824 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 7825 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7826 | return; |
| 7827 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 7828 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 7829 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7830 | /* We are using service discovery */ |
| 7831 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 7832 | scan_rsp_len)) |
| 7833 | return; |
| 7834 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 7835 | |
Johan Hedberg | 78b781c | 2016-01-05 13:19:32 +0200 | [diff] [blame] | 7836 | if (hdev->discovery.limited) { |
| 7837 | /* Check for limited discoverable bit */ |
| 7838 | if (dev_class) { |
| 7839 | if (!(dev_class[1] & 0x20)) |
| 7840 | return; |
| 7841 | } else { |
| 7842 | u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); |
| 7843 | if (!flags || !(flags[0] & LE_AD_LIMITED)) |
| 7844 | return; |
| 7845 | } |
| 7846 | } |
| 7847 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7848 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 7849 | * are for the potential CoD field. |
| 7850 | */ |
| 7851 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7852 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 7853 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7854 | memset(buf, 0, sizeof(buf)); |
| 7855 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 7856 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 7857 | * RSSI value was reported as 0 when not available. This behavior |
| 7858 | * is kept when using device discovery. This is required for full |
| 7859 | * backwards compatibility with the API. |
| 7860 | * |
| 7861 | * However when using service discovery, the value 127 will be |
| 7862 | * returned when the RSSI is not available. |
| 7863 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 7864 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 7865 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 7866 | rssi = 0; |
| 7867 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 7868 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 7869 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7870 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 7871 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7872 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7873 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7874 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7875 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7876 | |
Johan Hedberg | 0d3b7f6 | 2016-01-05 13:19:31 +0200 | [diff] [blame] | 7877 | if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
| 7878 | NULL)) |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7879 | 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] | 7880 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7881 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7882 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7883 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7884 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7885 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7886 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 7887 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 7888 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7889 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7890 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7891 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7892 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7893 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7894 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7895 | struct mgmt_ev_device_found *ev; |
| 7896 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 7897 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7898 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7899 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7900 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7901 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7902 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7903 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7904 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7905 | ev->rssi = rssi; |
| 7906 | |
| 7907 | 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] | 7908 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7909 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7910 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7911 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7912 | 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] | 7913 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7914 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7915 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7916 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7917 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 7918 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 7919 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 7920 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7921 | memset(&ev, 0, sizeof(ev)); |
| 7922 | ev.type = hdev->discovery.type; |
| 7923 | ev.discovering = discovering; |
| 7924 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7925 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7926 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 7927 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7928 | static struct hci_mgmt_chan chan = { |
| 7929 | .channel = HCI_CHANNEL_CONTROL, |
| 7930 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 7931 | .handlers = mgmt_handlers, |
Johan Hedberg | 88b94ce | 2015-03-17 13:48:49 +0200 | [diff] [blame] | 7932 | .hdev_init = mgmt_init_hdev, |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7933 | }; |
| 7934 | |
| 7935 | int mgmt_init(void) |
| 7936 | { |
| 7937 | return hci_mgmt_chan_register(&chan); |
| 7938 | } |
| 7939 | |
| 7940 | void mgmt_exit(void) |
| 7941 | { |
| 7942 | hci_mgmt_chan_unregister(&chan); |
| 7943 | } |