1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 BlueZ - Bluetooth protocol stack for Linux 4 5 Copyright (C) 2010 Nokia Corporation 6 Copyright (C) 2011-2012 Intel Corporation 7 8 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 9 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 10 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. 11 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY 12 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 13 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 17 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 18 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 19 SOFTWARE IS DISCLAIMED. 20 */ 21 22 /* Bluetooth HCI Management interface */ 23 24 #include <linux/module.h> 25 #include <linux/unaligned.h> 26 27 #include <net/bluetooth/bluetooth.h> 28 #include <net/bluetooth/hci_core.h> 29 #include <net/bluetooth/hci_sock.h> 30 #include <net/bluetooth/l2cap.h> 31 #include <net/bluetooth/mgmt.h> 32 33 #include "smp.h" 34 #include "mgmt_util.h" 35 #include "mgmt_config.h" 36 #include "msft.h" 37 #include "eir.h" 38 #include "aosp.h" 39 40 #define MGMT_VERSION 1 41 #define MGMT_REVISION 23 42 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, 50 MGMT_OP_SET_BONDABLE, 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, 81 MGMT_OP_SET_DEVICE_ID, 82 MGMT_OP_SET_ADVERTISING, 83 MGMT_OP_SET_BREDR, 84 MGMT_OP_SET_STATIC_ADDRESS, 85 MGMT_OP_SET_SCAN_PARAMS, 86 MGMT_OP_SET_SECURE_CONN, 87 MGMT_OP_SET_DEBUG_KEYS, 88 MGMT_OP_SET_PRIVACY, 89 MGMT_OP_LOAD_IRKS, 90 MGMT_OP_GET_CONN_INFO, 91 MGMT_OP_GET_CLOCK_INFO, 92 MGMT_OP_ADD_DEVICE, 93 MGMT_OP_REMOVE_DEVICE, 94 MGMT_OP_LOAD_CONN_PARAM, 95 MGMT_OP_READ_UNCONF_INDEX_LIST, 96 MGMT_OP_READ_CONFIG_INFO, 97 MGMT_OP_SET_EXTERNAL_CONFIG, 98 MGMT_OP_SET_PUBLIC_ADDRESS, 99 MGMT_OP_START_SERVICE_DISCOVERY, 100 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, 101 MGMT_OP_READ_EXT_INDEX_LIST, 102 MGMT_OP_READ_ADV_FEATURES, 103 MGMT_OP_ADD_ADVERTISING, 104 MGMT_OP_REMOVE_ADVERTISING, 105 MGMT_OP_GET_ADV_SIZE_INFO, 106 MGMT_OP_START_LIMITED_DISCOVERY, 107 MGMT_OP_READ_EXT_INFO, 108 MGMT_OP_SET_APPEARANCE, 109 MGMT_OP_GET_PHY_CONFIGURATION, 110 MGMT_OP_SET_PHY_CONFIGURATION, 111 MGMT_OP_SET_BLOCKED_KEYS, 112 MGMT_OP_SET_WIDEBAND_SPEECH, 113 MGMT_OP_READ_CONTROLLER_CAP, 114 MGMT_OP_READ_EXP_FEATURES_INFO, 115 MGMT_OP_SET_EXP_FEATURE, 116 MGMT_OP_READ_DEF_SYSTEM_CONFIG, 117 MGMT_OP_SET_DEF_SYSTEM_CONFIG, 118 MGMT_OP_READ_DEF_RUNTIME_CONFIG, 119 MGMT_OP_SET_DEF_RUNTIME_CONFIG, 120 MGMT_OP_GET_DEVICE_FLAGS, 121 MGMT_OP_SET_DEVICE_FLAGS, 122 MGMT_OP_READ_ADV_MONITOR_FEATURES, 123 MGMT_OP_ADD_ADV_PATTERNS_MONITOR, 124 MGMT_OP_REMOVE_ADV_MONITOR, 125 MGMT_OP_ADD_EXT_ADV_PARAMS, 126 MGMT_OP_ADD_EXT_ADV_DATA, 127 MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, 128 MGMT_OP_SET_MESH_RECEIVER, 129 MGMT_OP_MESH_READ_FEATURES, 130 MGMT_OP_MESH_SEND, 131 MGMT_OP_MESH_SEND_CANCEL, 132 MGMT_OP_HCI_CMD_SYNC, 133 }; 134 135 static const u16 mgmt_events[] = { 136 MGMT_EV_CONTROLLER_ERROR, 137 MGMT_EV_INDEX_ADDED, 138 MGMT_EV_INDEX_REMOVED, 139 MGMT_EV_NEW_SETTINGS, 140 MGMT_EV_CLASS_OF_DEV_CHANGED, 141 MGMT_EV_LOCAL_NAME_CHANGED, 142 MGMT_EV_NEW_LINK_KEY, 143 MGMT_EV_NEW_LONG_TERM_KEY, 144 MGMT_EV_DEVICE_CONNECTED, 145 MGMT_EV_DEVICE_DISCONNECTED, 146 MGMT_EV_CONNECT_FAILED, 147 MGMT_EV_PIN_CODE_REQUEST, 148 MGMT_EV_USER_CONFIRM_REQUEST, 149 MGMT_EV_USER_PASSKEY_REQUEST, 150 MGMT_EV_AUTH_FAILED, 151 MGMT_EV_DEVICE_FOUND, 152 MGMT_EV_DISCOVERING, 153 MGMT_EV_DEVICE_BLOCKED, 154 MGMT_EV_DEVICE_UNBLOCKED, 155 MGMT_EV_DEVICE_UNPAIRED, 156 MGMT_EV_PASSKEY_NOTIFY, 157 MGMT_EV_NEW_IRK, 158 MGMT_EV_NEW_CSRK, 159 MGMT_EV_DEVICE_ADDED, 160 MGMT_EV_DEVICE_REMOVED, 161 MGMT_EV_NEW_CONN_PARAM, 162 MGMT_EV_UNCONF_INDEX_ADDED, 163 MGMT_EV_UNCONF_INDEX_REMOVED, 164 MGMT_EV_NEW_CONFIG_OPTIONS, 165 MGMT_EV_EXT_INDEX_ADDED, 166 MGMT_EV_EXT_INDEX_REMOVED, 167 MGMT_EV_LOCAL_OOB_DATA_UPDATED, 168 MGMT_EV_ADVERTISING_ADDED, 169 MGMT_EV_ADVERTISING_REMOVED, 170 MGMT_EV_EXT_INFO_CHANGED, 171 MGMT_EV_PHY_CONFIGURATION_CHANGED, 172 MGMT_EV_EXP_FEATURE_CHANGED, 173 MGMT_EV_DEVICE_FLAGS_CHANGED, 174 MGMT_EV_ADV_MONITOR_ADDED, 175 MGMT_EV_ADV_MONITOR_REMOVED, 176 MGMT_EV_CONTROLLER_SUSPEND, 177 MGMT_EV_CONTROLLER_RESUME, 178 MGMT_EV_ADV_MONITOR_DEVICE_FOUND, 179 MGMT_EV_ADV_MONITOR_DEVICE_LOST, 180 MGMT_EV_SECURITY_LEVEL_CHANGED, 181 }; 182 183 static const u16 mgmt_untrusted_commands[] = { 184 MGMT_OP_READ_INDEX_LIST, 185 MGMT_OP_READ_INFO, 186 MGMT_OP_READ_UNCONF_INDEX_LIST, 187 MGMT_OP_READ_CONFIG_INFO, 188 MGMT_OP_READ_EXT_INDEX_LIST, 189 MGMT_OP_READ_EXT_INFO, 190 MGMT_OP_READ_CONTROLLER_CAP, 191 MGMT_OP_READ_EXP_FEATURES_INFO, 192 MGMT_OP_READ_DEF_SYSTEM_CONFIG, 193 MGMT_OP_READ_DEF_RUNTIME_CONFIG, 194 }; 195 196 static const u16 mgmt_untrusted_events[] = { 197 MGMT_EV_INDEX_ADDED, 198 MGMT_EV_INDEX_REMOVED, 199 MGMT_EV_NEW_SETTINGS, 200 MGMT_EV_CLASS_OF_DEV_CHANGED, 201 MGMT_EV_LOCAL_NAME_CHANGED, 202 MGMT_EV_UNCONF_INDEX_ADDED, 203 MGMT_EV_UNCONF_INDEX_REMOVED, 204 MGMT_EV_NEW_CONFIG_OPTIONS, 205 MGMT_EV_EXT_INDEX_ADDED, 206 MGMT_EV_EXT_INDEX_REMOVED, 207 MGMT_EV_EXT_INFO_CHANGED, 208 MGMT_EV_EXP_FEATURE_CHANGED, 209 }; 210 211 #define CACHE_TIMEOUT secs_to_jiffies(2) 212 213 #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ 214 "\x00\x00\x00\x00\x00\x00\x00\x00" 215 216 /* HCI to MGMT error code conversion table */ 217 static const u8 mgmt_status_table[] = { 218 MGMT_STATUS_SUCCESS, 219 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ 220 MGMT_STATUS_NOT_CONNECTED, /* No Connection */ 221 MGMT_STATUS_FAILED, /* Hardware Failure */ 222 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ 223 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ 224 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ 225 MGMT_STATUS_NO_RESOURCES, /* Memory Full */ 226 MGMT_STATUS_TIMEOUT, /* Connection Timeout */ 227 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ 228 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ 229 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ 230 MGMT_STATUS_BUSY, /* Command Disallowed */ 231 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ 232 MGMT_STATUS_REJECTED, /* Rejected Security */ 233 MGMT_STATUS_REJECTED, /* Rejected Personal */ 234 MGMT_STATUS_TIMEOUT, /* Host Timeout */ 235 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ 236 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ 237 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ 238 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ 239 MGMT_STATUS_DISCONNECTED, /* OE Power Off */ 240 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ 241 MGMT_STATUS_BUSY, /* Repeated Attempts */ 242 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ 243 MGMT_STATUS_FAILED, /* Unknown LMP PDU */ 244 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ 245 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ 246 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ 247 MGMT_STATUS_REJECTED, /* Air Mode Rejected */ 248 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ 249 MGMT_STATUS_FAILED, /* Unspecified Error */ 250 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ 251 MGMT_STATUS_FAILED, /* Role Change Not Allowed */ 252 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ 253 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ 254 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ 255 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ 256 MGMT_STATUS_FAILED, /* Unit Link Key Used */ 257 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ 258 MGMT_STATUS_TIMEOUT, /* Instant Passed */ 259 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ 260 MGMT_STATUS_FAILED, /* Transaction Collision */ 261 MGMT_STATUS_FAILED, /* Reserved for future use */ 262 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ 263 MGMT_STATUS_REJECTED, /* QoS Rejected */ 264 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ 265 MGMT_STATUS_REJECTED, /* Insufficient Security */ 266 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ 267 MGMT_STATUS_FAILED, /* Reserved for future use */ 268 MGMT_STATUS_BUSY, /* Role Switch Pending */ 269 MGMT_STATUS_FAILED, /* Reserved for future use */ 270 MGMT_STATUS_FAILED, /* Slot Violation */ 271 MGMT_STATUS_FAILED, /* Role Switch Failed */ 272 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ 273 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ 274 MGMT_STATUS_BUSY, /* Host Busy Pairing */ 275 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ 276 MGMT_STATUS_BUSY, /* Controller Busy */ 277 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ 278 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ 279 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ 280 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ 281 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ 282 }; 283 284 static u8 mgmt_errno_status(int err) 285 { 286 switch (err) { 287 case 0: 288 return MGMT_STATUS_SUCCESS; 289 case -EPERM: 290 return MGMT_STATUS_REJECTED; 291 case -EINVAL: 292 return MGMT_STATUS_INVALID_PARAMS; 293 case -EOPNOTSUPP: 294 return MGMT_STATUS_NOT_SUPPORTED; 295 case -EBUSY: 296 return MGMT_STATUS_BUSY; 297 case -ETIMEDOUT: 298 return MGMT_STATUS_AUTH_FAILED; 299 case -ENOMEM: 300 return MGMT_STATUS_NO_RESOURCES; 301 case -EISCONN: 302 return MGMT_STATUS_ALREADY_CONNECTED; 303 case -ENOTCONN: 304 return MGMT_STATUS_DISCONNECTED; 305 case -ECANCELED: 306 return MGMT_STATUS_CANCELLED; 307 case -ERFKILL: 308 return MGMT_STATUS_RFKILLED; 309 } 310 311 return MGMT_STATUS_FAILED; 312 } 313 314 static u8 mgmt_status(int err) 315 { 316 if (err < 0) 317 return mgmt_errno_status(err); 318 319 if (err < ARRAY_SIZE(mgmt_status_table)) 320 return mgmt_status_table[err]; 321 322 return MGMT_STATUS_FAILED; 323 } 324 325 static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, 326 u16 len, int flag) 327 { 328 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, 329 flag, NULL); 330 } 331 332 static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data, 333 u16 len, int flag, struct sock *skip_sk) 334 { 335 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, 336 flag, skip_sk); 337 } 338 339 static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, 340 struct sock *skip_sk) 341 { 342 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, 343 HCI_SOCK_TRUSTED, skip_sk); 344 } 345 346 static int mgmt_event_skb(struct sk_buff *skb, struct sock *skip_sk) 347 { 348 return mgmt_send_event_skb(HCI_CHANNEL_CONTROL, skb, HCI_SOCK_TRUSTED, 349 skip_sk); 350 } 351 352 static u8 le_addr_type(u8 mgmt_addr_type) 353 { 354 if (mgmt_addr_type == BDADDR_LE_PUBLIC) 355 return ADDR_LE_DEV_PUBLIC; 356 else 357 return ADDR_LE_DEV_RANDOM; 358 } 359 360 void mgmt_fill_version_info(void *ver) 361 { 362 struct mgmt_rp_read_version *rp = ver; 363 364 rp->version = MGMT_VERSION; 365 rp->revision = cpu_to_le16(MGMT_REVISION); 366 } 367 368 static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, 369 u16 data_len) 370 { 371 struct mgmt_rp_read_version rp; 372 373 bt_dev_dbg(hdev, "sock %p", sk); 374 375 mgmt_fill_version_info(&rp); 376 377 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, 378 &rp, sizeof(rp)); 379 } 380 381 static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, 382 u16 data_len) 383 { 384 struct mgmt_rp_read_commands *rp; 385 u16 num_commands, num_events; 386 size_t rp_size; 387 int i, err; 388 389 bt_dev_dbg(hdev, "sock %p", sk); 390 391 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { 392 num_commands = ARRAY_SIZE(mgmt_commands); 393 num_events = ARRAY_SIZE(mgmt_events); 394 } else { 395 num_commands = ARRAY_SIZE(mgmt_untrusted_commands); 396 num_events = ARRAY_SIZE(mgmt_untrusted_events); 397 } 398 399 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); 400 401 rp = kmalloc(rp_size, GFP_KERNEL); 402 if (!rp) 403 return -ENOMEM; 404 405 rp->num_commands = cpu_to_le16(num_commands); 406 rp->num_events = cpu_to_le16(num_events); 407 408 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) { 409 __le16 *opcode = rp->opcodes; 410 411 for (i = 0; i < num_commands; i++, opcode++) 412 put_unaligned_le16(mgmt_commands[i], opcode); 413 414 for (i = 0; i < num_events; i++, opcode++) 415 put_unaligned_le16(mgmt_events[i], opcode); 416 } else { 417 __le16 *opcode = rp->opcodes; 418 419 for (i = 0; i < num_commands; i++, opcode++) 420 put_unaligned_le16(mgmt_untrusted_commands[i], opcode); 421 422 for (i = 0; i < num_events; i++, opcode++) 423 put_unaligned_le16(mgmt_untrusted_events[i], opcode); 424 } 425 426 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, 427 rp, rp_size); 428 kfree(rp); 429 430 return err; 431 } 432 433 static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, 434 u16 data_len) 435 { 436 struct mgmt_rp_read_index_list *rp; 437 struct hci_dev *d; 438 size_t rp_len; 439 u16 count; 440 int err; 441 442 bt_dev_dbg(hdev, "sock %p", sk); 443 444 read_lock(&hci_dev_list_lock); 445 446 count = 0; 447 list_for_each_entry(d, &hci_dev_list, list) 448 count++; 449 450 rp_len = sizeof(*rp) + (2 * count); 451 rp = kmalloc(rp_len, GFP_ATOMIC); 452 if (!rp) { 453 read_unlock(&hci_dev_list_lock); 454 return -ENOMEM; 455 } 456 457 count = 0; 458 list_for_each_entry(d, &hci_dev_list, list) { 459 if (hci_dev_test_flag(d, HCI_SETUP) || 460 hci_dev_test_flag(d, HCI_CONFIG) || 461 hci_dev_test_flag(d, HCI_USER_CHANNEL)) 462 continue; 463 464 /* Devices marked as raw-only are neither configured 465 * nor unconfigured controllers. 466 */ 467 if (hci_test_quirk(d, HCI_QUIRK_RAW_DEVICE)) 468 continue; 469 470 if (!hci_dev_test_flag(d, HCI_UNCONFIGURED)) { 471 rp->index[count++] = cpu_to_le16(d->id); 472 bt_dev_dbg(hdev, "Added hci%u", d->id); 473 } 474 } 475 476 rp->num_controllers = cpu_to_le16(count); 477 rp_len = sizeof(*rp) + (2 * count); 478 479 read_unlock(&hci_dev_list_lock); 480 481 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, 482 0, rp, rp_len); 483 484 kfree(rp); 485 486 return err; 487 } 488 489 static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, 490 void *data, u16 data_len) 491 { 492 struct mgmt_rp_read_unconf_index_list *rp; 493 struct hci_dev *d; 494 size_t rp_len; 495 u16 count; 496 int err; 497 498 bt_dev_dbg(hdev, "sock %p", sk); 499 500 read_lock(&hci_dev_list_lock); 501 502 count = list_count_nodes(&hci_dev_list); 503 504 rp_len = sizeof(*rp) + (sizeof(__le16) * count); 505 rp = kmalloc(rp_len, GFP_ATOMIC); 506 if (!rp) { 507 read_unlock(&hci_dev_list_lock); 508 return -ENOMEM; 509 } 510 511 count = 0; 512 list_for_each_entry(d, &hci_dev_list, list) { 513 if (hci_dev_test_flag(d, HCI_SETUP) || 514 hci_dev_test_flag(d, HCI_CONFIG) || 515 hci_dev_test_flag(d, HCI_USER_CHANNEL)) 516 continue; 517 518 /* Devices marked as raw-only are neither configured 519 * nor unconfigured controllers. 520 */ 521 if (hci_test_quirk(d, HCI_QUIRK_RAW_DEVICE)) 522 continue; 523 524 if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) { 525 rp->index[count++] = cpu_to_le16(d->id); 526 bt_dev_dbg(hdev, "Added hci%u", d->id); 527 } 528 } 529 530 rp->num_controllers = cpu_to_le16(count); 531 rp_len = sizeof(*rp) + (2 * count); 532 533 read_unlock(&hci_dev_list_lock); 534 535 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, 536 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); 537 538 kfree(rp); 539 540 return err; 541 } 542 543 static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, 544 void *data, u16 data_len) 545 { 546 struct mgmt_rp_read_ext_index_list *rp; 547 struct hci_dev *d; 548 u16 count; 549 int err; 550 551 bt_dev_dbg(hdev, "sock %p", sk); 552 553 read_lock(&hci_dev_list_lock); 554 555 count = 0; 556 list_for_each_entry(d, &hci_dev_list, list) 557 count++; 558 559 rp = kmalloc_flex(*rp, entry, count, GFP_ATOMIC); 560 if (!rp) { 561 read_unlock(&hci_dev_list_lock); 562 return -ENOMEM; 563 } 564 565 count = 0; 566 list_for_each_entry(d, &hci_dev_list, list) { 567 if (hci_dev_test_flag(d, HCI_SETUP) || 568 hci_dev_test_flag(d, HCI_CONFIG) || 569 hci_dev_test_flag(d, HCI_USER_CHANNEL)) 570 continue; 571 572 /* Devices marked as raw-only are neither configured 573 * nor unconfigured controllers. 574 */ 575 if (hci_test_quirk(d, HCI_QUIRK_RAW_DEVICE)) 576 continue; 577 578 if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) 579 rp->entry[count].type = 0x01; 580 else 581 rp->entry[count].type = 0x00; 582 583 rp->entry[count].bus = d->bus; 584 rp->entry[count++].index = cpu_to_le16(d->id); 585 bt_dev_dbg(hdev, "Added hci%u", d->id); 586 } 587 588 rp->num_controllers = cpu_to_le16(count); 589 590 read_unlock(&hci_dev_list_lock); 591 592 /* If this command is called at least once, then all the 593 * default index and unconfigured index events are disabled 594 * and from now on only extended index events are used. 595 */ 596 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); 597 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); 598 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); 599 600 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, 601 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, 602 struct_size(rp, entry, count)); 603 604 kfree(rp); 605 606 return err; 607 } 608 609 static bool is_configured(struct hci_dev *hdev) 610 { 611 if (hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG) && 612 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) 613 return false; 614 615 if ((hci_test_quirk(hdev, HCI_QUIRK_INVALID_BDADDR) || 616 hci_test_quirk(hdev, HCI_QUIRK_USE_BDADDR_PROPERTY)) && 617 !bacmp(&hdev->public_addr, BDADDR_ANY)) 618 return false; 619 620 return true; 621 } 622 623 static __le32 get_missing_options(struct hci_dev *hdev) 624 { 625 u32 options = 0; 626 627 if (hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG) && 628 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) 629 options |= MGMT_OPTION_EXTERNAL_CONFIG; 630 631 if ((hci_test_quirk(hdev, HCI_QUIRK_INVALID_BDADDR) || 632 hci_test_quirk(hdev, HCI_QUIRK_USE_BDADDR_PROPERTY)) && 633 !bacmp(&hdev->public_addr, BDADDR_ANY)) 634 options |= MGMT_OPTION_PUBLIC_ADDRESS; 635 636 return cpu_to_le32(options); 637 } 638 639 static int new_options(struct hci_dev *hdev, struct sock *skip) 640 { 641 __le32 options = get_missing_options(hdev); 642 643 return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, 644 sizeof(options), HCI_MGMT_OPTION_EVENTS, skip); 645 } 646 647 static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) 648 { 649 __le32 options = get_missing_options(hdev); 650 651 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, 652 sizeof(options)); 653 } 654 655 static int read_config_info(struct sock *sk, struct hci_dev *hdev, 656 void *data, u16 data_len) 657 { 658 struct mgmt_rp_read_config_info rp; 659 u32 options = 0; 660 661 bt_dev_dbg(hdev, "sock %p", sk); 662 663 hci_dev_lock(hdev); 664 665 memset(&rp, 0, sizeof(rp)); 666 rp.manufacturer = cpu_to_le16(hdev->manufacturer); 667 668 if (hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG)) 669 options |= MGMT_OPTION_EXTERNAL_CONFIG; 670 671 if (hdev->set_bdaddr) 672 options |= MGMT_OPTION_PUBLIC_ADDRESS; 673 674 rp.supported_options = cpu_to_le32(options); 675 rp.missing_options = get_missing_options(hdev); 676 677 hci_dev_unlock(hdev); 678 679 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, 680 &rp, sizeof(rp)); 681 } 682 683 static u32 get_supported_phys(struct hci_dev *hdev) 684 { 685 u32 supported_phys = 0; 686 687 if (lmp_bredr_capable(hdev)) { 688 supported_phys |= MGMT_PHY_BR_1M_1SLOT; 689 690 if (hdev->features[0][0] & LMP_3SLOT) 691 supported_phys |= MGMT_PHY_BR_1M_3SLOT; 692 693 if (hdev->features[0][0] & LMP_5SLOT) 694 supported_phys |= MGMT_PHY_BR_1M_5SLOT; 695 696 if (lmp_edr_2m_capable(hdev)) { 697 supported_phys |= MGMT_PHY_EDR_2M_1SLOT; 698 699 if (lmp_edr_3slot_capable(hdev)) 700 supported_phys |= MGMT_PHY_EDR_2M_3SLOT; 701 702 if (lmp_edr_5slot_capable(hdev)) 703 supported_phys |= MGMT_PHY_EDR_2M_5SLOT; 704 705 if (lmp_edr_3m_capable(hdev)) { 706 supported_phys |= MGMT_PHY_EDR_3M_1SLOT; 707 708 if (lmp_edr_3slot_capable(hdev)) 709 supported_phys |= MGMT_PHY_EDR_3M_3SLOT; 710 711 if (lmp_edr_5slot_capable(hdev)) 712 supported_phys |= MGMT_PHY_EDR_3M_5SLOT; 713 } 714 } 715 } 716 717 if (lmp_le_capable(hdev)) { 718 supported_phys |= MGMT_PHY_LE_1M_TX; 719 supported_phys |= MGMT_PHY_LE_1M_RX; 720 721 if (hdev->le_features[1] & HCI_LE_PHY_2M) { 722 supported_phys |= MGMT_PHY_LE_2M_TX; 723 supported_phys |= MGMT_PHY_LE_2M_RX; 724 } 725 726 if (hdev->le_features[1] & HCI_LE_PHY_CODED) { 727 supported_phys |= MGMT_PHY_LE_CODED_TX; 728 supported_phys |= MGMT_PHY_LE_CODED_RX; 729 } 730 } 731 732 return supported_phys; 733 } 734 735 static u32 get_selected_phys(struct hci_dev *hdev) 736 { 737 u32 selected_phys = 0; 738 739 if (lmp_bredr_capable(hdev)) { 740 selected_phys |= MGMT_PHY_BR_1M_1SLOT; 741 742 if (hdev->pkt_type & (HCI_DM3 | HCI_DH3)) 743 selected_phys |= MGMT_PHY_BR_1M_3SLOT; 744 745 if (hdev->pkt_type & (HCI_DM5 | HCI_DH5)) 746 selected_phys |= MGMT_PHY_BR_1M_5SLOT; 747 748 if (lmp_edr_2m_capable(hdev)) { 749 if (!(hdev->pkt_type & HCI_2DH1)) 750 selected_phys |= MGMT_PHY_EDR_2M_1SLOT; 751 752 if (lmp_edr_3slot_capable(hdev) && 753 !(hdev->pkt_type & HCI_2DH3)) 754 selected_phys |= MGMT_PHY_EDR_2M_3SLOT; 755 756 if (lmp_edr_5slot_capable(hdev) && 757 !(hdev->pkt_type & HCI_2DH5)) 758 selected_phys |= MGMT_PHY_EDR_2M_5SLOT; 759 760 if (lmp_edr_3m_capable(hdev)) { 761 if (!(hdev->pkt_type & HCI_3DH1)) 762 selected_phys |= MGMT_PHY_EDR_3M_1SLOT; 763 764 if (lmp_edr_3slot_capable(hdev) && 765 !(hdev->pkt_type & HCI_3DH3)) 766 selected_phys |= MGMT_PHY_EDR_3M_3SLOT; 767 768 if (lmp_edr_5slot_capable(hdev) && 769 !(hdev->pkt_type & HCI_3DH5)) 770 selected_phys |= MGMT_PHY_EDR_3M_5SLOT; 771 } 772 } 773 } 774 775 if (lmp_le_capable(hdev)) { 776 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M) 777 selected_phys |= MGMT_PHY_LE_1M_TX; 778 779 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M) 780 selected_phys |= MGMT_PHY_LE_1M_RX; 781 782 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M) 783 selected_phys |= MGMT_PHY_LE_2M_TX; 784 785 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M) 786 selected_phys |= MGMT_PHY_LE_2M_RX; 787 788 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED) 789 selected_phys |= MGMT_PHY_LE_CODED_TX; 790 791 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED) 792 selected_phys |= MGMT_PHY_LE_CODED_RX; 793 } 794 795 return selected_phys; 796 } 797 798 static u32 get_configurable_phys(struct hci_dev *hdev) 799 { 800 return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT & 801 ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX); 802 } 803 804 static u32 get_supported_settings(struct hci_dev *hdev) 805 { 806 u32 settings = 0; 807 808 settings |= MGMT_SETTING_POWERED; 809 settings |= MGMT_SETTING_BONDABLE; 810 settings |= MGMT_SETTING_DEBUG_KEYS; 811 settings |= MGMT_SETTING_CONNECTABLE; 812 settings |= MGMT_SETTING_DISCOVERABLE; 813 814 if (lmp_bredr_capable(hdev)) { 815 if (hdev->hci_ver >= BLUETOOTH_VER_1_2) 816 settings |= MGMT_SETTING_FAST_CONNECTABLE; 817 settings |= MGMT_SETTING_BREDR; 818 settings |= MGMT_SETTING_LINK_SECURITY; 819 820 if (lmp_ssp_capable(hdev)) { 821 settings |= MGMT_SETTING_SSP; 822 } 823 824 if (lmp_sc_capable(hdev)) 825 settings |= MGMT_SETTING_SECURE_CONN; 826 827 if (hci_test_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED)) 828 settings |= MGMT_SETTING_WIDEBAND_SPEECH; 829 } 830 831 if (lmp_le_capable(hdev)) { 832 settings |= MGMT_SETTING_LE; 833 settings |= MGMT_SETTING_SECURE_CONN; 834 settings |= MGMT_SETTING_PRIVACY; 835 settings |= MGMT_SETTING_STATIC_ADDRESS; 836 settings |= MGMT_SETTING_ADVERTISING; 837 } 838 839 if (hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG) || hdev->set_bdaddr) 840 settings |= MGMT_SETTING_CONFIGURATION; 841 842 if (cis_central_capable(hdev)) 843 settings |= MGMT_SETTING_CIS_CENTRAL; 844 845 if (cis_peripheral_capable(hdev)) 846 settings |= MGMT_SETTING_CIS_PERIPHERAL; 847 848 if (bis_capable(hdev)) 849 settings |= MGMT_SETTING_ISO_BROADCASTER; 850 851 if (sync_recv_capable(hdev)) 852 settings |= MGMT_SETTING_ISO_SYNC_RECEIVER; 853 854 if (ll_privacy_capable(hdev)) 855 settings |= MGMT_SETTING_LL_PRIVACY; 856 857 if (past_sender_capable(hdev)) 858 settings |= MGMT_SETTING_PAST_SENDER; 859 860 if (past_receiver_capable(hdev)) 861 settings |= MGMT_SETTING_PAST_RECEIVER; 862 863 if (le_sci_capable(hdev)) 864 settings |= MGMT_SETTING_SCI; 865 866 settings |= MGMT_SETTING_PHY_CONFIGURATION; 867 868 return settings; 869 } 870 871 static u32 get_current_settings(struct hci_dev *hdev) 872 { 873 u32 settings = 0; 874 875 if (hdev_is_powered(hdev)) 876 settings |= MGMT_SETTING_POWERED; 877 878 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) 879 settings |= MGMT_SETTING_CONNECTABLE; 880 881 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) 882 settings |= MGMT_SETTING_FAST_CONNECTABLE; 883 884 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) 885 settings |= MGMT_SETTING_DISCOVERABLE; 886 887 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) 888 settings |= MGMT_SETTING_BONDABLE; 889 890 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 891 settings |= MGMT_SETTING_BREDR; 892 893 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 894 settings |= MGMT_SETTING_LE; 895 896 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) 897 settings |= MGMT_SETTING_LINK_SECURITY; 898 899 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) 900 settings |= MGMT_SETTING_SSP; 901 902 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) 903 settings |= MGMT_SETTING_ADVERTISING; 904 905 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) 906 settings |= MGMT_SETTING_SECURE_CONN; 907 908 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) 909 settings |= MGMT_SETTING_DEBUG_KEYS; 910 911 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) 912 settings |= MGMT_SETTING_PRIVACY; 913 914 /* The current setting for static address has two purposes. The 915 * first is to indicate if the static address will be used and 916 * the second is to indicate if it is actually set. 917 * 918 * This means if the static address is not configured, this flag 919 * will never be set. If the address is configured, then if the 920 * address is actually used decides if the flag is set or not. 921 * 922 * For single mode LE only controllers and dual-mode controllers 923 * with BR/EDR disabled, the existence of the static address will 924 * be evaluated. 925 */ 926 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || 927 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || 928 !bacmp(&hdev->bdaddr, BDADDR_ANY)) { 929 if (bacmp(&hdev->static_addr, BDADDR_ANY)) 930 settings |= MGMT_SETTING_STATIC_ADDRESS; 931 } 932 933 if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED)) 934 settings |= MGMT_SETTING_WIDEBAND_SPEECH; 935 936 if (cis_central_enabled(hdev)) 937 settings |= MGMT_SETTING_CIS_CENTRAL; 938 939 if (cis_peripheral_enabled(hdev)) 940 settings |= MGMT_SETTING_CIS_PERIPHERAL; 941 942 if (bis_enabled(hdev)) 943 settings |= MGMT_SETTING_ISO_BROADCASTER; 944 945 if (sync_recv_enabled(hdev)) 946 settings |= MGMT_SETTING_ISO_SYNC_RECEIVER; 947 948 if (ll_privacy_enabled(hdev)) 949 settings |= MGMT_SETTING_LL_PRIVACY; 950 951 if (past_sender_enabled(hdev)) 952 settings |= MGMT_SETTING_PAST_SENDER; 953 954 if (past_receiver_enabled(hdev)) 955 settings |= MGMT_SETTING_PAST_RECEIVER; 956 957 if (le_sci_enabled(hdev)) 958 settings |= MGMT_SETTING_SCI; 959 960 return settings; 961 } 962 963 static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev) 964 { 965 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev); 966 } 967 968 u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev) 969 { 970 struct mgmt_pending_cmd *cmd; 971 972 /* If there's a pending mgmt command the flags will not yet have 973 * their final values, so check for this first. 974 */ 975 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); 976 if (cmd) { 977 struct mgmt_mode *cp = cmd->param; 978 if (cp->val == 0x01) 979 return LE_AD_GENERAL; 980 else if (cp->val == 0x02) 981 return LE_AD_LIMITED; 982 } else { 983 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) 984 return LE_AD_LIMITED; 985 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) 986 return LE_AD_GENERAL; 987 } 988 989 return 0; 990 } 991 992 bool mgmt_get_connectable(struct hci_dev *hdev) 993 { 994 struct mgmt_pending_cmd *cmd; 995 996 /* If there's a pending mgmt command the flag will not yet have 997 * it's final value, so check for this first. 998 */ 999 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev); 1000 if (cmd) { 1001 struct mgmt_mode *cp = cmd->param; 1002 1003 return cp->val; 1004 } 1005 1006 return hci_dev_test_flag(hdev, HCI_CONNECTABLE); 1007 } 1008 1009 static int service_cache_sync(struct hci_dev *hdev, void *data) 1010 { 1011 hci_update_eir_sync(hdev); 1012 hci_update_class_sync(hdev); 1013 1014 return 0; 1015 } 1016 1017 static void service_cache_off(struct work_struct *work) 1018 { 1019 struct hci_dev *hdev = container_of(work, struct hci_dev, 1020 service_cache.work); 1021 1022 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) 1023 return; 1024 1025 hci_cmd_sync_queue(hdev, service_cache_sync, NULL, NULL); 1026 } 1027 1028 static int rpa_expired_sync(struct hci_dev *hdev, void *data) 1029 { 1030 /* The generation of a new RPA and programming it into the 1031 * controller happens in the hci_req_enable_advertising() 1032 * function. 1033 */ 1034 if (ext_adv_capable(hdev)) 1035 return hci_start_ext_adv_sync(hdev, hdev->cur_adv_instance); 1036 else 1037 return hci_enable_advertising_sync(hdev); 1038 } 1039 1040 static void rpa_expired(struct work_struct *work) 1041 { 1042 struct hci_dev *hdev = container_of(work, struct hci_dev, 1043 rpa_expired.work); 1044 1045 bt_dev_dbg(hdev, ""); 1046 1047 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); 1048 1049 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) 1050 return; 1051 1052 hci_cmd_sync_queue(hdev, rpa_expired_sync, NULL, NULL); 1053 } 1054 1055 static int set_discoverable_sync(struct hci_dev *hdev, void *data); 1056 1057 static void discov_off(struct work_struct *work) 1058 { 1059 struct hci_dev *hdev = container_of(work, struct hci_dev, 1060 discov_off.work); 1061 1062 bt_dev_dbg(hdev, ""); 1063 1064 hci_dev_lock(hdev); 1065 1066 /* When discoverable timeout triggers, then just make sure 1067 * the limited discoverable flag is cleared. Even in the case 1068 * of a timeout triggered from general discoverable, it is 1069 * safe to unconditionally clear the flag. 1070 */ 1071 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 1072 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 1073 hdev->discov_timeout = 0; 1074 1075 hci_cmd_sync_queue(hdev, set_discoverable_sync, NULL, NULL); 1076 1077 mgmt_new_settings(hdev); 1078 1079 hci_dev_unlock(hdev); 1080 } 1081 1082 static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev); 1083 1084 /* This function requires the caller holds hdev->lock */ 1085 static void mesh_send_complete(struct hci_dev *hdev, 1086 struct mgmt_mesh_tx *mesh_tx, bool silent) 1087 { 1088 u8 handle = mesh_tx->handle; 1089 1090 if (!silent) 1091 mgmt_event(MGMT_EV_MESH_PACKET_CMPLT, hdev, &handle, 1092 sizeof(handle), NULL); 1093 1094 mgmt_mesh_remove(mesh_tx); 1095 } 1096 1097 /* The cmd_sync entries identify the transmission by handle rather than by 1098 * pointer since the mesh_tx can be removed while the entry is queued or 1099 * running. 1100 */ 1101 #define MESH_TX_DATA(_handle) ((void *)(unsigned long)(_handle)) 1102 #define MESH_TX_HANDLE(_data) ((u8)(unsigned long)(_data)) 1103 1104 static int mesh_send_sync(struct hci_dev *hdev, void *data); 1105 static void mesh_send_start_complete(struct hci_dev *hdev, void *data, int err); 1106 1107 /* Start sending the first transmission unless one is already in progress, 1108 * HCI_MESH_SENDING is set while the first transmission is being sent. 1109 * Completion is not reported for @new, the handle of a transmission userspace 1110 * wasn't told about yet. 1111 * 1112 * This function requires the caller holds hdev->lock. 1113 */ 1114 static void __mesh_next(struct hci_dev *hdev, u8 new) 1115 { 1116 struct mgmt_mesh_tx *mesh_tx; 1117 int err; 1118 1119 while (!hci_dev_test_flag(hdev, HCI_MESH_SENDING)) { 1120 mesh_tx = mgmt_mesh_next(hdev, NULL); 1121 if (!mesh_tx) 1122 return; 1123 1124 err = hci_cmd_sync_queue(hdev, mesh_send_sync, 1125 MESH_TX_DATA(mesh_tx->handle), 1126 mesh_send_start_complete); 1127 if (err < 0) 1128 mesh_send_complete(hdev, mesh_tx, 1129 mesh_tx->handle == new); 1130 else 1131 hci_dev_set_flag(hdev, HCI_MESH_SENDING); 1132 } 1133 } 1134 1135 static void mesh_next(struct hci_dev *hdev, void *data, int err) 1136 { 1137 /* The entry is being cancelled with the cmd_sync lock held, which 1138 * can't be taken before hdev->lock. 1139 */ 1140 if (err == -ECANCELED) 1141 return; 1142 1143 hci_dev_lock(hdev); 1144 __mesh_next(hdev, 0); 1145 hci_dev_unlock(hdev); 1146 } 1147 1148 static int mesh_send_done_sync(struct hci_dev *hdev, void *data) 1149 { 1150 struct mgmt_mesh_tx *mesh_tx; 1151 u8 handle; 1152 bool disable; 1153 1154 hci_dev_lock(hdev); 1155 1156 mesh_tx = mgmt_mesh_next(hdev, NULL); 1157 if (!mesh_tx || !hci_dev_test_flag(hdev, HCI_MESH_SENDING)) { 1158 hci_dev_unlock(hdev); 1159 return 0; 1160 } 1161 1162 handle = mesh_tx->handle; 1163 disable = list_empty(&hdev->adv_instances); 1164 1165 hci_dev_unlock(hdev); 1166 1167 if (disable) 1168 hci_disable_advertising_sync(hdev); 1169 1170 hci_dev_lock(hdev); 1171 1172 /* The transmission can be cancelled meanwhile, in which case whoever 1173 * did so already moved on to the next one. HCI_MESH_SENDING is only 1174 * cleared with the transmission removed so Mesh Send doesn't start 1175 * another one in between. 1176 */ 1177 mesh_tx = mgmt_mesh_find(hdev, handle); 1178 if (mesh_tx) { 1179 mesh_send_complete(hdev, mesh_tx, false); 1180 hci_dev_clear_flag(hdev, HCI_MESH_SENDING); 1181 } 1182 1183 hci_dev_unlock(hdev); 1184 1185 return 0; 1186 } 1187 1188 static void mesh_send_done(struct work_struct *work) 1189 { 1190 struct hci_dev *hdev = container_of(work, struct hci_dev, 1191 mesh_send_done.work); 1192 1193 if (!hci_dev_test_flag(hdev, HCI_MESH_SENDING)) 1194 return; 1195 1196 hci_cmd_sync_queue(hdev, mesh_send_done_sync, NULL, mesh_next); 1197 } 1198 1199 /* Nothing is going to send the transmissions left, e.g. when the controller 1200 * is powered off. 1201 * 1202 * This function requires the caller holds hdev->lock. 1203 */ 1204 static void mesh_flush(struct hci_dev *hdev, bool silent) 1205 { 1206 struct mgmt_mesh_tx *mesh_tx; 1207 1208 /* Stop the timer first, it checks HCI_MESH_SENDING without hdev->lock 1209 * so once it is clear and the work is not running anymore nothing can 1210 * queue mesh_send_done_sync behind the dequeue below. 1211 */ 1212 hci_dev_clear_flag(hdev, HCI_MESH_SENDING); 1213 cancel_delayed_work_sync(&hdev->mesh_send_done); 1214 1215 /* Entries still queued would otherwise run after the controller is 1216 * powered on again and act on the transmissions started by then. The 1217 * destroy callbacks don't take hdev->lock when cancelled. 1218 */ 1219 hci_cmd_sync_dequeue(hdev, mesh_send_sync, NULL, NULL); 1220 hci_cmd_sync_dequeue(hdev, mesh_send_done_sync, NULL, NULL); 1221 1222 while ((mesh_tx = mgmt_mesh_next(hdev, NULL))) 1223 mesh_send_complete(hdev, mesh_tx, silent); 1224 } 1225 1226 static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) 1227 { 1228 if (hci_dev_test_flag(hdev, HCI_MGMT)) 1229 return; 1230 1231 BT_INFO("MGMT ver %d.%d", MGMT_VERSION, MGMT_REVISION); 1232 1233 INIT_DELAYED_WORK(&hdev->discov_off, discov_off); 1234 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); 1235 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); 1236 INIT_DELAYED_WORK(&hdev->mesh_send_done, mesh_send_done); 1237 1238 /* Non-mgmt controlled devices get this bit set 1239 * implicitly so that pairing works for them, however 1240 * for mgmt we require user-space to explicitly enable 1241 * it 1242 */ 1243 hci_dev_clear_flag(hdev, HCI_BONDABLE); 1244 1245 hci_dev_set_flag(hdev, HCI_MGMT); 1246 } 1247 1248 static int read_controller_info(struct sock *sk, struct hci_dev *hdev, 1249 void *data, u16 data_len) 1250 { 1251 struct mgmt_rp_read_info rp; 1252 1253 bt_dev_dbg(hdev, "sock %p", sk); 1254 1255 hci_dev_lock(hdev); 1256 1257 memset(&rp, 0, sizeof(rp)); 1258 1259 bacpy(&rp.bdaddr, &hdev->bdaddr); 1260 1261 rp.version = hdev->hci_ver; 1262 rp.manufacturer = cpu_to_le16(hdev->manufacturer); 1263 1264 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); 1265 rp.current_settings = cpu_to_le32(get_current_settings(hdev)); 1266 1267 memcpy(rp.dev_class, hdev->dev_class, 3); 1268 1269 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); 1270 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); 1271 1272 hci_dev_unlock(hdev); 1273 1274 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, 1275 sizeof(rp)); 1276 } 1277 1278 static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir) 1279 { 1280 u16 eir_len = 0; 1281 size_t name_len; 1282 1283 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 1284 eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV, 1285 hdev->dev_class, 3); 1286 1287 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 1288 eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE, 1289 hdev->appearance); 1290 1291 name_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name)); 1292 eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE, 1293 hdev->dev_name, name_len); 1294 1295 name_len = strnlen(hdev->short_name, sizeof(hdev->short_name)); 1296 eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT, 1297 hdev->short_name, name_len); 1298 1299 return eir_len; 1300 } 1301 1302 static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev, 1303 void *data, u16 data_len) 1304 { 1305 char buf[512]; 1306 struct mgmt_rp_read_ext_info *rp = (void *)buf; 1307 u16 eir_len; 1308 1309 bt_dev_dbg(hdev, "sock %p", sk); 1310 1311 memset(&buf, 0, sizeof(buf)); 1312 1313 hci_dev_lock(hdev); 1314 1315 bacpy(&rp->bdaddr, &hdev->bdaddr); 1316 1317 rp->version = hdev->hci_ver; 1318 rp->manufacturer = cpu_to_le16(hdev->manufacturer); 1319 1320 rp->supported_settings = cpu_to_le32(get_supported_settings(hdev)); 1321 rp->current_settings = cpu_to_le32(get_current_settings(hdev)); 1322 1323 1324 eir_len = append_eir_data_to_buf(hdev, rp->eir); 1325 rp->eir_len = cpu_to_le16(eir_len); 1326 1327 hci_dev_unlock(hdev); 1328 1329 /* If this command is called at least once, then the events 1330 * for class of device and local name changes are disabled 1331 * and only the new extended controller information event 1332 * is used. 1333 */ 1334 hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS); 1335 hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS); 1336 hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS); 1337 1338 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp, 1339 sizeof(*rp) + eir_len); 1340 } 1341 1342 static int ext_info_changed(struct hci_dev *hdev, struct sock *skip) 1343 { 1344 char buf[512]; 1345 struct mgmt_ev_ext_info_changed *ev = (void *)buf; 1346 u16 eir_len; 1347 1348 memset(buf, 0, sizeof(buf)); 1349 1350 eir_len = append_eir_data_to_buf(hdev, ev->eir); 1351 ev->eir_len = cpu_to_le16(eir_len); 1352 1353 return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev, 1354 sizeof(*ev) + eir_len, 1355 HCI_MGMT_EXT_INFO_EVENTS, skip); 1356 } 1357 1358 static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) 1359 { 1360 __le32 settings = cpu_to_le32(get_current_settings(hdev)); 1361 1362 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, 1363 sizeof(settings)); 1364 } 1365 1366 void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance) 1367 { 1368 struct mgmt_ev_advertising_added ev; 1369 1370 ev.instance = instance; 1371 1372 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk); 1373 } 1374 1375 void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev, 1376 u8 instance) 1377 { 1378 struct mgmt_ev_advertising_removed ev; 1379 1380 ev.instance = instance; 1381 1382 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk); 1383 } 1384 1385 static void cancel_adv_timeout(struct hci_dev *hdev) 1386 { 1387 if (hdev->adv_instance_timeout) { 1388 hdev->adv_instance_timeout = false; 1389 cancel_delayed_work(&hdev->adv_instance_expire); 1390 } 1391 } 1392 1393 /* This function requires the caller holds hdev->lock */ 1394 static void restart_le_actions(struct hci_dev *hdev) 1395 { 1396 struct hci_conn_params *p; 1397 1398 list_for_each_entry(p, &hdev->le_conn_params, list) { 1399 /* Needed for AUTO_OFF case where might not "really" 1400 * have been powered off. 1401 */ 1402 hci_pend_le_list_del_init(p); 1403 1404 switch (p->auto_connect) { 1405 case HCI_AUTO_CONN_DIRECT: 1406 case HCI_AUTO_CONN_ALWAYS: 1407 hci_pend_le_list_add(p, &hdev->pend_le_conns); 1408 break; 1409 case HCI_AUTO_CONN_REPORT: 1410 hci_pend_le_list_add(p, &hdev->pend_le_reports); 1411 break; 1412 default: 1413 break; 1414 } 1415 } 1416 } 1417 1418 static int new_settings(struct hci_dev *hdev, struct sock *skip) 1419 { 1420 __le32 ev = cpu_to_le32(get_current_settings(hdev)); 1421 1422 return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, 1423 sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip); 1424 } 1425 1426 static void mgmt_set_powered_complete(struct hci_dev *hdev, void *data, int err) 1427 { 1428 struct mgmt_pending_cmd *cmd = data; 1429 struct mgmt_mode *cp; 1430 1431 /* Make sure cmd still outstanding. */ 1432 if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) 1433 return; 1434 1435 cp = cmd->param; 1436 1437 bt_dev_dbg(hdev, "err %d", err); 1438 1439 if (!err) { 1440 if (cp->val) { 1441 hci_dev_lock(hdev); 1442 restart_le_actions(hdev); 1443 hci_update_passive_scan(hdev); 1444 hci_dev_unlock(hdev); 1445 } 1446 1447 send_settings_rsp(cmd->sk, cmd->opcode, hdev); 1448 1449 /* Only call new_setting for power on as power off is deferred 1450 * to hdev->power_off work which does call hci_dev_do_close. 1451 */ 1452 if (cp->val) 1453 new_settings(hdev, cmd->sk); 1454 } else { 1455 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, 1456 mgmt_status(err)); 1457 } 1458 1459 mgmt_pending_free(cmd); 1460 } 1461 1462 static int set_powered_sync(struct hci_dev *hdev, void *data) 1463 { 1464 struct mgmt_pending_cmd *cmd = data; 1465 struct mgmt_mode cp; 1466 1467 mutex_lock(&hdev->mgmt_pending_lock); 1468 1469 /* Make sure cmd still outstanding. */ 1470 if (!__mgmt_pending_listed(hdev, cmd)) { 1471 mutex_unlock(&hdev->mgmt_pending_lock); 1472 return -ECANCELED; 1473 } 1474 1475 memcpy(&cp, cmd->param, sizeof(cp)); 1476 1477 mutex_unlock(&hdev->mgmt_pending_lock); 1478 1479 BT_DBG("%s", hdev->name); 1480 1481 return hci_set_powered_sync(hdev, cp.val); 1482 } 1483 1484 static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data, 1485 u16 len) 1486 { 1487 struct mgmt_mode *cp = data; 1488 struct mgmt_pending_cmd *cmd; 1489 int err; 1490 1491 bt_dev_dbg(hdev, "sock %p", sk); 1492 1493 if (cp->val != 0x00 && cp->val != 0x01) 1494 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, 1495 MGMT_STATUS_INVALID_PARAMS); 1496 1497 hci_dev_lock(hdev); 1498 1499 if (!cp->val) { 1500 if (hci_dev_test_flag(hdev, HCI_POWERING_DOWN)) { 1501 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, 1502 MGMT_STATUS_BUSY); 1503 goto failed; 1504 } 1505 } 1506 1507 if (pending_find(MGMT_OP_SET_POWERED, hdev)) { 1508 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, 1509 MGMT_STATUS_BUSY); 1510 goto failed; 1511 } 1512 1513 if (!!cp->val == hdev_is_powered(hdev)) { 1514 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); 1515 goto failed; 1516 } 1517 1518 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); 1519 if (!cmd) { 1520 err = -ENOMEM; 1521 goto failed; 1522 } 1523 1524 /* Cancel potentially blocking sync operation before power off */ 1525 if (cp->val == 0x00) { 1526 hci_cmd_sync_cancel_sync(hdev, -EHOSTDOWN); 1527 err = hci_cmd_sync_queue(hdev, set_powered_sync, cmd, 1528 mgmt_set_powered_complete); 1529 } else { 1530 /* Use hci_cmd_sync_submit since hdev might not be running */ 1531 err = hci_cmd_sync_submit(hdev, set_powered_sync, cmd, 1532 mgmt_set_powered_complete); 1533 } 1534 1535 if (err < 0) 1536 mgmt_pending_remove(cmd); 1537 1538 failed: 1539 hci_dev_unlock(hdev); 1540 return err; 1541 } 1542 1543 int mgmt_new_settings(struct hci_dev *hdev) 1544 { 1545 return new_settings(hdev, NULL); 1546 } 1547 1548 struct cmd_lookup { 1549 struct sock *sk; 1550 struct hci_dev *hdev; 1551 u8 mgmt_status; 1552 }; 1553 1554 static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) 1555 { 1556 struct cmd_lookup *match = data; 1557 1558 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); 1559 1560 if (match->sk == NULL) { 1561 match->sk = cmd->sk; 1562 sock_hold(match->sk); 1563 } 1564 } 1565 1566 static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) 1567 { 1568 u8 *status = data; 1569 1570 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, *status); 1571 } 1572 1573 static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) 1574 { 1575 struct cmd_lookup *match = data; 1576 1577 /* dequeue cmd_sync entries using cmd as data as that is about to be 1578 * removed/freed. 1579 */ 1580 hci_cmd_sync_dequeue(match->hdev, NULL, cmd, NULL); 1581 1582 if (cmd->cmd_complete) { 1583 cmd->cmd_complete(cmd, match->mgmt_status); 1584 return; 1585 } 1586 1587 cmd_status_rsp(cmd, &match->mgmt_status); 1588 } 1589 1590 static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) 1591 { 1592 return mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, status, 1593 cmd->param, cmd->param_len); 1594 } 1595 1596 static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) 1597 { 1598 return mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, status, 1599 cmd->param, sizeof(struct mgmt_addr_info)); 1600 } 1601 1602 static u8 mgmt_bredr_support(struct hci_dev *hdev) 1603 { 1604 if (!lmp_bredr_capable(hdev)) 1605 return MGMT_STATUS_NOT_SUPPORTED; 1606 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 1607 return MGMT_STATUS_REJECTED; 1608 else 1609 return MGMT_STATUS_SUCCESS; 1610 } 1611 1612 static u8 mgmt_le_support(struct hci_dev *hdev) 1613 { 1614 if (!lmp_le_capable(hdev)) 1615 return MGMT_STATUS_NOT_SUPPORTED; 1616 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 1617 return MGMT_STATUS_REJECTED; 1618 else 1619 return MGMT_STATUS_SUCCESS; 1620 } 1621 1622 static void mgmt_set_discoverable_complete(struct hci_dev *hdev, void *data, 1623 int err) 1624 { 1625 struct mgmt_pending_cmd *cmd = data; 1626 1627 bt_dev_dbg(hdev, "err %d", err); 1628 1629 /* Make sure cmd still outstanding. */ 1630 if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) 1631 return; 1632 1633 hci_dev_lock(hdev); 1634 1635 if (err) { 1636 u8 mgmt_err = mgmt_status(err); 1637 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err); 1638 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 1639 goto done; 1640 } 1641 1642 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && 1643 hdev->discov_timeout > 0) { 1644 int to = secs_to_jiffies(hdev->discov_timeout); 1645 queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to); 1646 } 1647 1648 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); 1649 new_settings(hdev, cmd->sk); 1650 1651 done: 1652 mgmt_pending_free(cmd); 1653 hci_dev_unlock(hdev); 1654 } 1655 1656 static int set_discoverable_sync(struct hci_dev *hdev, void *data) 1657 { 1658 if (!mgmt_pending_listed(hdev, data)) 1659 return -ECANCELED; 1660 1661 BT_DBG("%s", hdev->name); 1662 1663 return hci_update_discoverable_sync(hdev); 1664 } 1665 1666 static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data, 1667 u16 len) 1668 { 1669 struct mgmt_cp_set_discoverable *cp = data; 1670 struct mgmt_pending_cmd *cmd; 1671 u16 timeout; 1672 int err; 1673 1674 bt_dev_dbg(hdev, "sock %p", sk); 1675 1676 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && 1677 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 1678 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1679 MGMT_STATUS_REJECTED); 1680 1681 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) 1682 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1683 MGMT_STATUS_INVALID_PARAMS); 1684 1685 timeout = __le16_to_cpu(cp->timeout); 1686 1687 /* Disabling discoverable requires that no timeout is set, 1688 * and enabling limited discoverable requires a timeout. 1689 */ 1690 if ((cp->val == 0x00 && timeout > 0) || 1691 (cp->val == 0x02 && timeout == 0)) 1692 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1693 MGMT_STATUS_INVALID_PARAMS); 1694 1695 hci_dev_lock(hdev); 1696 1697 if (!hdev_is_powered(hdev) && timeout > 0) { 1698 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1699 MGMT_STATUS_NOT_POWERED); 1700 goto failed; 1701 } 1702 1703 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || 1704 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { 1705 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1706 MGMT_STATUS_BUSY); 1707 goto failed; 1708 } 1709 1710 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { 1711 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1712 MGMT_STATUS_REJECTED); 1713 goto failed; 1714 } 1715 1716 if (hdev->advertising_paused) { 1717 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1718 MGMT_STATUS_BUSY); 1719 goto failed; 1720 } 1721 1722 if (!hdev_is_powered(hdev)) { 1723 bool changed = false; 1724 1725 /* Setting limited discoverable when powered off is 1726 * not a valid operation since it requires a timeout 1727 * and so no need to check HCI_LIMITED_DISCOVERABLE. 1728 */ 1729 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { 1730 hci_dev_change_flag(hdev, HCI_DISCOVERABLE); 1731 changed = true; 1732 } 1733 1734 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); 1735 if (err < 0) 1736 goto failed; 1737 1738 if (changed) 1739 err = new_settings(hdev, sk); 1740 1741 goto failed; 1742 } 1743 1744 /* If the current mode is the same, then just update the timeout 1745 * value with the new value. And if only the timeout gets updated, 1746 * then no need for any HCI transactions. 1747 */ 1748 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && 1749 (cp->val == 0x02) == hci_dev_test_flag(hdev, 1750 HCI_LIMITED_DISCOVERABLE)) { 1751 cancel_delayed_work(&hdev->discov_off); 1752 hdev->discov_timeout = timeout; 1753 1754 if (cp->val && hdev->discov_timeout > 0) { 1755 int to = secs_to_jiffies(hdev->discov_timeout); 1756 queue_delayed_work(hdev->req_workqueue, 1757 &hdev->discov_off, to); 1758 } 1759 1760 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); 1761 goto failed; 1762 } 1763 1764 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); 1765 if (!cmd) { 1766 err = -ENOMEM; 1767 goto failed; 1768 } 1769 1770 /* Cancel any potential discoverable timeout that might be 1771 * still active and store new timeout value. The arming of 1772 * the timeout happens in the complete handler. 1773 */ 1774 cancel_delayed_work(&hdev->discov_off); 1775 hdev->discov_timeout = timeout; 1776 1777 if (cp->val) 1778 hci_dev_set_flag(hdev, HCI_DISCOVERABLE); 1779 else 1780 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 1781 1782 /* Limited discoverable mode */ 1783 if (cp->val == 0x02) 1784 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); 1785 else 1786 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 1787 1788 err = hci_cmd_sync_queue(hdev, set_discoverable_sync, cmd, 1789 mgmt_set_discoverable_complete); 1790 1791 if (err < 0) 1792 mgmt_pending_remove(cmd); 1793 1794 failed: 1795 hci_dev_unlock(hdev); 1796 return err; 1797 } 1798 1799 static void mgmt_set_connectable_complete(struct hci_dev *hdev, void *data, 1800 int err) 1801 { 1802 struct mgmt_pending_cmd *cmd = data; 1803 1804 bt_dev_dbg(hdev, "err %d", err); 1805 1806 /* Make sure cmd still outstanding. */ 1807 if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) 1808 return; 1809 1810 hci_dev_lock(hdev); 1811 1812 if (err) { 1813 u8 mgmt_err = mgmt_status(err); 1814 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err); 1815 goto done; 1816 } 1817 1818 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); 1819 new_settings(hdev, cmd->sk); 1820 1821 done: 1822 mgmt_pending_free(cmd); 1823 1824 hci_dev_unlock(hdev); 1825 } 1826 1827 static int set_connectable_update_settings(struct hci_dev *hdev, 1828 struct sock *sk, u8 val) 1829 { 1830 bool changed = false; 1831 int err; 1832 1833 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) 1834 changed = true; 1835 1836 if (val) { 1837 hci_dev_set_flag(hdev, HCI_CONNECTABLE); 1838 } else { 1839 hci_dev_clear_flag(hdev, HCI_CONNECTABLE); 1840 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 1841 } 1842 1843 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); 1844 if (err < 0) 1845 return err; 1846 1847 if (changed) { 1848 hci_update_scan(hdev); 1849 hci_update_passive_scan(hdev); 1850 return new_settings(hdev, sk); 1851 } 1852 1853 return 0; 1854 } 1855 1856 static int set_connectable_sync(struct hci_dev *hdev, void *data) 1857 { 1858 if (!mgmt_pending_listed(hdev, data)) 1859 return -ECANCELED; 1860 1861 BT_DBG("%s", hdev->name); 1862 1863 return hci_update_connectable_sync(hdev); 1864 } 1865 1866 static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data, 1867 u16 len) 1868 { 1869 struct mgmt_mode *cp = data; 1870 struct mgmt_pending_cmd *cmd; 1871 int err; 1872 1873 bt_dev_dbg(hdev, "sock %p", sk); 1874 1875 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && 1876 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 1877 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, 1878 MGMT_STATUS_REJECTED); 1879 1880 if (cp->val != 0x00 && cp->val != 0x01) 1881 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, 1882 MGMT_STATUS_INVALID_PARAMS); 1883 1884 hci_dev_lock(hdev); 1885 1886 if (!hdev_is_powered(hdev)) { 1887 err = set_connectable_update_settings(hdev, sk, cp->val); 1888 goto failed; 1889 } 1890 1891 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || 1892 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { 1893 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, 1894 MGMT_STATUS_BUSY); 1895 goto failed; 1896 } 1897 1898 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); 1899 if (!cmd) { 1900 err = -ENOMEM; 1901 goto failed; 1902 } 1903 1904 if (cp->val) { 1905 hci_dev_set_flag(hdev, HCI_CONNECTABLE); 1906 } else { 1907 if (hdev->discov_timeout > 0) 1908 cancel_delayed_work(&hdev->discov_off); 1909 1910 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); 1911 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 1912 hci_dev_clear_flag(hdev, HCI_CONNECTABLE); 1913 } 1914 1915 err = hci_cmd_sync_queue(hdev, set_connectable_sync, cmd, 1916 mgmt_set_connectable_complete); 1917 1918 if (err < 0) 1919 mgmt_pending_remove(cmd); 1920 1921 failed: 1922 hci_dev_unlock(hdev); 1923 return err; 1924 } 1925 1926 static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data, 1927 u16 len) 1928 { 1929 struct mgmt_mode *cp = data; 1930 bool changed; 1931 int err; 1932 1933 bt_dev_dbg(hdev, "sock %p", sk); 1934 1935 if (cp->val != 0x00 && cp->val != 0x01) 1936 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, 1937 MGMT_STATUS_INVALID_PARAMS); 1938 1939 hci_dev_lock(hdev); 1940 1941 if (cp->val) 1942 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); 1943 else 1944 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); 1945 1946 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); 1947 if (err < 0) 1948 goto unlock; 1949 1950 if (changed) { 1951 /* In limited privacy mode the change of bondable mode 1952 * may affect the local advertising address. 1953 */ 1954 hci_update_discoverable(hdev); 1955 1956 err = new_settings(hdev, sk); 1957 } 1958 1959 unlock: 1960 hci_dev_unlock(hdev); 1961 return err; 1962 } 1963 1964 static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, 1965 u16 len) 1966 { 1967 struct mgmt_mode *cp = data; 1968 struct mgmt_pending_cmd *cmd; 1969 u8 val, status; 1970 int err; 1971 1972 bt_dev_dbg(hdev, "sock %p", sk); 1973 1974 status = mgmt_bredr_support(hdev); 1975 if (status) 1976 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, 1977 status); 1978 1979 if (cp->val != 0x00 && cp->val != 0x01) 1980 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, 1981 MGMT_STATUS_INVALID_PARAMS); 1982 1983 hci_dev_lock(hdev); 1984 1985 if (!hdev_is_powered(hdev)) { 1986 bool changed = false; 1987 1988 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { 1989 hci_dev_change_flag(hdev, HCI_LINK_SECURITY); 1990 changed = true; 1991 } 1992 1993 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); 1994 if (err < 0) 1995 goto failed; 1996 1997 if (changed) 1998 err = new_settings(hdev, sk); 1999 2000 goto failed; 2001 } 2002 2003 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { 2004 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, 2005 MGMT_STATUS_BUSY); 2006 goto failed; 2007 } 2008 2009 val = !!cp->val; 2010 2011 if (test_bit(HCI_AUTH, &hdev->flags) == val) { 2012 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); 2013 goto failed; 2014 } 2015 2016 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); 2017 if (!cmd) { 2018 err = -ENOMEM; 2019 goto failed; 2020 } 2021 2022 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); 2023 if (err < 0) { 2024 mgmt_pending_remove(cmd); 2025 goto failed; 2026 } 2027 2028 failed: 2029 hci_dev_unlock(hdev); 2030 return err; 2031 } 2032 2033 static void set_ssp_complete(struct hci_dev *hdev, void *data, int err) 2034 { 2035 struct cmd_lookup match = { NULL, hdev }; 2036 struct mgmt_pending_cmd *cmd = data; 2037 struct mgmt_mode *cp; 2038 u8 enable; 2039 bool changed; 2040 2041 /* Make sure cmd still outstanding. */ 2042 if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) 2043 return; 2044 2045 cp = cmd->param; 2046 enable = cp->val; 2047 2048 if (err) { 2049 u8 mgmt_err = mgmt_status(err); 2050 2051 if (enable && hci_dev_test_and_clear_flag(hdev, 2052 HCI_SSP_ENABLED)) { 2053 new_settings(hdev, NULL); 2054 } 2055 2056 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err); 2057 mgmt_pending_free(cmd); 2058 return; 2059 } 2060 2061 if (enable) { 2062 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); 2063 } else { 2064 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); 2065 } 2066 2067 settings_rsp(cmd, &match); 2068 2069 if (changed) 2070 new_settings(hdev, match.sk); 2071 2072 if (match.sk) 2073 sock_put(match.sk); 2074 2075 hci_update_eir_sync(hdev); 2076 mgmt_pending_free(cmd); 2077 } 2078 2079 static int set_ssp_sync(struct hci_dev *hdev, void *data) 2080 { 2081 struct mgmt_pending_cmd *cmd = data; 2082 struct mgmt_mode cp; 2083 bool changed = false; 2084 int err; 2085 2086 mutex_lock(&hdev->mgmt_pending_lock); 2087 2088 if (!__mgmt_pending_listed(hdev, cmd)) { 2089 mutex_unlock(&hdev->mgmt_pending_lock); 2090 return -ECANCELED; 2091 } 2092 2093 memcpy(&cp, cmd->param, sizeof(cp)); 2094 2095 mutex_unlock(&hdev->mgmt_pending_lock); 2096 2097 if (cp.val) 2098 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); 2099 2100 err = hci_write_ssp_mode_sync(hdev, cp.val); 2101 2102 if (!err && changed) 2103 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); 2104 2105 return err; 2106 } 2107 2108 static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 2109 { 2110 struct mgmt_mode *cp = data; 2111 struct mgmt_pending_cmd *cmd; 2112 u8 status; 2113 int err; 2114 2115 bt_dev_dbg(hdev, "sock %p", sk); 2116 2117 status = mgmt_bredr_support(hdev); 2118 if (status) 2119 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); 2120 2121 if (!lmp_ssp_capable(hdev)) 2122 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, 2123 MGMT_STATUS_NOT_SUPPORTED); 2124 2125 if (cp->val != 0x00 && cp->val != 0x01) 2126 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, 2127 MGMT_STATUS_INVALID_PARAMS); 2128 2129 hci_dev_lock(hdev); 2130 2131 if (!hdev_is_powered(hdev)) { 2132 bool changed; 2133 2134 if (cp->val) { 2135 changed = !hci_dev_test_and_set_flag(hdev, 2136 HCI_SSP_ENABLED); 2137 } else { 2138 changed = hci_dev_test_and_clear_flag(hdev, 2139 HCI_SSP_ENABLED); 2140 } 2141 2142 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); 2143 if (err < 0) 2144 goto failed; 2145 2146 if (changed) 2147 err = new_settings(hdev, sk); 2148 2149 goto failed; 2150 } 2151 2152 if (pending_find(MGMT_OP_SET_SSP, hdev)) { 2153 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, 2154 MGMT_STATUS_BUSY); 2155 goto failed; 2156 } 2157 2158 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { 2159 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); 2160 goto failed; 2161 } 2162 2163 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); 2164 if (!cmd) 2165 err = -ENOMEM; 2166 else 2167 err = hci_cmd_sync_queue(hdev, set_ssp_sync, cmd, 2168 set_ssp_complete); 2169 2170 if (err < 0) { 2171 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, 2172 MGMT_STATUS_FAILED); 2173 2174 if (cmd) 2175 mgmt_pending_remove(cmd); 2176 } 2177 2178 failed: 2179 hci_dev_unlock(hdev); 2180 return err; 2181 } 2182 2183 static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 2184 { 2185 bt_dev_dbg(hdev, "sock %p", sk); 2186 2187 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, 2188 MGMT_STATUS_NOT_SUPPORTED); 2189 } 2190 2191 static void set_le_complete(struct hci_dev *hdev, void *data, int err) 2192 { 2193 struct mgmt_pending_cmd *cmd = data; 2194 struct cmd_lookup match = { NULL, hdev }; 2195 u8 status = mgmt_status(err); 2196 2197 bt_dev_dbg(hdev, "err %d", err); 2198 2199 if (err == -ECANCELED || !mgmt_pending_valid(hdev, data)) 2200 return; 2201 2202 if (status) { 2203 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, status); 2204 goto done; 2205 } 2206 2207 settings_rsp(cmd, &match); 2208 2209 new_settings(hdev, match.sk); 2210 2211 if (match.sk) 2212 sock_put(match.sk); 2213 2214 done: 2215 mgmt_pending_free(cmd); 2216 } 2217 2218 static int set_le_sync(struct hci_dev *hdev, void *data) 2219 { 2220 struct mgmt_pending_cmd *cmd = data; 2221 struct mgmt_mode cp; 2222 u8 val; 2223 int err; 2224 2225 mutex_lock(&hdev->mgmt_pending_lock); 2226 2227 if (!__mgmt_pending_listed(hdev, cmd)) { 2228 mutex_unlock(&hdev->mgmt_pending_lock); 2229 return -ECANCELED; 2230 } 2231 2232 memcpy(&cp, cmd->param, sizeof(cp)); 2233 val = !!cp.val; 2234 2235 mutex_unlock(&hdev->mgmt_pending_lock); 2236 2237 if (!val) { 2238 hci_clear_adv_instance_sync(hdev, NULL, 0x00, true); 2239 2240 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) 2241 hci_disable_advertising_sync(hdev); 2242 2243 if (ext_adv_capable(hdev)) 2244 hci_remove_ext_adv_instance_sync(hdev, 0, cmd->sk); 2245 } else { 2246 hci_dev_set_flag(hdev, HCI_LE_ENABLED); 2247 } 2248 2249 err = hci_write_le_host_supported_sync(hdev, val, 0); 2250 2251 /* Make sure the controller has a good default for 2252 * advertising data. Restrict the update to when LE 2253 * has actually been enabled. During power on, the 2254 * update in powered_update_hci will take care of it. 2255 */ 2256 if (!err && hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 2257 if (ext_adv_capable(hdev)) { 2258 int status; 2259 2260 status = hci_setup_ext_adv_instance_sync(hdev, 0x00); 2261 if (!status) 2262 hci_update_scan_rsp_data_sync(hdev, 0x00); 2263 } else { 2264 hci_update_adv_data_sync(hdev, 0x00); 2265 hci_update_scan_rsp_data_sync(hdev, 0x00); 2266 } 2267 2268 hci_update_passive_scan(hdev); 2269 } 2270 2271 return err; 2272 } 2273 2274 static void set_mesh_complete(struct hci_dev *hdev, void *data, int err) 2275 { 2276 struct mgmt_pending_cmd *cmd = data; 2277 u8 status = mgmt_status(err); 2278 struct sock *sk; 2279 2280 if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) 2281 return; 2282 2283 sk = cmd->sk; 2284 2285 if (status) { 2286 mgmt_cmd_status(cmd->sk, hdev->id, cmd->opcode, status); 2287 goto done; 2288 } 2289 2290 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER, 0, NULL, 0); 2291 2292 done: 2293 mgmt_pending_free(cmd); 2294 } 2295 2296 static int set_mesh_sync(struct hci_dev *hdev, void *data) 2297 { 2298 struct mgmt_pending_cmd *cmd = data; 2299 DEFINE_FLEX(struct mgmt_cp_set_mesh, cp, ad_types, num_ad_types, 2300 sizeof(hdev->mesh_ad_types)); 2301 size_t len; 2302 2303 mutex_lock(&hdev->mgmt_pending_lock); 2304 2305 if (!__mgmt_pending_listed(hdev, cmd)) { 2306 mutex_unlock(&hdev->mgmt_pending_lock); 2307 return -ECANCELED; 2308 } 2309 2310 len = cmd->param_len; 2311 memcpy(cp, cmd->param, min(__struct_size(cp), len)); 2312 2313 mutex_unlock(&hdev->mgmt_pending_lock); 2314 2315 memset(hdev->mesh_ad_types, 0, sizeof(hdev->mesh_ad_types)); 2316 2317 if (cp->enable) 2318 hci_dev_set_flag(hdev, HCI_MESH); 2319 else 2320 hci_dev_clear_flag(hdev, HCI_MESH); 2321 2322 hdev->le_scan_interval = __le16_to_cpu(cp->period); 2323 hdev->le_scan_window = __le16_to_cpu(cp->window); 2324 2325 len -= sizeof(struct mgmt_cp_set_mesh); 2326 2327 /* If filters don't fit, forward all adv pkts */ 2328 if (len <= sizeof(hdev->mesh_ad_types)) 2329 memcpy(hdev->mesh_ad_types, cp->ad_types, len); 2330 2331 hci_update_passive_scan_sync(hdev); 2332 return 0; 2333 } 2334 2335 static int set_mesh(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 2336 { 2337 struct mgmt_cp_set_mesh *cp = data; 2338 struct mgmt_pending_cmd *cmd; 2339 __u16 period, window; 2340 int err = 0; 2341 2342 bt_dev_dbg(hdev, "sock %p", sk); 2343 2344 if (!lmp_le_capable(hdev) || 2345 !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL)) 2346 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER, 2347 MGMT_STATUS_NOT_SUPPORTED); 2348 2349 if (cp->enable != 0x00 && cp->enable != 0x01) 2350 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER, 2351 MGMT_STATUS_INVALID_PARAMS); 2352 2353 /* Keep allowed ranges in sync with set_scan_params() */ 2354 period = __le16_to_cpu(cp->period); 2355 2356 if (period < 0x0004 || period > 0x4000) 2357 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER, 2358 MGMT_STATUS_INVALID_PARAMS); 2359 2360 window = __le16_to_cpu(cp->window); 2361 2362 if (window < 0x0004 || window > 0x4000) 2363 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER, 2364 MGMT_STATUS_INVALID_PARAMS); 2365 2366 if (window > period) 2367 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER, 2368 MGMT_STATUS_INVALID_PARAMS); 2369 2370 hci_dev_lock(hdev); 2371 2372 cmd = mgmt_pending_add(sk, MGMT_OP_SET_MESH_RECEIVER, hdev, data, len); 2373 if (!cmd) 2374 err = -ENOMEM; 2375 else 2376 err = hci_cmd_sync_queue(hdev, set_mesh_sync, cmd, 2377 set_mesh_complete); 2378 2379 if (err < 0) { 2380 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER, 2381 MGMT_STATUS_FAILED); 2382 2383 if (cmd) 2384 mgmt_pending_remove(cmd); 2385 } 2386 2387 hci_dev_unlock(hdev); 2388 return err; 2389 } 2390 2391 static void mesh_send_start_complete(struct hci_dev *hdev, void *data, int err) 2392 { 2393 struct mgmt_mesh_tx *mesh_tx; 2394 struct mgmt_cp_mesh_send *send; 2395 u8 mgmt_err = mgmt_status(err); 2396 2397 /* The entry is being cancelled with the cmd_sync lock held, which 2398 * can't be taken before hdev->lock, so leave the removal of the 2399 * transmission, and HCI_MESH_SENDING, to whoever cancelled it or to 2400 * mgmt_power_off() if the controller is going down. 2401 */ 2402 if (err == -ECANCELED) 2403 return; 2404 2405 hci_dev_lock(hdev); 2406 2407 /* Cancelled while the entry was running, whoever did so already moved 2408 * on to the next transmission. 2409 */ 2410 mesh_tx = mgmt_mesh_find(hdev, MESH_TX_HANDLE(data)); 2411 if (!mesh_tx) { 2412 hci_dev_unlock(hdev); 2413 return; 2414 } 2415 2416 /* Report any errors here, but don't report completion */ 2417 2418 if (mgmt_err) { 2419 /* Send Complete Error Code for handle */ 2420 mesh_send_complete(hdev, mesh_tx, false); 2421 hci_dev_clear_flag(hdev, HCI_MESH_SENDING); 2422 __mesh_next(hdev, 0); 2423 hci_dev_unlock(hdev); 2424 return; 2425 } 2426 2427 /* Queued with hdev->lock held so a cancel can't miss it */ 2428 send = (void *)mesh_tx->param; 2429 queue_delayed_work(hdev->req_workqueue, &hdev->mesh_send_done, 2430 msecs_to_jiffies((send->cnt) * 25)); 2431 2432 hci_dev_unlock(hdev); 2433 } 2434 2435 static int mesh_send_sync(struct hci_dev *hdev, void *data) 2436 { 2437 struct mgmt_mesh_tx *mesh_tx; 2438 struct mgmt_cp_mesh_send *send; 2439 struct adv_info *adv, *next_instance; 2440 u8 instance = hdev->le_num_of_adv_sets + 1; 2441 u16 timeout, duration; 2442 int err = 0; 2443 2444 hci_dev_lock(hdev); 2445 2446 mesh_tx = mgmt_mesh_find(hdev, MESH_TX_HANDLE(data)); 2447 if (!mesh_tx) { 2448 hci_dev_unlock(hdev); 2449 return -ENOENT; 2450 } 2451 2452 send = (void *)mesh_tx->param; 2453 2454 if (hdev->le_num_of_adv_sets <= hdev->adv_instance_cnt) { 2455 hci_dev_unlock(hdev); 2456 return MGMT_STATUS_BUSY; 2457 } 2458 2459 timeout = 1000; 2460 duration = send->cnt * INTERVAL_TO_MS(hdev->le_adv_max_interval); 2461 adv = hci_add_adv_instance(hdev, instance, 0, 2462 send->adv_data_len, send->adv_data, 2463 0, NULL, 2464 timeout, duration, 2465 HCI_ADV_TX_POWER_NO_PREFERENCE, 2466 hdev->le_adv_min_interval, 2467 hdev->le_adv_max_interval, 2468 mesh_tx->handle); 2469 2470 if (!IS_ERR(adv)) 2471 mesh_tx->instance = instance; 2472 else 2473 err = PTR_ERR(adv); 2474 2475 if (hdev->cur_adv_instance == instance) { 2476 /* If the currently advertised instance is being changed then 2477 * cancel the current advertising and schedule the next 2478 * instance. If there is only one instance then the overridden 2479 * advertising data will be visible right away. 2480 */ 2481 cancel_adv_timeout(hdev); 2482 2483 next_instance = hci_get_next_instance(hdev, instance); 2484 if (next_instance) 2485 instance = next_instance->instance; 2486 else 2487 instance = 0; 2488 } else if (hdev->adv_instance_timeout) { 2489 /* Immediately advertise the new instance if no other, or 2490 * let it go naturally from queue if ADV is already happening 2491 */ 2492 instance = 0; 2493 } 2494 2495 hci_dev_unlock(hdev); 2496 2497 if (instance) 2498 return hci_schedule_adv_instance_sync(hdev, instance, true); 2499 2500 return err; 2501 } 2502 2503 static void send_count(struct mgmt_mesh_tx *mesh_tx, void *data) 2504 { 2505 struct mgmt_rp_mesh_read_features *rp = data; 2506 2507 if (rp->used_handles >= rp->max_handles) 2508 return; 2509 2510 rp->handles[rp->used_handles++] = mesh_tx->handle; 2511 } 2512 2513 static int mesh_features(struct sock *sk, struct hci_dev *hdev, 2514 void *data, u16 len) 2515 { 2516 struct mgmt_rp_mesh_read_features rp; 2517 2518 if (!lmp_le_capable(hdev) || 2519 !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL)) 2520 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_READ_FEATURES, 2521 MGMT_STATUS_NOT_SUPPORTED); 2522 2523 memset(&rp, 0, sizeof(rp)); 2524 rp.index = cpu_to_le16(hdev->id); 2525 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 2526 rp.max_handles = MESH_HANDLES_MAX; 2527 2528 hci_dev_lock(hdev); 2529 2530 if (rp.max_handles) 2531 mgmt_mesh_foreach(hdev, send_count, &rp, sk); 2532 2533 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_MESH_READ_FEATURES, 0, &rp, 2534 rp.used_handles + sizeof(rp) - MESH_HANDLES_MAX); 2535 2536 hci_dev_unlock(hdev); 2537 return 0; 2538 } 2539 2540 /* Cancel a single transmission, the handle is looked up again after the 2541 * entry has been dequeued since it may be gone or have completed meanwhile. 2542 */ 2543 static void mesh_cancel_tx(struct hci_dev *hdev, u8 handle) 2544 { 2545 struct mgmt_mesh_tx *mesh_tx; 2546 bool in_progress; 2547 2548 hci_cmd_sync_dequeue(hdev, mesh_send_sync, MESH_TX_DATA(handle), NULL); 2549 2550 hci_dev_lock(hdev); 2551 mesh_tx = mgmt_mesh_find(hdev, handle); 2552 in_progress = mesh_tx && hci_dev_test_flag(hdev, HCI_MESH_SENDING) && 2553 mesh_tx == mgmt_mesh_next(hdev, NULL); 2554 hci_dev_unlock(hdev); 2555 2556 /* The done timer may have fired already and be waiting to complete 2557 * the transmission in progress, which is being cancelled. 2558 */ 2559 if (in_progress) 2560 hci_cmd_sync_dequeue(hdev, mesh_send_done_sync, NULL, NULL); 2561 2562 hci_dev_lock(hdev); 2563 2564 mesh_tx = mgmt_mesh_find(hdev, handle); 2565 if (mesh_tx) { 2566 /* If it is the transmission in progress make sure the timer 2567 * doesn't complete the next one in its place. 2568 */ 2569 if (hci_dev_test_flag(hdev, HCI_MESH_SENDING) && 2570 mesh_tx == mgmt_mesh_next(hdev, NULL)) { 2571 cancel_delayed_work(&hdev->mesh_send_done); 2572 hci_dev_clear_flag(hdev, HCI_MESH_SENDING); 2573 } 2574 2575 mesh_send_complete(hdev, mesh_tx, false); 2576 } 2577 2578 hci_dev_unlock(hdev); 2579 } 2580 2581 static int send_cancel(struct hci_dev *hdev, void *data) 2582 { 2583 struct mgmt_pending_cmd *cmd = data; 2584 struct mgmt_cp_mesh_send_cancel *cancel = (void *)cmd->param; 2585 struct mgmt_mesh_tx *mesh_tx; 2586 u8 handle; 2587 2588 if (!cancel->handle) { 2589 for (;;) { 2590 hci_dev_lock(hdev); 2591 mesh_tx = mgmt_mesh_next(hdev, cmd->sk); 2592 handle = mesh_tx ? mesh_tx->handle : 0; 2593 hci_dev_unlock(hdev); 2594 2595 if (!mesh_tx) 2596 break; 2597 2598 mesh_cancel_tx(hdev, handle); 2599 } 2600 } else { 2601 hci_dev_lock(hdev); 2602 mesh_tx = mgmt_mesh_find(hdev, cancel->handle); 2603 handle = mesh_tx && mesh_tx->sk == cmd->sk ? mesh_tx->handle : 0; 2604 hci_dev_unlock(hdev); 2605 2606 if (handle) 2607 mesh_cancel_tx(hdev, handle); 2608 } 2609 2610 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL, 2611 0, NULL, 0); 2612 2613 mesh_next(hdev, NULL, 0); 2614 2615 return 0; 2616 } 2617 2618 static void send_cancel_destroy(struct hci_dev *hdev, void *data, int err) 2619 { 2620 mgmt_pending_free(data); 2621 } 2622 2623 static int mesh_send_cancel(struct sock *sk, struct hci_dev *hdev, 2624 void *data, u16 len) 2625 { 2626 struct mgmt_pending_cmd *cmd; 2627 int err; 2628 2629 if (!lmp_le_capable(hdev) || 2630 !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL)) 2631 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL, 2632 MGMT_STATUS_NOT_SUPPORTED); 2633 2634 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 2635 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL, 2636 MGMT_STATUS_REJECTED); 2637 2638 hci_dev_lock(hdev); 2639 cmd = mgmt_pending_new(sk, MGMT_OP_MESH_SEND_CANCEL, hdev, data, len); 2640 if (!cmd) 2641 err = -ENOMEM; 2642 else 2643 err = hci_cmd_sync_queue(hdev, send_cancel, cmd, 2644 send_cancel_destroy); 2645 2646 if (err < 0) { 2647 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL, 2648 MGMT_STATUS_FAILED); 2649 2650 if (cmd) 2651 mgmt_pending_free(cmd); 2652 } 2653 2654 hci_dev_unlock(hdev); 2655 return err; 2656 } 2657 2658 static int mesh_send(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 2659 { 2660 struct mgmt_mesh_tx *mesh_tx; 2661 struct mgmt_cp_mesh_send *send = data; 2662 struct mgmt_rp_mesh_read_features rp; 2663 u16 expected_len; 2664 int err = 0; 2665 2666 if (!lmp_le_capable(hdev) || 2667 !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL)) 2668 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND, 2669 MGMT_STATUS_NOT_SUPPORTED); 2670 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 2671 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND, 2672 MGMT_STATUS_REJECTED); 2673 2674 if (!send->adv_data_len || send->adv_data_len > 31) 2675 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND, 2676 MGMT_STATUS_REJECTED); 2677 2678 expected_len = struct_size(send, adv_data, send->adv_data_len); 2679 if (expected_len != len) 2680 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND, 2681 MGMT_STATUS_INVALID_PARAMS); 2682 2683 hci_dev_lock(hdev); 2684 2685 /* Transmissions are flushed when the controller is closed, which is 2686 * serialized with this by hdev->lock, so none can be left behind. 2687 */ 2688 if (!hdev_is_powered(hdev)) { 2689 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND, 2690 MGMT_STATUS_NOT_POWERED); 2691 goto done; 2692 } 2693 2694 memset(&rp, 0, sizeof(rp)); 2695 rp.max_handles = MESH_HANDLES_MAX; 2696 2697 mgmt_mesh_foreach(hdev, send_count, &rp, sk); 2698 2699 if (rp.max_handles <= rp.used_handles) { 2700 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND, 2701 MGMT_STATUS_BUSY); 2702 goto done; 2703 } 2704 2705 mesh_tx = mgmt_mesh_add(sk, hdev, send, len); 2706 if (!mesh_tx) { 2707 err = -ENOMEM; 2708 } else { 2709 u8 handle = mesh_tx->handle; 2710 2711 /* The first transmission is the one queued, which may not be 2712 * the one just added. 2713 */ 2714 __mesh_next(hdev, handle); 2715 2716 /* Queueing failed, the transmission was removed */ 2717 if (!mgmt_mesh_find(hdev, handle)) 2718 err = -EIO; 2719 } 2720 2721 if (err < 0) { 2722 bt_dev_err(hdev, "Send Mesh Failed %d", err); 2723 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND, 2724 MGMT_STATUS_FAILED); 2725 } else { 2726 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_MESH_SEND, 0, 2727 &mesh_tx->handle, 1); 2728 } 2729 2730 done: 2731 hci_dev_unlock(hdev); 2732 return err; 2733 } 2734 2735 static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 2736 { 2737 struct mgmt_mode *cp = data; 2738 struct mgmt_pending_cmd *cmd; 2739 int err; 2740 u8 val, enabled; 2741 2742 bt_dev_dbg(hdev, "sock %p", sk); 2743 2744 if (!lmp_le_capable(hdev)) 2745 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 2746 MGMT_STATUS_NOT_SUPPORTED); 2747 2748 if (cp->val != 0x00 && cp->val != 0x01) 2749 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 2750 MGMT_STATUS_INVALID_PARAMS); 2751 2752 /* Bluetooth single mode LE only controllers or dual-mode 2753 * controllers configured as LE only devices, do not allow 2754 * switching LE off. These have either LE enabled explicitly 2755 * or BR/EDR has been previously switched off. 2756 * 2757 * When trying to enable an already enabled LE, then gracefully 2758 * send a positive response. Trying to disable it however will 2759 * result into rejection. 2760 */ 2761 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 2762 if (cp->val == 0x01) 2763 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); 2764 2765 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 2766 MGMT_STATUS_REJECTED); 2767 } 2768 2769 hci_dev_lock(hdev); 2770 2771 val = !!cp->val; 2772 enabled = lmp_host_le_capable(hdev); 2773 2774 if (!hdev_is_powered(hdev) || val == enabled) { 2775 bool changed = false; 2776 2777 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { 2778 hci_dev_change_flag(hdev, HCI_LE_ENABLED); 2779 changed = true; 2780 } 2781 2782 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { 2783 hci_dev_clear_flag(hdev, HCI_ADVERTISING); 2784 changed = true; 2785 } 2786 2787 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); 2788 if (err < 0) 2789 goto unlock; 2790 2791 if (changed) 2792 err = new_settings(hdev, sk); 2793 2794 goto unlock; 2795 } 2796 2797 if (pending_find(MGMT_OP_SET_LE, hdev) || 2798 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { 2799 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 2800 MGMT_STATUS_BUSY); 2801 goto unlock; 2802 } 2803 2804 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); 2805 if (!cmd) 2806 err = -ENOMEM; 2807 else 2808 err = hci_cmd_sync_queue(hdev, set_le_sync, cmd, 2809 set_le_complete); 2810 2811 if (err < 0) { 2812 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 2813 MGMT_STATUS_FAILED); 2814 2815 if (cmd) 2816 mgmt_pending_remove(cmd); 2817 } 2818 2819 unlock: 2820 hci_dev_unlock(hdev); 2821 return err; 2822 } 2823 2824 static int send_hci_cmd_sync(struct hci_dev *hdev, void *data) 2825 { 2826 struct mgmt_pending_cmd *cmd = data; 2827 struct mgmt_cp_hci_cmd_sync *cp = cmd->param; 2828 struct sk_buff *skb; 2829 2830 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cp->opcode), 2831 le16_to_cpu(cp->params_len), cp->params, 2832 cp->event, cp->timeout ? 2833 secs_to_jiffies(cp->timeout) : 2834 HCI_CMD_TIMEOUT); 2835 if (IS_ERR(skb)) { 2836 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_HCI_CMD_SYNC, 2837 mgmt_status(PTR_ERR(skb))); 2838 return 0; 2839 } 2840 2841 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_HCI_CMD_SYNC, 0, 2842 skb->data, skb->len); 2843 2844 kfree_skb(skb); 2845 2846 return 0; 2847 } 2848 2849 static void send_hci_cmd_sync_destroy(struct hci_dev *hdev, void *data, int err) 2850 { 2851 mgmt_pending_free(data); 2852 } 2853 2854 static int mgmt_hci_cmd_sync(struct sock *sk, struct hci_dev *hdev, 2855 void *data, u16 len) 2856 { 2857 struct mgmt_cp_hci_cmd_sync *cp = data; 2858 struct mgmt_pending_cmd *cmd; 2859 int err; 2860 2861 if (len != (offsetof(struct mgmt_cp_hci_cmd_sync, params) + 2862 le16_to_cpu(cp->params_len))) 2863 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_HCI_CMD_SYNC, 2864 MGMT_STATUS_INVALID_PARAMS); 2865 2866 /* The HCI command header carries the parameter length in a u8, a 2867 * larger value would be truncated there while the parameters are 2868 * still appended to the frame in full. 2869 */ 2870 if (le16_to_cpu(cp->params_len) > U8_MAX) 2871 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_HCI_CMD_SYNC, 2872 MGMT_STATUS_INVALID_PARAMS); 2873 2874 hci_dev_lock(hdev); 2875 cmd = mgmt_pending_new(sk, MGMT_OP_HCI_CMD_SYNC, hdev, data, len); 2876 if (!cmd) 2877 err = -ENOMEM; 2878 else 2879 err = hci_cmd_sync_queue(hdev, send_hci_cmd_sync, cmd, 2880 send_hci_cmd_sync_destroy); 2881 2882 if (err < 0) { 2883 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_HCI_CMD_SYNC, 2884 MGMT_STATUS_FAILED); 2885 2886 if (cmd) 2887 mgmt_pending_free(cmd); 2888 } 2889 2890 hci_dev_unlock(hdev); 2891 return err; 2892 } 2893 2894 /* This is a helper function to test for pending mgmt commands that can 2895 * cause CoD or EIR HCI commands. We can only allow one such pending 2896 * mgmt command at a time since otherwise we cannot easily track what 2897 * the current values are, will be, and based on that calculate if a new 2898 * HCI command needs to be sent and if yes with what value. 2899 */ 2900 static bool pending_eir_or_class(struct hci_dev *hdev) 2901 { 2902 struct mgmt_pending_cmd *cmd; 2903 bool pending = false; 2904 2905 mutex_lock(&hdev->mgmt_pending_lock); 2906 2907 list_for_each_entry(cmd, &hdev->mgmt_pending, list) { 2908 switch (cmd->opcode) { 2909 case MGMT_OP_ADD_UUID: 2910 case MGMT_OP_REMOVE_UUID: 2911 case MGMT_OP_SET_DEV_CLASS: 2912 case MGMT_OP_SET_LOCAL_NAME: 2913 case MGMT_OP_SET_POWERED: 2914 pending = true; 2915 break; 2916 } 2917 2918 if (pending) 2919 break; 2920 } 2921 2922 mutex_unlock(&hdev->mgmt_pending_lock); 2923 2924 return pending; 2925 } 2926 2927 static const u8 bluetooth_base_uuid[] = { 2928 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 2929 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 2930 }; 2931 2932 static u8 get_uuid_size(const u8 *uuid) 2933 { 2934 u32 val; 2935 2936 if (memcmp(uuid, bluetooth_base_uuid, 12)) 2937 return 128; 2938 2939 val = get_unaligned_le32(&uuid[12]); 2940 if (val > 0xffff) 2941 return 32; 2942 2943 return 16; 2944 } 2945 2946 static void mgmt_class_complete(struct hci_dev *hdev, void *data, int err) 2947 { 2948 struct mgmt_pending_cmd *cmd = data; 2949 2950 bt_dev_dbg(hdev, "err %d", err); 2951 2952 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, 2953 mgmt_status(err), hdev->dev_class, 3); 2954 2955 mgmt_pending_free(cmd); 2956 } 2957 2958 static int add_uuid_sync(struct hci_dev *hdev, void *data) 2959 { 2960 int err; 2961 2962 err = hci_update_class_sync(hdev); 2963 if (err) 2964 return err; 2965 2966 return hci_update_eir_sync(hdev); 2967 } 2968 2969 static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 2970 { 2971 struct mgmt_cp_add_uuid *cp = data; 2972 struct mgmt_pending_cmd *cmd; 2973 struct bt_uuid *uuid; 2974 int err; 2975 2976 bt_dev_dbg(hdev, "sock %p", sk); 2977 2978 hci_dev_lock(hdev); 2979 2980 if (pending_eir_or_class(hdev)) { 2981 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, 2982 MGMT_STATUS_BUSY); 2983 goto failed; 2984 } 2985 2986 uuid = kmalloc_obj(*uuid); 2987 if (!uuid) { 2988 err = -ENOMEM; 2989 goto failed; 2990 } 2991 2992 memcpy(uuid->uuid, cp->uuid, 16); 2993 uuid->svc_hint = cp->svc_hint; 2994 uuid->size = get_uuid_size(cp->uuid); 2995 2996 list_add_tail(&uuid->list, &hdev->uuids); 2997 2998 cmd = mgmt_pending_new(sk, MGMT_OP_ADD_UUID, hdev, data, len); 2999 if (!cmd) { 3000 err = -ENOMEM; 3001 goto failed; 3002 } 3003 3004 /* MGMT_OP_ADD_UUID don't require adapter the UP/Running so use 3005 * hci_cmd_sync_submit instead of hci_cmd_sync_queue. 3006 */ 3007 err = hci_cmd_sync_submit(hdev, add_uuid_sync, cmd, 3008 mgmt_class_complete); 3009 if (err < 0) { 3010 mgmt_pending_free(cmd); 3011 goto failed; 3012 } 3013 3014 failed: 3015 hci_dev_unlock(hdev); 3016 return err; 3017 } 3018 3019 static bool enable_service_cache(struct hci_dev *hdev) 3020 { 3021 if (!hdev_is_powered(hdev)) 3022 return false; 3023 3024 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { 3025 queue_delayed_work(hdev->workqueue, &hdev->service_cache, 3026 CACHE_TIMEOUT); 3027 return true; 3028 } 3029 3030 return false; 3031 } 3032 3033 static int remove_uuid_sync(struct hci_dev *hdev, void *data) 3034 { 3035 int err; 3036 3037 err = hci_update_class_sync(hdev); 3038 if (err) 3039 return err; 3040 3041 return hci_update_eir_sync(hdev); 3042 } 3043 3044 static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data, 3045 u16 len) 3046 { 3047 struct mgmt_cp_remove_uuid *cp = data; 3048 struct mgmt_pending_cmd *cmd; 3049 struct bt_uuid *match, *tmp; 3050 static const u8 bt_uuid_any[] = { 3051 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 3052 }; 3053 int err, found; 3054 3055 bt_dev_dbg(hdev, "sock %p", sk); 3056 3057 hci_dev_lock(hdev); 3058 3059 if (pending_eir_or_class(hdev)) { 3060 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, 3061 MGMT_STATUS_BUSY); 3062 goto unlock; 3063 } 3064 3065 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { 3066 hci_uuids_clear(hdev); 3067 3068 if (enable_service_cache(hdev)) { 3069 err = mgmt_cmd_complete(sk, hdev->id, 3070 MGMT_OP_REMOVE_UUID, 3071 0, hdev->dev_class, 3); 3072 goto unlock; 3073 } 3074 3075 goto update_class; 3076 } 3077 3078 found = 0; 3079 3080 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { 3081 if (memcmp(match->uuid, cp->uuid, 16) != 0) 3082 continue; 3083 3084 list_del(&match->list); 3085 kfree(match); 3086 found++; 3087 } 3088 3089 if (found == 0) { 3090 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, 3091 MGMT_STATUS_INVALID_PARAMS); 3092 goto unlock; 3093 } 3094 3095 update_class: 3096 cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); 3097 if (!cmd) { 3098 err = -ENOMEM; 3099 goto unlock; 3100 } 3101 3102 /* MGMT_OP_REMOVE_UUID don't require adapter the UP/Running so use 3103 * hci_cmd_sync_submit instead of hci_cmd_sync_queue. 3104 */ 3105 err = hci_cmd_sync_submit(hdev, remove_uuid_sync, cmd, 3106 mgmt_class_complete); 3107 if (err < 0) 3108 mgmt_pending_free(cmd); 3109 3110 unlock: 3111 hci_dev_unlock(hdev); 3112 return err; 3113 } 3114 3115 static int set_class_sync(struct hci_dev *hdev, void *data) 3116 { 3117 int err = 0; 3118 3119 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { 3120 cancel_delayed_work_sync(&hdev->service_cache); 3121 err = hci_update_eir_sync(hdev); 3122 } 3123 3124 if (err) 3125 return err; 3126 3127 return hci_update_class_sync(hdev); 3128 } 3129 3130 static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data, 3131 u16 len) 3132 { 3133 struct mgmt_cp_set_dev_class *cp = data; 3134 struct mgmt_pending_cmd *cmd; 3135 int err; 3136 3137 bt_dev_dbg(hdev, "sock %p", sk); 3138 3139 if (!lmp_bredr_capable(hdev)) 3140 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 3141 MGMT_STATUS_NOT_SUPPORTED); 3142 3143 hci_dev_lock(hdev); 3144 3145 if (pending_eir_or_class(hdev)) { 3146 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 3147 MGMT_STATUS_BUSY); 3148 goto unlock; 3149 } 3150 3151 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { 3152 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 3153 MGMT_STATUS_INVALID_PARAMS); 3154 goto unlock; 3155 } 3156 3157 hdev->major_class = cp->major; 3158 hdev->minor_class = cp->minor; 3159 3160 if (!hdev_is_powered(hdev)) { 3161 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, 3162 hdev->dev_class, 3); 3163 goto unlock; 3164 } 3165 3166 cmd = mgmt_pending_new(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len); 3167 if (!cmd) { 3168 err = -ENOMEM; 3169 goto unlock; 3170 } 3171 3172 /* MGMT_OP_SET_DEV_CLASS don't require adapter the UP/Running so use 3173 * hci_cmd_sync_submit instead of hci_cmd_sync_queue. 3174 */ 3175 err = hci_cmd_sync_submit(hdev, set_class_sync, cmd, 3176 mgmt_class_complete); 3177 if (err < 0) 3178 mgmt_pending_free(cmd); 3179 3180 unlock: 3181 hci_dev_unlock(hdev); 3182 return err; 3183 } 3184 3185 static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, 3186 u16 len) 3187 { 3188 struct mgmt_cp_load_link_keys *cp = data; 3189 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / 3190 sizeof(struct mgmt_link_key_info)); 3191 u16 key_count, expected_len; 3192 bool changed; 3193 int i; 3194 3195 bt_dev_dbg(hdev, "sock %p", sk); 3196 3197 if (!lmp_bredr_capable(hdev)) 3198 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 3199 MGMT_STATUS_NOT_SUPPORTED); 3200 3201 key_count = __le16_to_cpu(cp->key_count); 3202 if (key_count > max_key_count) { 3203 bt_dev_err(hdev, "load_link_keys: too big key_count value %u", 3204 key_count); 3205 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 3206 MGMT_STATUS_INVALID_PARAMS); 3207 } 3208 3209 expected_len = struct_size(cp, keys, key_count); 3210 if (expected_len != len) { 3211 bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes", 3212 expected_len, len); 3213 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 3214 MGMT_STATUS_INVALID_PARAMS); 3215 } 3216 3217 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) 3218 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 3219 MGMT_STATUS_INVALID_PARAMS); 3220 3221 bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys, 3222 key_count); 3223 3224 hci_dev_lock(hdev); 3225 3226 hci_link_keys_clear(hdev); 3227 3228 if (cp->debug_keys) 3229 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); 3230 else 3231 changed = hci_dev_test_and_clear_flag(hdev, 3232 HCI_KEEP_DEBUG_KEYS); 3233 3234 if (changed) 3235 new_settings(hdev, NULL); 3236 3237 for (i = 0; i < key_count; i++) { 3238 struct mgmt_link_key_info *key = &cp->keys[i]; 3239 3240 if (hci_is_blocked_key(hdev, 3241 HCI_BLOCKED_KEY_TYPE_LINKKEY, 3242 key->val)) { 3243 bt_dev_warn(hdev, "Skipping blocked link key for %pMR", 3244 &key->addr.bdaddr); 3245 continue; 3246 } 3247 3248 if (key->addr.type != BDADDR_BREDR) { 3249 bt_dev_warn(hdev, 3250 "Invalid link address type %u for %pMR", 3251 key->addr.type, &key->addr.bdaddr); 3252 continue; 3253 } 3254 3255 if (key->type > 0x08) { 3256 bt_dev_warn(hdev, "Invalid link key type %u for %pMR", 3257 key->type, &key->addr.bdaddr); 3258 continue; 3259 } 3260 3261 /* Always ignore debug keys and require a new pairing if 3262 * the user wants to use them. 3263 */ 3264 if (key->type == HCI_LK_DEBUG_COMBINATION) 3265 continue; 3266 3267 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, 3268 key->type, key->pin_len, NULL); 3269 } 3270 3271 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0); 3272 3273 hci_dev_unlock(hdev); 3274 3275 return 0; 3276 } 3277 3278 static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, 3279 u8 addr_type, struct sock *skip_sk) 3280 { 3281 struct mgmt_ev_device_unpaired ev; 3282 3283 bacpy(&ev.addr.bdaddr, bdaddr); 3284 ev.addr.type = addr_type; 3285 3286 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), 3287 skip_sk); 3288 } 3289 3290 static void unpair_device_complete(struct hci_dev *hdev, void *data, int err) 3291 { 3292 struct mgmt_pending_cmd *cmd = data; 3293 struct mgmt_cp_unpair_device *cp = cmd->param; 3294 3295 if (!err) 3296 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); 3297 3298 cmd->cmd_complete(cmd, err); 3299 mgmt_pending_free(cmd); 3300 } 3301 3302 static int unpair_device_sync(struct hci_dev *hdev, void *data) 3303 { 3304 struct mgmt_pending_cmd *cmd = data; 3305 struct mgmt_cp_unpair_device *cp = cmd->param; 3306 struct hci_conn *conn; 3307 3308 hci_dev_lock(hdev); 3309 3310 if (cp->addr.type == BDADDR_BREDR) 3311 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, 3312 &cp->addr.bdaddr); 3313 else 3314 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, 3315 le_addr_type(cp->addr.type)); 3316 3317 if (conn) 3318 hci_conn_get(conn); 3319 3320 hci_dev_unlock(hdev); 3321 3322 if (!conn) 3323 return 0; 3324 3325 /* Disregard any possible error since the likes of hci_abort_conn_sync 3326 * will clean up the connection no matter the error. 3327 */ 3328 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); 3329 hci_conn_put(conn); 3330 3331 return 0; 3332 } 3333 3334 static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data, 3335 u16 len) 3336 { 3337 struct mgmt_cp_unpair_device *cp = data; 3338 struct mgmt_rp_unpair_device rp; 3339 struct hci_conn_params *params; 3340 struct mgmt_pending_cmd *cmd; 3341 struct hci_conn *conn; 3342 u8 addr_type; 3343 int err; 3344 3345 memset(&rp, 0, sizeof(rp)); 3346 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); 3347 rp.addr.type = cp->addr.type; 3348 3349 if (!bdaddr_type_is_valid(cp->addr.type)) 3350 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 3351 MGMT_STATUS_INVALID_PARAMS, 3352 &rp, sizeof(rp)); 3353 3354 if (cp->disconnect != 0x00 && cp->disconnect != 0x01) 3355 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 3356 MGMT_STATUS_INVALID_PARAMS, 3357 &rp, sizeof(rp)); 3358 3359 hci_dev_lock(hdev); 3360 3361 if (!hdev_is_powered(hdev)) { 3362 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 3363 MGMT_STATUS_NOT_POWERED, &rp, 3364 sizeof(rp)); 3365 goto unlock; 3366 } 3367 3368 if (cp->addr.type == BDADDR_BREDR) { 3369 /* If disconnection is requested, then look up the 3370 * connection. If the remote device is connected, it 3371 * will be later used to terminate the link. 3372 * 3373 * Setting it to NULL explicitly will cause no 3374 * termination of the link. 3375 */ 3376 if (cp->disconnect) 3377 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, 3378 &cp->addr.bdaddr); 3379 else 3380 conn = NULL; 3381 3382 err = hci_remove_link_key(hdev, &cp->addr.bdaddr); 3383 if (err < 0) { 3384 err = mgmt_cmd_complete(sk, hdev->id, 3385 MGMT_OP_UNPAIR_DEVICE, 3386 MGMT_STATUS_NOT_PAIRED, &rp, 3387 sizeof(rp)); 3388 goto unlock; 3389 } 3390 3391 goto done; 3392 } 3393 3394 /* LE address type */ 3395 addr_type = le_addr_type(cp->addr.type); 3396 3397 /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */ 3398 err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type); 3399 if (err < 0) { 3400 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 3401 MGMT_STATUS_NOT_PAIRED, &rp, 3402 sizeof(rp)); 3403 goto unlock; 3404 } 3405 3406 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type); 3407 if (!conn) { 3408 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); 3409 goto done; 3410 } 3411 3412 3413 /* Defer clearing up the connection parameters until closing to 3414 * give a chance of keeping them if a repairing happens. 3415 */ 3416 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, conn->flags); 3417 3418 /* Disable auto-connection parameters if present */ 3419 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type); 3420 if (params) { 3421 if (params->explicit_connect) 3422 params->auto_connect = HCI_AUTO_CONN_EXPLICIT; 3423 else 3424 params->auto_connect = HCI_AUTO_CONN_DISABLED; 3425 } 3426 3427 /* If disconnection is not requested, then clear the connection 3428 * variable so that the link is not terminated. 3429 */ 3430 if (!cp->disconnect) 3431 conn = NULL; 3432 3433 done: 3434 /* If the connection variable is set, then termination of the 3435 * link is requested. 3436 */ 3437 if (!conn) { 3438 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, 3439 &rp, sizeof(rp)); 3440 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); 3441 goto unlock; 3442 } 3443 3444 cmd = mgmt_pending_new(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, 3445 sizeof(*cp)); 3446 if (!cmd) { 3447 err = -ENOMEM; 3448 goto unlock; 3449 } 3450 3451 cmd->cmd_complete = addr_cmd_complete; 3452 3453 err = hci_cmd_sync_queue(hdev, unpair_device_sync, cmd, 3454 unpair_device_complete); 3455 if (err < 0) 3456 mgmt_pending_free(cmd); 3457 3458 unlock: 3459 hci_dev_unlock(hdev); 3460 return err; 3461 } 3462 3463 static void disconnect_complete(struct hci_dev *hdev, void *data, int err) 3464 { 3465 struct mgmt_pending_cmd *cmd = data; 3466 3467 cmd->cmd_complete(cmd, mgmt_status(err)); 3468 mgmt_pending_free(cmd); 3469 } 3470 3471 static int disconnect_sync(struct hci_dev *hdev, void *data) 3472 { 3473 struct mgmt_pending_cmd *cmd = data; 3474 struct mgmt_cp_disconnect *cp = cmd->param; 3475 struct hci_conn *conn; 3476 3477 hci_dev_lock(hdev); 3478 3479 if (cp->addr.type == BDADDR_BREDR) 3480 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, 3481 &cp->addr.bdaddr); 3482 else 3483 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, 3484 le_addr_type(cp->addr.type)); 3485 3486 if (conn) 3487 hci_conn_get(conn); 3488 3489 hci_dev_unlock(hdev); 3490 3491 if (!conn) 3492 return -ENOTCONN; 3493 3494 /* Disregard any possible error since the likes of hci_abort_conn_sync 3495 * will clean up the connection no matter the error. 3496 */ 3497 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); 3498 hci_conn_put(conn); 3499 3500 return 0; 3501 } 3502 3503 static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data, 3504 u16 len) 3505 { 3506 struct mgmt_cp_disconnect *cp = data; 3507 struct mgmt_rp_disconnect rp; 3508 struct mgmt_pending_cmd *cmd; 3509 int err; 3510 3511 bt_dev_dbg(hdev, "sock %p", sk); 3512 3513 memset(&rp, 0, sizeof(rp)); 3514 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); 3515 rp.addr.type = cp->addr.type; 3516 3517 if (!bdaddr_type_is_valid(cp->addr.type)) 3518 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, 3519 MGMT_STATUS_INVALID_PARAMS, 3520 &rp, sizeof(rp)); 3521 3522 hci_dev_lock(hdev); 3523 3524 if (!test_bit(HCI_UP, &hdev->flags)) { 3525 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, 3526 MGMT_STATUS_NOT_POWERED, &rp, 3527 sizeof(rp)); 3528 goto failed; 3529 } 3530 3531 cmd = mgmt_pending_new(sk, MGMT_OP_DISCONNECT, hdev, data, len); 3532 if (!cmd) { 3533 err = -ENOMEM; 3534 goto failed; 3535 } 3536 3537 cmd->cmd_complete = generic_cmd_complete; 3538 3539 err = hci_cmd_sync_queue(hdev, disconnect_sync, cmd, 3540 disconnect_complete); 3541 if (err < 0) 3542 mgmt_pending_free(cmd); 3543 3544 failed: 3545 hci_dev_unlock(hdev); 3546 return err; 3547 } 3548 3549 static u8 link_to_bdaddr(u8 link_type, u8 addr_type) 3550 { 3551 switch (link_type) { 3552 case CIS_LINK: 3553 case BIS_LINK: 3554 case PA_LINK: 3555 case LE_LINK: 3556 switch (addr_type) { 3557 case ADDR_LE_DEV_PUBLIC: 3558 return BDADDR_LE_PUBLIC; 3559 3560 default: 3561 /* Fallback to LE Random address type */ 3562 return BDADDR_LE_RANDOM; 3563 } 3564 3565 default: 3566 /* Fallback to BR/EDR type */ 3567 return BDADDR_BREDR; 3568 } 3569 } 3570 3571 static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, 3572 u16 data_len) 3573 { 3574 struct mgmt_rp_get_connections *rp; 3575 struct hci_conn *c; 3576 int err; 3577 u16 i; 3578 3579 bt_dev_dbg(hdev, "sock %p", sk); 3580 3581 hci_dev_lock(hdev); 3582 3583 if (!hdev_is_powered(hdev)) { 3584 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 3585 MGMT_STATUS_NOT_POWERED); 3586 goto unlock; 3587 } 3588 3589 i = 0; 3590 list_for_each_entry(c, &hdev->conn_hash.list, list) { 3591 if (test_bit(HCI_CONN_MGMT_CONNECTED, c->flags)) 3592 i++; 3593 } 3594 3595 rp = kmalloc_flex(*rp, addr, i); 3596 if (!rp) { 3597 err = -ENOMEM; 3598 goto unlock; 3599 } 3600 3601 i = 0; 3602 list_for_each_entry(c, &hdev->conn_hash.list, list) { 3603 if (!test_bit(HCI_CONN_MGMT_CONNECTED, c->flags)) 3604 continue; 3605 bacpy(&rp->addr[i].bdaddr, &c->dst); 3606 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); 3607 if (c->type == SCO_LINK || c->type == ESCO_LINK) 3608 continue; 3609 i++; 3610 } 3611 3612 rp->conn_count = cpu_to_le16(i); 3613 3614 /* Recalculate length in case of filtered SCO connections, etc */ 3615 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, 3616 struct_size(rp, addr, i)); 3617 3618 kfree(rp); 3619 3620 unlock: 3621 hci_dev_unlock(hdev); 3622 return err; 3623 } 3624 3625 static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, 3626 struct mgmt_cp_pin_code_neg_reply *cp) 3627 { 3628 struct mgmt_pending_cmd *cmd; 3629 int err; 3630 3631 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp, 3632 sizeof(*cp)); 3633 if (!cmd) 3634 return -ENOMEM; 3635 3636 cmd->cmd_complete = addr_cmd_complete; 3637 3638 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, 3639 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); 3640 if (err < 0) 3641 mgmt_pending_remove(cmd); 3642 3643 return err; 3644 } 3645 3646 static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data, 3647 u16 len) 3648 { 3649 struct hci_conn *conn; 3650 struct mgmt_cp_pin_code_reply *cp = data; 3651 struct hci_cp_pin_code_reply reply; 3652 struct mgmt_pending_cmd *cmd; 3653 int err; 3654 3655 bt_dev_dbg(hdev, "sock %p", sk); 3656 3657 hci_dev_lock(hdev); 3658 3659 if (!hdev_is_powered(hdev)) { 3660 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, 3661 MGMT_STATUS_NOT_POWERED); 3662 goto failed; 3663 } 3664 3665 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); 3666 if (!conn) { 3667 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, 3668 MGMT_STATUS_NOT_CONNECTED); 3669 goto failed; 3670 } 3671 3672 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { 3673 struct mgmt_cp_pin_code_neg_reply ncp; 3674 3675 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); 3676 3677 bt_dev_err(hdev, "PIN code is not 16 bytes long"); 3678 3679 err = send_pin_code_neg_reply(sk, hdev, &ncp); 3680 if (err >= 0) 3681 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, 3682 MGMT_STATUS_INVALID_PARAMS); 3683 3684 goto failed; 3685 } 3686 3687 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len); 3688 if (!cmd) { 3689 err = -ENOMEM; 3690 goto failed; 3691 } 3692 3693 cmd->cmd_complete = addr_cmd_complete; 3694 3695 bacpy(&reply.bdaddr, &cp->addr.bdaddr); 3696 reply.pin_len = cp->pin_len; 3697 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); 3698 3699 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); 3700 if (err < 0) 3701 mgmt_pending_remove(cmd); 3702 3703 failed: 3704 hci_dev_unlock(hdev); 3705 return err; 3706 } 3707 3708 static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, 3709 u16 len) 3710 { 3711 struct mgmt_cp_set_io_capability *cp = data; 3712 3713 bt_dev_dbg(hdev, "sock %p", sk); 3714 3715 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) 3716 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 3717 MGMT_STATUS_INVALID_PARAMS); 3718 3719 hci_dev_lock(hdev); 3720 3721 hdev->io_capability = cp->io_capability; 3722 3723 bt_dev_dbg(hdev, "IO capability set to 0x%02x", hdev->io_capability); 3724 3725 hci_dev_unlock(hdev); 3726 3727 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, 3728 NULL, 0); 3729 } 3730 3731 static struct mgmt_pending_cmd *remove_pairing(struct hci_conn *conn) 3732 { 3733 struct hci_dev *hdev = conn->hdev; 3734 struct mgmt_pending_cmd *cmd; 3735 3736 mutex_lock(&hdev->mgmt_pending_lock); 3737 3738 list_for_each_entry(cmd, &hdev->mgmt_pending, list) { 3739 if (cmd->opcode != MGMT_OP_PAIR_DEVICE) 3740 continue; 3741 3742 if (cmd->user_data != conn) 3743 continue; 3744 3745 list_del(&cmd->list); 3746 mutex_unlock(&hdev->mgmt_pending_lock); 3747 return cmd; 3748 } 3749 3750 mutex_unlock(&hdev->mgmt_pending_lock); 3751 3752 return NULL; 3753 } 3754 3755 static struct mgmt_pending_cmd *remove_pairing_by_addr(struct hci_dev *hdev, 3756 bdaddr_t *bdaddr) 3757 { 3758 struct mgmt_pending_cmd *cmd; 3759 struct hci_conn *conn; 3760 3761 mutex_lock(&hdev->mgmt_pending_lock); 3762 3763 list_for_each_entry(cmd, &hdev->mgmt_pending, list) { 3764 if (cmd->opcode != MGMT_OP_PAIR_DEVICE) 3765 continue; 3766 3767 conn = cmd->user_data; 3768 if (bacmp(bdaddr, &conn->dst) != 0) 3769 continue; 3770 3771 list_del(&cmd->list); 3772 mutex_unlock(&hdev->mgmt_pending_lock); 3773 return cmd; 3774 } 3775 3776 mutex_unlock(&hdev->mgmt_pending_lock); 3777 3778 return NULL; 3779 } 3780 3781 static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) 3782 { 3783 struct mgmt_rp_pair_device rp; 3784 struct hci_conn *conn = cmd->user_data; 3785 int err; 3786 3787 bacpy(&rp.addr.bdaddr, &conn->dst); 3788 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); 3789 3790 err = mgmt_cmd_complete(cmd->sk, cmd->hdev->id, MGMT_OP_PAIR_DEVICE, 3791 status, &rp, sizeof(rp)); 3792 3793 /* So we don't get further callbacks for this connection */ 3794 conn->connect_cfm_cb = NULL; 3795 conn->security_cfm_cb = NULL; 3796 conn->disconn_cfm_cb = NULL; 3797 3798 hci_conn_drop(conn); 3799 3800 /* The device is paired so there is no need to remove 3801 * its connection parameters anymore. 3802 */ 3803 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, conn->flags); 3804 3805 hci_conn_put(conn); 3806 3807 return err; 3808 } 3809 3810 void mgmt_smp_complete(struct hci_conn *conn, bool complete) 3811 { 3812 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; 3813 struct mgmt_pending_cmd *cmd; 3814 3815 cmd = remove_pairing(conn); 3816 if (cmd) { 3817 cmd->cmd_complete(cmd, status); 3818 mgmt_pending_free(cmd); 3819 } 3820 } 3821 3822 static void pairing_complete_cb(struct hci_conn *conn, u8 status) 3823 { 3824 struct mgmt_pending_cmd *cmd; 3825 3826 BT_DBG("status %u", status); 3827 3828 cmd = remove_pairing(conn); 3829 if (!cmd) { 3830 BT_DBG("Unable to find a pending command"); 3831 return; 3832 } 3833 3834 cmd->cmd_complete(cmd, mgmt_status(status)); 3835 mgmt_pending_free(cmd); 3836 } 3837 3838 static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) 3839 { 3840 struct mgmt_pending_cmd *cmd; 3841 3842 BT_DBG("status %u", status); 3843 3844 if (!status) 3845 return; 3846 3847 cmd = remove_pairing(conn); 3848 if (!cmd) { 3849 BT_DBG("Unable to find a pending command"); 3850 return; 3851 } 3852 3853 cmd->cmd_complete(cmd, mgmt_status(status)); 3854 mgmt_pending_free(cmd); 3855 } 3856 3857 static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, 3858 u16 len) 3859 { 3860 struct mgmt_cp_pair_device *cp = data; 3861 struct mgmt_rp_pair_device rp; 3862 struct mgmt_pending_cmd *cmd; 3863 u8 sec_level, auth_type; 3864 struct hci_conn *conn; 3865 int err; 3866 3867 bt_dev_dbg(hdev, "sock %p", sk); 3868 3869 memset(&rp, 0, sizeof(rp)); 3870 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); 3871 rp.addr.type = cp->addr.type; 3872 3873 if (!bdaddr_type_is_valid(cp->addr.type)) 3874 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3875 MGMT_STATUS_INVALID_PARAMS, 3876 &rp, sizeof(rp)); 3877 3878 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) 3879 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3880 MGMT_STATUS_INVALID_PARAMS, 3881 &rp, sizeof(rp)); 3882 3883 hci_dev_lock(hdev); 3884 3885 if (!hdev_is_powered(hdev)) { 3886 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3887 MGMT_STATUS_NOT_POWERED, &rp, 3888 sizeof(rp)); 3889 goto unlock; 3890 } 3891 3892 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { 3893 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3894 MGMT_STATUS_ALREADY_PAIRED, &rp, 3895 sizeof(rp)); 3896 goto unlock; 3897 } 3898 3899 sec_level = BT_SECURITY_MEDIUM; 3900 auth_type = HCI_AT_DEDICATED_BONDING; 3901 3902 if (cp->addr.type == BDADDR_BREDR) { 3903 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, 3904 auth_type, CONN_REASON_PAIR_DEVICE, 3905 HCI_ACL_CONN_TIMEOUT); 3906 } else { 3907 u8 addr_type = le_addr_type(cp->addr.type); 3908 struct hci_conn_params *p; 3909 3910 /* When pairing a new device, it is expected to remember 3911 * this device for future connections. Adding the connection 3912 * parameter information ahead of time allows tracking 3913 * of the peripheral preferred values and will speed up any 3914 * further connection establishment. 3915 * 3916 * If connection parameters already exist, then they 3917 * will be kept and this function does nothing. 3918 */ 3919 p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); 3920 if (!p) { 3921 err = -EIO; 3922 goto unlock; 3923 } 3924 3925 if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT) 3926 p->auto_connect = HCI_AUTO_CONN_DISABLED; 3927 3928 conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type, 3929 sec_level, HCI_LE_CONN_TIMEOUT, 3930 CONN_REASON_PAIR_DEVICE); 3931 } 3932 3933 if (IS_ERR(conn)) { 3934 int status; 3935 3936 if (PTR_ERR(conn) == -EBUSY) 3937 status = MGMT_STATUS_BUSY; 3938 else if (PTR_ERR(conn) == -EOPNOTSUPP) 3939 status = MGMT_STATUS_NOT_SUPPORTED; 3940 else if (PTR_ERR(conn) == -ECONNREFUSED) 3941 status = MGMT_STATUS_REJECTED; 3942 else 3943 status = MGMT_STATUS_CONNECT_FAILED; 3944 3945 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3946 status, &rp, sizeof(rp)); 3947 goto unlock; 3948 } 3949 3950 if (conn->connect_cfm_cb) { 3951 hci_conn_drop(conn); 3952 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3953 MGMT_STATUS_BUSY, &rp, sizeof(rp)); 3954 goto unlock; 3955 } 3956 3957 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); 3958 if (!cmd) { 3959 err = -ENOMEM; 3960 hci_conn_drop(conn); 3961 goto unlock; 3962 } 3963 3964 cmd->cmd_complete = pairing_complete; 3965 3966 /* For LE, just connecting isn't a proof that the pairing finished */ 3967 if (cp->addr.type == BDADDR_BREDR) { 3968 conn->connect_cfm_cb = pairing_complete_cb; 3969 conn->security_cfm_cb = pairing_complete_cb; 3970 conn->disconn_cfm_cb = pairing_complete_cb; 3971 } else { 3972 conn->connect_cfm_cb = le_pairing_complete_cb; 3973 conn->security_cfm_cb = le_pairing_complete_cb; 3974 conn->disconn_cfm_cb = le_pairing_complete_cb; 3975 } 3976 3977 conn->io_capability = cp->io_cap; 3978 cmd->user_data = hci_conn_get(conn); 3979 3980 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && 3981 hci_conn_security(conn, sec_level, auth_type, true)) { 3982 cmd->cmd_complete(cmd, 0); 3983 mgmt_pending_remove(cmd); 3984 } 3985 3986 err = 0; 3987 3988 unlock: 3989 hci_dev_unlock(hdev); 3990 return err; 3991 } 3992 3993 static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, 3994 u16 len) 3995 { 3996 struct mgmt_addr_info *addr = data; 3997 struct mgmt_pending_cmd *cmd; 3998 struct hci_conn *conn; 3999 int err; 4000 4001 bt_dev_dbg(hdev, "sock %p", sk); 4002 4003 hci_dev_lock(hdev); 4004 4005 if (!hdev_is_powered(hdev)) { 4006 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 4007 MGMT_STATUS_NOT_POWERED); 4008 goto unlock; 4009 } 4010 4011 cmd = remove_pairing_by_addr(hdev, &addr->bdaddr); 4012 if (!cmd) { 4013 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 4014 MGMT_STATUS_INVALID_PARAMS); 4015 goto unlock; 4016 } 4017 4018 conn = hci_conn_get(cmd->user_data); 4019 4020 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); 4021 mgmt_pending_free(cmd); 4022 4023 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, 4024 addr, sizeof(*addr)); 4025 4026 /* Since user doesn't want to proceed with the connection, abort any 4027 * ongoing pairing and then terminate the link if it was created 4028 * because of the pair device action. 4029 */ 4030 if (addr->type == BDADDR_BREDR) 4031 hci_remove_link_key(hdev, &addr->bdaddr); 4032 else 4033 smp_cancel_and_remove_pairing(hdev, &addr->bdaddr, 4034 le_addr_type(addr->type)); 4035 4036 if (conn->conn_reason == CONN_REASON_PAIR_DEVICE) 4037 hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM); 4038 4039 hci_conn_put(conn); 4040 4041 unlock: 4042 hci_dev_unlock(hdev); 4043 return err; 4044 } 4045 4046 static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, 4047 struct mgmt_addr_info *addr, u16 mgmt_op, 4048 u16 hci_op, __le32 passkey) 4049 { 4050 struct mgmt_pending_cmd *cmd; 4051 struct hci_conn *conn; 4052 int err; 4053 4054 hci_dev_lock(hdev); 4055 4056 if (!hdev_is_powered(hdev)) { 4057 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, 4058 MGMT_STATUS_NOT_POWERED, addr, 4059 sizeof(*addr)); 4060 goto done; 4061 } 4062 4063 if (addr->type == BDADDR_BREDR) 4064 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); 4065 else 4066 conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr, 4067 le_addr_type(addr->type)); 4068 4069 if (!conn) { 4070 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, 4071 MGMT_STATUS_NOT_CONNECTED, addr, 4072 sizeof(*addr)); 4073 goto done; 4074 } 4075 4076 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { 4077 lockdep_assert_held(&conn->hdev->lock); 4078 4079 err = smp_user_confirm_reply(conn, mgmt_op, passkey); 4080 if (!err) 4081 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, 4082 MGMT_STATUS_SUCCESS, addr, 4083 sizeof(*addr)); 4084 else 4085 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, 4086 MGMT_STATUS_FAILED, addr, 4087 sizeof(*addr)); 4088 4089 goto done; 4090 } 4091 4092 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); 4093 if (!cmd) { 4094 err = -ENOMEM; 4095 goto done; 4096 } 4097 4098 cmd->cmd_complete = addr_cmd_complete; 4099 4100 /* Continue with pairing via HCI */ 4101 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { 4102 struct hci_cp_user_passkey_reply cp; 4103 4104 bacpy(&cp.bdaddr, &addr->bdaddr); 4105 cp.passkey = passkey; 4106 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); 4107 } else 4108 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), 4109 &addr->bdaddr); 4110 4111 if (err < 0) 4112 mgmt_pending_remove(cmd); 4113 4114 done: 4115 hci_dev_unlock(hdev); 4116 return err; 4117 } 4118 4119 static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, 4120 void *data, u16 len) 4121 { 4122 struct mgmt_cp_pin_code_neg_reply *cp = data; 4123 4124 bt_dev_dbg(hdev, "sock %p", sk); 4125 4126 return user_pairing_resp(sk, hdev, &cp->addr, 4127 MGMT_OP_PIN_CODE_NEG_REPLY, 4128 HCI_OP_PIN_CODE_NEG_REPLY, 0); 4129 } 4130 4131 static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, 4132 u16 len) 4133 { 4134 struct mgmt_cp_user_confirm_reply *cp = data; 4135 4136 bt_dev_dbg(hdev, "sock %p", sk); 4137 4138 if (len != sizeof(*cp)) 4139 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, 4140 MGMT_STATUS_INVALID_PARAMS); 4141 4142 return user_pairing_resp(sk, hdev, &cp->addr, 4143 MGMT_OP_USER_CONFIRM_REPLY, 4144 HCI_OP_USER_CONFIRM_REPLY, 0); 4145 } 4146 4147 static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev, 4148 void *data, u16 len) 4149 { 4150 struct mgmt_cp_user_confirm_neg_reply *cp = data; 4151 4152 bt_dev_dbg(hdev, "sock %p", sk); 4153 4154 return user_pairing_resp(sk, hdev, &cp->addr, 4155 MGMT_OP_USER_CONFIRM_NEG_REPLY, 4156 HCI_OP_USER_CONFIRM_NEG_REPLY, 0); 4157 } 4158 4159 static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, 4160 u16 len) 4161 { 4162 struct mgmt_cp_user_passkey_reply *cp = data; 4163 4164 bt_dev_dbg(hdev, "sock %p", sk); 4165 4166 return user_pairing_resp(sk, hdev, &cp->addr, 4167 MGMT_OP_USER_PASSKEY_REPLY, 4168 HCI_OP_USER_PASSKEY_REPLY, cp->passkey); 4169 } 4170 4171 static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev, 4172 void *data, u16 len) 4173 { 4174 struct mgmt_cp_user_passkey_neg_reply *cp = data; 4175 4176 bt_dev_dbg(hdev, "sock %p", sk); 4177 4178 return user_pairing_resp(sk, hdev, &cp->addr, 4179 MGMT_OP_USER_PASSKEY_NEG_REPLY, 4180 HCI_OP_USER_PASSKEY_NEG_REPLY, 0); 4181 } 4182 4183 static int adv_expire_sync(struct hci_dev *hdev, u32 flags) 4184 { 4185 struct adv_info *adv_instance; 4186 u8 instance; 4187 4188 hci_dev_lock(hdev); 4189 4190 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance); 4191 if (!adv_instance) 4192 goto unlock; 4193 4194 /* stop if current instance doesn't need to be changed */ 4195 if (!(adv_instance->flags & flags)) 4196 goto unlock; 4197 4198 cancel_adv_timeout(hdev); 4199 4200 adv_instance = hci_get_next_instance(hdev, adv_instance->instance); 4201 if (!adv_instance) 4202 goto unlock; 4203 4204 instance = adv_instance->instance; 4205 4206 hci_dev_unlock(hdev); 4207 4208 hci_schedule_adv_instance_sync(hdev, instance, true); 4209 4210 return 0; 4211 4212 unlock: 4213 hci_dev_unlock(hdev); 4214 4215 return 0; 4216 } 4217 4218 static int name_changed_sync(struct hci_dev *hdev, void *data) 4219 { 4220 return adv_expire_sync(hdev, MGMT_ADV_FLAG_LOCAL_NAME); 4221 } 4222 4223 static void set_name_complete(struct hci_dev *hdev, void *data, int err) 4224 { 4225 struct mgmt_pending_cmd *cmd = data; 4226 struct mgmt_cp_set_local_name *cp; 4227 u8 status = mgmt_status(err); 4228 4229 bt_dev_dbg(hdev, "err %d", err); 4230 4231 if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) 4232 return; 4233 4234 cp = cmd->param; 4235 4236 if (status) { 4237 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 4238 status); 4239 } else { 4240 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, 4241 cp, sizeof(*cp)); 4242 4243 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) 4244 hci_cmd_sync_queue(hdev, name_changed_sync, NULL, NULL); 4245 } 4246 4247 mgmt_pending_free(cmd); 4248 } 4249 4250 static int set_name_sync(struct hci_dev *hdev, void *data) 4251 { 4252 struct mgmt_pending_cmd *cmd = data; 4253 struct mgmt_cp_set_local_name cp; 4254 4255 mutex_lock(&hdev->mgmt_pending_lock); 4256 4257 if (!__mgmt_pending_listed(hdev, cmd)) { 4258 mutex_unlock(&hdev->mgmt_pending_lock); 4259 return -ECANCELED; 4260 } 4261 4262 memcpy(&cp, cmd->param, sizeof(cp)); 4263 4264 mutex_unlock(&hdev->mgmt_pending_lock); 4265 4266 if (lmp_bredr_capable(hdev)) { 4267 hci_update_name_sync(hdev, cp.name); 4268 hci_update_eir_sync(hdev); 4269 } 4270 4271 /* The name is stored in the scan response data and so 4272 * no need to update the advertising data here. 4273 */ 4274 if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING)) 4275 hci_update_scan_rsp_data_sync(hdev, hdev->cur_adv_instance); 4276 4277 return 0; 4278 } 4279 4280 static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data, 4281 u16 len) 4282 { 4283 struct mgmt_cp_set_local_name *cp = data; 4284 struct mgmt_pending_cmd *cmd; 4285 int err; 4286 4287 bt_dev_dbg(hdev, "sock %p", sk); 4288 4289 hci_dev_lock(hdev); 4290 4291 /* If the old values are the same as the new ones just return a 4292 * direct command complete event. 4293 */ 4294 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && 4295 !memcmp(hdev->short_name, cp->short_name, 4296 sizeof(hdev->short_name))) { 4297 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, 4298 data, len); 4299 goto failed; 4300 } 4301 4302 if (hdev_is_powered(hdev) && pending_eir_or_class(hdev)) { 4303 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 4304 MGMT_STATUS_BUSY); 4305 goto failed; 4306 } 4307 4308 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); 4309 4310 if (!hdev_is_powered(hdev)) { 4311 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); 4312 4313 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, 4314 data, len); 4315 if (err < 0) 4316 goto failed; 4317 4318 err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, 4319 len, HCI_MGMT_LOCAL_NAME_EVENTS, sk); 4320 ext_info_changed(hdev, sk); 4321 4322 goto failed; 4323 } 4324 4325 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len); 4326 if (!cmd) 4327 err = -ENOMEM; 4328 else 4329 err = hci_cmd_sync_queue(hdev, set_name_sync, cmd, 4330 set_name_complete); 4331 4332 if (err < 0) { 4333 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 4334 MGMT_STATUS_FAILED); 4335 4336 if (cmd) 4337 mgmt_pending_remove(cmd); 4338 4339 goto failed; 4340 } 4341 4342 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); 4343 4344 failed: 4345 hci_dev_unlock(hdev); 4346 return err; 4347 } 4348 4349 static int appearance_changed_sync(struct hci_dev *hdev, void *data) 4350 { 4351 return adv_expire_sync(hdev, MGMT_ADV_FLAG_APPEARANCE); 4352 } 4353 4354 static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data, 4355 u16 len) 4356 { 4357 struct mgmt_cp_set_appearance *cp = data; 4358 u16 appearance; 4359 int err; 4360 4361 bt_dev_dbg(hdev, "sock %p", sk); 4362 4363 if (!lmp_le_capable(hdev)) 4364 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 4365 MGMT_STATUS_NOT_SUPPORTED); 4366 4367 appearance = le16_to_cpu(cp->appearance); 4368 4369 hci_dev_lock(hdev); 4370 4371 if (hdev->appearance != appearance) { 4372 hdev->appearance = appearance; 4373 4374 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) 4375 hci_cmd_sync_queue(hdev, appearance_changed_sync, NULL, 4376 NULL); 4377 4378 ext_info_changed(hdev, sk); 4379 } 4380 4381 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL, 4382 0); 4383 4384 hci_dev_unlock(hdev); 4385 4386 return err; 4387 } 4388 4389 static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev, 4390 void *data, u16 len) 4391 { 4392 struct mgmt_rp_get_phy_configuration rp; 4393 4394 bt_dev_dbg(hdev, "sock %p", sk); 4395 4396 hci_dev_lock(hdev); 4397 4398 memset(&rp, 0, sizeof(rp)); 4399 4400 rp.supported_phys = cpu_to_le32(get_supported_phys(hdev)); 4401 rp.selected_phys = cpu_to_le32(get_selected_phys(hdev)); 4402 rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev)); 4403 4404 hci_dev_unlock(hdev); 4405 4406 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0, 4407 &rp, sizeof(rp)); 4408 } 4409 4410 int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip) 4411 { 4412 struct mgmt_ev_phy_configuration_changed ev; 4413 4414 memset(&ev, 0, sizeof(ev)); 4415 4416 ev.selected_phys = cpu_to_le32(get_selected_phys(hdev)); 4417 4418 return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev, 4419 sizeof(ev), skip); 4420 } 4421 4422 static void set_default_phy_complete(struct hci_dev *hdev, void *data, int err) 4423 { 4424 struct mgmt_pending_cmd *cmd = data; 4425 struct sk_buff *skb; 4426 u8 status = mgmt_status(err); 4427 4428 skb = cmd->skb; 4429 4430 if (!status) { 4431 if (!skb) 4432 status = MGMT_STATUS_FAILED; 4433 else if (IS_ERR(skb)) 4434 status = mgmt_status(PTR_ERR(skb)); 4435 else 4436 status = mgmt_status(skb->data[0]); 4437 } 4438 4439 bt_dev_dbg(hdev, "status %d", status); 4440 4441 if (status) { 4442 mgmt_cmd_status(cmd->sk, hdev->id, 4443 MGMT_OP_SET_PHY_CONFIGURATION, status); 4444 } else { 4445 mgmt_cmd_complete(cmd->sk, hdev->id, 4446 MGMT_OP_SET_PHY_CONFIGURATION, 0, 4447 NULL, 0); 4448 4449 mgmt_phy_configuration_changed(hdev, cmd->sk); 4450 } 4451 4452 if (skb && !IS_ERR(skb)) 4453 kfree_skb(skb); 4454 4455 mgmt_pending_free(cmd); 4456 } 4457 4458 static int set_default_phy_sync(struct hci_dev *hdev, void *data) 4459 { 4460 struct mgmt_pending_cmd *cmd = data; 4461 struct mgmt_cp_set_phy_configuration *cp = cmd->param; 4462 struct hci_cp_le_set_default_phy cp_phy; 4463 u32 selected_phys; 4464 4465 selected_phys = __le32_to_cpu(cp->selected_phys); 4466 4467 memset(&cp_phy, 0, sizeof(cp_phy)); 4468 4469 if (!(selected_phys & MGMT_PHY_LE_TX_MASK)) 4470 cp_phy.all_phys |= 0x01; 4471 4472 if (!(selected_phys & MGMT_PHY_LE_RX_MASK)) 4473 cp_phy.all_phys |= 0x02; 4474 4475 if (selected_phys & MGMT_PHY_LE_1M_TX) 4476 cp_phy.tx_phys |= HCI_LE_SET_PHY_1M; 4477 4478 if (selected_phys & MGMT_PHY_LE_2M_TX) 4479 cp_phy.tx_phys |= HCI_LE_SET_PHY_2M; 4480 4481 if (selected_phys & MGMT_PHY_LE_CODED_TX) 4482 cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED; 4483 4484 if (selected_phys & MGMT_PHY_LE_1M_RX) 4485 cp_phy.rx_phys |= HCI_LE_SET_PHY_1M; 4486 4487 if (selected_phys & MGMT_PHY_LE_2M_RX) 4488 cp_phy.rx_phys |= HCI_LE_SET_PHY_2M; 4489 4490 if (selected_phys & MGMT_PHY_LE_CODED_RX) 4491 cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED; 4492 4493 cmd->skb = __hci_cmd_sync(hdev, HCI_OP_LE_SET_DEFAULT_PHY, 4494 sizeof(cp_phy), &cp_phy, HCI_CMD_TIMEOUT); 4495 4496 return 0; 4497 } 4498 4499 static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev, 4500 void *data, u16 len) 4501 { 4502 struct mgmt_cp_set_phy_configuration *cp = data; 4503 struct mgmt_pending_cmd *cmd; 4504 u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys; 4505 u16 pkt_type = (HCI_DH1 | HCI_DM1); 4506 bool changed = false; 4507 int err; 4508 4509 bt_dev_dbg(hdev, "sock %p", sk); 4510 4511 configurable_phys = get_configurable_phys(hdev); 4512 supported_phys = get_supported_phys(hdev); 4513 selected_phys = __le32_to_cpu(cp->selected_phys); 4514 4515 if (selected_phys & ~supported_phys) 4516 return mgmt_cmd_status(sk, hdev->id, 4517 MGMT_OP_SET_PHY_CONFIGURATION, 4518 MGMT_STATUS_INVALID_PARAMS); 4519 4520 unconfigure_phys = supported_phys & ~configurable_phys; 4521 4522 if ((selected_phys & unconfigure_phys) != unconfigure_phys) 4523 return mgmt_cmd_status(sk, hdev->id, 4524 MGMT_OP_SET_PHY_CONFIGURATION, 4525 MGMT_STATUS_INVALID_PARAMS); 4526 4527 if (selected_phys == get_selected_phys(hdev)) 4528 return mgmt_cmd_complete(sk, hdev->id, 4529 MGMT_OP_SET_PHY_CONFIGURATION, 4530 0, NULL, 0); 4531 4532 hci_dev_lock(hdev); 4533 4534 if (!hdev_is_powered(hdev)) { 4535 err = mgmt_cmd_status(sk, hdev->id, 4536 MGMT_OP_SET_PHY_CONFIGURATION, 4537 MGMT_STATUS_REJECTED); 4538 goto unlock; 4539 } 4540 4541 if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) { 4542 err = mgmt_cmd_status(sk, hdev->id, 4543 MGMT_OP_SET_PHY_CONFIGURATION, 4544 MGMT_STATUS_BUSY); 4545 goto unlock; 4546 } 4547 4548 if (selected_phys & MGMT_PHY_BR_1M_3SLOT) 4549 pkt_type |= (HCI_DH3 | HCI_DM3); 4550 else 4551 pkt_type &= ~(HCI_DH3 | HCI_DM3); 4552 4553 if (selected_phys & MGMT_PHY_BR_1M_5SLOT) 4554 pkt_type |= (HCI_DH5 | HCI_DM5); 4555 else 4556 pkt_type &= ~(HCI_DH5 | HCI_DM5); 4557 4558 if (selected_phys & MGMT_PHY_EDR_2M_1SLOT) 4559 pkt_type &= ~HCI_2DH1; 4560 else 4561 pkt_type |= HCI_2DH1; 4562 4563 if (selected_phys & MGMT_PHY_EDR_2M_3SLOT) 4564 pkt_type &= ~HCI_2DH3; 4565 else 4566 pkt_type |= HCI_2DH3; 4567 4568 if (selected_phys & MGMT_PHY_EDR_2M_5SLOT) 4569 pkt_type &= ~HCI_2DH5; 4570 else 4571 pkt_type |= HCI_2DH5; 4572 4573 if (selected_phys & MGMT_PHY_EDR_3M_1SLOT) 4574 pkt_type &= ~HCI_3DH1; 4575 else 4576 pkt_type |= HCI_3DH1; 4577 4578 if (selected_phys & MGMT_PHY_EDR_3M_3SLOT) 4579 pkt_type &= ~HCI_3DH3; 4580 else 4581 pkt_type |= HCI_3DH3; 4582 4583 if (selected_phys & MGMT_PHY_EDR_3M_5SLOT) 4584 pkt_type &= ~HCI_3DH5; 4585 else 4586 pkt_type |= HCI_3DH5; 4587 4588 if (pkt_type != hdev->pkt_type) { 4589 hdev->pkt_type = pkt_type; 4590 changed = true; 4591 } 4592 4593 if ((selected_phys & MGMT_PHY_LE_MASK) == 4594 (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) { 4595 if (changed) 4596 mgmt_phy_configuration_changed(hdev, sk); 4597 4598 err = mgmt_cmd_complete(sk, hdev->id, 4599 MGMT_OP_SET_PHY_CONFIGURATION, 4600 0, NULL, 0); 4601 4602 goto unlock; 4603 } 4604 4605 cmd = mgmt_pending_new(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data, 4606 len); 4607 if (!cmd) 4608 err = -ENOMEM; 4609 else 4610 err = hci_cmd_sync_queue(hdev, set_default_phy_sync, cmd, 4611 set_default_phy_complete); 4612 4613 if (err < 0) { 4614 err = mgmt_cmd_status(sk, hdev->id, 4615 MGMT_OP_SET_PHY_CONFIGURATION, 4616 MGMT_STATUS_FAILED); 4617 4618 if (cmd) 4619 mgmt_pending_remove(cmd); 4620 } 4621 4622 unlock: 4623 hci_dev_unlock(hdev); 4624 4625 return err; 4626 } 4627 4628 static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data, 4629 u16 len) 4630 { 4631 int err = MGMT_STATUS_SUCCESS; 4632 struct mgmt_cp_set_blocked_keys *keys = data; 4633 const u16 max_key_count = ((U16_MAX - sizeof(*keys)) / 4634 sizeof(struct mgmt_blocked_key_info)); 4635 u16 key_count, expected_len; 4636 int i; 4637 4638 bt_dev_dbg(hdev, "sock %p", sk); 4639 4640 key_count = __le16_to_cpu(keys->key_count); 4641 if (key_count > max_key_count) { 4642 bt_dev_err(hdev, "too big key_count value %u", key_count); 4643 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, 4644 MGMT_STATUS_INVALID_PARAMS); 4645 } 4646 4647 expected_len = struct_size(keys, keys, key_count); 4648 if (expected_len != len) { 4649 bt_dev_err(hdev, "expected %u bytes, got %u bytes", 4650 expected_len, len); 4651 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, 4652 MGMT_STATUS_INVALID_PARAMS); 4653 } 4654 4655 hci_dev_lock(hdev); 4656 4657 hci_blocked_keys_clear(hdev); 4658 4659 for (i = 0; i < key_count; ++i) { 4660 struct blocked_key *b = kzalloc_obj(*b); 4661 4662 if (!b) { 4663 err = MGMT_STATUS_NO_RESOURCES; 4664 break; 4665 } 4666 4667 b->type = keys->keys[i].type; 4668 memcpy(b->val, keys->keys[i].val, sizeof(b->val)); 4669 list_add_rcu(&b->list, &hdev->blocked_keys); 4670 } 4671 hci_dev_unlock(hdev); 4672 4673 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS, 4674 err, NULL, 0); 4675 } 4676 4677 static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev, 4678 void *data, u16 len) 4679 { 4680 struct mgmt_mode *cp = data; 4681 int err; 4682 bool changed = false; 4683 4684 bt_dev_dbg(hdev, "sock %p", sk); 4685 4686 if (!hci_test_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED)) 4687 return mgmt_cmd_status(sk, hdev->id, 4688 MGMT_OP_SET_WIDEBAND_SPEECH, 4689 MGMT_STATUS_NOT_SUPPORTED); 4690 4691 if (cp->val != 0x00 && cp->val != 0x01) 4692 return mgmt_cmd_status(sk, hdev->id, 4693 MGMT_OP_SET_WIDEBAND_SPEECH, 4694 MGMT_STATUS_INVALID_PARAMS); 4695 4696 hci_dev_lock(hdev); 4697 4698 if (hdev_is_powered(hdev) && 4699 !!cp->val != hci_dev_test_flag(hdev, 4700 HCI_WIDEBAND_SPEECH_ENABLED)) { 4701 err = mgmt_cmd_status(sk, hdev->id, 4702 MGMT_OP_SET_WIDEBAND_SPEECH, 4703 MGMT_STATUS_REJECTED); 4704 goto unlock; 4705 } 4706 4707 if (cp->val) 4708 changed = !hci_dev_test_and_set_flag(hdev, 4709 HCI_WIDEBAND_SPEECH_ENABLED); 4710 else 4711 changed = hci_dev_test_and_clear_flag(hdev, 4712 HCI_WIDEBAND_SPEECH_ENABLED); 4713 4714 err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev); 4715 if (err < 0) 4716 goto unlock; 4717 4718 if (changed) 4719 err = new_settings(hdev, sk); 4720 4721 unlock: 4722 hci_dev_unlock(hdev); 4723 return err; 4724 } 4725 4726 static int read_controller_cap(struct sock *sk, struct hci_dev *hdev, 4727 void *data, u16 data_len) 4728 { 4729 char buf[20]; 4730 struct mgmt_rp_read_controller_cap *rp = (void *)buf; 4731 u16 cap_len = 0; 4732 u8 flags = 0; 4733 u8 tx_power_range[2]; 4734 4735 bt_dev_dbg(hdev, "sock %p", sk); 4736 4737 memset(&buf, 0, sizeof(buf)); 4738 4739 hci_dev_lock(hdev); 4740 4741 /* When the Read Simple Pairing Options command is supported, then 4742 * the remote public key validation is supported. 4743 * 4744 * Alternatively, when Microsoft extensions are available, they can 4745 * indicate support for public key validation as well. 4746 */ 4747 if ((hdev->commands[41] & 0x08) || msft_curve_validity(hdev)) 4748 flags |= 0x01; /* Remote public key validation (BR/EDR) */ 4749 4750 flags |= 0x02; /* Remote public key validation (LE) */ 4751 4752 /* When the Read Encryption Key Size command is supported, then the 4753 * encryption key size is enforced. 4754 */ 4755 if (hdev->commands[20] & 0x10) 4756 flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */ 4757 4758 flags |= 0x08; /* Encryption key size enforcement (LE) */ 4759 4760 cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_SEC_FLAGS, 4761 &flags, 1); 4762 4763 /* When the Read Simple Pairing Options command is supported, then 4764 * also max encryption key size information is provided. 4765 */ 4766 if (hdev->commands[41] & 0x08) 4767 cap_len = eir_append_le16(rp->cap, cap_len, 4768 MGMT_CAP_MAX_ENC_KEY_SIZE, 4769 hdev->max_enc_key_size); 4770 4771 cap_len = eir_append_le16(rp->cap, cap_len, 4772 MGMT_CAP_SMP_MAX_ENC_KEY_SIZE, 4773 SMP_MAX_ENC_KEY_SIZE); 4774 4775 /* Append the min/max LE tx power parameters if we were able to fetch 4776 * it from the controller 4777 */ 4778 if (hdev->commands[38] & 0x80) { 4779 memcpy(&tx_power_range[0], &hdev->min_le_tx_power, 1); 4780 memcpy(&tx_power_range[1], &hdev->max_le_tx_power, 1); 4781 cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_LE_TX_PWR, 4782 tx_power_range, 2); 4783 } 4784 4785 rp->cap_len = cpu_to_le16(cap_len); 4786 4787 hci_dev_unlock(hdev); 4788 4789 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONTROLLER_CAP, 0, 4790 rp, sizeof(*rp) + cap_len); 4791 } 4792 4793 #ifdef CONFIG_BT_FEATURE_DEBUG 4794 /* d4992530-b9ec-469f-ab01-6c481c47da1c */ 4795 static const u8 debug_uuid[16] = { 4796 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab, 4797 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4, 4798 }; 4799 #endif 4800 4801 /* 330859bc-7506-492d-9370-9a6f0614037f */ 4802 static const u8 quality_report_uuid[16] = { 4803 0x7f, 0x03, 0x14, 0x06, 0x6f, 0x9a, 0x70, 0x93, 4804 0x2d, 0x49, 0x06, 0x75, 0xbc, 0x59, 0x08, 0x33, 4805 }; 4806 4807 /* a6695ace-ee7f-4fb9-881a-5fac66c629af */ 4808 static const u8 offload_codecs_uuid[16] = { 4809 0xaf, 0x29, 0xc6, 0x66, 0xac, 0x5f, 0x1a, 0x88, 4810 0xb9, 0x4f, 0x7f, 0xee, 0xce, 0x5a, 0x69, 0xa6, 4811 }; 4812 4813 /* 671b10b5-42c0-4696-9227-eb28d1b049d6 */ 4814 static const u8 le_simultaneous_roles_uuid[16] = { 4815 0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92, 4816 0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67, 4817 }; 4818 4819 /* 6fbaf188-05e0-496a-9885-d6ddfdb4e03e */ 4820 static const u8 iso_socket_uuid[16] = { 4821 0x3e, 0xe0, 0xb4, 0xfd, 0xdd, 0xd6, 0x85, 0x98, 4822 0x6a, 0x49, 0xe0, 0x05, 0x88, 0xf1, 0xba, 0x6f, 4823 }; 4824 4825 /* 2ce463d7-7a03-4d8d-bf05-5f24e8f36e76 */ 4826 static const u8 mgmt_mesh_uuid[16] = { 4827 0x76, 0x6e, 0xf3, 0xe8, 0x24, 0x5f, 0x05, 0xbf, 4828 0x8d, 0x4d, 0x03, 0x7a, 0xd7, 0x63, 0xe4, 0x2c, 4829 }; 4830 4831 static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev, 4832 void *data, u16 data_len) 4833 { 4834 struct mgmt_rp_read_exp_features_info *rp; 4835 size_t len; 4836 u16 idx = 0; 4837 u32 flags; 4838 int status; 4839 4840 bt_dev_dbg(hdev, "sock %p", sk); 4841 4842 /* Enough space for 7 features */ 4843 len = sizeof(*rp) + (sizeof(rp->features[0]) * 7); 4844 rp = kzalloc(len, GFP_KERNEL); 4845 if (!rp) 4846 return -ENOMEM; 4847 4848 #ifdef CONFIG_BT_FEATURE_DEBUG 4849 flags = bt_dbg_get() ? BIT(0) : 0; 4850 4851 memcpy(rp->features[idx].uuid, debug_uuid, 16); 4852 rp->features[idx].flags = cpu_to_le32(flags); 4853 idx++; 4854 #endif 4855 4856 if (hdev && hci_dev_le_state_simultaneous(hdev)) { 4857 if (hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)) 4858 flags = BIT(0); 4859 else 4860 flags = 0; 4861 4862 memcpy(rp->features[idx].uuid, le_simultaneous_roles_uuid, 16); 4863 rp->features[idx].flags = cpu_to_le32(flags); 4864 idx++; 4865 } 4866 4867 if (hdev && (aosp_has_quality_report(hdev) || 4868 hdev->set_quality_report)) { 4869 if (hci_dev_test_flag(hdev, HCI_QUALITY_REPORT)) 4870 flags = BIT(0); 4871 else 4872 flags = 0; 4873 4874 memcpy(rp->features[idx].uuid, quality_report_uuid, 16); 4875 rp->features[idx].flags = cpu_to_le32(flags); 4876 idx++; 4877 } 4878 4879 if (hdev && hdev->get_data_path_id) { 4880 if (hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)) 4881 flags = BIT(0); 4882 else 4883 flags = 0; 4884 4885 memcpy(rp->features[idx].uuid, offload_codecs_uuid, 16); 4886 rp->features[idx].flags = cpu_to_le32(flags); 4887 idx++; 4888 } 4889 4890 if (IS_ENABLED(CONFIG_BT_LE)) { 4891 flags = iso_inited() ? BIT(0) : 0; 4892 memcpy(rp->features[idx].uuid, iso_socket_uuid, 16); 4893 rp->features[idx].flags = cpu_to_le32(flags); 4894 idx++; 4895 } 4896 4897 if (hdev && lmp_le_capable(hdev)) { 4898 if (hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL)) 4899 flags = BIT(0); 4900 else 4901 flags = 0; 4902 4903 memcpy(rp->features[idx].uuid, mgmt_mesh_uuid, 16); 4904 rp->features[idx].flags = cpu_to_le32(flags); 4905 idx++; 4906 } 4907 4908 rp->feature_count = cpu_to_le16(idx); 4909 4910 /* After reading the experimental features information, enable 4911 * the events to update client on any future change. 4912 */ 4913 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); 4914 4915 status = mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, 4916 MGMT_OP_READ_EXP_FEATURES_INFO, 4917 0, rp, sizeof(*rp) + (20 * idx)); 4918 4919 kfree(rp); 4920 return status; 4921 } 4922 4923 static int exp_feature_changed(struct hci_dev *hdev, const u8 *uuid, 4924 bool enabled, struct sock *skip) 4925 { 4926 struct mgmt_ev_exp_feature_changed ev; 4927 4928 memset(&ev, 0, sizeof(ev)); 4929 memcpy(ev.uuid, uuid, 16); 4930 ev.flags = cpu_to_le32(enabled ? BIT(0) : 0); 4931 4932 return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev, 4933 &ev, sizeof(ev), 4934 HCI_MGMT_EXP_FEATURE_EVENTS, skip); 4935 } 4936 4937 #define EXP_FEAT(_uuid, _set_func) \ 4938 { \ 4939 .uuid = _uuid, \ 4940 .set_func = _set_func, \ 4941 } 4942 4943 /* The zero key uuid is special. Multiple exp features are set through it. */ 4944 static int set_zero_key_func(struct sock *sk, struct hci_dev *hdev, 4945 struct mgmt_cp_set_exp_feature *cp, u16 data_len) 4946 { 4947 struct mgmt_rp_set_exp_feature rp; 4948 4949 memset(rp.uuid, 0, 16); 4950 rp.flags = cpu_to_le32(0); 4951 4952 #ifdef CONFIG_BT_FEATURE_DEBUG 4953 if (!hdev) { 4954 bool changed = bt_dbg_get(); 4955 4956 bt_dbg_set(false); 4957 4958 if (changed) 4959 exp_feature_changed(NULL, ZERO_KEY, false, sk); 4960 } 4961 #endif 4962 4963 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); 4964 4965 return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE, 4966 MGMT_OP_SET_EXP_FEATURE, 0, 4967 &rp, sizeof(rp)); 4968 } 4969 4970 #ifdef CONFIG_BT_FEATURE_DEBUG 4971 static int set_debug_func(struct sock *sk, struct hci_dev *hdev, 4972 struct mgmt_cp_set_exp_feature *cp, u16 data_len) 4973 { 4974 struct mgmt_rp_set_exp_feature rp; 4975 4976 bool val, changed; 4977 int err; 4978 4979 /* Command requires to use the non-controller index */ 4980 if (hdev) 4981 return mgmt_cmd_status(sk, hdev->id, 4982 MGMT_OP_SET_EXP_FEATURE, 4983 MGMT_STATUS_INVALID_INDEX); 4984 4985 /* Parameters are limited to a single octet */ 4986 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) 4987 return mgmt_cmd_status(sk, MGMT_INDEX_NONE, 4988 MGMT_OP_SET_EXP_FEATURE, 4989 MGMT_STATUS_INVALID_PARAMS); 4990 4991 /* Only boolean on/off is supported */ 4992 if (cp->param[0] != 0x00 && cp->param[0] != 0x01) 4993 return mgmt_cmd_status(sk, MGMT_INDEX_NONE, 4994 MGMT_OP_SET_EXP_FEATURE, 4995 MGMT_STATUS_INVALID_PARAMS); 4996 4997 val = !!cp->param[0]; 4998 changed = val ? !bt_dbg_get() : bt_dbg_get(); 4999 bt_dbg_set(val); 5000 5001 memcpy(rp.uuid, debug_uuid, 16); 5002 rp.flags = cpu_to_le32(val ? BIT(0) : 0); 5003 5004 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); 5005 5006 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, 5007 MGMT_OP_SET_EXP_FEATURE, 0, 5008 &rp, sizeof(rp)); 5009 5010 if (changed) 5011 exp_feature_changed(hdev, debug_uuid, val, sk); 5012 5013 return err; 5014 } 5015 #endif 5016 5017 static int set_mgmt_mesh_func(struct sock *sk, struct hci_dev *hdev, 5018 struct mgmt_cp_set_exp_feature *cp, u16 data_len) 5019 { 5020 struct mgmt_rp_set_exp_feature rp; 5021 bool val, changed; 5022 int err; 5023 5024 /* Command requires to use the controller index */ 5025 if (!hdev) 5026 return mgmt_cmd_status(sk, MGMT_INDEX_NONE, 5027 MGMT_OP_SET_EXP_FEATURE, 5028 MGMT_STATUS_INVALID_INDEX); 5029 5030 /* Parameters are limited to a single octet */ 5031 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) 5032 return mgmt_cmd_status(sk, hdev->id, 5033 MGMT_OP_SET_EXP_FEATURE, 5034 MGMT_STATUS_INVALID_PARAMS); 5035 5036 /* Only boolean on/off is supported */ 5037 if (cp->param[0] != 0x00 && cp->param[0] != 0x01) 5038 return mgmt_cmd_status(sk, hdev->id, 5039 MGMT_OP_SET_EXP_FEATURE, 5040 MGMT_STATUS_INVALID_PARAMS); 5041 5042 val = !!cp->param[0]; 5043 5044 if (val) { 5045 changed = !hci_dev_test_and_set_flag(hdev, 5046 HCI_MESH_EXPERIMENTAL); 5047 } else { 5048 hci_dev_clear_flag(hdev, HCI_MESH); 5049 changed = hci_dev_test_and_clear_flag(hdev, 5050 HCI_MESH_EXPERIMENTAL); 5051 } 5052 5053 memcpy(rp.uuid, mgmt_mesh_uuid, 16); 5054 rp.flags = cpu_to_le32(val ? BIT(0) : 0); 5055 5056 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); 5057 5058 err = mgmt_cmd_complete(sk, hdev->id, 5059 MGMT_OP_SET_EXP_FEATURE, 0, 5060 &rp, sizeof(rp)); 5061 5062 if (changed) 5063 exp_feature_changed(hdev, mgmt_mesh_uuid, val, sk); 5064 5065 return err; 5066 } 5067 5068 static int set_quality_report_func(struct sock *sk, struct hci_dev *hdev, 5069 struct mgmt_cp_set_exp_feature *cp, 5070 u16 data_len) 5071 { 5072 struct mgmt_rp_set_exp_feature rp; 5073 bool val, changed; 5074 int err; 5075 5076 /* Command requires to use a valid controller index */ 5077 if (!hdev) 5078 return mgmt_cmd_status(sk, MGMT_INDEX_NONE, 5079 MGMT_OP_SET_EXP_FEATURE, 5080 MGMT_STATUS_INVALID_INDEX); 5081 5082 /* Parameters are limited to a single octet */ 5083 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) 5084 return mgmt_cmd_status(sk, hdev->id, 5085 MGMT_OP_SET_EXP_FEATURE, 5086 MGMT_STATUS_INVALID_PARAMS); 5087 5088 /* Only boolean on/off is supported */ 5089 if (cp->param[0] != 0x00 && cp->param[0] != 0x01) 5090 return mgmt_cmd_status(sk, hdev->id, 5091 MGMT_OP_SET_EXP_FEATURE, 5092 MGMT_STATUS_INVALID_PARAMS); 5093 5094 hci_req_sync_lock(hdev); 5095 5096 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) { 5097 err = mgmt_cmd_status(sk, hdev->id, 5098 MGMT_OP_SET_EXP_FEATURE, 5099 MGMT_STATUS_INVALID_INDEX); 5100 goto unlock_quality_report; 5101 } 5102 5103 val = !!cp->param[0]; 5104 changed = (val != hci_dev_test_flag(hdev, HCI_QUALITY_REPORT)); 5105 5106 if (!aosp_has_quality_report(hdev) && !hdev->set_quality_report) { 5107 err = mgmt_cmd_status(sk, hdev->id, 5108 MGMT_OP_SET_EXP_FEATURE, 5109 MGMT_STATUS_NOT_SUPPORTED); 5110 goto unlock_quality_report; 5111 } 5112 5113 if (changed) { 5114 if (hdev->set_quality_report) 5115 err = hdev->set_quality_report(hdev, val); 5116 else 5117 err = aosp_set_quality_report(hdev, val); 5118 5119 if (err) { 5120 err = mgmt_cmd_status(sk, hdev->id, 5121 MGMT_OP_SET_EXP_FEATURE, 5122 MGMT_STATUS_FAILED); 5123 goto unlock_quality_report; 5124 } 5125 5126 if (val) 5127 hci_dev_set_flag(hdev, HCI_QUALITY_REPORT); 5128 else 5129 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT); 5130 } 5131 5132 bt_dev_dbg(hdev, "quality report enable %d changed %d", val, changed); 5133 5134 memcpy(rp.uuid, quality_report_uuid, 16); 5135 rp.flags = cpu_to_le32(val ? BIT(0) : 0); 5136 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); 5137 5138 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_EXP_FEATURE, 0, 5139 &rp, sizeof(rp)); 5140 5141 if (changed) 5142 exp_feature_changed(hdev, quality_report_uuid, val, sk); 5143 5144 unlock_quality_report: 5145 hci_req_sync_unlock(hdev); 5146 return err; 5147 } 5148 5149 static int set_offload_codec_func(struct sock *sk, struct hci_dev *hdev, 5150 struct mgmt_cp_set_exp_feature *cp, 5151 u16 data_len) 5152 { 5153 bool val, changed; 5154 int err; 5155 struct mgmt_rp_set_exp_feature rp; 5156 5157 /* Command requires to use a valid controller index */ 5158 if (!hdev) 5159 return mgmt_cmd_status(sk, MGMT_INDEX_NONE, 5160 MGMT_OP_SET_EXP_FEATURE, 5161 MGMT_STATUS_INVALID_INDEX); 5162 5163 /* Parameters are limited to a single octet */ 5164 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) 5165 return mgmt_cmd_status(sk, hdev->id, 5166 MGMT_OP_SET_EXP_FEATURE, 5167 MGMT_STATUS_INVALID_PARAMS); 5168 5169 /* Only boolean on/off is supported */ 5170 if (cp->param[0] != 0x00 && cp->param[0] != 0x01) 5171 return mgmt_cmd_status(sk, hdev->id, 5172 MGMT_OP_SET_EXP_FEATURE, 5173 MGMT_STATUS_INVALID_PARAMS); 5174 5175 val = !!cp->param[0]; 5176 changed = (val != hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED)); 5177 5178 if (!hdev->get_data_path_id) { 5179 return mgmt_cmd_status(sk, hdev->id, 5180 MGMT_OP_SET_EXP_FEATURE, 5181 MGMT_STATUS_NOT_SUPPORTED); 5182 } 5183 5184 if (changed) { 5185 if (val) 5186 hci_dev_set_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED); 5187 else 5188 hci_dev_clear_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED); 5189 } 5190 5191 bt_dev_info(hdev, "offload codecs enable %d changed %d", 5192 val, changed); 5193 5194 memcpy(rp.uuid, offload_codecs_uuid, 16); 5195 rp.flags = cpu_to_le32(val ? BIT(0) : 0); 5196 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); 5197 err = mgmt_cmd_complete(sk, hdev->id, 5198 MGMT_OP_SET_EXP_FEATURE, 0, 5199 &rp, sizeof(rp)); 5200 5201 if (changed) 5202 exp_feature_changed(hdev, offload_codecs_uuid, val, sk); 5203 5204 return err; 5205 } 5206 5207 static int set_le_simultaneous_roles_func(struct sock *sk, struct hci_dev *hdev, 5208 struct mgmt_cp_set_exp_feature *cp, 5209 u16 data_len) 5210 { 5211 bool val, changed; 5212 int err; 5213 struct mgmt_rp_set_exp_feature rp; 5214 5215 /* Command requires to use a valid controller index */ 5216 if (!hdev) 5217 return mgmt_cmd_status(sk, MGMT_INDEX_NONE, 5218 MGMT_OP_SET_EXP_FEATURE, 5219 MGMT_STATUS_INVALID_INDEX); 5220 5221 /* Parameters are limited to a single octet */ 5222 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) 5223 return mgmt_cmd_status(sk, hdev->id, 5224 MGMT_OP_SET_EXP_FEATURE, 5225 MGMT_STATUS_INVALID_PARAMS); 5226 5227 /* Only boolean on/off is supported */ 5228 if (cp->param[0] != 0x00 && cp->param[0] != 0x01) 5229 return mgmt_cmd_status(sk, hdev->id, 5230 MGMT_OP_SET_EXP_FEATURE, 5231 MGMT_STATUS_INVALID_PARAMS); 5232 5233 val = !!cp->param[0]; 5234 changed = (val != hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES)); 5235 5236 if (!hci_dev_le_state_simultaneous(hdev)) { 5237 return mgmt_cmd_status(sk, hdev->id, 5238 MGMT_OP_SET_EXP_FEATURE, 5239 MGMT_STATUS_NOT_SUPPORTED); 5240 } 5241 5242 if (changed) { 5243 if (val) 5244 hci_dev_set_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES); 5245 else 5246 hci_dev_clear_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES); 5247 } 5248 5249 bt_dev_info(hdev, "LE simultaneous roles enable %d changed %d", 5250 val, changed); 5251 5252 memcpy(rp.uuid, le_simultaneous_roles_uuid, 16); 5253 rp.flags = cpu_to_le32(val ? BIT(0) : 0); 5254 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); 5255 err = mgmt_cmd_complete(sk, hdev->id, 5256 MGMT_OP_SET_EXP_FEATURE, 0, 5257 &rp, sizeof(rp)); 5258 5259 if (changed) 5260 exp_feature_changed(hdev, le_simultaneous_roles_uuid, val, sk); 5261 5262 return err; 5263 } 5264 5265 #ifdef CONFIG_BT_LE 5266 static int set_iso_socket_func(struct sock *sk, struct hci_dev *hdev, 5267 struct mgmt_cp_set_exp_feature *cp, u16 data_len) 5268 { 5269 struct mgmt_rp_set_exp_feature rp; 5270 bool val, changed = false; 5271 int err; 5272 5273 /* Command requires to use the non-controller index */ 5274 if (hdev) 5275 return mgmt_cmd_status(sk, hdev->id, 5276 MGMT_OP_SET_EXP_FEATURE, 5277 MGMT_STATUS_INVALID_INDEX); 5278 5279 /* Parameters are limited to a single octet */ 5280 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1) 5281 return mgmt_cmd_status(sk, MGMT_INDEX_NONE, 5282 MGMT_OP_SET_EXP_FEATURE, 5283 MGMT_STATUS_INVALID_PARAMS); 5284 5285 /* Only boolean on/off is supported */ 5286 if (cp->param[0] != 0x00 && cp->param[0] != 0x01) 5287 return mgmt_cmd_status(sk, MGMT_INDEX_NONE, 5288 MGMT_OP_SET_EXP_FEATURE, 5289 MGMT_STATUS_INVALID_PARAMS); 5290 5291 val = cp->param[0] ? true : false; 5292 if (val) 5293 err = iso_init(); 5294 else 5295 err = iso_exit(); 5296 5297 if (!err) 5298 changed = true; 5299 5300 memcpy(rp.uuid, iso_socket_uuid, 16); 5301 rp.flags = cpu_to_le32(val ? BIT(0) : 0); 5302 5303 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS); 5304 5305 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, 5306 MGMT_OP_SET_EXP_FEATURE, 0, 5307 &rp, sizeof(rp)); 5308 5309 if (changed) 5310 exp_feature_changed(hdev, iso_socket_uuid, val, sk); 5311 5312 return err; 5313 } 5314 #endif 5315 5316 static const struct mgmt_exp_feature { 5317 const u8 *uuid; 5318 int (*set_func)(struct sock *sk, struct hci_dev *hdev, 5319 struct mgmt_cp_set_exp_feature *cp, u16 data_len); 5320 } exp_features[] = { 5321 EXP_FEAT(ZERO_KEY, set_zero_key_func), 5322 #ifdef CONFIG_BT_FEATURE_DEBUG 5323 EXP_FEAT(debug_uuid, set_debug_func), 5324 #endif 5325 EXP_FEAT(mgmt_mesh_uuid, set_mgmt_mesh_func), 5326 EXP_FEAT(quality_report_uuid, set_quality_report_func), 5327 EXP_FEAT(offload_codecs_uuid, set_offload_codec_func), 5328 EXP_FEAT(le_simultaneous_roles_uuid, set_le_simultaneous_roles_func), 5329 #ifdef CONFIG_BT_LE 5330 EXP_FEAT(iso_socket_uuid, set_iso_socket_func), 5331 #endif 5332 5333 /* end with a null feature */ 5334 EXP_FEAT(NULL, NULL) 5335 }; 5336 5337 static int set_exp_feature(struct sock *sk, struct hci_dev *hdev, 5338 void *data, u16 data_len) 5339 { 5340 struct mgmt_cp_set_exp_feature *cp = data; 5341 size_t i = 0; 5342 5343 bt_dev_dbg(hdev, "sock %p", sk); 5344 5345 for (i = 0; exp_features[i].uuid; i++) { 5346 if (!memcmp(cp->uuid, exp_features[i].uuid, 16)) 5347 return exp_features[i].set_func(sk, hdev, cp, data_len); 5348 } 5349 5350 return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE, 5351 MGMT_OP_SET_EXP_FEATURE, 5352 MGMT_STATUS_NOT_SUPPORTED); 5353 } 5354 5355 static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, 5356 u16 data_len) 5357 { 5358 struct mgmt_cp_get_device_flags *cp = data; 5359 struct mgmt_rp_get_device_flags rp; 5360 struct bdaddr_list_with_flags *br_params; 5361 struct hci_conn_params *params; 5362 u32 supported_flags; 5363 u32 current_flags = 0; 5364 u8 status = MGMT_STATUS_INVALID_PARAMS; 5365 5366 bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n", 5367 &cp->addr.bdaddr, cp->addr.type); 5368 5369 hci_dev_lock(hdev); 5370 5371 supported_flags = hdev->conn_flags; 5372 5373 memset(&rp, 0, sizeof(rp)); 5374 5375 if (cp->addr.type == BDADDR_BREDR) { 5376 br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, 5377 &cp->addr.bdaddr, 5378 cp->addr.type); 5379 if (!br_params) 5380 goto done; 5381 5382 current_flags = br_params->flags; 5383 } else { 5384 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, 5385 le_addr_type(cp->addr.type)); 5386 if (!params) 5387 goto done; 5388 5389 current_flags = params->flags; 5390 } 5391 5392 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); 5393 rp.addr.type = cp->addr.type; 5394 rp.supported_flags = cpu_to_le32(supported_flags); 5395 rp.current_flags = cpu_to_le32(current_flags); 5396 5397 status = MGMT_STATUS_SUCCESS; 5398 5399 done: 5400 hci_dev_unlock(hdev); 5401 5402 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status, 5403 &rp, sizeof(rp)); 5404 } 5405 5406 static void device_flags_changed(struct sock *sk, struct hci_dev *hdev, 5407 bdaddr_t *bdaddr, u8 bdaddr_type, 5408 u32 supported_flags, u32 current_flags) 5409 { 5410 struct mgmt_ev_device_flags_changed ev; 5411 5412 bacpy(&ev.addr.bdaddr, bdaddr); 5413 ev.addr.type = bdaddr_type; 5414 ev.supported_flags = cpu_to_le32(supported_flags); 5415 ev.current_flags = cpu_to_le32(current_flags); 5416 5417 mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk); 5418 } 5419 5420 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) 5421 { 5422 struct hci_conn *conn; 5423 5424 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); 5425 if (!conn) 5426 return false; 5427 5428 if (conn->dst_type != type) 5429 return false; 5430 5431 if (conn->state != BT_CONNECTED) 5432 return false; 5433 5434 return true; 5435 } 5436 5437 /* This function requires the caller holds hdev->lock */ 5438 static struct hci_conn_params *hci_conn_params_set(struct hci_dev *hdev, 5439 bdaddr_t *addr, u8 addr_type, 5440 u8 auto_connect) 5441 { 5442 struct hci_conn_params *params; 5443 5444 params = hci_conn_params_add(hdev, addr, addr_type); 5445 if (!params) 5446 return NULL; 5447 5448 if (params->auto_connect == auto_connect) 5449 return params; 5450 5451 hci_pend_le_list_del_init(params); 5452 5453 switch (auto_connect) { 5454 case HCI_AUTO_CONN_DISABLED: 5455 case HCI_AUTO_CONN_LINK_LOSS: 5456 /* If auto connect is being disabled when we're trying to 5457 * connect to device, keep connecting. 5458 */ 5459 if (params->explicit_connect) 5460 hci_pend_le_list_add(params, &hdev->pend_le_conns); 5461 break; 5462 case HCI_AUTO_CONN_REPORT: 5463 if (params->explicit_connect) 5464 hci_pend_le_list_add(params, &hdev->pend_le_conns); 5465 else 5466 hci_pend_le_list_add(params, &hdev->pend_le_reports); 5467 break; 5468 case HCI_AUTO_CONN_DIRECT: 5469 case HCI_AUTO_CONN_ALWAYS: 5470 if (!is_connected(hdev, addr, addr_type)) 5471 hci_pend_le_list_add(params, &hdev->pend_le_conns); 5472 break; 5473 } 5474 5475 params->auto_connect = auto_connect; 5476 5477 bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u", 5478 addr, addr_type, auto_connect); 5479 5480 return params; 5481 } 5482 5483 static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data, 5484 u16 len) 5485 { 5486 struct mgmt_cp_set_device_flags *cp = data; 5487 struct bdaddr_list_with_flags *br_params; 5488 struct hci_conn_params *params; 5489 u8 status = MGMT_STATUS_INVALID_PARAMS; 5490 u32 supported_flags; 5491 u32 current_flags = __le32_to_cpu(cp->current_flags); 5492 5493 bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x", 5494 &cp->addr.bdaddr, cp->addr.type, current_flags); 5495 5496 // We should take hci_dev_lock() early, I think.. conn_flags can change 5497 supported_flags = hdev->conn_flags; 5498 5499 if ((supported_flags | current_flags) != supported_flags) { 5500 bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)", 5501 current_flags, supported_flags); 5502 goto done; 5503 } 5504 5505 hci_dev_lock(hdev); 5506 5507 if (cp->addr.type == BDADDR_BREDR) { 5508 br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, 5509 &cp->addr.bdaddr, 5510 cp->addr.type); 5511 5512 if (br_params) { 5513 br_params->flags = current_flags; 5514 status = MGMT_STATUS_SUCCESS; 5515 } else { 5516 bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)", 5517 &cp->addr.bdaddr, cp->addr.type); 5518 } 5519 5520 goto unlock; 5521 } 5522 5523 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, 5524 le_addr_type(cp->addr.type)); 5525 if (!params) { 5526 /* Create a new hci_conn_params if it doesn't exist */ 5527 params = hci_conn_params_set(hdev, &cp->addr.bdaddr, 5528 le_addr_type(cp->addr.type), 5529 HCI_AUTO_CONN_DISABLED); 5530 if (!params) { 5531 bt_dev_warn(hdev, "No such LE device %pMR (0x%x)", 5532 &cp->addr.bdaddr, 5533 le_addr_type(cp->addr.type)); 5534 goto unlock; 5535 } 5536 } 5537 5538 supported_flags = hdev->conn_flags; 5539 5540 if ((supported_flags | current_flags) != supported_flags) { 5541 bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)", 5542 current_flags, supported_flags); 5543 goto unlock; 5544 } 5545 5546 WRITE_ONCE(params->flags, current_flags); 5547 status = MGMT_STATUS_SUCCESS; 5548 5549 /* Update passive scan if HCI_CONN_FLAG_DEVICE_PRIVACY 5550 * has been set. 5551 */ 5552 if (params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY) 5553 hci_update_passive_scan(hdev); 5554 5555 unlock: 5556 hci_dev_unlock(hdev); 5557 5558 done: 5559 if (status == MGMT_STATUS_SUCCESS) 5560 device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type, 5561 supported_flags, current_flags); 5562 5563 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status, 5564 &cp->addr, sizeof(cp->addr)); 5565 } 5566 5567 static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev, 5568 u16 handle) 5569 { 5570 struct mgmt_ev_adv_monitor_added ev; 5571 5572 ev.monitor_handle = cpu_to_le16(handle); 5573 5574 mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk); 5575 } 5576 5577 static void mgmt_adv_monitor_removed(struct sock *sk, struct hci_dev *hdev, 5578 __le16 handle) 5579 { 5580 struct mgmt_ev_adv_monitor_removed ev; 5581 5582 ev.monitor_handle = handle; 5583 5584 mgmt_event(MGMT_EV_ADV_MONITOR_REMOVED, hdev, &ev, sizeof(ev), sk); 5585 } 5586 5587 static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev, 5588 void *data, u16 len) 5589 { 5590 struct adv_monitor *monitor = NULL; 5591 struct mgmt_rp_read_adv_monitor_features *rp = NULL; 5592 int handle, err; 5593 size_t rp_size = 0; 5594 __u32 supported = 0; 5595 __u32 enabled = 0; 5596 __u16 num_handles = 0; 5597 __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES]; 5598 5599 BT_DBG("request for %s", hdev->name); 5600 5601 hci_dev_lock(hdev); 5602 5603 if (msft_monitor_supported(hdev)) 5604 supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS; 5605 5606 idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle) 5607 handles[num_handles++] = monitor->handle; 5608 5609 hci_dev_unlock(hdev); 5610 5611 rp_size = sizeof(*rp) + (num_handles * sizeof(u16)); 5612 rp = kmalloc(rp_size, GFP_KERNEL); 5613 if (!rp) 5614 return -ENOMEM; 5615 5616 /* All supported features are currently enabled */ 5617 enabled = supported; 5618 5619 rp->supported_features = cpu_to_le32(supported); 5620 rp->enabled_features = cpu_to_le32(enabled); 5621 rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES); 5622 rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS; 5623 rp->num_handles = cpu_to_le16(num_handles); 5624 if (num_handles) 5625 memcpy(&rp->handles, &handles, (num_handles * sizeof(u16))); 5626 5627 err = mgmt_cmd_complete(sk, hdev->id, 5628 MGMT_OP_READ_ADV_MONITOR_FEATURES, 5629 MGMT_STATUS_SUCCESS, rp, rp_size); 5630 5631 kfree(rp); 5632 5633 return err; 5634 } 5635 5636 static void mgmt_add_adv_patterns_monitor_complete(struct hci_dev *hdev, 5637 void *data, int status) 5638 { 5639 struct mgmt_rp_add_adv_patterns_monitor rp; 5640 struct mgmt_pending_cmd *cmd = data; 5641 struct adv_monitor *monitor; 5642 5643 /* This is likely the result of hdev being closed and mgmt_index_removed 5644 * is attempting to clean up any pending command so 5645 * hci_adv_monitors_clear is about to be called which will take care of 5646 * freeing the adv_monitor instances. 5647 */ 5648 if (status == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) 5649 return; 5650 5651 monitor = cmd->user_data; 5652 5653 hci_dev_lock(hdev); 5654 5655 rp.monitor_handle = cpu_to_le16(monitor->handle); 5656 5657 if (!status) { 5658 mgmt_adv_monitor_added(cmd->sk, hdev, monitor->handle); 5659 hdev->adv_monitors_cnt++; 5660 if (monitor->state == ADV_MONITOR_STATE_NOT_REGISTERED) 5661 monitor->state = ADV_MONITOR_STATE_REGISTERED; 5662 hci_update_passive_scan(hdev); 5663 } else { 5664 hci_free_adv_monitor(hdev, monitor); 5665 } 5666 5667 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, 5668 mgmt_status(status), &rp, sizeof(rp)); 5669 mgmt_pending_free(cmd); 5670 5671 hci_dev_unlock(hdev); 5672 bt_dev_dbg(hdev, "add monitor %d complete, status %d", 5673 rp.monitor_handle, status); 5674 } 5675 5676 static int mgmt_add_adv_patterns_monitor_sync(struct hci_dev *hdev, void *data) 5677 { 5678 struct mgmt_pending_cmd *cmd = data; 5679 struct adv_monitor *mon; 5680 5681 mutex_lock(&hdev->mgmt_pending_lock); 5682 5683 if (!__mgmt_pending_listed(hdev, cmd)) { 5684 mutex_unlock(&hdev->mgmt_pending_lock); 5685 return -ECANCELED; 5686 } 5687 5688 mon = cmd->user_data; 5689 5690 mutex_unlock(&hdev->mgmt_pending_lock); 5691 5692 return hci_add_adv_monitor(hdev, mon); 5693 } 5694 5695 static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, 5696 struct adv_monitor *m, u8 status, 5697 void *data, u16 len, u16 op) 5698 { 5699 struct mgmt_pending_cmd *cmd; 5700 int err; 5701 5702 hci_dev_lock(hdev); 5703 5704 if (status) 5705 goto unlock; 5706 5707 if (pending_find(MGMT_OP_SET_LE, hdev) || 5708 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || 5709 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) { 5710 status = MGMT_STATUS_BUSY; 5711 goto unlock; 5712 } 5713 5714 cmd = mgmt_pending_add(sk, op, hdev, data, len); 5715 if (!cmd) { 5716 status = MGMT_STATUS_NO_RESOURCES; 5717 goto unlock; 5718 } 5719 5720 cmd->user_data = m; 5721 err = hci_cmd_sync_queue(hdev, mgmt_add_adv_patterns_monitor_sync, cmd, 5722 mgmt_add_adv_patterns_monitor_complete); 5723 if (err) { 5724 if (err == -ENOMEM) 5725 status = MGMT_STATUS_NO_RESOURCES; 5726 else 5727 status = MGMT_STATUS_FAILED; 5728 5729 goto unlock; 5730 } 5731 5732 hci_dev_unlock(hdev); 5733 5734 return 0; 5735 5736 unlock: 5737 hci_free_adv_monitor(hdev, m); 5738 hci_dev_unlock(hdev); 5739 return mgmt_cmd_status(sk, hdev->id, op, status); 5740 } 5741 5742 static void parse_adv_monitor_rssi(struct adv_monitor *m, 5743 struct mgmt_adv_rssi_thresholds *rssi) 5744 { 5745 if (rssi) { 5746 m->rssi.low_threshold = rssi->low_threshold; 5747 m->rssi.low_threshold_timeout = 5748 __le16_to_cpu(rssi->low_threshold_timeout); 5749 m->rssi.high_threshold = rssi->high_threshold; 5750 m->rssi.high_threshold_timeout = 5751 __le16_to_cpu(rssi->high_threshold_timeout); 5752 m->rssi.sampling_period = rssi->sampling_period; 5753 } else { 5754 /* Default values. These numbers are the least constricting 5755 * parameters for MSFT API to work, so it behaves as if there 5756 * are no rssi parameter to consider. May need to be changed 5757 * if other API are to be supported. 5758 */ 5759 m->rssi.low_threshold = -127; 5760 m->rssi.low_threshold_timeout = 60; 5761 m->rssi.high_threshold = -127; 5762 m->rssi.high_threshold_timeout = 0; 5763 m->rssi.sampling_period = 0; 5764 } 5765 } 5766 5767 static u8 parse_adv_monitor_pattern(struct adv_monitor *m, u8 pattern_count, 5768 struct mgmt_adv_pattern *patterns) 5769 { 5770 u8 offset = 0, length = 0; 5771 struct adv_pattern *p = NULL; 5772 int i; 5773 5774 for (i = 0; i < pattern_count; i++) { 5775 offset = patterns[i].offset; 5776 length = patterns[i].length; 5777 if (offset >= HCI_MAX_AD_LENGTH || 5778 length > HCI_MAX_AD_LENGTH || 5779 (offset + length) > HCI_MAX_AD_LENGTH) 5780 return MGMT_STATUS_INVALID_PARAMS; 5781 5782 p = kmalloc_obj(*p); 5783 if (!p) 5784 return MGMT_STATUS_NO_RESOURCES; 5785 5786 p->ad_type = patterns[i].ad_type; 5787 p->offset = patterns[i].offset; 5788 p->length = patterns[i].length; 5789 memcpy(p->value, patterns[i].value, p->length); 5790 5791 INIT_LIST_HEAD(&p->list); 5792 list_add(&p->list, &m->patterns); 5793 } 5794 5795 return MGMT_STATUS_SUCCESS; 5796 } 5797 5798 static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev, 5799 void *data, u16 len) 5800 { 5801 struct mgmt_cp_add_adv_patterns_monitor *cp = data; 5802 struct adv_monitor *m = NULL; 5803 u8 status = MGMT_STATUS_SUCCESS; 5804 size_t expected_size = sizeof(*cp); 5805 5806 BT_DBG("request for %s", hdev->name); 5807 5808 if (len <= sizeof(*cp)) { 5809 status = MGMT_STATUS_INVALID_PARAMS; 5810 goto done; 5811 } 5812 5813 expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); 5814 if (len != expected_size) { 5815 status = MGMT_STATUS_INVALID_PARAMS; 5816 goto done; 5817 } 5818 5819 m = kzalloc_obj(*m); 5820 if (!m) { 5821 status = MGMT_STATUS_NO_RESOURCES; 5822 goto done; 5823 } 5824 5825 INIT_LIST_HEAD(&m->patterns); 5826 5827 parse_adv_monitor_rssi(m, NULL); 5828 status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); 5829 5830 done: 5831 return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, 5832 MGMT_OP_ADD_ADV_PATTERNS_MONITOR); 5833 } 5834 5835 static int add_adv_patterns_monitor_rssi(struct sock *sk, struct hci_dev *hdev, 5836 void *data, u16 len) 5837 { 5838 struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = data; 5839 struct adv_monitor *m = NULL; 5840 u8 status = MGMT_STATUS_SUCCESS; 5841 size_t expected_size = sizeof(*cp); 5842 5843 BT_DBG("request for %s", hdev->name); 5844 5845 if (len <= sizeof(*cp)) { 5846 status = MGMT_STATUS_INVALID_PARAMS; 5847 goto done; 5848 } 5849 5850 expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern); 5851 if (len != expected_size) { 5852 status = MGMT_STATUS_INVALID_PARAMS; 5853 goto done; 5854 } 5855 5856 m = kzalloc_obj(*m); 5857 if (!m) { 5858 status = MGMT_STATUS_NO_RESOURCES; 5859 goto done; 5860 } 5861 5862 INIT_LIST_HEAD(&m->patterns); 5863 5864 parse_adv_monitor_rssi(m, &cp->rssi); 5865 status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns); 5866 5867 done: 5868 return __add_adv_patterns_monitor(sk, hdev, m, status, data, len, 5869 MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI); 5870 } 5871 5872 static void mgmt_remove_adv_monitor_complete(struct hci_dev *hdev, 5873 void *data, int status) 5874 { 5875 struct mgmt_rp_remove_adv_monitor rp; 5876 struct mgmt_pending_cmd *cmd = data; 5877 struct mgmt_cp_remove_adv_monitor *cp; 5878 5879 /* Reply to a cancelled command so bluetoothd's serialised mgmt queue 5880 * is not blocked. 5881 */ 5882 if (status == -ECANCELED) { 5883 cp = cmd->param; 5884 rp.monitor_handle = cp->monitor_handle; 5885 5886 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, 5887 mgmt_status(status), &rp, sizeof(rp)); 5888 mgmt_pending_free(cmd); 5889 return; 5890 } 5891 5892 hci_dev_lock(hdev); 5893 5894 cp = cmd->param; 5895 5896 rp.monitor_handle = cp->monitor_handle; 5897 5898 if (!status) { 5899 mgmt_adv_monitor_removed(cmd->sk, hdev, cp->monitor_handle); 5900 hci_update_passive_scan(hdev); 5901 } 5902 5903 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, 5904 mgmt_status(status), &rp, sizeof(rp)); 5905 mgmt_pending_free(cmd); 5906 5907 hci_dev_unlock(hdev); 5908 bt_dev_dbg(hdev, "remove monitor %d complete, status %d", 5909 rp.monitor_handle, status); 5910 } 5911 5912 static int mgmt_remove_adv_monitor_sync(struct hci_dev *hdev, void *data) 5913 { 5914 struct mgmt_pending_cmd *cmd = data; 5915 struct mgmt_cp_remove_adv_monitor *cp = cmd->param; 5916 u16 handle = __le16_to_cpu(cp->monitor_handle); 5917 5918 if (!handle) 5919 return hci_remove_all_adv_monitor(hdev); 5920 5921 return hci_remove_single_adv_monitor(hdev, handle); 5922 } 5923 5924 static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev, 5925 void *data, u16 len) 5926 { 5927 struct mgmt_pending_cmd *cmd; 5928 int err, status; 5929 5930 hci_dev_lock(hdev); 5931 5932 if (pending_find(MGMT_OP_SET_LE, hdev) || 5933 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) || 5934 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) { 5935 status = MGMT_STATUS_BUSY; 5936 goto unlock; 5937 } 5938 5939 cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_ADV_MONITOR, hdev, data, len); 5940 if (!cmd) { 5941 status = MGMT_STATUS_NO_RESOURCES; 5942 goto unlock; 5943 } 5944 5945 err = hci_cmd_sync_submit(hdev, mgmt_remove_adv_monitor_sync, cmd, 5946 mgmt_remove_adv_monitor_complete); 5947 5948 if (err) { 5949 mgmt_pending_free(cmd); 5950 5951 if (err == -ENOMEM) 5952 status = MGMT_STATUS_NO_RESOURCES; 5953 else 5954 status = MGMT_STATUS_FAILED; 5955 5956 goto unlock; 5957 } 5958 5959 hci_dev_unlock(hdev); 5960 5961 return 0; 5962 5963 unlock: 5964 hci_dev_unlock(hdev); 5965 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR, 5966 status); 5967 } 5968 5969 static void read_local_oob_data_complete(struct hci_dev *hdev, void *data, 5970 int err) 5971 { 5972 struct mgmt_rp_read_local_oob_data mgmt_rp; 5973 size_t rp_size = sizeof(mgmt_rp); 5974 struct mgmt_pending_cmd *cmd = data; 5975 struct sk_buff *skb = cmd->skb; 5976 u8 status = mgmt_status(err); 5977 5978 if (!status) { 5979 if (!skb) 5980 status = MGMT_STATUS_FAILED; 5981 else if (IS_ERR(skb)) 5982 status = mgmt_status(PTR_ERR(skb)); 5983 else 5984 status = mgmt_status(skb->data[0]); 5985 } 5986 5987 bt_dev_dbg(hdev, "status %d", status); 5988 5989 if (status) { 5990 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 5991 status); 5992 goto remove; 5993 } 5994 5995 memset(&mgmt_rp, 0, sizeof(mgmt_rp)); 5996 5997 if (!bredr_sc_enabled(hdev)) { 5998 struct hci_rp_read_local_oob_data *rp = (void *) skb->data; 5999 6000 if (skb->len < sizeof(*rp)) { 6001 mgmt_cmd_status(cmd->sk, hdev->id, 6002 MGMT_OP_READ_LOCAL_OOB_DATA, 6003 MGMT_STATUS_FAILED); 6004 goto remove; 6005 } 6006 6007 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash)); 6008 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand)); 6009 6010 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256); 6011 } else { 6012 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; 6013 6014 if (skb->len < sizeof(*rp)) { 6015 mgmt_cmd_status(cmd->sk, hdev->id, 6016 MGMT_OP_READ_LOCAL_OOB_DATA, 6017 MGMT_STATUS_FAILED); 6018 goto remove; 6019 } 6020 6021 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192)); 6022 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192)); 6023 6024 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256)); 6025 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256)); 6026 } 6027 6028 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 6029 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size); 6030 6031 remove: 6032 if (skb && !IS_ERR(skb)) 6033 kfree_skb(skb); 6034 6035 mgmt_pending_free(cmd); 6036 } 6037 6038 static int read_local_oob_data_sync(struct hci_dev *hdev, void *data) 6039 { 6040 struct mgmt_pending_cmd *cmd = data; 6041 6042 if (bredr_sc_enabled(hdev)) 6043 cmd->skb = hci_read_local_oob_data_sync(hdev, true, cmd->sk); 6044 else 6045 cmd->skb = hci_read_local_oob_data_sync(hdev, false, cmd->sk); 6046 6047 if (IS_ERR(cmd->skb)) 6048 return PTR_ERR(cmd->skb); 6049 else 6050 return 0; 6051 } 6052 6053 static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev, 6054 void *data, u16 data_len) 6055 { 6056 struct mgmt_pending_cmd *cmd; 6057 int err; 6058 6059 bt_dev_dbg(hdev, "sock %p", sk); 6060 6061 hci_dev_lock(hdev); 6062 6063 if (!hdev_is_powered(hdev)) { 6064 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 6065 MGMT_STATUS_NOT_POWERED); 6066 goto unlock; 6067 } 6068 6069 if (!lmp_ssp_capable(hdev)) { 6070 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 6071 MGMT_STATUS_NOT_SUPPORTED); 6072 goto unlock; 6073 } 6074 6075 cmd = mgmt_pending_new(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0); 6076 if (!cmd) 6077 err = -ENOMEM; 6078 else 6079 err = hci_cmd_sync_queue(hdev, read_local_oob_data_sync, cmd, 6080 read_local_oob_data_complete); 6081 6082 if (err < 0) { 6083 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 6084 MGMT_STATUS_FAILED); 6085 6086 if (cmd) 6087 mgmt_pending_free(cmd); 6088 } 6089 6090 unlock: 6091 hci_dev_unlock(hdev); 6092 return err; 6093 } 6094 6095 static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev, 6096 void *data, u16 len) 6097 { 6098 struct mgmt_addr_info *addr = data; 6099 int err; 6100 6101 bt_dev_dbg(hdev, "sock %p", sk); 6102 6103 if (!bdaddr_type_is_valid(addr->type)) 6104 return mgmt_cmd_complete(sk, hdev->id, 6105 MGMT_OP_ADD_REMOTE_OOB_DATA, 6106 MGMT_STATUS_INVALID_PARAMS, 6107 addr, sizeof(*addr)); 6108 6109 hci_dev_lock(hdev); 6110 6111 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { 6112 struct mgmt_cp_add_remote_oob_data *cp = data; 6113 u8 status; 6114 6115 if (cp->addr.type != BDADDR_BREDR) { 6116 err = mgmt_cmd_complete(sk, hdev->id, 6117 MGMT_OP_ADD_REMOTE_OOB_DATA, 6118 MGMT_STATUS_INVALID_PARAMS, 6119 &cp->addr, sizeof(cp->addr)); 6120 goto unlock; 6121 } 6122 6123 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, 6124 cp->addr.type, cp->hash, 6125 cp->rand, NULL, NULL); 6126 if (err < 0) 6127 status = MGMT_STATUS_FAILED; 6128 else 6129 status = MGMT_STATUS_SUCCESS; 6130 6131 err = mgmt_cmd_complete(sk, hdev->id, 6132 MGMT_OP_ADD_REMOTE_OOB_DATA, status, 6133 &cp->addr, sizeof(cp->addr)); 6134 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { 6135 struct mgmt_cp_add_remote_oob_ext_data *cp = data; 6136 u8 *rand192, *hash192, *rand256, *hash256; 6137 u8 status; 6138 6139 if (bdaddr_type_is_le(cp->addr.type)) { 6140 /* Enforce zero-valued 192-bit parameters as 6141 * long as legacy SMP OOB isn't implemented. 6142 */ 6143 if (memcmp(cp->rand192, ZERO_KEY, 16) || 6144 memcmp(cp->hash192, ZERO_KEY, 16)) { 6145 err = mgmt_cmd_complete(sk, hdev->id, 6146 MGMT_OP_ADD_REMOTE_OOB_DATA, 6147 MGMT_STATUS_INVALID_PARAMS, 6148 addr, sizeof(*addr)); 6149 goto unlock; 6150 } 6151 6152 rand192 = NULL; 6153 hash192 = NULL; 6154 } else { 6155 /* In case one of the P-192 values is set to zero, 6156 * then just disable OOB data for P-192. 6157 */ 6158 if (!memcmp(cp->rand192, ZERO_KEY, 16) || 6159 !memcmp(cp->hash192, ZERO_KEY, 16)) { 6160 rand192 = NULL; 6161 hash192 = NULL; 6162 } else { 6163 rand192 = cp->rand192; 6164 hash192 = cp->hash192; 6165 } 6166 } 6167 6168 /* In case one of the P-256 values is set to zero, then just 6169 * disable OOB data for P-256. 6170 */ 6171 if (!memcmp(cp->rand256, ZERO_KEY, 16) || 6172 !memcmp(cp->hash256, ZERO_KEY, 16)) { 6173 rand256 = NULL; 6174 hash256 = NULL; 6175 } else { 6176 rand256 = cp->rand256; 6177 hash256 = cp->hash256; 6178 } 6179 6180 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, 6181 cp->addr.type, hash192, rand192, 6182 hash256, rand256); 6183 if (err < 0) 6184 status = MGMT_STATUS_FAILED; 6185 else 6186 status = MGMT_STATUS_SUCCESS; 6187 6188 err = mgmt_cmd_complete(sk, hdev->id, 6189 MGMT_OP_ADD_REMOTE_OOB_DATA, 6190 status, &cp->addr, sizeof(cp->addr)); 6191 } else { 6192 bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes", 6193 len); 6194 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, 6195 MGMT_STATUS_INVALID_PARAMS); 6196 } 6197 6198 unlock: 6199 hci_dev_unlock(hdev); 6200 return err; 6201 } 6202 6203 static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev, 6204 void *data, u16 len) 6205 { 6206 struct mgmt_cp_remove_remote_oob_data *cp = data; 6207 u8 status; 6208 int err; 6209 6210 bt_dev_dbg(hdev, "sock %p", sk); 6211 6212 if (cp->addr.type != BDADDR_BREDR) 6213 return mgmt_cmd_complete(sk, hdev->id, 6214 MGMT_OP_REMOVE_REMOTE_OOB_DATA, 6215 MGMT_STATUS_INVALID_PARAMS, 6216 &cp->addr, sizeof(cp->addr)); 6217 6218 hci_dev_lock(hdev); 6219 6220 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { 6221 hci_remote_oob_data_clear(hdev); 6222 status = MGMT_STATUS_SUCCESS; 6223 goto done; 6224 } 6225 6226 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type); 6227 if (err < 0) 6228 status = MGMT_STATUS_INVALID_PARAMS; 6229 else 6230 status = MGMT_STATUS_SUCCESS; 6231 6232 done: 6233 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, 6234 status, &cp->addr, sizeof(cp->addr)); 6235 6236 hci_dev_unlock(hdev); 6237 return err; 6238 } 6239 6240 static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type, 6241 uint8_t *mgmt_status) 6242 { 6243 switch (type) { 6244 case DISCOV_TYPE_LE: 6245 *mgmt_status = mgmt_le_support(hdev); 6246 if (*mgmt_status) 6247 return false; 6248 break; 6249 case DISCOV_TYPE_INTERLEAVED: 6250 *mgmt_status = mgmt_le_support(hdev); 6251 if (*mgmt_status) 6252 return false; 6253 fallthrough; 6254 case DISCOV_TYPE_BREDR: 6255 *mgmt_status = mgmt_bredr_support(hdev); 6256 if (*mgmt_status) 6257 return false; 6258 break; 6259 default: 6260 *mgmt_status = MGMT_STATUS_INVALID_PARAMS; 6261 return false; 6262 } 6263 6264 return true; 6265 } 6266 6267 static void start_discovery_complete(struct hci_dev *hdev, void *data, int err) 6268 { 6269 struct mgmt_pending_cmd *cmd = data; 6270 6271 bt_dev_dbg(hdev, "err %d", err); 6272 6273 if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) 6274 return; 6275 6276 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_status(err), 6277 cmd->param, 1); 6278 mgmt_pending_free(cmd); 6279 6280 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED: 6281 DISCOVERY_FINDING); 6282 } 6283 6284 static int start_discovery_sync(struct hci_dev *hdev, void *data) 6285 { 6286 if (!mgmt_pending_listed(hdev, data)) 6287 return -ECANCELED; 6288 6289 return hci_start_discovery_sync(hdev); 6290 } 6291 6292 static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev, 6293 u16 op, void *data, u16 len) 6294 { 6295 struct mgmt_cp_start_discovery *cp = data; 6296 struct mgmt_pending_cmd *cmd; 6297 u8 status; 6298 int err; 6299 6300 bt_dev_dbg(hdev, "sock %p", sk); 6301 6302 hci_dev_lock(hdev); 6303 6304 if (!hdev_is_powered(hdev)) { 6305 err = mgmt_cmd_complete(sk, hdev->id, op, 6306 MGMT_STATUS_NOT_POWERED, 6307 &cp->type, sizeof(cp->type)); 6308 goto failed; 6309 } 6310 6311 if (hdev->discovery.state != DISCOVERY_STOPPED || 6312 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { 6313 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, 6314 &cp->type, sizeof(cp->type)); 6315 goto failed; 6316 } 6317 6318 if (!discovery_type_is_valid(hdev, cp->type, &status)) { 6319 err = mgmt_cmd_complete(sk, hdev->id, op, status, 6320 &cp->type, sizeof(cp->type)); 6321 goto failed; 6322 } 6323 6324 /* Can't start discovery when it is paused */ 6325 if (hdev->discovery_paused) { 6326 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY, 6327 &cp->type, sizeof(cp->type)); 6328 goto failed; 6329 } 6330 6331 /* Clear the discovery filter first to free any previously 6332 * allocated memory for the UUID list. 6333 */ 6334 hci_discovery_filter_clear(hdev); 6335 6336 hdev->discovery.type = cp->type; 6337 hdev->discovery.report_invalid_rssi = false; 6338 if (op == MGMT_OP_START_LIMITED_DISCOVERY) 6339 hdev->discovery.limited = true; 6340 else 6341 hdev->discovery.limited = false; 6342 6343 cmd = mgmt_pending_add(sk, op, hdev, data, len); 6344 if (!cmd) { 6345 err = -ENOMEM; 6346 goto failed; 6347 } 6348 6349 err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd, 6350 start_discovery_complete); 6351 if (err < 0) { 6352 mgmt_pending_remove(cmd); 6353 goto failed; 6354 } 6355 6356 hci_discovery_set_state(hdev, DISCOVERY_STARTING); 6357 6358 failed: 6359 hci_dev_unlock(hdev); 6360 return err; 6361 } 6362 6363 static int start_discovery(struct sock *sk, struct hci_dev *hdev, 6364 void *data, u16 len) 6365 { 6366 return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY, 6367 data, len); 6368 } 6369 6370 static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev, 6371 void *data, u16 len) 6372 { 6373 return start_discovery_internal(sk, hdev, 6374 MGMT_OP_START_LIMITED_DISCOVERY, 6375 data, len); 6376 } 6377 6378 static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, 6379 void *data, u16 len) 6380 { 6381 struct mgmt_cp_start_service_discovery *cp = data; 6382 struct mgmt_pending_cmd *cmd; 6383 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); 6384 u16 uuid_count, expected_len; 6385 u8 (*uuids)[16] = NULL; 6386 u8 status; 6387 int err; 6388 6389 bt_dev_dbg(hdev, "sock %p", sk); 6390 6391 hci_dev_lock(hdev); 6392 6393 if (!hdev_is_powered(hdev)) { 6394 err = mgmt_cmd_complete(sk, hdev->id, 6395 MGMT_OP_START_SERVICE_DISCOVERY, 6396 MGMT_STATUS_NOT_POWERED, 6397 &cp->type, sizeof(cp->type)); 6398 goto failed; 6399 } 6400 6401 if (hdev->discovery.state != DISCOVERY_STOPPED || 6402 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { 6403 err = mgmt_cmd_complete(sk, hdev->id, 6404 MGMT_OP_START_SERVICE_DISCOVERY, 6405 MGMT_STATUS_BUSY, &cp->type, 6406 sizeof(cp->type)); 6407 goto failed; 6408 } 6409 6410 if (hdev->discovery_paused) { 6411 err = mgmt_cmd_complete(sk, hdev->id, 6412 MGMT_OP_START_SERVICE_DISCOVERY, 6413 MGMT_STATUS_BUSY, &cp->type, 6414 sizeof(cp->type)); 6415 goto failed; 6416 } 6417 6418 uuid_count = __le16_to_cpu(cp->uuid_count); 6419 if (uuid_count > max_uuid_count) { 6420 bt_dev_err(hdev, "service_discovery: too big uuid_count value %u", 6421 uuid_count); 6422 err = mgmt_cmd_complete(sk, hdev->id, 6423 MGMT_OP_START_SERVICE_DISCOVERY, 6424 MGMT_STATUS_INVALID_PARAMS, &cp->type, 6425 sizeof(cp->type)); 6426 goto failed; 6427 } 6428 6429 expected_len = sizeof(*cp) + uuid_count * 16; 6430 if (expected_len != len) { 6431 bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes", 6432 expected_len, len); 6433 err = mgmt_cmd_complete(sk, hdev->id, 6434 MGMT_OP_START_SERVICE_DISCOVERY, 6435 MGMT_STATUS_INVALID_PARAMS, &cp->type, 6436 sizeof(cp->type)); 6437 goto failed; 6438 } 6439 6440 if (!discovery_type_is_valid(hdev, cp->type, &status)) { 6441 err = mgmt_cmd_complete(sk, hdev->id, 6442 MGMT_OP_START_SERVICE_DISCOVERY, 6443 status, &cp->type, sizeof(cp->type)); 6444 goto failed; 6445 } 6446 6447 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, 6448 hdev, data, len); 6449 if (!cmd) { 6450 err = -ENOMEM; 6451 goto failed; 6452 } 6453 6454 /* Clear the discovery filter first to free any previously 6455 * allocated memory for the UUID list. 6456 */ 6457 hci_discovery_filter_clear(hdev); 6458 6459 hdev->discovery.result_filtering = true; 6460 hdev->discovery.type = cp->type; 6461 hdev->discovery.rssi = cp->rssi; 6462 6463 if (uuid_count > 0) { 6464 uuids = kmemdup(cp->uuids, uuid_count * sizeof(*uuids), GFP_KERNEL); 6465 if (!uuids) { 6466 err = mgmt_cmd_complete(sk, hdev->id, 6467 MGMT_OP_START_SERVICE_DISCOVERY, 6468 MGMT_STATUS_FAILED, 6469 &cp->type, sizeof(cp->type)); 6470 mgmt_pending_remove(cmd); 6471 goto failed; 6472 } 6473 } 6474 6475 spin_lock(&hdev->discovery.lock); 6476 hdev->discovery.uuids = uuids; 6477 hdev->discovery.uuid_count = uuid_count; 6478 spin_unlock(&hdev->discovery.lock); 6479 6480 err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd, 6481 start_discovery_complete); 6482 if (err < 0) { 6483 mgmt_pending_remove(cmd); 6484 goto failed; 6485 } 6486 6487 hci_discovery_set_state(hdev, DISCOVERY_STARTING); 6488 6489 failed: 6490 hci_dev_unlock(hdev); 6491 return err; 6492 } 6493 6494 static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err) 6495 { 6496 struct mgmt_pending_cmd *cmd = data; 6497 6498 if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) 6499 return; 6500 6501 bt_dev_dbg(hdev, "err %d", err); 6502 6503 hci_dev_lock(hdev); 6504 6505 /* Change the state before replying, so that a client acting on the 6506 * reply sees it, and only while the discovery is still stopping, as 6507 * Inquiry Cancel may already have stopped it and a new Start Discovery 6508 * may have followed. Go back to DISCOVERY_FINDING when stopping failed 6509 * while the inquiry or the active LE scan of the discovery is still 6510 * running, so that it can be stopped again. 6511 */ 6512 if (hdev->discovery.state == DISCOVERY_STOPPING) { 6513 if (err && (test_bit(HCI_INQUIRY, &hdev->flags) || 6514 (hci_dev_test_flag(hdev, HCI_LE_SCAN) && 6515 hdev->le_scan_type == LE_SCAN_ACTIVE))) 6516 hci_discovery_set_state(hdev, DISCOVERY_FINDING); 6517 else 6518 hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 6519 } 6520 6521 hci_dev_unlock(hdev); 6522 6523 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_status(err), 6524 cmd->param, 1); 6525 mgmt_pending_free(cmd); 6526 } 6527 6528 static int stop_discovery_sync(struct hci_dev *hdev, void *data) 6529 { 6530 if (!mgmt_pending_listed(hdev, data)) 6531 return -ECANCELED; 6532 6533 return hci_stop_discovery_sync(hdev); 6534 } 6535 6536 static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data, 6537 u16 len) 6538 { 6539 struct mgmt_cp_stop_discovery *mgmt_cp = data; 6540 struct mgmt_pending_cmd *cmd; 6541 int err; 6542 6543 bt_dev_dbg(hdev, "sock %p", sk); 6544 6545 hci_dev_lock(hdev); 6546 6547 if (!hci_discovery_active(hdev)) { 6548 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 6549 MGMT_STATUS_REJECTED, &mgmt_cp->type, 6550 sizeof(mgmt_cp->type)); 6551 goto unlock; 6552 } 6553 6554 if (hdev->discovery.type != mgmt_cp->type) { 6555 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 6556 MGMT_STATUS_INVALID_PARAMS, 6557 &mgmt_cp->type, sizeof(mgmt_cp->type)); 6558 goto unlock; 6559 } 6560 6561 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); 6562 if (!cmd) { 6563 err = -ENOMEM; 6564 goto unlock; 6565 } 6566 6567 err = hci_cmd_sync_queue(hdev, stop_discovery_sync, cmd, 6568 stop_discovery_complete); 6569 if (err < 0) { 6570 mgmt_pending_remove(cmd); 6571 goto unlock; 6572 } 6573 6574 hci_discovery_set_state(hdev, DISCOVERY_STOPPING); 6575 6576 unlock: 6577 hci_dev_unlock(hdev); 6578 return err; 6579 } 6580 6581 static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data, 6582 u16 len) 6583 { 6584 struct mgmt_cp_confirm_name *cp = data; 6585 struct inquiry_entry *e; 6586 int err; 6587 6588 bt_dev_dbg(hdev, "sock %p", sk); 6589 6590 hci_dev_lock(hdev); 6591 6592 if (!hci_discovery_active(hdev)) { 6593 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 6594 MGMT_STATUS_FAILED, &cp->addr, 6595 sizeof(cp->addr)); 6596 goto failed; 6597 } 6598 6599 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); 6600 if (!e) { 6601 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 6602 MGMT_STATUS_INVALID_PARAMS, &cp->addr, 6603 sizeof(cp->addr)); 6604 goto failed; 6605 } 6606 6607 if (cp->name_known) { 6608 e->name_state = NAME_KNOWN; 6609 list_del(&e->list); 6610 } else { 6611 e->name_state = NAME_NEEDED; 6612 hci_inquiry_cache_update_resolve(hdev, e); 6613 } 6614 6615 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, 6616 &cp->addr, sizeof(cp->addr)); 6617 6618 failed: 6619 hci_dev_unlock(hdev); 6620 return err; 6621 } 6622 6623 static int block_device(struct sock *sk, struct hci_dev *hdev, void *data, 6624 u16 len) 6625 { 6626 struct mgmt_cp_block_device *cp = data; 6627 u8 status; 6628 int err; 6629 6630 bt_dev_dbg(hdev, "sock %p", sk); 6631 6632 if (!bdaddr_type_is_valid(cp->addr.type)) 6633 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, 6634 MGMT_STATUS_INVALID_PARAMS, 6635 &cp->addr, sizeof(cp->addr)); 6636 6637 hci_dev_lock(hdev); 6638 6639 err = hci_bdaddr_list_add(&hdev->reject_list, &cp->addr.bdaddr, 6640 cp->addr.type); 6641 if (err < 0) { 6642 status = MGMT_STATUS_FAILED; 6643 goto done; 6644 } 6645 6646 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), 6647 sk); 6648 status = MGMT_STATUS_SUCCESS; 6649 6650 done: 6651 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, 6652 &cp->addr, sizeof(cp->addr)); 6653 6654 hci_dev_unlock(hdev); 6655 6656 return err; 6657 } 6658 6659 static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data, 6660 u16 len) 6661 { 6662 struct mgmt_cp_unblock_device *cp = data; 6663 u8 status; 6664 int err; 6665 6666 bt_dev_dbg(hdev, "sock %p", sk); 6667 6668 if (!bdaddr_type_is_valid(cp->addr.type)) 6669 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, 6670 MGMT_STATUS_INVALID_PARAMS, 6671 &cp->addr, sizeof(cp->addr)); 6672 6673 hci_dev_lock(hdev); 6674 6675 err = hci_bdaddr_list_del(&hdev->reject_list, &cp->addr.bdaddr, 6676 cp->addr.type); 6677 if (err < 0) { 6678 status = MGMT_STATUS_INVALID_PARAMS; 6679 goto done; 6680 } 6681 6682 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), 6683 sk); 6684 status = MGMT_STATUS_SUCCESS; 6685 6686 done: 6687 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, 6688 &cp->addr, sizeof(cp->addr)); 6689 6690 hci_dev_unlock(hdev); 6691 6692 return err; 6693 } 6694 6695 static int set_device_id_sync(struct hci_dev *hdev, void *data) 6696 { 6697 return hci_update_eir_sync(hdev); 6698 } 6699 6700 static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, 6701 u16 len) 6702 { 6703 struct mgmt_cp_set_device_id *cp = data; 6704 int err; 6705 __u16 source; 6706 6707 bt_dev_dbg(hdev, "sock %p", sk); 6708 6709 source = __le16_to_cpu(cp->source); 6710 6711 if (source > 0x0002) 6712 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 6713 MGMT_STATUS_INVALID_PARAMS); 6714 6715 hci_dev_lock(hdev); 6716 6717 hdev->devid_source = source; 6718 hdev->devid_vendor = __le16_to_cpu(cp->vendor); 6719 hdev->devid_product = __le16_to_cpu(cp->product); 6720 hdev->devid_version = __le16_to_cpu(cp->version); 6721 6722 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, 6723 NULL, 0); 6724 6725 hci_cmd_sync_queue(hdev, set_device_id_sync, NULL, NULL); 6726 6727 hci_dev_unlock(hdev); 6728 6729 return err; 6730 } 6731 6732 static void enable_advertising_instance(struct hci_dev *hdev, int err) 6733 { 6734 if (err) 6735 bt_dev_err(hdev, "failed to re-configure advertising %d", err); 6736 else 6737 bt_dev_dbg(hdev, "status %d", err); 6738 } 6739 6740 static void set_advertising_complete(struct hci_dev *hdev, void *data, int err) 6741 { 6742 struct mgmt_pending_cmd *cmd = data; 6743 struct cmd_lookup match = { NULL, hdev }; 6744 u8 instance; 6745 struct adv_info *adv_instance; 6746 u8 status = mgmt_status(err); 6747 6748 if (err == -ECANCELED || !mgmt_pending_valid(hdev, data)) 6749 return; 6750 6751 if (status) { 6752 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, status); 6753 mgmt_pending_free(cmd); 6754 return; 6755 } 6756 6757 /* With extended advertising global advertising (instance 0) has its 6758 * own advertising set so only consider its state, other instances may 6759 * still be enabled. 6760 */ 6761 if (ext_adv_capable(hdev) ? hci_dev_test_flag(hdev, HCI_LE_ADV_0) : 6762 hci_dev_test_flag(hdev, HCI_LE_ADV)) 6763 hci_dev_set_flag(hdev, HCI_ADVERTISING); 6764 else 6765 hci_dev_clear_flag(hdev, HCI_ADVERTISING); 6766 6767 settings_rsp(cmd, &match); 6768 mgmt_pending_free(cmd); 6769 6770 new_settings(hdev, match.sk); 6771 6772 if (match.sk) 6773 sock_put(match.sk); 6774 6775 /* If "Set Advertising" was just disabled and instance advertising was 6776 * set up earlier, then re-enable multi-instance advertising. 6777 */ 6778 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || 6779 list_empty(&hdev->adv_instances)) 6780 return; 6781 6782 /* With extended advertising instances are not overridden by global 6783 * advertising, except the ones sharing its set, so only (re)start the 6784 * instances that are not already enabled. 6785 * 6786 * Lookup instances by identifier while holding hdev->lock since the 6787 * list may change while waiting for the HCI commands to complete. 6788 */ 6789 if (ext_adv_capable(hdev)) { 6790 u16 i; 6791 6792 for (i = 1; i <= hdev->le_num_of_adv_sets + 1; i++) { 6793 bool enabled; 6794 6795 hci_dev_lock(hdev); 6796 adv_instance = hci_find_adv_instance(hdev, i); 6797 enabled = !adv_instance || adv_instance->enabled; 6798 hci_dev_unlock(hdev); 6799 6800 if (enabled) 6801 continue; 6802 6803 err = hci_schedule_adv_instance_sync(hdev, i, true); 6804 enable_advertising_instance(hdev, err); 6805 } 6806 6807 return; 6808 } 6809 6810 instance = hdev->cur_adv_instance; 6811 if (!instance) { 6812 hci_dev_lock(hdev); 6813 adv_instance = list_first_entry_or_null(&hdev->adv_instances, 6814 struct adv_info, list); 6815 if (adv_instance) 6816 instance = adv_instance->instance; 6817 hci_dev_unlock(hdev); 6818 6819 if (!instance) 6820 return; 6821 } 6822 6823 err = hci_schedule_adv_instance_sync(hdev, instance, true); 6824 6825 enable_advertising_instance(hdev, err); 6826 } 6827 6828 static int set_adv_sync(struct hci_dev *hdev, void *data) 6829 { 6830 struct mgmt_pending_cmd *cmd = data; 6831 struct mgmt_mode cp; 6832 u8 val; 6833 6834 mutex_lock(&hdev->mgmt_pending_lock); 6835 6836 if (!__mgmt_pending_listed(hdev, cmd)) { 6837 mutex_unlock(&hdev->mgmt_pending_lock); 6838 return -ECANCELED; 6839 } 6840 6841 memcpy(&cp, cmd->param, sizeof(cp)); 6842 6843 mutex_unlock(&hdev->mgmt_pending_lock); 6844 6845 val = !!cp.val; 6846 6847 if (cp.val == 0x02) 6848 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); 6849 else 6850 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); 6851 6852 cancel_adv_timeout(hdev); 6853 6854 if (val) { 6855 /* Switch to instance "0" for the Set Advertising setting. 6856 * We cannot use update_[adv|scan_rsp]_data() here as the 6857 * HCI_ADVERTISING flag is not yet set. 6858 */ 6859 hdev->cur_adv_instance = 0x00; 6860 6861 if (ext_adv_capable(hdev)) { 6862 hci_start_ext_adv_sync(hdev, 0x00); 6863 } else { 6864 hci_update_adv_data_sync(hdev, 0x00); 6865 hci_update_scan_rsp_data_sync(hdev, 0x00); 6866 hci_enable_advertising_sync(hdev); 6867 } 6868 } else if (ext_adv_capable(hdev)) { 6869 /* Only disable global advertising (instance 0) leaving other 6870 * instances untouched. 6871 */ 6872 hci_disable_ext_adv_legacy_instance_sync(hdev); 6873 } else { 6874 hci_disable_advertising_sync(hdev); 6875 } 6876 6877 return 0; 6878 } 6879 6880 static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, 6881 u16 len) 6882 { 6883 struct mgmt_mode *cp = data; 6884 struct mgmt_pending_cmd *cmd; 6885 u8 val, status; 6886 int err; 6887 6888 bt_dev_dbg(hdev, "sock %p", sk); 6889 6890 status = mgmt_le_support(hdev); 6891 if (status) 6892 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, 6893 status); 6894 6895 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) 6896 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, 6897 MGMT_STATUS_INVALID_PARAMS); 6898 6899 if (hdev->advertising_paused) 6900 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, 6901 MGMT_STATUS_BUSY); 6902 6903 hci_dev_lock(hdev); 6904 6905 val = !!cp->val; 6906 6907 /* The following conditions are ones which mean that we should 6908 * not do any HCI communication but directly send a mgmt 6909 * response to user space (after toggling the flag if 6910 * necessary). 6911 */ 6912 if (!hdev_is_powered(hdev) || 6913 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && 6914 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || 6915 hci_dev_test_flag(hdev, HCI_MESH) || 6916 hci_conn_num(hdev, LE_LINK) > 0 || 6917 (hci_dev_test_flag(hdev, HCI_LE_SCAN) && 6918 hdev->le_scan_type == LE_SCAN_ACTIVE)) { 6919 bool changed; 6920 6921 if (cp->val) { 6922 hdev->cur_adv_instance = 0x00; 6923 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); 6924 if (cp->val == 0x02) 6925 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); 6926 else 6927 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); 6928 } else { 6929 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); 6930 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); 6931 } 6932 6933 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); 6934 if (err < 0) 6935 goto unlock; 6936 6937 if (changed) 6938 err = new_settings(hdev, sk); 6939 6940 goto unlock; 6941 } 6942 6943 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) || 6944 pending_find(MGMT_OP_SET_LE, hdev)) { 6945 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, 6946 MGMT_STATUS_BUSY); 6947 goto unlock; 6948 } 6949 6950 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); 6951 if (!cmd) 6952 err = -ENOMEM; 6953 else 6954 err = hci_cmd_sync_queue(hdev, set_adv_sync, cmd, 6955 set_advertising_complete); 6956 6957 if (err < 0 && cmd) 6958 mgmt_pending_remove(cmd); 6959 6960 unlock: 6961 hci_dev_unlock(hdev); 6962 return err; 6963 } 6964 6965 static int set_static_address(struct sock *sk, struct hci_dev *hdev, 6966 void *data, u16 len) 6967 { 6968 struct mgmt_cp_set_static_address *cp = data; 6969 int err; 6970 6971 bt_dev_dbg(hdev, "sock %p", sk); 6972 6973 if (!lmp_le_capable(hdev)) 6974 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 6975 MGMT_STATUS_NOT_SUPPORTED); 6976 6977 if (hdev_is_powered(hdev)) 6978 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 6979 MGMT_STATUS_REJECTED); 6980 6981 if (bacmp(&cp->bdaddr, BDADDR_ANY)) { 6982 if (!bacmp(&cp->bdaddr, BDADDR_NONE)) 6983 return mgmt_cmd_status(sk, hdev->id, 6984 MGMT_OP_SET_STATIC_ADDRESS, 6985 MGMT_STATUS_INVALID_PARAMS); 6986 6987 /* Two most significant bits shall be set */ 6988 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) 6989 return mgmt_cmd_status(sk, hdev->id, 6990 MGMT_OP_SET_STATIC_ADDRESS, 6991 MGMT_STATUS_INVALID_PARAMS); 6992 } 6993 6994 hci_dev_lock(hdev); 6995 6996 bacpy(&hdev->static_addr, &cp->bdaddr); 6997 6998 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); 6999 if (err < 0) 7000 goto unlock; 7001 7002 err = new_settings(hdev, sk); 7003 7004 unlock: 7005 hci_dev_unlock(hdev); 7006 return err; 7007 } 7008 7009 static int set_scan_params(struct sock *sk, struct hci_dev *hdev, 7010 void *data, u16 len) 7011 { 7012 struct mgmt_cp_set_scan_params *cp = data; 7013 __u16 interval, window; 7014 int err; 7015 7016 bt_dev_dbg(hdev, "sock %p", sk); 7017 7018 if (!lmp_le_capable(hdev)) 7019 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 7020 MGMT_STATUS_NOT_SUPPORTED); 7021 7022 /* Keep allowed ranges in sync with set_mesh() */ 7023 interval = __le16_to_cpu(cp->interval); 7024 7025 if (interval < 0x0004 || interval > 0x4000) 7026 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 7027 MGMT_STATUS_INVALID_PARAMS); 7028 7029 window = __le16_to_cpu(cp->window); 7030 7031 if (window < 0x0004 || window > 0x4000) 7032 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 7033 MGMT_STATUS_INVALID_PARAMS); 7034 7035 if (window > interval) 7036 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 7037 MGMT_STATUS_INVALID_PARAMS); 7038 7039 hci_dev_lock(hdev); 7040 7041 hdev->le_scan_interval = interval; 7042 hdev->le_scan_window = window; 7043 7044 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, 7045 NULL, 0); 7046 7047 /* If background scan is running, restart it so new parameters are 7048 * loaded. 7049 */ 7050 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && 7051 hdev->discovery.state == DISCOVERY_STOPPED) 7052 hci_update_passive_scan(hdev); 7053 7054 hci_dev_unlock(hdev); 7055 7056 return err; 7057 } 7058 7059 static void fast_connectable_complete(struct hci_dev *hdev, void *data, int err) 7060 { 7061 struct mgmt_pending_cmd *cmd = data; 7062 7063 bt_dev_dbg(hdev, "err %d", err); 7064 7065 if (err) { 7066 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 7067 mgmt_status(err)); 7068 } else { 7069 struct mgmt_mode *cp = cmd->param; 7070 7071 if (cp->val) 7072 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); 7073 else 7074 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); 7075 7076 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); 7077 new_settings(hdev, cmd->sk); 7078 } 7079 7080 mgmt_pending_free(cmd); 7081 } 7082 7083 static int write_fast_connectable_sync(struct hci_dev *hdev, void *data) 7084 { 7085 struct mgmt_pending_cmd *cmd = data; 7086 struct mgmt_mode *cp = cmd->param; 7087 7088 return hci_write_fast_connectable_sync(hdev, cp->val); 7089 } 7090 7091 static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev, 7092 void *data, u16 len) 7093 { 7094 struct mgmt_mode *cp = data; 7095 struct mgmt_pending_cmd *cmd; 7096 int err; 7097 7098 bt_dev_dbg(hdev, "sock %p", sk); 7099 7100 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || 7101 hdev->hci_ver < BLUETOOTH_VER_1_2) 7102 return mgmt_cmd_status(sk, hdev->id, 7103 MGMT_OP_SET_FAST_CONNECTABLE, 7104 MGMT_STATUS_NOT_SUPPORTED); 7105 7106 if (cp->val != 0x00 && cp->val != 0x01) 7107 return mgmt_cmd_status(sk, hdev->id, 7108 MGMT_OP_SET_FAST_CONNECTABLE, 7109 MGMT_STATUS_INVALID_PARAMS); 7110 7111 hci_dev_lock(hdev); 7112 7113 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { 7114 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); 7115 goto unlock; 7116 } 7117 7118 if (!hdev_is_powered(hdev)) { 7119 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); 7120 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); 7121 new_settings(hdev, sk); 7122 goto unlock; 7123 } 7124 7125 cmd = mgmt_pending_new(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, data, 7126 len); 7127 if (!cmd) 7128 err = -ENOMEM; 7129 else 7130 err = hci_cmd_sync_queue(hdev, write_fast_connectable_sync, cmd, 7131 fast_connectable_complete); 7132 7133 if (err < 0) { 7134 mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 7135 MGMT_STATUS_FAILED); 7136 7137 if (cmd) 7138 mgmt_pending_free(cmd); 7139 } 7140 7141 unlock: 7142 hci_dev_unlock(hdev); 7143 7144 return err; 7145 } 7146 7147 static void set_bredr_complete(struct hci_dev *hdev, void *data, int err) 7148 { 7149 struct mgmt_pending_cmd *cmd = data; 7150 7151 bt_dev_dbg(hdev, "err %d", err); 7152 7153 if (err) { 7154 u8 mgmt_err = mgmt_status(err); 7155 7156 /* We need to restore the flag if related HCI commands 7157 * failed. 7158 */ 7159 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); 7160 7161 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err); 7162 } else { 7163 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); 7164 new_settings(hdev, cmd->sk); 7165 } 7166 7167 mgmt_pending_free(cmd); 7168 } 7169 7170 static int set_bredr_sync(struct hci_dev *hdev, void *data) 7171 { 7172 int status; 7173 7174 status = hci_write_fast_connectable_sync(hdev, false); 7175 7176 if (!status) 7177 status = hci_update_scan_sync(hdev); 7178 7179 /* Since only the advertising data flags will change, there 7180 * is no need to update the scan response data. 7181 */ 7182 if (!status) 7183 status = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance); 7184 7185 return status; 7186 } 7187 7188 static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 7189 { 7190 struct mgmt_mode *cp = data; 7191 struct mgmt_pending_cmd *cmd; 7192 int err; 7193 7194 bt_dev_dbg(hdev, "sock %p", sk); 7195 7196 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) 7197 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 7198 MGMT_STATUS_NOT_SUPPORTED); 7199 7200 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 7201 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 7202 MGMT_STATUS_REJECTED); 7203 7204 if (cp->val != 0x00 && cp->val != 0x01) 7205 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 7206 MGMT_STATUS_INVALID_PARAMS); 7207 7208 hci_dev_lock(hdev); 7209 7210 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 7211 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); 7212 goto unlock; 7213 } 7214 7215 if (!hdev_is_powered(hdev)) { 7216 if (!cp->val) { 7217 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); 7218 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); 7219 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); 7220 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); 7221 } 7222 7223 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); 7224 7225 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); 7226 if (err < 0) 7227 goto unlock; 7228 7229 err = new_settings(hdev, sk); 7230 goto unlock; 7231 } 7232 7233 /* Reject disabling when powered on */ 7234 if (!cp->val) { 7235 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 7236 MGMT_STATUS_REJECTED); 7237 goto unlock; 7238 } else { 7239 /* When configuring a dual-mode controller to operate 7240 * with LE only and using a static address, then switching 7241 * BR/EDR back on is not allowed. 7242 * 7243 * Dual-mode controllers shall operate with the public 7244 * address as its identity address for BR/EDR and LE. So 7245 * reject the attempt to create an invalid configuration. 7246 * 7247 * The same restrictions applies when secure connections 7248 * has been enabled. For BR/EDR this is a controller feature 7249 * while for LE it is a host stack feature. This means that 7250 * switching BR/EDR back on when secure connections has been 7251 * enabled is not a supported transaction. 7252 */ 7253 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && 7254 (bacmp(&hdev->static_addr, BDADDR_ANY) || 7255 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { 7256 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 7257 MGMT_STATUS_REJECTED); 7258 goto unlock; 7259 } 7260 } 7261 7262 cmd = mgmt_pending_new(sk, MGMT_OP_SET_BREDR, hdev, data, len); 7263 if (!cmd) 7264 err = -ENOMEM; 7265 else 7266 err = hci_cmd_sync_queue(hdev, set_bredr_sync, cmd, 7267 set_bredr_complete); 7268 7269 if (err < 0) { 7270 mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 7271 MGMT_STATUS_FAILED); 7272 if (cmd) 7273 mgmt_pending_free(cmd); 7274 7275 goto unlock; 7276 } 7277 7278 /* We need to flip the bit already here so that 7279 * hci_req_update_adv_data generates the correct flags. 7280 */ 7281 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); 7282 7283 unlock: 7284 hci_dev_unlock(hdev); 7285 return err; 7286 } 7287 7288 static void set_secure_conn_complete(struct hci_dev *hdev, void *data, int err) 7289 { 7290 struct mgmt_pending_cmd *cmd = data; 7291 struct mgmt_mode *cp; 7292 7293 bt_dev_dbg(hdev, "err %d", err); 7294 7295 if (err) { 7296 u8 mgmt_err = mgmt_status(err); 7297 7298 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, mgmt_err); 7299 goto done; 7300 } 7301 7302 cp = cmd->param; 7303 7304 switch (cp->val) { 7305 case 0x00: 7306 hci_dev_clear_flag(hdev, HCI_SC_ENABLED); 7307 hci_dev_clear_flag(hdev, HCI_SC_ONLY); 7308 break; 7309 case 0x01: 7310 hci_dev_set_flag(hdev, HCI_SC_ENABLED); 7311 hci_dev_clear_flag(hdev, HCI_SC_ONLY); 7312 break; 7313 case 0x02: 7314 hci_dev_set_flag(hdev, HCI_SC_ENABLED); 7315 hci_dev_set_flag(hdev, HCI_SC_ONLY); 7316 break; 7317 } 7318 7319 send_settings_rsp(cmd->sk, cmd->opcode, hdev); 7320 new_settings(hdev, cmd->sk); 7321 7322 done: 7323 mgmt_pending_free(cmd); 7324 } 7325 7326 static int set_secure_conn_sync(struct hci_dev *hdev, void *data) 7327 { 7328 struct mgmt_pending_cmd *cmd = data; 7329 struct mgmt_mode *cp = cmd->param; 7330 u8 val = !!cp->val; 7331 7332 /* Force write of val */ 7333 hci_dev_set_flag(hdev, HCI_SC_ENABLED); 7334 7335 return hci_write_sc_support_sync(hdev, val); 7336 } 7337 7338 static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, 7339 void *data, u16 len) 7340 { 7341 struct mgmt_mode *cp = data; 7342 struct mgmt_pending_cmd *cmd; 7343 u8 val; 7344 int err; 7345 7346 bt_dev_dbg(hdev, "sock %p", sk); 7347 7348 if (!lmp_sc_capable(hdev) && 7349 !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) 7350 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, 7351 MGMT_STATUS_NOT_SUPPORTED); 7352 7353 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && 7354 lmp_sc_capable(hdev) && 7355 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) 7356 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, 7357 MGMT_STATUS_REJECTED); 7358 7359 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) 7360 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, 7361 MGMT_STATUS_INVALID_PARAMS); 7362 7363 hci_dev_lock(hdev); 7364 7365 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || 7366 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { 7367 bool changed; 7368 7369 if (cp->val) { 7370 changed = !hci_dev_test_and_set_flag(hdev, 7371 HCI_SC_ENABLED); 7372 if (cp->val == 0x02) 7373 hci_dev_set_flag(hdev, HCI_SC_ONLY); 7374 else 7375 hci_dev_clear_flag(hdev, HCI_SC_ONLY); 7376 } else { 7377 changed = hci_dev_test_and_clear_flag(hdev, 7378 HCI_SC_ENABLED); 7379 hci_dev_clear_flag(hdev, HCI_SC_ONLY); 7380 } 7381 7382 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); 7383 if (err < 0) 7384 goto failed; 7385 7386 if (changed) 7387 err = new_settings(hdev, sk); 7388 7389 goto failed; 7390 } 7391 7392 val = !!cp->val; 7393 7394 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && 7395 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { 7396 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); 7397 goto failed; 7398 } 7399 7400 cmd = mgmt_pending_new(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); 7401 if (!cmd) 7402 err = -ENOMEM; 7403 else 7404 err = hci_cmd_sync_queue(hdev, set_secure_conn_sync, cmd, 7405 set_secure_conn_complete); 7406 7407 if (err < 0) { 7408 mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, 7409 MGMT_STATUS_FAILED); 7410 if (cmd) 7411 mgmt_pending_free(cmd); 7412 } 7413 7414 failed: 7415 hci_dev_unlock(hdev); 7416 return err; 7417 } 7418 7419 static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, 7420 void *data, u16 len) 7421 { 7422 struct mgmt_mode *cp = data; 7423 bool changed, use_changed; 7424 int err; 7425 7426 bt_dev_dbg(hdev, "sock %p", sk); 7427 7428 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) 7429 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, 7430 MGMT_STATUS_INVALID_PARAMS); 7431 7432 hci_dev_lock(hdev); 7433 7434 if (cp->val) 7435 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); 7436 else 7437 changed = hci_dev_test_and_clear_flag(hdev, 7438 HCI_KEEP_DEBUG_KEYS); 7439 7440 if (cp->val == 0x02) 7441 use_changed = !hci_dev_test_and_set_flag(hdev, 7442 HCI_USE_DEBUG_KEYS); 7443 else 7444 use_changed = hci_dev_test_and_clear_flag(hdev, 7445 HCI_USE_DEBUG_KEYS); 7446 7447 if (hdev_is_powered(hdev) && use_changed && 7448 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { 7449 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; 7450 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, 7451 sizeof(mode), &mode); 7452 } 7453 7454 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); 7455 if (err < 0) 7456 goto unlock; 7457 7458 if (changed) 7459 err = new_settings(hdev, sk); 7460 7461 unlock: 7462 hci_dev_unlock(hdev); 7463 return err; 7464 } 7465 7466 static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, 7467 u16 len) 7468 { 7469 struct mgmt_cp_set_privacy *cp = cp_data; 7470 bool changed; 7471 int err; 7472 7473 bt_dev_dbg(hdev, "sock %p", sk); 7474 7475 if (!lmp_le_capable(hdev)) 7476 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, 7477 MGMT_STATUS_NOT_SUPPORTED); 7478 7479 if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02) 7480 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, 7481 MGMT_STATUS_INVALID_PARAMS); 7482 7483 if (hdev_is_powered(hdev)) 7484 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, 7485 MGMT_STATUS_REJECTED); 7486 7487 hci_dev_lock(hdev); 7488 7489 /* If user space supports this command it is also expected to 7490 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. 7491 */ 7492 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); 7493 7494 if (cp->privacy) { 7495 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); 7496 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); 7497 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); 7498 hci_adv_instances_set_rpa_expired(hdev, true); 7499 if (cp->privacy == 0x02) 7500 hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY); 7501 else 7502 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); 7503 } else { 7504 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); 7505 memset(hdev->irk, 0, sizeof(hdev->irk)); 7506 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); 7507 hci_adv_instances_set_rpa_expired(hdev, false); 7508 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY); 7509 } 7510 7511 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); 7512 if (err < 0) 7513 goto unlock; 7514 7515 if (changed) 7516 err = new_settings(hdev, sk); 7517 7518 unlock: 7519 hci_dev_unlock(hdev); 7520 return err; 7521 } 7522 7523 static bool irk_is_valid(struct mgmt_irk_info *irk) 7524 { 7525 switch (irk->addr.type) { 7526 case BDADDR_LE_PUBLIC: 7527 return true; 7528 7529 case BDADDR_LE_RANDOM: 7530 /* Two most significant bits shall be set */ 7531 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) 7532 return false; 7533 return true; 7534 } 7535 7536 return false; 7537 } 7538 7539 static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, 7540 u16 len) 7541 { 7542 struct mgmt_cp_load_irks *cp = cp_data; 7543 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / 7544 sizeof(struct mgmt_irk_info)); 7545 u16 irk_count, expected_len; 7546 int i, err; 7547 7548 bt_dev_dbg(hdev, "sock %p", sk); 7549 7550 if (!lmp_le_capable(hdev)) 7551 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, 7552 MGMT_STATUS_NOT_SUPPORTED); 7553 7554 irk_count = __le16_to_cpu(cp->irk_count); 7555 if (irk_count > max_irk_count) { 7556 bt_dev_err(hdev, "load_irks: too big irk_count value %u", 7557 irk_count); 7558 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, 7559 MGMT_STATUS_INVALID_PARAMS); 7560 } 7561 7562 expected_len = struct_size(cp, irks, irk_count); 7563 if (expected_len != len) { 7564 bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes", 7565 expected_len, len); 7566 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, 7567 MGMT_STATUS_INVALID_PARAMS); 7568 } 7569 7570 bt_dev_dbg(hdev, "irk_count %u", irk_count); 7571 7572 for (i = 0; i < irk_count; i++) { 7573 struct mgmt_irk_info *key = &cp->irks[i]; 7574 7575 if (!irk_is_valid(key)) 7576 return mgmt_cmd_status(sk, hdev->id, 7577 MGMT_OP_LOAD_IRKS, 7578 MGMT_STATUS_INVALID_PARAMS); 7579 } 7580 7581 hci_dev_lock(hdev); 7582 7583 hci_smp_irks_clear(hdev); 7584 7585 for (i = 0; i < irk_count; i++) { 7586 struct mgmt_irk_info *irk = &cp->irks[i]; 7587 7588 if (hci_is_blocked_key(hdev, 7589 HCI_BLOCKED_KEY_TYPE_IRK, 7590 irk->val)) { 7591 bt_dev_warn(hdev, "Skipping blocked IRK for %pMR", 7592 &irk->addr.bdaddr); 7593 continue; 7594 } 7595 7596 hci_add_irk(hdev, &irk->addr.bdaddr, 7597 le_addr_type(irk->addr.type), irk->val, 7598 BDADDR_ANY); 7599 } 7600 7601 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); 7602 7603 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); 7604 7605 hci_dev_unlock(hdev); 7606 7607 return err; 7608 } 7609 7610 static bool ltk_is_valid(struct mgmt_ltk_info *key) 7611 { 7612 if (key->initiator != 0x00 && key->initiator != 0x01) 7613 return false; 7614 7615 if (key->enc_size > sizeof(key->val)) 7616 return false; 7617 7618 switch (key->addr.type) { 7619 case BDADDR_LE_PUBLIC: 7620 return true; 7621 7622 case BDADDR_LE_RANDOM: 7623 /* Two most significant bits shall be set */ 7624 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) 7625 return false; 7626 return true; 7627 } 7628 7629 return false; 7630 } 7631 7632 static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev, 7633 void *cp_data, u16 len) 7634 { 7635 struct mgmt_cp_load_long_term_keys *cp = cp_data; 7636 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / 7637 sizeof(struct mgmt_ltk_info)); 7638 u16 key_count, expected_len; 7639 int i, err; 7640 7641 bt_dev_dbg(hdev, "sock %p", sk); 7642 7643 if (!lmp_le_capable(hdev)) 7644 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 7645 MGMT_STATUS_NOT_SUPPORTED); 7646 7647 key_count = __le16_to_cpu(cp->key_count); 7648 if (key_count > max_key_count) { 7649 bt_dev_err(hdev, "load_ltks: too big key_count value %u", 7650 key_count); 7651 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 7652 MGMT_STATUS_INVALID_PARAMS); 7653 } 7654 7655 expected_len = struct_size(cp, keys, key_count); 7656 if (expected_len != len) { 7657 bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes", 7658 expected_len, len); 7659 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 7660 MGMT_STATUS_INVALID_PARAMS); 7661 } 7662 7663 bt_dev_dbg(hdev, "key_count %u", key_count); 7664 7665 hci_dev_lock(hdev); 7666 7667 hci_smp_ltks_clear(hdev); 7668 7669 for (i = 0; i < key_count; i++) { 7670 struct mgmt_ltk_info *key = &cp->keys[i]; 7671 u8 type, authenticated; 7672 7673 if (hci_is_blocked_key(hdev, 7674 HCI_BLOCKED_KEY_TYPE_LTK, 7675 key->val)) { 7676 bt_dev_warn(hdev, "Skipping blocked LTK for %pMR", 7677 &key->addr.bdaddr); 7678 continue; 7679 } 7680 7681 if (!ltk_is_valid(key)) { 7682 bt_dev_warn(hdev, "Invalid LTK for %pMR", 7683 &key->addr.bdaddr); 7684 continue; 7685 } 7686 7687 switch (key->type) { 7688 case MGMT_LTK_UNAUTHENTICATED: 7689 authenticated = 0x00; 7690 type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER; 7691 break; 7692 case MGMT_LTK_AUTHENTICATED: 7693 authenticated = 0x01; 7694 type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER; 7695 break; 7696 case MGMT_LTK_P256_UNAUTH: 7697 authenticated = 0x00; 7698 type = SMP_LTK_P256; 7699 break; 7700 case MGMT_LTK_P256_AUTH: 7701 authenticated = 0x01; 7702 type = SMP_LTK_P256; 7703 break; 7704 case MGMT_LTK_P256_DEBUG: 7705 authenticated = 0x00; 7706 type = SMP_LTK_P256_DEBUG; 7707 fallthrough; 7708 default: 7709 continue; 7710 } 7711 7712 hci_add_ltk(hdev, &key->addr.bdaddr, 7713 le_addr_type(key->addr.type), type, authenticated, 7714 key->val, key->enc_size, key->ediv, key->rand); 7715 } 7716 7717 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, 7718 NULL, 0); 7719 7720 hci_dev_unlock(hdev); 7721 7722 return err; 7723 } 7724 7725 static void get_conn_info_complete(struct hci_dev *hdev, void *data, int err) 7726 { 7727 struct mgmt_pending_cmd *cmd = data; 7728 struct hci_conn *conn = cmd->user_data; 7729 struct mgmt_cp_get_conn_info *cp = cmd->param; 7730 struct mgmt_rp_get_conn_info rp; 7731 u8 status; 7732 7733 bt_dev_dbg(hdev, "err %d", err); 7734 7735 memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); 7736 7737 status = mgmt_status(err); 7738 if (status == MGMT_STATUS_SUCCESS) { 7739 rp.rssi = conn->rssi; 7740 rp.tx_power = conn->tx_power; 7741 rp.max_tx_power = conn->max_tx_power; 7742 } else { 7743 rp.rssi = HCI_RSSI_INVALID; 7744 rp.tx_power = HCI_TX_POWER_INVALID; 7745 rp.max_tx_power = HCI_TX_POWER_INVALID; 7746 } 7747 7748 if (conn) 7749 hci_conn_put(conn); 7750 7751 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, MGMT_OP_GET_CONN_INFO, status, 7752 &rp, sizeof(rp)); 7753 7754 mgmt_pending_free(cmd); 7755 } 7756 7757 static int get_conn_info_sync(struct hci_dev *hdev, void *data) 7758 { 7759 struct mgmt_pending_cmd *cmd = data; 7760 struct mgmt_cp_get_conn_info *cp = cmd->param; 7761 struct hci_conn *conn; 7762 int err; 7763 __le16 handle; 7764 7765 hci_dev_lock(hdev); 7766 7767 /* Make sure we are still connected */ 7768 if (cp->addr.type == BDADDR_BREDR) 7769 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, 7770 &cp->addr.bdaddr); 7771 else 7772 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); 7773 7774 if (!conn || conn->state != BT_CONNECTED) { 7775 hci_dev_unlock(hdev); 7776 return MGMT_STATUS_NOT_CONNECTED; 7777 } 7778 7779 cmd->user_data = hci_conn_get(conn); 7780 handle = cpu_to_le16(conn->handle); 7781 7782 hci_dev_unlock(hdev); 7783 7784 /* Refresh RSSI each time */ 7785 err = hci_read_rssi_sync(hdev, handle); 7786 7787 /* For LE links TX power does not change thus we don't need to 7788 * query for it once value is known. 7789 */ 7790 if (!err && (!bdaddr_type_is_le(cp->addr.type) || 7791 conn->tx_power == HCI_TX_POWER_INVALID)) 7792 err = hci_read_tx_power_sync(hdev, handle, 0x00); 7793 7794 /* Max TX power needs to be read only once per connection */ 7795 if (!err && conn->max_tx_power == HCI_TX_POWER_INVALID) 7796 err = hci_read_tx_power_sync(hdev, handle, 0x01); 7797 7798 return err; 7799 } 7800 7801 static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, 7802 u16 len) 7803 { 7804 struct mgmt_cp_get_conn_info *cp = data; 7805 struct mgmt_rp_get_conn_info rp; 7806 struct hci_conn *conn; 7807 unsigned long conn_info_age; 7808 int err = 0; 7809 7810 bt_dev_dbg(hdev, "sock %p", sk); 7811 7812 memset(&rp, 0, sizeof(rp)); 7813 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); 7814 rp.addr.type = cp->addr.type; 7815 7816 if (!bdaddr_type_is_valid(cp->addr.type)) 7817 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 7818 MGMT_STATUS_INVALID_PARAMS, 7819 &rp, sizeof(rp)); 7820 7821 hci_dev_lock(hdev); 7822 7823 if (!hdev_is_powered(hdev)) { 7824 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 7825 MGMT_STATUS_NOT_POWERED, &rp, 7826 sizeof(rp)); 7827 goto unlock; 7828 } 7829 7830 if (cp->addr.type == BDADDR_BREDR) 7831 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, 7832 &cp->addr.bdaddr); 7833 else 7834 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); 7835 7836 if (!conn || conn->state != BT_CONNECTED) { 7837 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 7838 MGMT_STATUS_NOT_CONNECTED, &rp, 7839 sizeof(rp)); 7840 goto unlock; 7841 } 7842 7843 /* To avoid client trying to guess when to poll again for information we 7844 * calculate conn info age as random value between min/max set in hdev. 7845 */ 7846 conn_info_age = get_random_u32_inclusive(hdev->conn_info_min_age, 7847 hdev->conn_info_max_age - 1); 7848 7849 /* Query controller to refresh cached values if they are too old or were 7850 * never read. 7851 */ 7852 if (time_after(jiffies, conn->conn_info_timestamp + 7853 msecs_to_jiffies(conn_info_age)) || 7854 !conn->conn_info_timestamp) { 7855 struct mgmt_pending_cmd *cmd; 7856 7857 cmd = mgmt_pending_new(sk, MGMT_OP_GET_CONN_INFO, hdev, data, 7858 len); 7859 if (!cmd) { 7860 err = -ENOMEM; 7861 } else { 7862 err = hci_cmd_sync_queue(hdev, get_conn_info_sync, 7863 cmd, get_conn_info_complete); 7864 } 7865 7866 if (err < 0) { 7867 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 7868 MGMT_STATUS_FAILED, &rp, sizeof(rp)); 7869 7870 if (cmd) 7871 mgmt_pending_free(cmd); 7872 7873 goto unlock; 7874 } 7875 7876 conn->conn_info_timestamp = jiffies; 7877 } else { 7878 /* Cache is valid, just reply with values cached in hci_conn */ 7879 rp.rssi = conn->rssi; 7880 rp.tx_power = conn->tx_power; 7881 rp.max_tx_power = conn->max_tx_power; 7882 7883 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 7884 MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); 7885 } 7886 7887 unlock: 7888 hci_dev_unlock(hdev); 7889 return err; 7890 } 7891 7892 static void get_clock_info_complete(struct hci_dev *hdev, void *data, int err) 7893 { 7894 struct mgmt_pending_cmd *cmd = data; 7895 struct mgmt_cp_get_clock_info *cp = cmd->param; 7896 struct mgmt_rp_get_clock_info rp; 7897 struct hci_conn *conn = cmd->user_data; 7898 u8 status = mgmt_status(err); 7899 7900 bt_dev_dbg(hdev, "err %d", err); 7901 7902 memset(&rp, 0, sizeof(rp)); 7903 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); 7904 rp.addr.type = cp->addr.type; 7905 7906 if (err) 7907 goto complete; 7908 7909 rp.local_clock = cpu_to_le32(hdev->clock); 7910 7911 if (conn) { 7912 rp.piconet_clock = cpu_to_le32(conn->clock); 7913 rp.accuracy = cpu_to_le16(conn->clock_accuracy); 7914 } 7915 7916 complete: 7917 if (conn) 7918 hci_conn_put(conn); 7919 7920 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, status, &rp, 7921 sizeof(rp)); 7922 7923 mgmt_pending_free(cmd); 7924 } 7925 7926 static int get_clock_info_sync(struct hci_dev *hdev, void *data) 7927 { 7928 struct mgmt_pending_cmd *cmd = data; 7929 struct mgmt_cp_get_clock_info *cp = cmd->param; 7930 struct hci_cp_read_clock hci_cp; 7931 struct hci_conn *conn; 7932 7933 memset(&hci_cp, 0, sizeof(hci_cp)); 7934 hci_read_clock_sync(hdev, &hci_cp); 7935 7936 hci_dev_lock(hdev); 7937 7938 /* Make sure connection still exists */ 7939 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); 7940 if (!conn || conn->state != BT_CONNECTED) { 7941 hci_dev_unlock(hdev); 7942 return MGMT_STATUS_NOT_CONNECTED; 7943 } 7944 7945 cmd->user_data = hci_conn_get(conn); 7946 hci_cp.handle = cpu_to_le16(conn->handle); 7947 hci_cp.which = 0x01; /* Piconet clock */ 7948 7949 hci_dev_unlock(hdev); 7950 7951 return hci_read_clock_sync(hdev, &hci_cp); 7952 } 7953 7954 static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, 7955 u16 len) 7956 { 7957 struct mgmt_cp_get_clock_info *cp = data; 7958 struct mgmt_rp_get_clock_info rp; 7959 struct mgmt_pending_cmd *cmd; 7960 struct hci_conn *conn; 7961 int err; 7962 7963 bt_dev_dbg(hdev, "sock %p", sk); 7964 7965 memset(&rp, 0, sizeof(rp)); 7966 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); 7967 rp.addr.type = cp->addr.type; 7968 7969 if (cp->addr.type != BDADDR_BREDR) 7970 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, 7971 MGMT_STATUS_INVALID_PARAMS, 7972 &rp, sizeof(rp)); 7973 7974 hci_dev_lock(hdev); 7975 7976 if (!hdev_is_powered(hdev)) { 7977 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, 7978 MGMT_STATUS_NOT_POWERED, &rp, 7979 sizeof(rp)); 7980 goto unlock; 7981 } 7982 7983 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { 7984 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, 7985 &cp->addr.bdaddr); 7986 if (!conn || conn->state != BT_CONNECTED) { 7987 err = mgmt_cmd_complete(sk, hdev->id, 7988 MGMT_OP_GET_CLOCK_INFO, 7989 MGMT_STATUS_NOT_CONNECTED, 7990 &rp, sizeof(rp)); 7991 goto unlock; 7992 } 7993 } else { 7994 conn = NULL; 7995 } 7996 7997 cmd = mgmt_pending_new(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); 7998 if (!cmd) 7999 err = -ENOMEM; 8000 else 8001 err = hci_cmd_sync_queue(hdev, get_clock_info_sync, cmd, 8002 get_clock_info_complete); 8003 8004 if (err < 0) { 8005 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, 8006 MGMT_STATUS_FAILED, &rp, sizeof(rp)); 8007 8008 if (cmd) 8009 mgmt_pending_free(cmd); 8010 } 8011 8012 8013 unlock: 8014 hci_dev_unlock(hdev); 8015 return err; 8016 } 8017 8018 static void device_added(struct sock *sk, struct hci_dev *hdev, 8019 bdaddr_t *bdaddr, u8 type, u8 action) 8020 { 8021 struct mgmt_ev_device_added ev; 8022 8023 bacpy(&ev.addr.bdaddr, bdaddr); 8024 ev.addr.type = type; 8025 ev.action = action; 8026 8027 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); 8028 } 8029 8030 static void add_device_complete(struct hci_dev *hdev, void *data, int err) 8031 { 8032 struct mgmt_pending_cmd *cmd = data; 8033 struct mgmt_cp_add_device *cp = cmd->param; 8034 8035 if (!err) { 8036 struct hci_conn_params *params; 8037 8038 hci_dev_lock(hdev); 8039 8040 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, 8041 le_addr_type(cp->addr.type)); 8042 8043 device_added(cmd->sk, hdev, &cp->addr.bdaddr, cp->addr.type, 8044 cp->action); 8045 device_flags_changed(NULL, hdev, &cp->addr.bdaddr, 8046 cp->addr.type, hdev->conn_flags, 8047 params ? params->flags : 0); 8048 hci_dev_unlock(hdev); 8049 } 8050 8051 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_ADD_DEVICE, 8052 mgmt_status(err), &cp->addr, sizeof(cp->addr)); 8053 mgmt_pending_free(cmd); 8054 } 8055 8056 static int add_device_sync(struct hci_dev *hdev, void *data) 8057 { 8058 return hci_update_passive_scan_sync(hdev); 8059 } 8060 8061 static int add_device(struct sock *sk, struct hci_dev *hdev, 8062 void *data, u16 len) 8063 { 8064 struct mgmt_pending_cmd *cmd; 8065 struct mgmt_cp_add_device *cp = data; 8066 u8 auto_conn, addr_type; 8067 struct hci_conn_params *params; 8068 int err; 8069 u32 current_flags = 0; 8070 u32 supported_flags; 8071 8072 bt_dev_dbg(hdev, "sock %p", sk); 8073 8074 if (!bdaddr_type_is_valid(cp->addr.type) || 8075 !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) 8076 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, 8077 MGMT_STATUS_INVALID_PARAMS, 8078 &cp->addr, sizeof(cp->addr)); 8079 8080 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) 8081 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, 8082 MGMT_STATUS_INVALID_PARAMS, 8083 &cp->addr, sizeof(cp->addr)); 8084 8085 hci_dev_lock(hdev); 8086 8087 if (cp->addr.type == BDADDR_BREDR) { 8088 /* Only incoming connections action is supported for now */ 8089 if (cp->action != 0x01) { 8090 err = mgmt_cmd_complete(sk, hdev->id, 8091 MGMT_OP_ADD_DEVICE, 8092 MGMT_STATUS_INVALID_PARAMS, 8093 &cp->addr, sizeof(cp->addr)); 8094 goto unlock; 8095 } 8096 8097 err = hci_bdaddr_list_add_with_flags(&hdev->accept_list, 8098 &cp->addr.bdaddr, 8099 cp->addr.type, 0); 8100 if (err) 8101 goto unlock; 8102 8103 hci_update_scan(hdev); 8104 8105 goto added; 8106 } 8107 8108 addr_type = le_addr_type(cp->addr.type); 8109 8110 if (cp->action == 0x02) 8111 auto_conn = HCI_AUTO_CONN_ALWAYS; 8112 else if (cp->action == 0x01) 8113 auto_conn = HCI_AUTO_CONN_DIRECT; 8114 else 8115 auto_conn = HCI_AUTO_CONN_REPORT; 8116 8117 /* Kernel internally uses conn_params with resolvable private 8118 * address, but Add Device allows only identity addresses. 8119 * Make sure it is enforced before calling 8120 * hci_conn_params_lookup. 8121 */ 8122 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { 8123 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, 8124 MGMT_STATUS_INVALID_PARAMS, 8125 &cp->addr, sizeof(cp->addr)); 8126 goto unlock; 8127 } 8128 8129 /* If the connection parameters don't exist for this device, 8130 * they will be created and configured with defaults. 8131 */ 8132 params = hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, 8133 auto_conn); 8134 if (!params) { 8135 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, 8136 MGMT_STATUS_FAILED, &cp->addr, 8137 sizeof(cp->addr)); 8138 goto unlock; 8139 } 8140 8141 cmd = mgmt_pending_new(sk, MGMT_OP_ADD_DEVICE, hdev, data, len); 8142 if (!cmd) { 8143 err = -ENOMEM; 8144 goto unlock; 8145 } 8146 8147 err = hci_cmd_sync_queue(hdev, add_device_sync, cmd, 8148 add_device_complete); 8149 if (err < 0) { 8150 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, 8151 MGMT_STATUS_FAILED, &cp->addr, 8152 sizeof(cp->addr)); 8153 mgmt_pending_free(cmd); 8154 } 8155 8156 goto unlock; 8157 8158 added: 8159 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); 8160 supported_flags = hdev->conn_flags; 8161 device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type, 8162 supported_flags, current_flags); 8163 8164 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, 8165 MGMT_STATUS_SUCCESS, &cp->addr, 8166 sizeof(cp->addr)); 8167 8168 unlock: 8169 hci_dev_unlock(hdev); 8170 return err; 8171 } 8172 8173 static void device_removed(struct sock *sk, struct hci_dev *hdev, 8174 bdaddr_t *bdaddr, u8 type) 8175 { 8176 struct mgmt_ev_device_removed ev; 8177 8178 bacpy(&ev.addr.bdaddr, bdaddr); 8179 ev.addr.type = type; 8180 8181 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); 8182 } 8183 8184 static int remove_device_sync(struct hci_dev *hdev, void *data) 8185 { 8186 return hci_update_passive_scan_sync(hdev); 8187 } 8188 8189 static int remove_device(struct sock *sk, struct hci_dev *hdev, 8190 void *data, u16 len) 8191 { 8192 struct mgmt_cp_remove_device *cp = data; 8193 int err; 8194 8195 bt_dev_dbg(hdev, "sock %p", sk); 8196 8197 hci_dev_lock(hdev); 8198 8199 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { 8200 struct hci_conn_params *params; 8201 u8 addr_type; 8202 8203 if (!bdaddr_type_is_valid(cp->addr.type)) { 8204 err = mgmt_cmd_complete(sk, hdev->id, 8205 MGMT_OP_REMOVE_DEVICE, 8206 MGMT_STATUS_INVALID_PARAMS, 8207 &cp->addr, sizeof(cp->addr)); 8208 goto unlock; 8209 } 8210 8211 if (cp->addr.type == BDADDR_BREDR) { 8212 err = hci_bdaddr_list_del(&hdev->accept_list, 8213 &cp->addr.bdaddr, 8214 cp->addr.type); 8215 if (err) { 8216 err = mgmt_cmd_complete(sk, hdev->id, 8217 MGMT_OP_REMOVE_DEVICE, 8218 MGMT_STATUS_INVALID_PARAMS, 8219 &cp->addr, 8220 sizeof(cp->addr)); 8221 goto unlock; 8222 } 8223 8224 hci_update_scan(hdev); 8225 8226 device_removed(sk, hdev, &cp->addr.bdaddr, 8227 cp->addr.type); 8228 goto complete; 8229 } 8230 8231 addr_type = le_addr_type(cp->addr.type); 8232 8233 /* Kernel internally uses conn_params with resolvable private 8234 * address, but Remove Device allows only identity addresses. 8235 * Make sure it is enforced before calling 8236 * hci_conn_params_lookup. 8237 */ 8238 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) { 8239 err = mgmt_cmd_complete(sk, hdev->id, 8240 MGMT_OP_REMOVE_DEVICE, 8241 MGMT_STATUS_INVALID_PARAMS, 8242 &cp->addr, sizeof(cp->addr)); 8243 goto unlock; 8244 } 8245 8246 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, 8247 addr_type); 8248 if (!params) { 8249 err = mgmt_cmd_complete(sk, hdev->id, 8250 MGMT_OP_REMOVE_DEVICE, 8251 MGMT_STATUS_INVALID_PARAMS, 8252 &cp->addr, sizeof(cp->addr)); 8253 goto unlock; 8254 } 8255 8256 if (params->auto_connect == HCI_AUTO_CONN_DISABLED || 8257 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) { 8258 err = mgmt_cmd_complete(sk, hdev->id, 8259 MGMT_OP_REMOVE_DEVICE, 8260 MGMT_STATUS_INVALID_PARAMS, 8261 &cp->addr, sizeof(cp->addr)); 8262 goto unlock; 8263 } 8264 8265 hci_conn_params_free(params); 8266 8267 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); 8268 } else { 8269 struct hci_conn_params *p, *tmp; 8270 struct bdaddr_list *b, *btmp; 8271 8272 if (cp->addr.type) { 8273 err = mgmt_cmd_complete(sk, hdev->id, 8274 MGMT_OP_REMOVE_DEVICE, 8275 MGMT_STATUS_INVALID_PARAMS, 8276 &cp->addr, sizeof(cp->addr)); 8277 goto unlock; 8278 } 8279 8280 list_for_each_entry_safe(b, btmp, &hdev->accept_list, list) { 8281 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); 8282 list_del(&b->list); 8283 kfree(b); 8284 } 8285 8286 hci_update_scan(hdev); 8287 8288 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { 8289 if (p->auto_connect == HCI_AUTO_CONN_DISABLED) 8290 continue; 8291 device_removed(sk, hdev, &p->addr, p->addr_type); 8292 if (p->explicit_connect) { 8293 p->auto_connect = HCI_AUTO_CONN_EXPLICIT; 8294 continue; 8295 } 8296 hci_conn_params_free(p); 8297 } 8298 8299 bt_dev_dbg(hdev, "All LE connection parameters were removed"); 8300 } 8301 8302 hci_cmd_sync_queue(hdev, remove_device_sync, NULL, NULL); 8303 8304 complete: 8305 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, 8306 MGMT_STATUS_SUCCESS, &cp->addr, 8307 sizeof(cp->addr)); 8308 unlock: 8309 hci_dev_unlock(hdev); 8310 return err; 8311 } 8312 8313 static int conn_update_sync(struct hci_dev *hdev, void *data) 8314 { 8315 struct hci_conn *conn = data; 8316 struct hci_conn_params *params; 8317 struct hci_conn_params local = {}; 8318 8319 hci_dev_lock(hdev); 8320 8321 if (!hci_conn_valid(hdev, conn) || conn->role != HCI_ROLE_MASTER) 8322 goto cancel; 8323 8324 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type); 8325 if (!params) 8326 goto cancel; 8327 8328 local.conn_min_interval = params->conn_min_interval; 8329 local.conn_max_interval = params->conn_max_interval; 8330 local.conn_latency = params->conn_latency; 8331 local.supervision_timeout = params->supervision_timeout; 8332 8333 hci_dev_unlock(hdev); 8334 8335 return hci_le_conn_update_sync(hdev, conn, &local); 8336 8337 cancel: 8338 hci_dev_unlock(hdev); 8339 return -ECANCELED; 8340 } 8341 8342 static void conn_update_sync_destroy(struct hci_dev *hdev, void *data, int err) 8343 { 8344 hci_conn_put(data); 8345 } 8346 8347 static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, 8348 u16 len) 8349 { 8350 struct mgmt_cp_load_conn_param *cp = data; 8351 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / 8352 sizeof(struct mgmt_conn_param)); 8353 u16 param_count, expected_len; 8354 int i; 8355 8356 if (!lmp_le_capable(hdev)) 8357 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 8358 MGMT_STATUS_NOT_SUPPORTED); 8359 8360 param_count = __le16_to_cpu(cp->param_count); 8361 if (param_count > max_param_count) { 8362 bt_dev_err(hdev, "load_conn_param: too big param_count value %u", 8363 param_count); 8364 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 8365 MGMT_STATUS_INVALID_PARAMS); 8366 } 8367 8368 expected_len = struct_size(cp, params, param_count); 8369 if (expected_len != len) { 8370 bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes", 8371 expected_len, len); 8372 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 8373 MGMT_STATUS_INVALID_PARAMS); 8374 } 8375 8376 bt_dev_dbg(hdev, "param_count %u", param_count); 8377 8378 hci_dev_lock(hdev); 8379 8380 /* A single entry may be an update for one device, so keep the other 8381 * disabled parameters then, but clear them otherwise, including when 8382 * no entries are loaded. 8383 */ 8384 if (param_count != 1) 8385 hci_conn_params_clear_disabled(hdev); 8386 8387 for (i = 0; i < param_count; i++) { 8388 struct mgmt_conn_param *param = &cp->params[i]; 8389 struct hci_conn_params *hci_param; 8390 u16 min, max, latency, timeout; 8391 bool update = false; 8392 u8 addr_type; 8393 8394 bt_dev_dbg(hdev, "Adding %pMR (type %u)", ¶m->addr.bdaddr, 8395 param->addr.type); 8396 8397 if (param->addr.type == BDADDR_LE_PUBLIC) { 8398 addr_type = ADDR_LE_DEV_PUBLIC; 8399 } else if (param->addr.type == BDADDR_LE_RANDOM) { 8400 addr_type = ADDR_LE_DEV_RANDOM; 8401 } else { 8402 bt_dev_err(hdev, "ignoring invalid connection parameters"); 8403 continue; 8404 } 8405 8406 min = le16_to_cpu(param->min_interval); 8407 max = le16_to_cpu(param->max_interval); 8408 latency = le16_to_cpu(param->latency); 8409 timeout = le16_to_cpu(param->timeout); 8410 8411 bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", 8412 min, max, latency, timeout); 8413 8414 if (hci_check_conn_params(min, max, latency, timeout) < 0) { 8415 bt_dev_err(hdev, "ignoring invalid connection parameters"); 8416 continue; 8417 } 8418 8419 /* Detect when the loading is for an existing parameter then 8420 * attempt to trigger the connection update procedure. 8421 */ 8422 if (!i && param_count == 1) { 8423 hci_param = hci_conn_params_lookup(hdev, 8424 ¶m->addr.bdaddr, 8425 addr_type); 8426 if (hci_param) 8427 update = true; 8428 else 8429 hci_conn_params_clear_disabled(hdev); 8430 } 8431 8432 hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, 8433 addr_type); 8434 if (!hci_param) { 8435 bt_dev_err(hdev, "failed to add connection parameters"); 8436 continue; 8437 } 8438 8439 hci_param->conn_min_interval = min; 8440 hci_param->conn_max_interval = max; 8441 hci_param->conn_latency = latency; 8442 hci_param->supervision_timeout = timeout; 8443 8444 /* Check if we need to trigger a connection update */ 8445 if (update) { 8446 struct hci_conn *conn; 8447 8448 /* Lookup for existing connection as central and check 8449 * if parameters match and if they don't then trigger 8450 * a connection update. 8451 */ 8452 conn = hci_conn_hash_lookup_le(hdev, &hci_param->addr, 8453 addr_type); 8454 if (conn && conn->role == HCI_ROLE_MASTER && 8455 (conn->le_conn_min_interval != min || 8456 conn->le_conn_max_interval != max || 8457 conn->le_conn_latency != latency || 8458 conn->le_supv_timeout != timeout)) { 8459 hci_conn_get(conn); 8460 if (hci_cmd_sync_queue(hdev, conn_update_sync, 8461 conn, 8462 conn_update_sync_destroy) < 0) 8463 hci_conn_put(conn); 8464 } 8465 } 8466 } 8467 8468 hci_dev_unlock(hdev); 8469 8470 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, 8471 NULL, 0); 8472 } 8473 8474 static int load_conn_subrate(struct sock *sk, struct hci_dev *hdev, void *data, 8475 u16 len) 8476 { 8477 struct mgmt_cp_load_conn_subrate *cp = data; 8478 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / 8479 sizeof(struct mgmt_conn_subrate)); 8480 u16 param_count, expected_len; 8481 int i; 8482 8483 if (!lmp_le_capable(hdev) || !le_sci_capable(hdev)) 8484 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_SUBRATE, 8485 MGMT_STATUS_NOT_SUPPORTED); 8486 8487 param_count = __le16_to_cpu(cp->param_count); 8488 if (param_count > max_param_count) { 8489 bt_dev_err(hdev, "too big param_count value %u", param_count); 8490 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_SUBRATE, 8491 MGMT_STATUS_INVALID_PARAMS); 8492 } 8493 8494 expected_len = struct_size(cp, params, param_count); 8495 if (expected_len != len) { 8496 bt_dev_err(hdev, "expected %u bytes, got %u bytes", 8497 expected_len, len); 8498 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_SUBRATE, 8499 MGMT_STATUS_INVALID_PARAMS); 8500 } 8501 8502 bt_dev_dbg(hdev, "param_count %u", param_count); 8503 8504 hci_dev_lock(hdev); 8505 8506 for (i = 0; i < param_count; i++) { 8507 struct mgmt_conn_subrate *param = &cp->params[i]; 8508 struct hci_conn_params *hci_param; 8509 u16 min, max, subrate_min, subrate_max; 8510 u16 max_latency, cont_num, supv_timeout; 8511 u8 addr_type; 8512 8513 bt_dev_dbg(hdev, "Adding subrate %pMR (type %u)", 8514 ¶m->addr.bdaddr, param->addr.type); 8515 8516 if (param->addr.type == BDADDR_LE_PUBLIC) { 8517 addr_type = ADDR_LE_DEV_PUBLIC; 8518 } else if (param->addr.type == BDADDR_LE_RANDOM) { 8519 addr_type = ADDR_LE_DEV_RANDOM; 8520 } else { 8521 bt_dev_err(hdev, "ignoring invalid connection subrate parameters"); 8522 continue; 8523 } 8524 8525 min = le16_to_cpu(param->min_interval); 8526 max = le16_to_cpu(param->max_interval); 8527 subrate_min = le16_to_cpu(param->subrate_min); 8528 subrate_max = le16_to_cpu(param->subrate_max); 8529 max_latency = le16_to_cpu(param->max_latency); 8530 cont_num = le16_to_cpu(param->cont_num); 8531 supv_timeout = le16_to_cpu(param->supv_timeout); 8532 8533 /* Validate the parameters before storing them. Reject 8534 * logically inconsistent values instead of forwarding them to 8535 * the controller. 8536 */ 8537 if (min > max || subrate_min > subrate_max || 8538 subrate_min < 1 || supv_timeout < 1) { 8539 bt_dev_err(hdev, "ignoring invalid connection subrate parameters"); 8540 continue; 8541 } 8542 8543 hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, 8544 addr_type); 8545 if (!hci_param) { 8546 bt_dev_err(hdev, "failed to add connection parameters"); 8547 continue; 8548 } 8549 8550 hci_param->rate_min_interval = min; 8551 hci_param->rate_max_interval = max; 8552 hci_param->subrate_min = subrate_min; 8553 hci_param->subrate_max = subrate_max; 8554 hci_param->max_latency = max_latency; 8555 hci_param->cont_num = cont_num; 8556 hci_param->rate_supv_timeout = supv_timeout; 8557 8558 /* If the device is connected check if the connection rate 8559 * parameters need to be updated, either role can initiate the 8560 * Connection Rate Update procedure, e.g. a HID device acting 8561 * as peripheral once the HID host requests a HID SCI mode. 8562 */ 8563 if (!i && param_count == 1) { 8564 struct hci_conn *conn; 8565 8566 conn = hci_conn_hash_lookup_le(hdev, 8567 &hci_param->addr, 8568 addr_type); 8569 if (conn && conn->state == BT_CONNECTED && 8570 (conn->le_rate_interval < min || 8571 conn->le_rate_interval > max || 8572 conn->le_subrate < subrate_min || 8573 conn->le_subrate > subrate_max || 8574 conn->le_rate_latency != max_latency || 8575 conn->le_cont_num != cont_num || 8576 conn->le_rate_supv_timeout != supv_timeout)) 8577 hci_le_conn_rate_request(hdev, conn); 8578 } 8579 } 8580 8581 hci_dev_unlock(hdev); 8582 8583 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_SUBRATE, 0, 8584 NULL, 0); 8585 } 8586 8587 static int set_external_config(struct sock *sk, struct hci_dev *hdev, 8588 void *data, u16 len) 8589 { 8590 struct mgmt_cp_set_external_config *cp = data; 8591 bool changed; 8592 int err; 8593 8594 bt_dev_dbg(hdev, "sock %p", sk); 8595 8596 if (hdev_is_powered(hdev)) 8597 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, 8598 MGMT_STATUS_REJECTED); 8599 8600 if (cp->config != 0x00 && cp->config != 0x01) 8601 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, 8602 MGMT_STATUS_INVALID_PARAMS); 8603 8604 if (!hci_test_quirk(hdev, HCI_QUIRK_EXTERNAL_CONFIG)) 8605 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, 8606 MGMT_STATUS_NOT_SUPPORTED); 8607 8608 hci_dev_lock(hdev); 8609 8610 if (cp->config) 8611 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); 8612 else 8613 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); 8614 8615 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); 8616 if (err < 0) 8617 goto unlock; 8618 8619 if (!changed) 8620 goto unlock; 8621 8622 err = new_options(hdev, sk); 8623 8624 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { 8625 mgmt_index_removed(hdev); 8626 8627 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { 8628 hci_dev_set_flag(hdev, HCI_CONFIG); 8629 hci_dev_set_flag(hdev, HCI_AUTO_OFF); 8630 8631 queue_work(hdev->req_workqueue, &hdev->power_on); 8632 } else { 8633 set_bit(HCI_RAW, &hdev->flags); 8634 mgmt_index_added(hdev); 8635 } 8636 } 8637 8638 unlock: 8639 hci_dev_unlock(hdev); 8640 return err; 8641 } 8642 8643 static int set_public_address(struct sock *sk, struct hci_dev *hdev, 8644 void *data, u16 len) 8645 { 8646 struct mgmt_cp_set_public_address *cp = data; 8647 bool changed; 8648 int err; 8649 8650 bt_dev_dbg(hdev, "sock %p", sk); 8651 8652 if (hdev_is_powered(hdev)) 8653 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, 8654 MGMT_STATUS_REJECTED); 8655 8656 if (!bacmp(&cp->bdaddr, BDADDR_ANY)) 8657 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, 8658 MGMT_STATUS_INVALID_PARAMS); 8659 8660 if (!hdev->set_bdaddr) 8661 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, 8662 MGMT_STATUS_NOT_SUPPORTED); 8663 8664 hci_dev_lock(hdev); 8665 8666 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); 8667 bacpy(&hdev->public_addr, &cp->bdaddr); 8668 8669 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); 8670 if (err < 0) 8671 goto unlock; 8672 8673 if (!changed) 8674 goto unlock; 8675 8676 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) 8677 err = new_options(hdev, sk); 8678 8679 if (is_configured(hdev)) { 8680 mgmt_index_removed(hdev); 8681 8682 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); 8683 8684 hci_dev_set_flag(hdev, HCI_CONFIG); 8685 hci_dev_set_flag(hdev, HCI_AUTO_OFF); 8686 8687 queue_work(hdev->req_workqueue, &hdev->power_on); 8688 } 8689 8690 unlock: 8691 hci_dev_unlock(hdev); 8692 return err; 8693 } 8694 8695 static void read_local_oob_ext_data_complete(struct hci_dev *hdev, void *data, 8696 int err) 8697 { 8698 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp; 8699 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp; 8700 u8 *h192, *r192, *h256, *r256; 8701 struct mgmt_pending_cmd *cmd = data; 8702 struct sk_buff *skb = cmd->skb; 8703 u8 status = mgmt_status(err); 8704 u16 eir_len; 8705 8706 if (!status) { 8707 if (!skb) 8708 status = MGMT_STATUS_FAILED; 8709 else if (IS_ERR(skb)) 8710 status = mgmt_status(PTR_ERR(skb)); 8711 else 8712 status = mgmt_status(skb->data[0]); 8713 } 8714 8715 bt_dev_dbg(hdev, "status %u", status); 8716 8717 mgmt_cp = cmd->param; 8718 8719 if (status) { 8720 status = mgmt_status(status); 8721 eir_len = 0; 8722 8723 h192 = NULL; 8724 r192 = NULL; 8725 h256 = NULL; 8726 r256 = NULL; 8727 } else if (!bredr_sc_enabled(hdev)) { 8728 struct hci_rp_read_local_oob_data *rp; 8729 8730 if (skb->len != sizeof(*rp)) { 8731 status = MGMT_STATUS_FAILED; 8732 eir_len = 0; 8733 } else { 8734 status = MGMT_STATUS_SUCCESS; 8735 rp = (void *)skb->data; 8736 8737 eir_len = 5 + 18 + 18; 8738 h192 = rp->hash; 8739 r192 = rp->rand; 8740 h256 = NULL; 8741 r256 = NULL; 8742 } 8743 } else { 8744 struct hci_rp_read_local_oob_ext_data *rp; 8745 8746 if (skb->len != sizeof(*rp)) { 8747 status = MGMT_STATUS_FAILED; 8748 eir_len = 0; 8749 } else { 8750 status = MGMT_STATUS_SUCCESS; 8751 rp = (void *)skb->data; 8752 8753 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) { 8754 eir_len = 5 + 18 + 18; 8755 h192 = NULL; 8756 r192 = NULL; 8757 } else { 8758 eir_len = 5 + 18 + 18 + 18 + 18; 8759 h192 = rp->hash192; 8760 r192 = rp->rand192; 8761 } 8762 8763 h256 = rp->hash256; 8764 r256 = rp->rand256; 8765 } 8766 } 8767 8768 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL); 8769 if (!mgmt_rp) 8770 goto done; 8771 8772 if (eir_len == 0) 8773 goto send_rsp; 8774 8775 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV, 8776 hdev->dev_class, 3); 8777 8778 if (h192 && r192) { 8779 eir_len = eir_append_data(mgmt_rp->eir, eir_len, 8780 EIR_SSP_HASH_C192, h192, 16); 8781 eir_len = eir_append_data(mgmt_rp->eir, eir_len, 8782 EIR_SSP_RAND_R192, r192, 16); 8783 } 8784 8785 if (h256 && r256) { 8786 eir_len = eir_append_data(mgmt_rp->eir, eir_len, 8787 EIR_SSP_HASH_C256, h256, 16); 8788 eir_len = eir_append_data(mgmt_rp->eir, eir_len, 8789 EIR_SSP_RAND_R256, r256, 16); 8790 } 8791 8792 send_rsp: 8793 mgmt_rp->type = mgmt_cp->type; 8794 mgmt_rp->eir_len = cpu_to_le16(eir_len); 8795 8796 err = mgmt_cmd_complete(cmd->sk, hdev->id, 8797 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status, 8798 mgmt_rp, sizeof(*mgmt_rp) + eir_len); 8799 if (err < 0 || status) 8800 goto done; 8801 8802 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS); 8803 8804 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, 8805 mgmt_rp, sizeof(*mgmt_rp) + eir_len, 8806 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk); 8807 done: 8808 if (skb && !IS_ERR(skb)) 8809 kfree_skb(skb); 8810 8811 kfree(mgmt_rp); 8812 mgmt_pending_free(cmd); 8813 } 8814 8815 static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk, 8816 struct mgmt_cp_read_local_oob_ext_data *cp) 8817 { 8818 struct mgmt_pending_cmd *cmd; 8819 int err; 8820 8821 cmd = mgmt_pending_new(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev, 8822 cp, sizeof(*cp)); 8823 if (!cmd) 8824 return -ENOMEM; 8825 8826 err = hci_cmd_sync_queue(hdev, read_local_oob_data_sync, cmd, 8827 read_local_oob_ext_data_complete); 8828 8829 if (err < 0) { 8830 mgmt_pending_remove(cmd); 8831 return err; 8832 } 8833 8834 return 0; 8835 } 8836 8837 static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev, 8838 void *data, u16 data_len) 8839 { 8840 struct mgmt_cp_read_local_oob_ext_data *cp = data; 8841 struct mgmt_rp_read_local_oob_ext_data *rp; 8842 size_t rp_len; 8843 u16 eir_len; 8844 u8 status, flags, role, addr[7], hash[16], rand[16]; 8845 int err; 8846 8847 bt_dev_dbg(hdev, "sock %p", sk); 8848 8849 if (hdev_is_powered(hdev)) { 8850 switch (cp->type) { 8851 case BIT(BDADDR_BREDR): 8852 status = mgmt_bredr_support(hdev); 8853 if (status) 8854 eir_len = 0; 8855 else 8856 eir_len = 5; 8857 break; 8858 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): 8859 status = mgmt_le_support(hdev); 8860 if (status) 8861 eir_len = 0; 8862 else 8863 eir_len = 9 + 3 + 18 + 18 + 3; 8864 break; 8865 default: 8866 status = MGMT_STATUS_INVALID_PARAMS; 8867 eir_len = 0; 8868 break; 8869 } 8870 } else { 8871 status = MGMT_STATUS_NOT_POWERED; 8872 eir_len = 0; 8873 } 8874 8875 rp_len = sizeof(*rp) + eir_len; 8876 rp = kmalloc(rp_len, GFP_ATOMIC); 8877 if (!rp) 8878 return -ENOMEM; 8879 8880 if (!status && !lmp_ssp_capable(hdev)) { 8881 status = MGMT_STATUS_NOT_SUPPORTED; 8882 eir_len = 0; 8883 } 8884 8885 if (status) 8886 goto complete; 8887 8888 hci_dev_lock(hdev); 8889 8890 eir_len = 0; 8891 switch (cp->type) { 8892 case BIT(BDADDR_BREDR): 8893 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { 8894 err = read_local_ssp_oob_req(hdev, sk, cp); 8895 hci_dev_unlock(hdev); 8896 if (!err) 8897 goto done; 8898 8899 status = MGMT_STATUS_FAILED; 8900 goto complete; 8901 } else { 8902 eir_len = eir_append_data(rp->eir, eir_len, 8903 EIR_CLASS_OF_DEV, 8904 hdev->dev_class, 3); 8905 } 8906 break; 8907 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)): 8908 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) && 8909 smp_generate_oob(hdev, hash, rand) < 0) { 8910 hci_dev_unlock(hdev); 8911 status = MGMT_STATUS_FAILED; 8912 goto complete; 8913 } 8914 8915 /* This should return the active RPA, but since the RPA 8916 * is only programmed on demand, it is really hard to fill 8917 * this in at the moment. For now disallow retrieving 8918 * local out-of-band data when privacy is in use. 8919 * 8920 * Returning the identity address will not help here since 8921 * pairing happens before the identity resolving key is 8922 * known and thus the connection establishment happens 8923 * based on the RPA and not the identity address. 8924 */ 8925 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { 8926 hci_dev_unlock(hdev); 8927 status = MGMT_STATUS_REJECTED; 8928 goto complete; 8929 } 8930 8931 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || 8932 !bacmp(&hdev->bdaddr, BDADDR_ANY) || 8933 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && 8934 bacmp(&hdev->static_addr, BDADDR_ANY))) { 8935 memcpy(addr, &hdev->static_addr, 6); 8936 addr[6] = 0x01; 8937 } else { 8938 memcpy(addr, &hdev->bdaddr, 6); 8939 addr[6] = 0x00; 8940 } 8941 8942 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR, 8943 addr, sizeof(addr)); 8944 8945 if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) 8946 role = 0x02; 8947 else 8948 role = 0x01; 8949 8950 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE, 8951 &role, sizeof(role)); 8952 8953 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) { 8954 eir_len = eir_append_data(rp->eir, eir_len, 8955 EIR_LE_SC_CONFIRM, 8956 hash, sizeof(hash)); 8957 8958 eir_len = eir_append_data(rp->eir, eir_len, 8959 EIR_LE_SC_RANDOM, 8960 rand, sizeof(rand)); 8961 } 8962 8963 flags = mgmt_get_adv_discov_flags(hdev); 8964 8965 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) 8966 flags |= LE_AD_NO_BREDR; 8967 8968 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS, 8969 &flags, sizeof(flags)); 8970 break; 8971 } 8972 8973 hci_dev_unlock(hdev); 8974 8975 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS); 8976 8977 status = MGMT_STATUS_SUCCESS; 8978 8979 complete: 8980 rp->type = cp->type; 8981 rp->eir_len = cpu_to_le16(eir_len); 8982 8983 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, 8984 status, rp, sizeof(*rp) + eir_len); 8985 if (err < 0 || status) 8986 goto done; 8987 8988 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev, 8989 rp, sizeof(*rp) + eir_len, 8990 HCI_MGMT_OOB_DATA_EVENTS, sk); 8991 8992 done: 8993 kfree(rp); 8994 8995 return err; 8996 } 8997 8998 static u32 get_supported_adv_flags(struct hci_dev *hdev) 8999 { 9000 u32 flags = 0; 9001 9002 flags |= MGMT_ADV_FLAG_CONNECTABLE; 9003 flags |= MGMT_ADV_FLAG_DISCOV; 9004 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV; 9005 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS; 9006 flags |= MGMT_ADV_FLAG_APPEARANCE; 9007 flags |= MGMT_ADV_FLAG_LOCAL_NAME; 9008 flags |= MGMT_ADV_PARAM_DURATION; 9009 flags |= MGMT_ADV_PARAM_TIMEOUT; 9010 flags |= MGMT_ADV_PARAM_INTERVALS; 9011 flags |= MGMT_ADV_PARAM_TX_POWER; 9012 flags |= MGMT_ADV_PARAM_SCAN_RSP; 9013 9014 /* In extended adv TX_POWER returned from Set Adv Param 9015 * will be always valid. 9016 */ 9017 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID || ext_adv_capable(hdev)) 9018 flags |= MGMT_ADV_FLAG_TX_POWER; 9019 9020 if (ext_adv_capable(hdev)) { 9021 flags |= MGMT_ADV_FLAG_SEC_1M; 9022 flags |= MGMT_ADV_FLAG_HW_OFFLOAD; 9023 flags |= MGMT_ADV_FLAG_CAN_SET_TX_POWER; 9024 9025 if (le_2m_capable(hdev)) 9026 flags |= MGMT_ADV_FLAG_SEC_2M; 9027 9028 if (le_coded_capable(hdev)) 9029 flags |= MGMT_ADV_FLAG_SEC_CODED; 9030 } 9031 9032 return flags; 9033 } 9034 9035 static int read_adv_features(struct sock *sk, struct hci_dev *hdev, 9036 void *data, u16 data_len) 9037 { 9038 struct mgmt_rp_read_adv_features *rp; 9039 size_t rp_len; 9040 int err; 9041 struct adv_info *adv_instance; 9042 u32 supported_flags; 9043 u8 *instance; 9044 9045 bt_dev_dbg(hdev, "sock %p", sk); 9046 9047 if (!lmp_le_capable(hdev)) 9048 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, 9049 MGMT_STATUS_REJECTED); 9050 9051 hci_dev_lock(hdev); 9052 9053 rp_len = sizeof(*rp) + hdev->adv_instance_cnt; 9054 rp = kmalloc(rp_len, GFP_ATOMIC); 9055 if (!rp) { 9056 hci_dev_unlock(hdev); 9057 return -ENOMEM; 9058 } 9059 9060 supported_flags = get_supported_adv_flags(hdev); 9061 9062 rp->supported_flags = cpu_to_le32(supported_flags); 9063 rp->max_adv_data_len = max_adv_len(hdev); 9064 rp->max_scan_rsp_len = max_adv_len(hdev); 9065 rp->max_instances = hdev->le_num_of_adv_sets; 9066 rp->num_instances = hdev->adv_instance_cnt; 9067 9068 instance = rp->instance; 9069 list_for_each_entry(adv_instance, &hdev->adv_instances, list) { 9070 /* Only instances 1-le_num_of_adv_sets are externally visible */ 9071 if (adv_instance->instance <= hdev->adv_instance_cnt) { 9072 *instance = adv_instance->instance; 9073 instance++; 9074 } else { 9075 rp->num_instances--; 9076 rp_len--; 9077 } 9078 } 9079 9080 hci_dev_unlock(hdev); 9081 9082 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, 9083 MGMT_STATUS_SUCCESS, rp, rp_len); 9084 9085 kfree(rp); 9086 9087 return err; 9088 } 9089 9090 static u8 calculate_name_len(struct hci_dev *hdev) 9091 { 9092 u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 2]; /* len + type + name */ 9093 9094 return eir_append_local_name(hdev, buf, 0); 9095 } 9096 9097 static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags, 9098 bool is_adv_data) 9099 { 9100 u8 max_len = max_adv_len(hdev); 9101 9102 if (is_adv_data) { 9103 if (adv_flags & (MGMT_ADV_FLAG_DISCOV | 9104 MGMT_ADV_FLAG_LIMITED_DISCOV | 9105 MGMT_ADV_FLAG_MANAGED_FLAGS)) 9106 max_len -= 3; 9107 9108 if (adv_flags & MGMT_ADV_FLAG_TX_POWER) 9109 max_len -= 3; 9110 } else { 9111 if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME) 9112 max_len -= calculate_name_len(hdev); 9113 9114 if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE)) 9115 max_len -= 4; 9116 } 9117 9118 return max_len; 9119 } 9120 9121 static bool flags_managed(u32 adv_flags) 9122 { 9123 return adv_flags & (MGMT_ADV_FLAG_DISCOV | 9124 MGMT_ADV_FLAG_LIMITED_DISCOV | 9125 MGMT_ADV_FLAG_MANAGED_FLAGS); 9126 } 9127 9128 static bool tx_power_managed(u32 adv_flags) 9129 { 9130 return adv_flags & MGMT_ADV_FLAG_TX_POWER; 9131 } 9132 9133 static bool name_managed(u32 adv_flags) 9134 { 9135 return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME; 9136 } 9137 9138 static bool appearance_managed(u32 adv_flags) 9139 { 9140 return adv_flags & MGMT_ADV_FLAG_APPEARANCE; 9141 } 9142 9143 static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data, 9144 u8 len, bool is_adv_data) 9145 { 9146 int i, cur_len; 9147 u8 max_len; 9148 9149 max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data); 9150 9151 if (len > max_len) 9152 return false; 9153 9154 /* Make sure that the data is correctly formatted. */ 9155 for (i = 0; i < len; i += (cur_len + 1)) { 9156 cur_len = data[i]; 9157 9158 if (!cur_len) 9159 continue; 9160 9161 /* If the current field length would exceed the total data 9162 * length, then it's invalid. 9163 */ 9164 if (i + cur_len >= len) 9165 return false; 9166 9167 if (data[i + 1] == EIR_FLAGS && 9168 (!is_adv_data || flags_managed(adv_flags))) 9169 return false; 9170 9171 if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags)) 9172 return false; 9173 9174 if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags)) 9175 return false; 9176 9177 if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags)) 9178 return false; 9179 9180 if (data[i + 1] == EIR_APPEARANCE && 9181 appearance_managed(adv_flags)) 9182 return false; 9183 } 9184 9185 return true; 9186 } 9187 9188 static bool requested_adv_flags_are_valid(struct hci_dev *hdev, u32 adv_flags) 9189 { 9190 u32 supported_flags, phy_flags; 9191 9192 /* The current implementation only supports a subset of the specified 9193 * flags. Also need to check mutual exclusiveness of sec flags. 9194 */ 9195 supported_flags = get_supported_adv_flags(hdev); 9196 phy_flags = adv_flags & MGMT_ADV_FLAG_SEC_MASK; 9197 if (adv_flags & ~supported_flags || 9198 ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags))))) 9199 return false; 9200 9201 return true; 9202 } 9203 9204 static bool adv_busy(struct hci_dev *hdev) 9205 { 9206 return pending_find(MGMT_OP_SET_LE, hdev); 9207 } 9208 9209 /* Global advertising, enabled with MGMT_OP_SET_ADVERTISING, uses instance 0 9210 * and takes precedence over any instance that would share its advertising 9211 * set: with legacy advertising there is only a single set so all instances 9212 * are overridden, whereas with extended advertising only an instance using 9213 * handle 0x00 is. 9214 */ 9215 static bool adv_overridden_by_global(struct hci_dev *hdev, 9216 struct adv_info *adv) 9217 { 9218 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) 9219 return false; 9220 9221 return !ext_adv_capable(hdev) || !adv->handle; 9222 } 9223 9224 static void add_adv_complete(struct hci_dev *hdev, struct sock *sk, u8 instance, 9225 int err) 9226 { 9227 struct adv_info *adv, *n; 9228 9229 bt_dev_dbg(hdev, "err %d", err); 9230 9231 hci_dev_lock(hdev); 9232 9233 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) { 9234 u8 instance; 9235 9236 if (!adv->pending) 9237 continue; 9238 9239 if (!err) { 9240 adv->pending = false; 9241 continue; 9242 } 9243 9244 instance = adv->instance; 9245 9246 if (hdev->cur_adv_instance == instance) 9247 cancel_adv_timeout(hdev); 9248 9249 hci_remove_adv_instance(hdev, instance); 9250 mgmt_advertising_removed(sk, hdev, instance); 9251 } 9252 9253 hci_dev_unlock(hdev); 9254 } 9255 9256 static void add_advertising_complete(struct hci_dev *hdev, void *data, int err) 9257 { 9258 struct mgmt_pending_cmd *cmd = data; 9259 struct mgmt_cp_add_advertising *cp = cmd->param; 9260 struct mgmt_rp_add_advertising rp; 9261 9262 memset(&rp, 0, sizeof(rp)); 9263 9264 rp.instance = cp->instance; 9265 9266 if (err) 9267 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, 9268 mgmt_status(err)); 9269 else 9270 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, 9271 mgmt_status(err), &rp, sizeof(rp)); 9272 9273 add_adv_complete(hdev, cmd->sk, cp->instance, err); 9274 9275 mgmt_pending_free(cmd); 9276 } 9277 9278 static int add_advertising_sync(struct hci_dev *hdev, void *data) 9279 { 9280 struct mgmt_pending_cmd *cmd = data; 9281 u8 instance = PTR_UINT(cmd->user_data); 9282 9283 return hci_schedule_adv_instance_sync(hdev, instance, true); 9284 } 9285 9286 static int add_advertising(struct sock *sk, struct hci_dev *hdev, 9287 void *data, u16 data_len) 9288 { 9289 struct mgmt_cp_add_advertising *cp = data; 9290 struct mgmt_rp_add_advertising rp; 9291 u32 flags; 9292 u8 status; 9293 u16 timeout, duration; 9294 unsigned int prev_instance_cnt; 9295 u8 schedule_instance = 0; 9296 struct adv_info *adv, *next_instance; 9297 int err; 9298 struct mgmt_pending_cmd *cmd; 9299 9300 bt_dev_dbg(hdev, "sock %p", sk); 9301 9302 status = mgmt_le_support(hdev); 9303 if (status) 9304 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, 9305 status); 9306 9307 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) 9308 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, 9309 MGMT_STATUS_INVALID_PARAMS); 9310 9311 if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len) 9312 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, 9313 MGMT_STATUS_INVALID_PARAMS); 9314 9315 flags = __le32_to_cpu(cp->flags); 9316 timeout = __le16_to_cpu(cp->timeout); 9317 duration = __le16_to_cpu(cp->duration); 9318 9319 if (!requested_adv_flags_are_valid(hdev, flags)) 9320 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, 9321 MGMT_STATUS_INVALID_PARAMS); 9322 9323 hci_dev_lock(hdev); 9324 9325 if (timeout && !hdev_is_powered(hdev)) { 9326 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, 9327 MGMT_STATUS_REJECTED); 9328 goto unlock; 9329 } 9330 9331 if (adv_busy(hdev)) { 9332 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, 9333 MGMT_STATUS_BUSY); 9334 goto unlock; 9335 } 9336 9337 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) || 9338 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len, 9339 cp->scan_rsp_len, false)) { 9340 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, 9341 MGMT_STATUS_INVALID_PARAMS); 9342 goto unlock; 9343 } 9344 9345 prev_instance_cnt = hdev->adv_instance_cnt; 9346 9347 adv = hci_add_adv_instance(hdev, cp->instance, flags, 9348 cp->adv_data_len, cp->data, 9349 cp->scan_rsp_len, 9350 cp->data + cp->adv_data_len, 9351 timeout, duration, 9352 HCI_ADV_TX_POWER_NO_PREFERENCE, 9353 hdev->le_adv_min_interval, 9354 hdev->le_adv_max_interval, 0); 9355 if (IS_ERR(adv)) { 9356 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, 9357 MGMT_STATUS_FAILED); 9358 goto unlock; 9359 } 9360 9361 /* Only trigger an advertising added event if a new instance was 9362 * actually added. 9363 */ 9364 if (hdev->adv_instance_cnt > prev_instance_cnt) 9365 mgmt_advertising_added(sk, hdev, cp->instance); 9366 9367 if (ext_adv_capable(hdev)) { 9368 schedule_instance = cp->instance; 9369 } else if (hdev->cur_adv_instance == cp->instance) { 9370 /* If the currently advertised instance is being changed then 9371 * cancel the current advertising and schedule the next 9372 * instance. If there is only one instance then the overridden 9373 * advertising data will be visible right away. 9374 */ 9375 cancel_adv_timeout(hdev); 9376 9377 next_instance = hci_get_next_instance(hdev, cp->instance); 9378 if (next_instance) 9379 schedule_instance = next_instance->instance; 9380 } else if (!hdev->adv_instance_timeout) { 9381 /* Immediately advertise the new instance if no other 9382 * instance is currently being advertised. 9383 */ 9384 schedule_instance = cp->instance; 9385 } 9386 9387 /* If the instance is overridden by global advertising or the device 9388 * isn't powered or there is no instance to be advertised then we have 9389 * no HCI communication to make. Simply return. 9390 */ 9391 if (!hdev_is_powered(hdev) || 9392 adv_overridden_by_global(hdev, adv) || 9393 !schedule_instance) { 9394 /* Instance has been accepted so it shall no longer be 9395 * considered pending, otherwise a later failure in 9396 * add_adv_complete would remove it. 9397 */ 9398 adv->pending = false; 9399 9400 rp.instance = cp->instance; 9401 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING, 9402 MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); 9403 goto unlock; 9404 } 9405 9406 /* We're good to go, update advertising data, parameters, and start 9407 * advertising. 9408 */ 9409 cmd = mgmt_pending_new(sk, MGMT_OP_ADD_ADVERTISING, hdev, data, 9410 data_len); 9411 if (!cmd) { 9412 err = -ENOMEM; 9413 goto unlock; 9414 } 9415 9416 cmd->user_data = UINT_PTR(schedule_instance); 9417 9418 err = hci_cmd_sync_queue(hdev, add_advertising_sync, cmd, 9419 add_advertising_complete); 9420 if (err < 0) 9421 mgmt_pending_free(cmd); 9422 9423 unlock: 9424 hci_dev_unlock(hdev); 9425 9426 return err; 9427 } 9428 9429 static void add_ext_adv_params_complete(struct hci_dev *hdev, void *data, 9430 int err) 9431 { 9432 struct mgmt_pending_cmd *cmd = data; 9433 struct mgmt_cp_add_ext_adv_params *cp = cmd->param; 9434 struct mgmt_rp_add_ext_adv_params rp; 9435 struct adv_info *adv; 9436 u32 flags; 9437 9438 BT_DBG("%s", hdev->name); 9439 9440 hci_dev_lock(hdev); 9441 9442 adv = hci_find_adv_instance(hdev, cp->instance); 9443 if (!adv) 9444 goto unlock; 9445 9446 rp.instance = cp->instance; 9447 rp.tx_power = adv->tx_power; 9448 9449 /* While we're at it, inform userspace of the available space for this 9450 * advertisement, given the flags that will be used. 9451 */ 9452 flags = __le32_to_cpu(cp->flags); 9453 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); 9454 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); 9455 9456 if (err) { 9457 /* If this advertisement was previously advertising and we 9458 * failed to update it, we signal that it has been removed and 9459 * delete its structure 9460 */ 9461 if (!adv->pending) 9462 mgmt_advertising_removed(cmd->sk, hdev, cp->instance); 9463 9464 hci_remove_adv_instance(hdev, cp->instance); 9465 9466 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, 9467 mgmt_status(err)); 9468 } else { 9469 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, 9470 mgmt_status(err), &rp, sizeof(rp)); 9471 } 9472 9473 unlock: 9474 mgmt_pending_free(cmd); 9475 9476 hci_dev_unlock(hdev); 9477 } 9478 9479 static int add_ext_adv_params_sync(struct hci_dev *hdev, void *data) 9480 { 9481 struct mgmt_pending_cmd *cmd = data; 9482 struct mgmt_cp_add_ext_adv_params *cp = cmd->param; 9483 9484 return hci_setup_ext_adv_instance_sync(hdev, cp->instance); 9485 } 9486 9487 static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev, 9488 void *data, u16 data_len) 9489 { 9490 struct mgmt_cp_add_ext_adv_params *cp = data; 9491 struct mgmt_rp_add_ext_adv_params rp; 9492 struct mgmt_pending_cmd *cmd = NULL; 9493 struct adv_info *adv; 9494 u32 flags, min_interval, max_interval; 9495 u16 timeout, duration; 9496 u8 status; 9497 s8 tx_power; 9498 int err; 9499 9500 BT_DBG("%s", hdev->name); 9501 9502 status = mgmt_le_support(hdev); 9503 if (status) 9504 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, 9505 status); 9506 9507 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) 9508 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, 9509 MGMT_STATUS_INVALID_PARAMS); 9510 9511 /* The purpose of breaking add_advertising into two separate MGMT calls 9512 * for params and data is to allow more parameters to be added to this 9513 * structure in the future. For this reason, we verify that we have the 9514 * bare minimum structure we know of when the interface was defined. Any 9515 * extra parameters we don't know about will be ignored in this request. 9516 */ 9517 if (data_len < MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE) 9518 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, 9519 MGMT_STATUS_INVALID_PARAMS); 9520 9521 flags = __le32_to_cpu(cp->flags); 9522 9523 if (!requested_adv_flags_are_valid(hdev, flags)) 9524 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, 9525 MGMT_STATUS_INVALID_PARAMS); 9526 9527 hci_dev_lock(hdev); 9528 9529 /* In new interface, we require that we are powered to register */ 9530 if (!hdev_is_powered(hdev)) { 9531 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, 9532 MGMT_STATUS_REJECTED); 9533 goto unlock; 9534 } 9535 9536 if (adv_busy(hdev)) { 9537 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, 9538 MGMT_STATUS_BUSY); 9539 goto unlock; 9540 } 9541 9542 /* Parse defined parameters from request, use defaults otherwise */ 9543 timeout = (flags & MGMT_ADV_PARAM_TIMEOUT) ? 9544 __le16_to_cpu(cp->timeout) : 0; 9545 9546 duration = (flags & MGMT_ADV_PARAM_DURATION) ? 9547 __le16_to_cpu(cp->duration) : 9548 hdev->def_multi_adv_rotation_duration; 9549 9550 min_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? 9551 __le32_to_cpu(cp->min_interval) : 9552 hdev->le_adv_min_interval; 9553 9554 max_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ? 9555 __le32_to_cpu(cp->max_interval) : 9556 hdev->le_adv_max_interval; 9557 9558 tx_power = (flags & MGMT_ADV_PARAM_TX_POWER) ? 9559 cp->tx_power : 9560 HCI_ADV_TX_POWER_NO_PREFERENCE; 9561 9562 /* Create advertising instance with no advertising or response data */ 9563 adv = hci_add_adv_instance(hdev, cp->instance, flags, 0, NULL, 0, NULL, 9564 timeout, duration, tx_power, min_interval, 9565 max_interval, 0); 9566 9567 if (IS_ERR(adv)) { 9568 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS, 9569 MGMT_STATUS_FAILED); 9570 goto unlock; 9571 } 9572 9573 /* Submit request for advertising params if ext adv available, unless 9574 * the set is in use by global advertising in which case the params 9575 * will be programmed once the instance is scheduled. 9576 */ 9577 if (ext_adv_capable(hdev) && !adv_overridden_by_global(hdev, adv)) { 9578 cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_PARAMS, hdev, 9579 data, data_len); 9580 if (!cmd) { 9581 err = -ENOMEM; 9582 goto remove; 9583 } 9584 9585 err = hci_cmd_sync_queue(hdev, add_ext_adv_params_sync, cmd, 9586 add_ext_adv_params_complete); 9587 if (err < 0) { 9588 mgmt_pending_free(cmd); 9589 goto remove; 9590 } 9591 } else { 9592 rp.instance = cp->instance; 9593 rp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE; 9594 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); 9595 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); 9596 err = mgmt_cmd_complete(sk, hdev->id, 9597 MGMT_OP_ADD_EXT_ADV_PARAMS, 9598 MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); 9599 } 9600 9601 goto unlock; 9602 9603 remove: 9604 /* The instance may have been modified already so remove it, like it 9605 * is done in add_ext_adv_params_complete, signaling it has been 9606 * removed if it was previously added. 9607 */ 9608 if (!adv->pending) 9609 mgmt_advertising_removed(sk, hdev, cp->instance); 9610 9611 hci_remove_adv_instance(hdev, cp->instance); 9612 9613 unlock: 9614 hci_dev_unlock(hdev); 9615 9616 return err; 9617 } 9618 9619 static void add_ext_adv_data_complete(struct hci_dev *hdev, void *data, int err) 9620 { 9621 struct mgmt_pending_cmd *cmd = data; 9622 struct mgmt_cp_add_ext_adv_data *cp = cmd->param; 9623 struct mgmt_rp_add_advertising rp; 9624 9625 add_adv_complete(hdev, cmd->sk, cp->instance, err); 9626 9627 memset(&rp, 0, sizeof(rp)); 9628 9629 rp.instance = cp->instance; 9630 9631 if (err) 9632 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, 9633 mgmt_status(err)); 9634 else 9635 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, 9636 mgmt_status(err), &rp, sizeof(rp)); 9637 9638 mgmt_pending_free(cmd); 9639 } 9640 9641 static int add_ext_adv_data_sync(struct hci_dev *hdev, void *data) 9642 { 9643 struct mgmt_pending_cmd *cmd = data; 9644 struct mgmt_cp_add_ext_adv_data *cp = cmd->param; 9645 u8 instance = PTR_UINT(cmd->user_data); 9646 int err; 9647 9648 if (ext_adv_capable(hdev)) { 9649 err = hci_update_adv_data_sync(hdev, cp->instance); 9650 if (err) 9651 return err; 9652 9653 err = hci_update_scan_rsp_data_sync(hdev, cp->instance); 9654 if (err) 9655 return err; 9656 9657 return hci_enable_ext_advertising_sync(hdev, cp->instance); 9658 } 9659 9660 return hci_schedule_adv_instance_sync(hdev, instance, true); 9661 } 9662 9663 static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data, 9664 u16 data_len) 9665 { 9666 struct mgmt_cp_add_ext_adv_data *cp = data; 9667 struct mgmt_rp_add_ext_adv_data rp; 9668 u8 schedule_instance = 0; 9669 struct adv_info *next_instance; 9670 struct adv_info *adv_instance; 9671 int err = 0; 9672 struct mgmt_pending_cmd *cmd; 9673 u16 expected_len; 9674 9675 BT_DBG("%s", hdev->name); 9676 9677 expected_len = struct_size(cp, data, cp->adv_data_len + 9678 cp->scan_rsp_len); 9679 if (expected_len > data_len) 9680 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, 9681 MGMT_STATUS_INVALID_PARAMS); 9682 9683 hci_dev_lock(hdev); 9684 9685 adv_instance = hci_find_adv_instance(hdev, cp->instance); 9686 9687 if (!adv_instance) { 9688 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, 9689 MGMT_STATUS_INVALID_PARAMS); 9690 goto unlock; 9691 } 9692 9693 /* In new interface, we require that we are powered to register */ 9694 if (!hdev_is_powered(hdev)) { 9695 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, 9696 MGMT_STATUS_REJECTED); 9697 goto clear_new_instance; 9698 } 9699 9700 if (adv_busy(hdev)) { 9701 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, 9702 MGMT_STATUS_BUSY); 9703 goto clear_new_instance; 9704 } 9705 9706 /* Validate new data */ 9707 if (!tlv_data_is_valid(hdev, adv_instance->flags, cp->data, 9708 cp->adv_data_len, true) || 9709 !tlv_data_is_valid(hdev, adv_instance->flags, cp->data + 9710 cp->adv_data_len, cp->scan_rsp_len, false)) { 9711 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, 9712 MGMT_STATUS_INVALID_PARAMS); 9713 goto clear_new_instance; 9714 } 9715 9716 /* Set the data in the advertising instance */ 9717 hci_set_adv_instance_data(hdev, cp->instance, cp->adv_data_len, 9718 cp->data, cp->scan_rsp_len, 9719 cp->data + cp->adv_data_len); 9720 9721 /* If using software rotation, determine next instance to use */ 9722 if (ext_adv_capable(hdev)) { 9723 schedule_instance = cp->instance; 9724 } else if (hdev->cur_adv_instance == cp->instance) { 9725 /* If the currently advertised instance is being changed 9726 * then cancel the current advertising and schedule the 9727 * next instance. If there is only one instance then the 9728 * overridden advertising data will be visible right 9729 * away 9730 */ 9731 cancel_adv_timeout(hdev); 9732 9733 next_instance = hci_get_next_instance(hdev, cp->instance); 9734 if (next_instance) 9735 schedule_instance = next_instance->instance; 9736 } else if (!hdev->adv_instance_timeout) { 9737 /* Immediately advertise the new instance if no other 9738 * instance is currently being advertised. 9739 */ 9740 schedule_instance = cp->instance; 9741 } 9742 9743 /* If the instance is overridden by global advertising or there is no 9744 * instance to be advertised then we have no HCI communication to make. 9745 * Simply return. 9746 */ 9747 if (adv_overridden_by_global(hdev, adv_instance) || 9748 !schedule_instance) { 9749 if (adv_instance->pending) { 9750 mgmt_advertising_added(sk, hdev, cp->instance); 9751 adv_instance->pending = false; 9752 } 9753 rp.instance = cp->instance; 9754 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA, 9755 MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); 9756 goto unlock; 9757 } 9758 9759 cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_DATA, hdev, data, 9760 data_len); 9761 if (!cmd) { 9762 err = -ENOMEM; 9763 goto clear_new_instance; 9764 } 9765 9766 cmd->user_data = UINT_PTR(schedule_instance); 9767 9768 err = hci_cmd_sync_queue(hdev, add_ext_adv_data_sync, cmd, 9769 add_ext_adv_data_complete); 9770 if (err < 0) { 9771 mgmt_pending_free(cmd); 9772 goto clear_new_instance; 9773 } 9774 9775 /* We were successful in updating data, so trigger advertising_added 9776 * event if this is an instance that wasn't previously advertising. If 9777 * a failure occurs in the requests we initiated, we will remove the 9778 * instance again in add_advertising_complete 9779 */ 9780 if (adv_instance->pending) 9781 mgmt_advertising_added(sk, hdev, cp->instance); 9782 9783 goto unlock; 9784 9785 clear_new_instance: 9786 hci_remove_adv_instance(hdev, cp->instance); 9787 9788 unlock: 9789 hci_dev_unlock(hdev); 9790 9791 return err; 9792 } 9793 9794 static void remove_advertising_complete(struct hci_dev *hdev, void *data, 9795 int err) 9796 { 9797 struct mgmt_pending_cmd *cmd = data; 9798 struct mgmt_cp_remove_advertising *cp = cmd->param; 9799 struct mgmt_rp_remove_advertising rp; 9800 9801 bt_dev_dbg(hdev, "err %d", err); 9802 9803 memset(&rp, 0, sizeof(rp)); 9804 rp.instance = cp->instance; 9805 9806 if (err) 9807 mgmt_cmd_status(cmd->sk, cmd->hdev->id, cmd->opcode, 9808 mgmt_status(err)); 9809 else 9810 mgmt_cmd_complete(cmd->sk, cmd->hdev->id, cmd->opcode, 9811 MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); 9812 9813 mgmt_pending_free(cmd); 9814 } 9815 9816 static int remove_advertising_sync(struct hci_dev *hdev, void *data) 9817 { 9818 struct mgmt_pending_cmd *cmd = data; 9819 struct mgmt_cp_remove_advertising *cp = cmd->param; 9820 int err; 9821 9822 err = hci_remove_advertising_sync(hdev, cmd->sk, cp->instance, true); 9823 if (err) 9824 return err; 9825 9826 /* Don't disable advertising if global advertising (instance 0) is 9827 * enabled since it is not affected by removing instances. 9828 */ 9829 if (list_empty(&hdev->adv_instances) && 9830 !hci_dev_test_flag(hdev, HCI_ADVERTISING)) 9831 err = hci_disable_advertising_sync(hdev); 9832 9833 return err; 9834 } 9835 9836 static int remove_advertising(struct sock *sk, struct hci_dev *hdev, 9837 void *data, u16 data_len) 9838 { 9839 struct mgmt_cp_remove_advertising *cp = data; 9840 struct mgmt_pending_cmd *cmd; 9841 int err; 9842 9843 bt_dev_dbg(hdev, "sock %p", sk); 9844 9845 hci_dev_lock(hdev); 9846 9847 if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) { 9848 err = mgmt_cmd_status(sk, hdev->id, 9849 MGMT_OP_REMOVE_ADVERTISING, 9850 MGMT_STATUS_INVALID_PARAMS); 9851 goto unlock; 9852 } 9853 9854 if (pending_find(MGMT_OP_SET_LE, hdev)) { 9855 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, 9856 MGMT_STATUS_BUSY); 9857 goto unlock; 9858 } 9859 9860 if (list_empty(&hdev->adv_instances)) { 9861 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING, 9862 MGMT_STATUS_INVALID_PARAMS); 9863 goto unlock; 9864 } 9865 9866 cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data, 9867 data_len); 9868 if (!cmd) { 9869 err = -ENOMEM; 9870 goto unlock; 9871 } 9872 9873 err = hci_cmd_sync_queue(hdev, remove_advertising_sync, cmd, 9874 remove_advertising_complete); 9875 if (err < 0) 9876 mgmt_pending_free(cmd); 9877 9878 unlock: 9879 hci_dev_unlock(hdev); 9880 9881 return err; 9882 } 9883 9884 static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev, 9885 void *data, u16 data_len) 9886 { 9887 struct mgmt_cp_get_adv_size_info *cp = data; 9888 struct mgmt_rp_get_adv_size_info rp; 9889 u32 flags, supported_flags; 9890 9891 bt_dev_dbg(hdev, "sock %p", sk); 9892 9893 if (!lmp_le_capable(hdev)) 9894 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, 9895 MGMT_STATUS_REJECTED); 9896 9897 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets) 9898 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, 9899 MGMT_STATUS_INVALID_PARAMS); 9900 9901 flags = __le32_to_cpu(cp->flags); 9902 9903 /* The current implementation only supports a subset of the specified 9904 * flags. 9905 */ 9906 supported_flags = get_supported_adv_flags(hdev); 9907 if (flags & ~supported_flags) 9908 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, 9909 MGMT_STATUS_INVALID_PARAMS); 9910 9911 rp.instance = cp->instance; 9912 rp.flags = cp->flags; 9913 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true); 9914 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false); 9915 9916 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO, 9917 MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); 9918 } 9919 9920 static const struct hci_mgmt_handler mgmt_handlers[] = { 9921 { NULL }, /* 0x0000 (no command) */ 9922 { read_version, MGMT_READ_VERSION_SIZE, 9923 HCI_MGMT_NO_HDEV | 9924 HCI_MGMT_UNTRUSTED }, 9925 { read_commands, MGMT_READ_COMMANDS_SIZE, 9926 HCI_MGMT_NO_HDEV | 9927 HCI_MGMT_UNTRUSTED }, 9928 { read_index_list, MGMT_READ_INDEX_LIST_SIZE, 9929 HCI_MGMT_NO_HDEV | 9930 HCI_MGMT_UNTRUSTED }, 9931 { read_controller_info, MGMT_READ_INFO_SIZE, 9932 HCI_MGMT_UNTRUSTED }, 9933 { set_powered, MGMT_SETTING_SIZE }, 9934 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, 9935 { set_connectable, MGMT_SETTING_SIZE }, 9936 { set_fast_connectable, MGMT_SETTING_SIZE }, 9937 { set_bondable, MGMT_SETTING_SIZE }, 9938 { set_link_security, MGMT_SETTING_SIZE }, 9939 { set_ssp, MGMT_SETTING_SIZE }, 9940 { set_hs, MGMT_SETTING_SIZE }, 9941 { set_le, MGMT_SETTING_SIZE }, 9942 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, 9943 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, 9944 { add_uuid, MGMT_ADD_UUID_SIZE }, 9945 { remove_uuid, MGMT_REMOVE_UUID_SIZE }, 9946 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, 9947 HCI_MGMT_VAR_LEN }, 9948 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, 9949 HCI_MGMT_VAR_LEN }, 9950 { disconnect, MGMT_DISCONNECT_SIZE }, 9951 { get_connections, MGMT_GET_CONNECTIONS_SIZE }, 9952 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, 9953 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, 9954 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, 9955 { pair_device, MGMT_PAIR_DEVICE_SIZE }, 9956 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, 9957 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, 9958 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, 9959 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, 9960 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, 9961 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, 9962 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, 9963 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, 9964 HCI_MGMT_VAR_LEN }, 9965 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, 9966 { start_discovery, MGMT_START_DISCOVERY_SIZE }, 9967 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, 9968 { confirm_name, MGMT_CONFIRM_NAME_SIZE }, 9969 { block_device, MGMT_BLOCK_DEVICE_SIZE }, 9970 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, 9971 { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, 9972 { set_advertising, MGMT_SETTING_SIZE }, 9973 { set_bredr, MGMT_SETTING_SIZE }, 9974 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, 9975 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, 9976 { set_secure_conn, MGMT_SETTING_SIZE }, 9977 { set_debug_keys, MGMT_SETTING_SIZE }, 9978 { set_privacy, MGMT_SET_PRIVACY_SIZE }, 9979 { load_irks, MGMT_LOAD_IRKS_SIZE, 9980 HCI_MGMT_VAR_LEN }, 9981 { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, 9982 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, 9983 { add_device, MGMT_ADD_DEVICE_SIZE }, 9984 { remove_device, MGMT_REMOVE_DEVICE_SIZE }, 9985 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, 9986 HCI_MGMT_VAR_LEN }, 9987 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, 9988 HCI_MGMT_NO_HDEV | 9989 HCI_MGMT_UNTRUSTED }, 9990 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, 9991 HCI_MGMT_UNCONFIGURED | 9992 HCI_MGMT_UNTRUSTED }, 9993 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, 9994 HCI_MGMT_UNCONFIGURED }, 9995 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, 9996 HCI_MGMT_UNCONFIGURED }, 9997 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, 9998 HCI_MGMT_VAR_LEN }, 9999 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE }, 10000 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, 10001 HCI_MGMT_NO_HDEV | 10002 HCI_MGMT_UNTRUSTED }, 10003 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, 10004 { add_advertising, MGMT_ADD_ADVERTISING_SIZE, 10005 HCI_MGMT_VAR_LEN }, 10006 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE }, 10007 { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE }, 10008 { start_limited_discovery, MGMT_START_DISCOVERY_SIZE }, 10009 { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE, 10010 HCI_MGMT_UNTRUSTED }, 10011 { set_appearance, MGMT_SET_APPEARANCE_SIZE }, 10012 { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE }, 10013 { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE }, 10014 { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE, 10015 HCI_MGMT_VAR_LEN }, 10016 { set_wideband_speech, MGMT_SETTING_SIZE }, 10017 { read_controller_cap, MGMT_READ_CONTROLLER_CAP_SIZE, 10018 HCI_MGMT_UNTRUSTED }, 10019 { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE, 10020 HCI_MGMT_UNTRUSTED | 10021 HCI_MGMT_HDEV_OPTIONAL }, 10022 { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE, 10023 HCI_MGMT_VAR_LEN | 10024 HCI_MGMT_HDEV_OPTIONAL }, 10025 { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE, 10026 HCI_MGMT_UNTRUSTED }, 10027 { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE, 10028 HCI_MGMT_VAR_LEN }, 10029 { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE, 10030 HCI_MGMT_UNTRUSTED }, 10031 { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE, 10032 HCI_MGMT_VAR_LEN }, 10033 { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE }, 10034 { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE }, 10035 { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE }, 10036 { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE, 10037 HCI_MGMT_VAR_LEN }, 10038 { remove_adv_monitor, MGMT_REMOVE_ADV_MONITOR_SIZE }, 10039 { add_ext_adv_params, MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE, 10040 HCI_MGMT_VAR_LEN }, 10041 { add_ext_adv_data, MGMT_ADD_EXT_ADV_DATA_SIZE, 10042 HCI_MGMT_VAR_LEN }, 10043 { add_adv_patterns_monitor_rssi, 10044 MGMT_ADD_ADV_PATTERNS_MONITOR_RSSI_SIZE, 10045 HCI_MGMT_VAR_LEN }, 10046 { set_mesh, MGMT_SET_MESH_RECEIVER_SIZE, 10047 HCI_MGMT_VAR_LEN }, 10048 { mesh_features, MGMT_MESH_READ_FEATURES_SIZE }, 10049 { mesh_send, MGMT_MESH_SEND_SIZE, 10050 HCI_MGMT_VAR_LEN }, 10051 { mesh_send_cancel, MGMT_MESH_SEND_CANCEL_SIZE }, 10052 { mgmt_hci_cmd_sync, MGMT_HCI_CMD_SYNC_SIZE, HCI_MGMT_VAR_LEN }, 10053 { load_conn_subrate, MGMT_LOAD_CONN_SUBRATE_SIZE, 10054 HCI_MGMT_VAR_LEN }, 10055 }; 10056 10057 void mgmt_index_added(struct hci_dev *hdev) 10058 { 10059 struct mgmt_ev_ext_index ev; 10060 10061 if (hci_test_quirk(hdev, HCI_QUIRK_RAW_DEVICE)) 10062 return; 10063 10064 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 10065 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, 10066 HCI_MGMT_UNCONF_INDEX_EVENTS); 10067 ev.type = 0x01; 10068 } else { 10069 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, 10070 HCI_MGMT_INDEX_EVENTS); 10071 ev.type = 0x00; 10072 } 10073 10074 ev.bus = hdev->bus; 10075 10076 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), 10077 HCI_MGMT_EXT_INDEX_EVENTS); 10078 } 10079 10080 void mgmt_index_removed(struct hci_dev *hdev) 10081 { 10082 struct mgmt_ev_ext_index ev; 10083 struct cmd_lookup match = { NULL, hdev, MGMT_STATUS_INVALID_INDEX }; 10084 10085 if (hci_test_quirk(hdev, HCI_QUIRK_RAW_DEVICE)) 10086 return; 10087 10088 mgmt_pending_foreach(0, hdev, true, cmd_complete_rsp, &match); 10089 10090 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { 10091 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, 10092 HCI_MGMT_UNCONF_INDEX_EVENTS); 10093 ev.type = 0x01; 10094 } else { 10095 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, 10096 HCI_MGMT_INDEX_EVENTS); 10097 ev.type = 0x00; 10098 } 10099 10100 ev.bus = hdev->bus; 10101 10102 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), 10103 HCI_MGMT_EXT_INDEX_EVENTS); 10104 10105 /* Cancel any remaining timed work */ 10106 if (!hci_dev_test_flag(hdev, HCI_MGMT)) 10107 return; 10108 cancel_delayed_work_sync(&hdev->discov_off); 10109 cancel_delayed_work_sync(&hdev->service_cache); 10110 cancel_delayed_work_sync(&hdev->rpa_expired); 10111 10112 mesh_flush(hdev, true); 10113 } 10114 10115 void mgmt_power_on(struct hci_dev *hdev, int err) 10116 { 10117 struct cmd_lookup match = { NULL, hdev }; 10118 10119 bt_dev_dbg(hdev, "err %d", err); 10120 10121 hci_dev_lock(hdev); 10122 10123 if (!err) { 10124 restart_le_actions(hdev); 10125 hci_update_passive_scan(hdev); 10126 } 10127 10128 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, true, settings_rsp, 10129 &match); 10130 10131 new_settings(hdev, match.sk); 10132 10133 if (match.sk) 10134 sock_put(match.sk); 10135 10136 hci_dev_unlock(hdev); 10137 } 10138 10139 /* This function requires the caller holds hdev->lock */ 10140 void mgmt_mesh_flush(struct hci_dev *hdev) 10141 { 10142 /* The mesh work is only initialized once mgmt is used */ 10143 if (!hci_dev_test_flag(hdev, HCI_MGMT)) 10144 return; 10145 10146 mesh_flush(hdev, false); 10147 } 10148 10149 void __mgmt_power_off(struct hci_dev *hdev) 10150 { 10151 struct cmd_lookup match = { NULL, hdev }; 10152 u8 zero_cod[] = { 0, 0, 0 }; 10153 10154 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, true, settings_rsp, 10155 &match); 10156 10157 /* If the power off is because of hdev unregistration let 10158 * use the appropriate INVALID_INDEX status. Otherwise use 10159 * NOT_POWERED. We cover both scenarios here since later in 10160 * mgmt_index_removed() any hci_conn callbacks will have already 10161 * been triggered, potentially causing misleading DISCONNECTED 10162 * status responses. 10163 */ 10164 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) 10165 match.mgmt_status = MGMT_STATUS_INVALID_INDEX; 10166 else 10167 match.mgmt_status = MGMT_STATUS_NOT_POWERED; 10168 10169 mgmt_pending_foreach(0, hdev, true, cmd_complete_rsp, &match); 10170 10171 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) { 10172 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, 10173 zero_cod, sizeof(zero_cod), 10174 HCI_MGMT_DEV_CLASS_EVENTS, NULL); 10175 ext_info_changed(hdev, NULL); 10176 } 10177 10178 new_settings(hdev, match.sk); 10179 10180 if (match.sk) 10181 sock_put(match.sk); 10182 } 10183 10184 void mgmt_set_powered_failed(struct hci_dev *hdev, int err) 10185 { 10186 struct mgmt_pending_cmd *cmd; 10187 u8 status; 10188 10189 cmd = pending_find(MGMT_OP_SET_POWERED, hdev); 10190 if (!cmd) 10191 return; 10192 10193 if (err == -ERFKILL) 10194 status = MGMT_STATUS_RFKILLED; 10195 else 10196 status = MGMT_STATUS_FAILED; 10197 10198 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); 10199 10200 mgmt_pending_remove(cmd); 10201 } 10202 10203 void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, 10204 bool persistent) 10205 { 10206 struct mgmt_ev_new_link_key ev; 10207 10208 memset(&ev, 0, sizeof(ev)); 10209 10210 ev.store_hint = persistent; 10211 bacpy(&ev.key.addr.bdaddr, &key->bdaddr); 10212 ev.key.addr.type = BDADDR_BREDR; 10213 ev.key.type = key->type; 10214 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); 10215 ev.key.pin_len = key->pin_len; 10216 10217 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); 10218 } 10219 10220 static u8 mgmt_ltk_type(struct smp_ltk *ltk) 10221 { 10222 switch (ltk->type) { 10223 case SMP_LTK: 10224 case SMP_LTK_RESPONDER: 10225 if (ltk->authenticated) 10226 return MGMT_LTK_AUTHENTICATED; 10227 return MGMT_LTK_UNAUTHENTICATED; 10228 case SMP_LTK_P256: 10229 if (ltk->authenticated) 10230 return MGMT_LTK_P256_AUTH; 10231 return MGMT_LTK_P256_UNAUTH; 10232 case SMP_LTK_P256_DEBUG: 10233 return MGMT_LTK_P256_DEBUG; 10234 } 10235 10236 return MGMT_LTK_UNAUTHENTICATED; 10237 } 10238 10239 void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent) 10240 { 10241 struct mgmt_ev_new_long_term_key ev; 10242 10243 memset(&ev, 0, sizeof(ev)); 10244 10245 /* Devices using resolvable or non-resolvable random addresses 10246 * without providing an identity resolving key don't require 10247 * to store long term keys. Their addresses will change the 10248 * next time around. 10249 * 10250 * Only when a remote device provides an identity address 10251 * make sure the long term key is stored. If the remote 10252 * identity is known, the long term keys are internally 10253 * mapped to the identity address. So allow static random 10254 * and public addresses here. 10255 */ 10256 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && 10257 (key->bdaddr.b[5] & 0xc0) != 0xc0) 10258 ev.store_hint = 0x00; 10259 else 10260 ev.store_hint = persistent; 10261 10262 bacpy(&ev.key.addr.bdaddr, &key->bdaddr); 10263 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); 10264 ev.key.type = mgmt_ltk_type(key); 10265 ev.key.enc_size = key->enc_size; 10266 ev.key.ediv = key->ediv; 10267 ev.key.rand = key->rand; 10268 10269 if (key->type == SMP_LTK) 10270 ev.key.initiator = 1; 10271 10272 /* Make sure we copy only the significant bytes based on the 10273 * encryption key size, and set the rest of the value to zeroes. 10274 */ 10275 memcpy(ev.key.val, key->val, key->enc_size); 10276 memset(ev.key.val + key->enc_size, 0, 10277 sizeof(ev.key.val) - key->enc_size); 10278 10279 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL); 10280 } 10281 10282 void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent) 10283 { 10284 struct mgmt_ev_new_irk ev; 10285 10286 memset(&ev, 0, sizeof(ev)); 10287 10288 ev.store_hint = persistent; 10289 10290 bacpy(&ev.rpa, &irk->rpa); 10291 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); 10292 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); 10293 memcpy(ev.irk.val, irk->val, sizeof(irk->val)); 10294 10295 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); 10296 } 10297 10298 void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, 10299 bool persistent) 10300 { 10301 struct mgmt_ev_new_csrk ev; 10302 10303 memset(&ev, 0, sizeof(ev)); 10304 10305 /* Devices using resolvable or non-resolvable random addresses 10306 * without providing an identity resolving key don't require 10307 * to store signature resolving keys. Their addresses will change 10308 * the next time around. 10309 * 10310 * Only when a remote device provides an identity address 10311 * make sure the signature resolving key is stored. So allow 10312 * static random and public addresses here. 10313 */ 10314 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && 10315 (csrk->bdaddr.b[5] & 0xc0) != 0xc0) 10316 ev.store_hint = 0x00; 10317 else 10318 ev.store_hint = persistent; 10319 10320 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); 10321 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); 10322 ev.key.type = csrk->type; 10323 memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); 10324 10325 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); 10326 } 10327 10328 void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, 10329 u8 bdaddr_type, u8 store_hint, u16 min_interval, 10330 u16 max_interval, u16 latency, u16 timeout) 10331 { 10332 struct mgmt_ev_new_conn_param ev; 10333 10334 if (!hci_is_identity_address(bdaddr, bdaddr_type)) 10335 return; 10336 10337 memset(&ev, 0, sizeof(ev)); 10338 bacpy(&ev.addr.bdaddr, bdaddr); 10339 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); 10340 ev.store_hint = store_hint; 10341 ev.min_interval = cpu_to_le16(min_interval); 10342 ev.max_interval = cpu_to_le16(max_interval); 10343 ev.latency = cpu_to_le16(latency); 10344 ev.timeout = cpu_to_le16(timeout); 10345 10346 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); 10347 } 10348 10349 void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, 10350 u8 *name, u8 name_len) 10351 { 10352 struct sk_buff *skb; 10353 struct mgmt_ev_device_connected *ev; 10354 u16 eir_len = 0; 10355 u32 flags = 0; 10356 10357 if (test_and_set_bit(HCI_CONN_MGMT_CONNECTED, conn->flags)) 10358 return; 10359 10360 /* allocate buff for LE or BR/EDR adv */ 10361 if (conn->le_adv_data_len > 0) 10362 skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_CONNECTED, 10363 sizeof(*ev) + conn->le_adv_data_len); 10364 else 10365 skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_CONNECTED, 10366 sizeof(*ev) + (name ? eir_precalc_len(name_len) : 0) + 10367 eir_precalc_len(sizeof(conn->dev_class))); 10368 10369 if (!skb) 10370 return; 10371 10372 ev = skb_put(skb, sizeof(*ev)); 10373 bacpy(&ev->addr.bdaddr, &conn->dst); 10374 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); 10375 10376 if (conn->out) 10377 flags |= MGMT_DEV_FOUND_INITIATED_CONN; 10378 10379 ev->flags = __cpu_to_le32(flags); 10380 10381 /* We must ensure that the EIR Data fields are ordered and 10382 * unique. Keep it simple for now and avoid the problem by not 10383 * adding any BR/EDR data to the LE adv. 10384 */ 10385 if (conn->le_adv_data_len > 0) { 10386 skb_put_data(skb, conn->le_adv_data, conn->le_adv_data_len); 10387 eir_len = conn->le_adv_data_len; 10388 } else { 10389 if (name) 10390 eir_len += eir_skb_put_data(skb, EIR_NAME_COMPLETE, name, name_len); 10391 10392 if (memcmp(conn->dev_class, "\0\0\0", sizeof(conn->dev_class))) 10393 eir_len += eir_skb_put_data(skb, EIR_CLASS_OF_DEV, 10394 conn->dev_class, sizeof(conn->dev_class)); 10395 } 10396 10397 ev->eir_len = cpu_to_le16(eir_len); 10398 10399 mgmt_event_skb(skb, NULL); 10400 10401 /* Connections that negotiated encryption as part of setup (e.g. via 10402 * an existing link key) had their security level change suppressed 10403 * since userspace did not know about them yet, so report it now. 10404 * Unencrypted connections have no change to report. 10405 */ 10406 if (test_bit(HCI_CONN_ENCRYPT, conn->flags)) 10407 mgmt_security_level_changed(conn); 10408 } 10409 10410 static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) 10411 { 10412 struct hci_dev *hdev = data; 10413 struct mgmt_cp_unpair_device *cp = cmd->param; 10414 10415 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); 10416 10417 cmd->cmd_complete(cmd, 0); 10418 } 10419 10420 bool mgmt_powering_down(struct hci_dev *hdev) 10421 { 10422 struct mgmt_pending_cmd *cmd; 10423 struct mgmt_mode *cp; 10424 bool powering_down = false; 10425 10426 if (hci_dev_test_flag(hdev, HCI_POWERING_DOWN)) 10427 return true; 10428 10429 mutex_lock(&hdev->mgmt_pending_lock); 10430 10431 list_for_each_entry(cmd, &hdev->mgmt_pending, list) { 10432 if (hci_sock_get_channel(cmd->sk) != HCI_CHANNEL_CONTROL || 10433 cmd->opcode != MGMT_OP_SET_POWERED) 10434 continue; 10435 10436 /* cmd can only be freed after it is unlinked under the lock */ 10437 cp = cmd->param; 10438 powering_down = !cp->val; 10439 break; 10440 } 10441 10442 mutex_unlock(&hdev->mgmt_pending_lock); 10443 10444 return powering_down; 10445 } 10446 10447 u8 hci_to_mgmt_reason(u8 err) 10448 { 10449 switch (err) { 10450 case HCI_ERROR_CONNECTION_TIMEOUT: 10451 return MGMT_DEV_DISCONN_TIMEOUT; 10452 case HCI_ERROR_REMOTE_USER_TERM: 10453 case HCI_ERROR_REMOTE_LOW_RESOURCES: 10454 case HCI_ERROR_REMOTE_POWER_OFF: 10455 return MGMT_DEV_DISCONN_REMOTE; 10456 case HCI_ERROR_LOCAL_HOST_TERM: 10457 return MGMT_DEV_DISCONN_LOCAL_HOST; 10458 default: 10459 return MGMT_DEV_DISCONN_UNKNOWN; 10460 } 10461 } 10462 10463 void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, 10464 u8 link_type, u8 addr_type, u8 reason, 10465 bool mgmt_connected) 10466 { 10467 struct mgmt_ev_device_disconnected ev; 10468 struct sock *sk = NULL; 10469 10470 if (!mgmt_connected) 10471 return; 10472 10473 if (link_type != ACL_LINK && 10474 link_type != LE_LINK && 10475 link_type != BIS_LINK) 10476 return; 10477 10478 bacpy(&ev.addr.bdaddr, bdaddr); 10479 ev.addr.type = link_to_bdaddr(link_type, addr_type); 10480 ev.reason = reason; 10481 10482 /* Report disconnects due to suspend */ 10483 if (hdev->suspended) 10484 ev.reason = MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND; 10485 10486 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); 10487 10488 if (sk) 10489 sock_put(sk); 10490 } 10491 10492 void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, 10493 u8 link_type, u8 addr_type, u8 status) 10494 { 10495 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); 10496 struct mgmt_cp_disconnect *cp; 10497 struct mgmt_pending_cmd *cmd; 10498 10499 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, true, 10500 unpair_device_rsp, hdev); 10501 10502 cmd = pending_find(MGMT_OP_DISCONNECT, hdev); 10503 if (!cmd) 10504 return; 10505 10506 cp = cmd->param; 10507 10508 if (bacmp(bdaddr, &cp->addr.bdaddr)) 10509 return; 10510 10511 if (cp->addr.type != bdaddr_type) 10512 return; 10513 10514 cmd->cmd_complete(cmd, mgmt_status(status)); 10515 mgmt_pending_remove(cmd); 10516 } 10517 10518 void mgmt_connect_failed(struct hci_dev *hdev, struct hci_conn *conn, u8 status) 10519 { 10520 struct mgmt_ev_connect_failed ev; 10521 10522 /* Only peek at the flag here: hci_conn_del() (via hci_conn_unlink()) 10523 * still needs it set to know whether to report the security level 10524 * reset, so it is consumed there instead of here. 10525 */ 10526 if (test_bit(HCI_CONN_MGMT_CONNECTED, conn->flags)) { 10527 mgmt_device_disconnected(hdev, &conn->dst, conn->type, 10528 conn->dst_type, 10529 hci_to_mgmt_reason(status), true); 10530 return; 10531 } 10532 10533 bacpy(&ev.addr.bdaddr, &conn->dst); 10534 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); 10535 ev.status = mgmt_status(status); 10536 10537 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); 10538 } 10539 10540 void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure) 10541 { 10542 struct mgmt_ev_pin_code_request ev; 10543 10544 bacpy(&ev.addr.bdaddr, bdaddr); 10545 ev.addr.type = BDADDR_BREDR; 10546 ev.secure = secure; 10547 10548 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); 10549 } 10550 10551 void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 10552 u8 status) 10553 { 10554 struct mgmt_pending_cmd *cmd; 10555 10556 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); 10557 if (!cmd) 10558 return; 10559 10560 cmd->cmd_complete(cmd, mgmt_status(status)); 10561 mgmt_pending_remove(cmd); 10562 } 10563 10564 void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 10565 u8 status) 10566 { 10567 struct mgmt_pending_cmd *cmd; 10568 10569 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); 10570 if (!cmd) 10571 return; 10572 10573 cmd->cmd_complete(cmd, mgmt_status(status)); 10574 mgmt_pending_remove(cmd); 10575 } 10576 10577 int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, 10578 u8 link_type, u8 addr_type, u32 value, 10579 u8 confirm_hint) 10580 { 10581 struct mgmt_ev_user_confirm_request ev; 10582 10583 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); 10584 10585 bacpy(&ev.addr.bdaddr, bdaddr); 10586 ev.addr.type = link_to_bdaddr(link_type, addr_type); 10587 ev.confirm_hint = confirm_hint; 10588 ev.value = cpu_to_le32(value); 10589 10590 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), 10591 NULL); 10592 } 10593 10594 int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, 10595 u8 link_type, u8 addr_type) 10596 { 10597 struct mgmt_ev_user_passkey_request ev; 10598 10599 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); 10600 10601 bacpy(&ev.addr.bdaddr, bdaddr); 10602 ev.addr.type = link_to_bdaddr(link_type, addr_type); 10603 10604 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev), 10605 NULL); 10606 } 10607 10608 static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 10609 u8 link_type, u8 addr_type, u8 status, 10610 u8 opcode) 10611 { 10612 struct mgmt_pending_cmd *cmd; 10613 10614 cmd = pending_find(opcode, hdev); 10615 if (!cmd) 10616 return -ENOENT; 10617 10618 cmd->cmd_complete(cmd, mgmt_status(status)); 10619 mgmt_pending_remove(cmd); 10620 10621 return 0; 10622 } 10623 10624 int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 10625 u8 link_type, u8 addr_type, u8 status) 10626 { 10627 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, 10628 status, MGMT_OP_USER_CONFIRM_REPLY); 10629 } 10630 10631 int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 10632 u8 link_type, u8 addr_type, u8 status) 10633 { 10634 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, 10635 status, 10636 MGMT_OP_USER_CONFIRM_NEG_REPLY); 10637 } 10638 10639 int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 10640 u8 link_type, u8 addr_type, u8 status) 10641 { 10642 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, 10643 status, MGMT_OP_USER_PASSKEY_REPLY); 10644 } 10645 10646 int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 10647 u8 link_type, u8 addr_type, u8 status) 10648 { 10649 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, 10650 status, 10651 MGMT_OP_USER_PASSKEY_NEG_REPLY); 10652 } 10653 10654 int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, 10655 u8 link_type, u8 addr_type, u32 passkey, 10656 u8 entered) 10657 { 10658 struct mgmt_ev_passkey_notify ev; 10659 10660 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr); 10661 10662 bacpy(&ev.addr.bdaddr, bdaddr); 10663 ev.addr.type = link_to_bdaddr(link_type, addr_type); 10664 ev.passkey = __cpu_to_le32(passkey); 10665 ev.entered = entered; 10666 10667 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); 10668 } 10669 10670 void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) 10671 { 10672 struct mgmt_ev_auth_failed ev; 10673 struct mgmt_pending_cmd *cmd; 10674 u8 status = mgmt_status(hci_status); 10675 10676 bacpy(&ev.addr.bdaddr, &conn->dst); 10677 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); 10678 ev.status = status; 10679 10680 cmd = remove_pairing(conn); 10681 10682 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), 10683 cmd ? cmd->sk : NULL); 10684 10685 if (cmd) { 10686 cmd->cmd_complete(cmd, status); 10687 mgmt_pending_free(cmd); 10688 } 10689 } 10690 10691 void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) 10692 { 10693 struct cmd_lookup match = { NULL, hdev }; 10694 bool changed; 10695 10696 if (status) { 10697 u8 mgmt_err = mgmt_status(status); 10698 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, true, 10699 cmd_status_rsp, &mgmt_err); 10700 return; 10701 } 10702 10703 if (test_bit(HCI_AUTH, &hdev->flags)) 10704 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); 10705 else 10706 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); 10707 10708 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, true, 10709 settings_rsp, &match); 10710 10711 if (changed) 10712 new_settings(hdev, match.sk); 10713 10714 if (match.sk) 10715 sock_put(match.sk); 10716 } 10717 10718 static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) 10719 { 10720 struct cmd_lookup *match = data; 10721 10722 if (match->sk == NULL) { 10723 match->sk = cmd->sk; 10724 sock_hold(match->sk); 10725 } 10726 } 10727 10728 void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, 10729 u8 status) 10730 { 10731 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; 10732 10733 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, false, sk_lookup, 10734 &match); 10735 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, false, sk_lookup, 10736 &match); 10737 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, false, sk_lookup, 10738 &match); 10739 10740 if (!status) { 10741 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 10742 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL); 10743 ext_info_changed(hdev, NULL); 10744 } 10745 10746 if (match.sk) 10747 sock_put(match.sk); 10748 } 10749 10750 void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) 10751 { 10752 struct mgmt_cp_set_local_name ev; 10753 struct mgmt_pending_cmd *cmd; 10754 10755 if (status) 10756 return; 10757 10758 memset(&ev, 0, sizeof(ev)); 10759 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); 10760 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); 10761 10762 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); 10763 if (!cmd) { 10764 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); 10765 10766 /* If this is a HCI command related to powering on the 10767 * HCI dev don't send any mgmt signals. 10768 */ 10769 if (hci_dev_test_flag(hdev, HCI_POWERING_DOWN)) 10770 return; 10771 10772 if (pending_find(MGMT_OP_SET_POWERED, hdev)) 10773 return; 10774 } 10775 10776 mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), 10777 HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL); 10778 ext_info_changed(hdev, cmd ? cmd->sk : NULL); 10779 } 10780 10781 static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) 10782 { 10783 int i; 10784 10785 for (i = 0; i < uuid_count; i++) { 10786 if (!memcmp(uuid, uuids[i], 16)) 10787 return true; 10788 } 10789 10790 return false; 10791 } 10792 10793 static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) 10794 { 10795 u16 parsed = 0; 10796 10797 while (parsed < eir_len) { 10798 u8 field_len = eir[0]; 10799 u8 uuid[16]; 10800 int i; 10801 10802 if (field_len == 0) 10803 break; 10804 10805 if (eir_len - parsed < field_len + 1) 10806 break; 10807 10808 switch (eir[1]) { 10809 case EIR_UUID16_ALL: 10810 case EIR_UUID16_SOME: 10811 for (i = 0; i + 3 <= field_len; i += 2) { 10812 memcpy(uuid, bluetooth_base_uuid, 16); 10813 uuid[13] = eir[i + 3]; 10814 uuid[12] = eir[i + 2]; 10815 if (has_uuid(uuid, uuid_count, uuids)) 10816 return true; 10817 } 10818 break; 10819 case EIR_UUID32_ALL: 10820 case EIR_UUID32_SOME: 10821 for (i = 0; i + 5 <= field_len; i += 4) { 10822 memcpy(uuid, bluetooth_base_uuid, 16); 10823 uuid[15] = eir[i + 5]; 10824 uuid[14] = eir[i + 4]; 10825 uuid[13] = eir[i + 3]; 10826 uuid[12] = eir[i + 2]; 10827 if (has_uuid(uuid, uuid_count, uuids)) 10828 return true; 10829 } 10830 break; 10831 case EIR_UUID128_ALL: 10832 case EIR_UUID128_SOME: 10833 for (i = 0; i + 17 <= field_len; i += 16) { 10834 memcpy(uuid, eir + i + 2, 16); 10835 if (has_uuid(uuid, uuid_count, uuids)) 10836 return true; 10837 } 10838 break; 10839 } 10840 10841 parsed += field_len + 1; 10842 eir += field_len + 1; 10843 } 10844 10845 return false; 10846 } 10847 10848 static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, 10849 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) 10850 { 10851 /* If a RSSI threshold has been specified, and 10852 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with 10853 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk 10854 * is set, let it through for further processing, as we might need to 10855 * restart the scan. 10856 * 10857 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, 10858 * the results are also dropped. 10859 */ 10860 if (hdev->discovery.rssi != HCI_RSSI_INVALID && 10861 (rssi == HCI_RSSI_INVALID || 10862 (rssi < hdev->discovery.rssi && 10863 !hci_test_quirk(hdev, HCI_QUIRK_STRICT_DUPLICATE_FILTER)))) 10864 return false; 10865 10866 spin_lock(&hdev->discovery.lock); 10867 if (hdev->discovery.uuid_count != 0) { 10868 /* If a list of UUIDs is provided in filter, results with no 10869 * matching UUID should be dropped. 10870 */ 10871 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, 10872 hdev->discovery.uuids) && 10873 !eir_has_uuids(scan_rsp, scan_rsp_len, 10874 hdev->discovery.uuid_count, 10875 hdev->discovery.uuids)) { 10876 spin_unlock(&hdev->discovery.lock); 10877 return false; 10878 } 10879 } 10880 spin_unlock(&hdev->discovery.lock); 10881 10882 /* If duplicate filtering does not report RSSI changes, then restart 10883 * scanning to ensure updated result with updated RSSI values. 10884 */ 10885 if (hci_test_quirk(hdev, HCI_QUIRK_STRICT_DUPLICATE_FILTER)) { 10886 /* Validate RSSI value against the RSSI threshold once more. */ 10887 if (hdev->discovery.rssi != HCI_RSSI_INVALID && 10888 rssi < hdev->discovery.rssi) 10889 return false; 10890 } 10891 10892 return true; 10893 } 10894 10895 void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle, 10896 bdaddr_t *bdaddr, u8 addr_type) 10897 { 10898 struct mgmt_ev_adv_monitor_device_lost ev; 10899 10900 ev.monitor_handle = cpu_to_le16(handle); 10901 bacpy(&ev.addr.bdaddr, bdaddr); 10902 ev.addr.type = addr_type; 10903 10904 mgmt_event(MGMT_EV_ADV_MONITOR_DEVICE_LOST, hdev, &ev, sizeof(ev), 10905 NULL); 10906 } 10907 10908 static void mgmt_send_adv_monitor_device_found(struct hci_dev *hdev, 10909 struct sk_buff *skb, 10910 struct sock *skip_sk, 10911 u16 handle) 10912 { 10913 struct sk_buff *advmon_skb; 10914 size_t advmon_skb_len; 10915 __le16 *monitor_handle; 10916 10917 if (!skb) 10918 return; 10919 10920 advmon_skb_len = (sizeof(struct mgmt_ev_adv_monitor_device_found) - 10921 sizeof(struct mgmt_ev_device_found)) + skb->len; 10922 advmon_skb = mgmt_alloc_skb(hdev, MGMT_EV_ADV_MONITOR_DEVICE_FOUND, 10923 advmon_skb_len); 10924 if (!advmon_skb) 10925 return; 10926 10927 /* ADV_MONITOR_DEVICE_FOUND is similar to DEVICE_FOUND event except 10928 * that it also has 'monitor_handle'. Make a copy of DEVICE_FOUND and 10929 * store monitor_handle of the matched monitor. 10930 */ 10931 monitor_handle = skb_put(advmon_skb, sizeof(*monitor_handle)); 10932 *monitor_handle = cpu_to_le16(handle); 10933 skb_put_data(advmon_skb, skb->data, skb->len); 10934 10935 mgmt_event_skb(advmon_skb, skip_sk); 10936 } 10937 10938 static void mgmt_adv_monitor_device_found(struct hci_dev *hdev, 10939 bdaddr_t *bdaddr, bool report_device, 10940 struct sk_buff *skb, 10941 struct sock *skip_sk) 10942 { 10943 struct monitored_device *dev, *tmp; 10944 bool matched = false; 10945 bool notified = false; 10946 10947 /* We have received the Advertisement Report because: 10948 * 1. the kernel has initiated active discovery 10949 * 2. if not, we have pend_le_reports > 0 in which case we are doing 10950 * passive scanning 10951 * 3. if none of the above is true, we have one or more active 10952 * Advertisement Monitor 10953 * 10954 * For case 1 and 2, report all advertisements via MGMT_EV_DEVICE_FOUND 10955 * and report ONLY one advertisement per device for the matched Monitor 10956 * via MGMT_EV_ADV_MONITOR_DEVICE_FOUND event. 10957 * 10958 * For case 3, since we are not active scanning and all advertisements 10959 * received are due to a matched Advertisement Monitor, report all 10960 * advertisements ONLY via MGMT_EV_ADV_MONITOR_DEVICE_FOUND event. 10961 */ 10962 if (report_device && !hdev->advmon_pend_notify) { 10963 mgmt_event_skb(skb, skip_sk); 10964 return; 10965 } 10966 10967 hdev->advmon_pend_notify = false; 10968 10969 list_for_each_entry_safe(dev, tmp, &hdev->monitored_devices, list) { 10970 if (!bacmp(&dev->bdaddr, bdaddr)) { 10971 matched = true; 10972 10973 if (!dev->notified) { 10974 mgmt_send_adv_monitor_device_found(hdev, skb, 10975 skip_sk, 10976 dev->handle); 10977 notified = true; 10978 dev->notified = true; 10979 } 10980 } 10981 10982 if (!dev->notified) 10983 hdev->advmon_pend_notify = true; 10984 } 10985 10986 if (!report_device && 10987 ((matched && !notified) || !msft_monitor_supported(hdev))) { 10988 /* Handle 0 indicates that we are not active scanning and this 10989 * is a subsequent advertisement report for an already matched 10990 * Advertisement Monitor or the controller offloading support 10991 * is not available. 10992 */ 10993 mgmt_send_adv_monitor_device_found(hdev, skb, skip_sk, 0); 10994 } 10995 10996 if (report_device) 10997 mgmt_event_skb(skb, skip_sk); 10998 else 10999 kfree_skb(skb); 11000 } 11001 11002 static void mesh_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, 11003 u8 addr_type, s8 rssi, u32 flags, u8 *eir, 11004 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len, 11005 u64 instant) 11006 { 11007 struct sk_buff *skb; 11008 struct mgmt_ev_mesh_device_found *ev; 11009 int i, j; 11010 11011 if (!hdev->mesh_ad_types[0]) 11012 goto accepted; 11013 11014 /* Scan for requested AD types */ 11015 if (eir_len > 0) { 11016 for (i = 0; i + 1 < eir_len; i += eir[i] + 1) { 11017 for (j = 0; j < sizeof(hdev->mesh_ad_types); j++) { 11018 if (!hdev->mesh_ad_types[j]) 11019 break; 11020 11021 if (hdev->mesh_ad_types[j] == eir[i + 1]) 11022 goto accepted; 11023 } 11024 } 11025 } 11026 11027 if (scan_rsp_len > 0) { 11028 for (i = 0; i + 1 < scan_rsp_len; i += scan_rsp[i] + 1) { 11029 for (j = 0; j < sizeof(hdev->mesh_ad_types); j++) { 11030 if (!hdev->mesh_ad_types[j]) 11031 break; 11032 11033 if (hdev->mesh_ad_types[j] == scan_rsp[i + 1]) 11034 goto accepted; 11035 } 11036 } 11037 } 11038 11039 return; 11040 11041 accepted: 11042 skb = mgmt_alloc_skb(hdev, MGMT_EV_MESH_DEVICE_FOUND, 11043 sizeof(*ev) + eir_len + scan_rsp_len); 11044 if (!skb) 11045 return; 11046 11047 ev = skb_put(skb, sizeof(*ev)); 11048 11049 bacpy(&ev->addr.bdaddr, bdaddr); 11050 ev->addr.type = link_to_bdaddr(LE_LINK, addr_type); 11051 ev->rssi = rssi; 11052 ev->flags = cpu_to_le32(flags); 11053 ev->instant = cpu_to_le64(instant); 11054 11055 if (eir_len > 0) 11056 /* Copy EIR or advertising data into event */ 11057 skb_put_data(skb, eir, eir_len); 11058 11059 if (scan_rsp_len > 0) 11060 /* Append scan response data to event */ 11061 skb_put_data(skb, scan_rsp, scan_rsp_len); 11062 11063 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); 11064 11065 mgmt_event_skb(skb, NULL); 11066 } 11067 11068 void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, 11069 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, 11070 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len, 11071 u64 instant) 11072 { 11073 struct sk_buff *skb; 11074 struct mgmt_ev_device_found *ev; 11075 bool report_device = hci_discovery_active(hdev); 11076 11077 if (hci_dev_test_flag(hdev, HCI_MESH) && link_type == LE_LINK) 11078 mesh_device_found(hdev, bdaddr, addr_type, rssi, flags, 11079 eir, eir_len, scan_rsp, scan_rsp_len, 11080 instant); 11081 11082 /* Don't send events for a non-kernel initiated discovery. With 11083 * LE one exception is if we have pend_le_reports > 0 in which 11084 * case we're doing passive scanning and want these events. 11085 */ 11086 if (!hci_discovery_active(hdev)) { 11087 if (link_type == ACL_LINK) 11088 return; 11089 if (link_type == LE_LINK && !list_empty(&hdev->pend_le_reports)) 11090 report_device = true; 11091 else if (!hci_is_adv_monitoring(hdev)) 11092 return; 11093 } 11094 11095 if (hdev->discovery.result_filtering) { 11096 /* We are using service discovery */ 11097 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, 11098 scan_rsp_len)) 11099 return; 11100 } 11101 11102 if (hdev->discovery.limited) { 11103 /* Check for limited discoverable bit */ 11104 if (dev_class) { 11105 if (!(dev_class[1] & 0x20)) 11106 return; 11107 } else { 11108 u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL); 11109 if (!flags || !(flags[0] & LE_AD_LIMITED)) 11110 return; 11111 } 11112 } 11113 11114 /* Allocate skb. The 5 extra bytes are for the potential CoD field */ 11115 skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_FOUND, 11116 sizeof(*ev) + eir_len + scan_rsp_len + 5); 11117 if (!skb) 11118 return; 11119 11120 ev = skb_put(skb, sizeof(*ev)); 11121 11122 /* In case of device discovery with BR/EDR devices (pre 1.2), the 11123 * RSSI value was reported as 0 when not available. This behavior 11124 * is kept when using device discovery. This is required for full 11125 * backwards compatibility with the API. 11126 * 11127 * However when using service discovery, the value 127 will be 11128 * returned when the RSSI is not available. 11129 */ 11130 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && 11131 link_type == ACL_LINK) 11132 rssi = 0; 11133 11134 bacpy(&ev->addr.bdaddr, bdaddr); 11135 ev->addr.type = link_to_bdaddr(link_type, addr_type); 11136 ev->rssi = rssi; 11137 ev->flags = cpu_to_le32(flags); 11138 11139 if (eir_len > 0) 11140 /* Copy EIR or advertising data into event */ 11141 skb_put_data(skb, eir, eir_len); 11142 11143 if (dev_class && !eir_get_data(eir, eir_len, EIR_CLASS_OF_DEV, NULL)) { 11144 u8 eir_cod[5]; 11145 11146 eir_len += eir_append_data(eir_cod, 0, EIR_CLASS_OF_DEV, 11147 dev_class, 3); 11148 skb_put_data(skb, eir_cod, sizeof(eir_cod)); 11149 } 11150 11151 if (scan_rsp_len > 0) 11152 /* Append scan response data to event */ 11153 skb_put_data(skb, scan_rsp, scan_rsp_len); 11154 11155 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); 11156 11157 mgmt_adv_monitor_device_found(hdev, bdaddr, report_device, skb, NULL); 11158 } 11159 11160 void mgmt_security_level_changed(struct hci_conn *conn) 11161 { 11162 struct mgmt_ev_security_level_changed *ev; 11163 struct mgmt_tlv *tlv; 11164 u8 *tlv_data; 11165 u8 tlv_len; 11166 11167 if (!test_bit(HCI_CONN_MGMT_CONNECTED, conn->flags)) 11168 return; 11169 11170 /* Only ACL, LE and BIS links are tracked as mgmt connections, so 11171 * restrict the event to those link types. Other transports (SCO, 11172 * CIS, PA sync, ...) share the parent's address and would otherwise 11173 * report a spurious security level change for it. 11174 */ 11175 if (conn->type != ACL_LINK && conn->type != LE_LINK && 11176 conn->type != BIS_LINK) 11177 return; 11178 11179 tlv_len = 2 * (sizeof(*tlv) + sizeof(__u8)); 11180 ev = kzalloc_flex(*ev, tlv_data, tlv_len); 11181 if (!ev) 11182 return; 11183 11184 bacpy(&ev->addr.bdaddr, &conn->dst); 11185 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); 11186 ev->tlv_length = tlv_len; 11187 11188 tlv_data = ev->tlv_data; 11189 tlv = (void *)tlv_data; 11190 tlv->type = cpu_to_le16(MGMT_SEC_LEVEL_CHANGED_PARAM_LEVEL); 11191 tlv->length = sizeof(__u8); 11192 tlv->value[0] = conn->sec_level; 11193 11194 tlv_data += sizeof(*tlv) + sizeof(__u8); 11195 tlv = (void *)tlv_data; 11196 tlv->type = cpu_to_le16(MGMT_SEC_LEVEL_CHANGED_PARAM_ENC_TYPE); 11197 tlv->length = sizeof(__u8); 11198 if (!test_bit(HCI_CONN_ENCRYPT, conn->flags)) 11199 tlv->value[0] = MGMT_CONN_SEC_ENCRYPT_NONE; 11200 else if (test_bit(HCI_CONN_AES_CCM, conn->flags)) 11201 tlv->value[0] = MGMT_CONN_SEC_ENCRYPT_AES_CCM; 11202 else 11203 tlv->value[0] = MGMT_CONN_SEC_ENCRYPT_E0; 11204 11205 mgmt_event(MGMT_EV_SECURITY_LEVEL_CHANGED, conn->hdev, ev, 11206 struct_size(ev, tlv_data, tlv_len), NULL); 11207 11208 kfree(ev); 11209 } 11210 11211 void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, 11212 u8 addr_type, s8 rssi, u8 *name, u8 name_len) 11213 { 11214 struct sk_buff *skb; 11215 struct mgmt_ev_device_found *ev; 11216 u16 eir_len = 0; 11217 u32 flags = 0; 11218 11219 skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_FOUND, 11220 sizeof(*ev) + (name ? eir_precalc_len(name_len) : 0)); 11221 if (!skb) 11222 return; 11223 11224 ev = skb_put(skb, sizeof(*ev)); 11225 bacpy(&ev->addr.bdaddr, bdaddr); 11226 ev->addr.type = link_to_bdaddr(link_type, addr_type); 11227 ev->rssi = rssi; 11228 11229 if (name) 11230 eir_len += eir_skb_put_data(skb, EIR_NAME_COMPLETE, name, name_len); 11231 else 11232 flags = MGMT_DEV_FOUND_NAME_REQUEST_FAILED; 11233 11234 ev->eir_len = cpu_to_le16(eir_len); 11235 ev->flags = cpu_to_le32(flags); 11236 11237 mgmt_event_skb(skb, NULL); 11238 } 11239 11240 void mgmt_discovering(struct hci_dev *hdev, u8 discovering) 11241 { 11242 struct mgmt_ev_discovering ev; 11243 11244 bt_dev_dbg(hdev, "discovering %u", discovering); 11245 11246 memset(&ev, 0, sizeof(ev)); 11247 ev.type = hdev->discovery.type; 11248 ev.discovering = discovering; 11249 11250 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); 11251 } 11252 11253 void mgmt_suspending(struct hci_dev *hdev, u8 state) 11254 { 11255 struct mgmt_ev_controller_suspend ev; 11256 11257 ev.suspend_state = state; 11258 mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL); 11259 } 11260 11261 void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr, 11262 u8 addr_type) 11263 { 11264 struct mgmt_ev_controller_resume ev; 11265 11266 ev.wake_reason = reason; 11267 if (bdaddr) { 11268 bacpy(&ev.addr.bdaddr, bdaddr); 11269 ev.addr.type = addr_type; 11270 } else { 11271 memset(&ev.addr, 0, sizeof(ev.addr)); 11272 } 11273 11274 mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL); 11275 } 11276 11277 static struct hci_mgmt_chan chan = { 11278 .channel = HCI_CHANNEL_CONTROL, 11279 .handler_count = ARRAY_SIZE(mgmt_handlers), 11280 .handlers = mgmt_handlers, 11281 .hdev_init = mgmt_init_hdev, 11282 }; 11283 11284 int mgmt_init(void) 11285 { 11286 return hci_mgmt_chan_register(&chan); 11287 } 11288 11289 void mgmt_conn_subrate_notify(struct hci_dev *hdev, struct hci_conn *conn, 11290 u8 status) 11291 { 11292 struct mgmt_ev_conn_subrate ev; 11293 11294 bacpy(&ev.addr.bdaddr, &conn->dst); 11295 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); 11296 ev.status = mgmt_status(status); 11297 ev.interval = cpu_to_le16(conn->le_rate_interval); 11298 ev.subrate = cpu_to_le16(conn->le_subrate); 11299 ev.latency = cpu_to_le16(conn->le_rate_latency); 11300 ev.cont_num = cpu_to_le16(conn->le_cont_num); 11301 ev.supv_timeout = cpu_to_le16(conn->le_rate_supv_timeout); 11302 11303 mgmt_event(MGMT_EV_CONN_SUBRATE, hdev, &ev, sizeof(ev), NULL); 11304 } 11305 11306 void mgmt_exit(void) 11307 { 11308 hci_mgmt_chan_unregister(&chan); 11309 } 11310 11311 void mgmt_cleanup(struct sock *sk) 11312 { 11313 struct mgmt_mesh_tx *mesh_tx; 11314 struct hci_dev *hdev; 11315 11316 read_lock(&hci_dev_list_lock); 11317 11318 list_for_each_entry(hdev, &hci_dev_list, list) { 11319 do { 11320 mesh_tx = mgmt_mesh_next(hdev, sk); 11321 11322 if (mesh_tx) 11323 mesh_send_complete(hdev, mesh_tx, true); 11324 } while (mesh_tx); 11325 } 11326 11327 read_unlock(&hci_dev_list_lock); 11328 } 11329