Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 3 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4 | Copyright (C) 2010 Nokia Corporation |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 5 | Copyright (C) 2011-2012 Intel Corporation |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
| 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI Management interface */ |
| 26 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | 7129069 | 2015-02-20 13:26:23 +0200 | [diff] [blame] | 32 | #include <net/bluetooth/hci_sock.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 33 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 34 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 35 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 36 | #include "hci_request.h" |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 37 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 38 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 39 | #define MGMT_VERSION 1 |
Marcel Holtmann | beb1c21 | 2015-03-10 14:04:52 -0700 | [diff] [blame] | 40 | #define MGMT_REVISION 9 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 41 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 42 | static const u16 mgmt_commands[] = { |
| 43 | MGMT_OP_READ_INDEX_LIST, |
| 44 | MGMT_OP_READ_INFO, |
| 45 | MGMT_OP_SET_POWERED, |
| 46 | MGMT_OP_SET_DISCOVERABLE, |
| 47 | MGMT_OP_SET_CONNECTABLE, |
| 48 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 49 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 50 | MGMT_OP_SET_LINK_SECURITY, |
| 51 | MGMT_OP_SET_SSP, |
| 52 | MGMT_OP_SET_HS, |
| 53 | MGMT_OP_SET_LE, |
| 54 | MGMT_OP_SET_DEV_CLASS, |
| 55 | MGMT_OP_SET_LOCAL_NAME, |
| 56 | MGMT_OP_ADD_UUID, |
| 57 | MGMT_OP_REMOVE_UUID, |
| 58 | MGMT_OP_LOAD_LINK_KEYS, |
| 59 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 60 | MGMT_OP_DISCONNECT, |
| 61 | MGMT_OP_GET_CONNECTIONS, |
| 62 | MGMT_OP_PIN_CODE_REPLY, |
| 63 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 64 | MGMT_OP_SET_IO_CAPABILITY, |
| 65 | MGMT_OP_PAIR_DEVICE, |
| 66 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 67 | MGMT_OP_UNPAIR_DEVICE, |
| 68 | MGMT_OP_USER_CONFIRM_REPLY, |
| 69 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 70 | MGMT_OP_USER_PASSKEY_REPLY, |
| 71 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 72 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 73 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 74 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 75 | MGMT_OP_START_DISCOVERY, |
| 76 | MGMT_OP_STOP_DISCOVERY, |
| 77 | MGMT_OP_CONFIRM_NAME, |
| 78 | MGMT_OP_BLOCK_DEVICE, |
| 79 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 80 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 81 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 82 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 83 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 84 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 85 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 86 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 87 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 88 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 89 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 90 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 91 | MGMT_OP_ADD_DEVICE, |
| 92 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 93 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 94 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 95 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 96 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 97 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 98 | MGMT_OP_START_SERVICE_DISCOVERY, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 99 | MGMT_OP_READ_EXT_INDEX_LIST, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 100 | MGMT_OP_READ_ADV_FEATURES, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static const u16 mgmt_events[] = { |
| 104 | MGMT_EV_CONTROLLER_ERROR, |
| 105 | MGMT_EV_INDEX_ADDED, |
| 106 | MGMT_EV_INDEX_REMOVED, |
| 107 | MGMT_EV_NEW_SETTINGS, |
| 108 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 109 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 110 | MGMT_EV_NEW_LINK_KEY, |
| 111 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 112 | MGMT_EV_DEVICE_CONNECTED, |
| 113 | MGMT_EV_DEVICE_DISCONNECTED, |
| 114 | MGMT_EV_CONNECT_FAILED, |
| 115 | MGMT_EV_PIN_CODE_REQUEST, |
| 116 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 117 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 118 | MGMT_EV_AUTH_FAILED, |
| 119 | MGMT_EV_DEVICE_FOUND, |
| 120 | MGMT_EV_DISCOVERING, |
| 121 | MGMT_EV_DEVICE_BLOCKED, |
| 122 | MGMT_EV_DEVICE_UNBLOCKED, |
| 123 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 124 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 125 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 126 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 127 | MGMT_EV_DEVICE_ADDED, |
| 128 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 129 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 130 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 131 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 132 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 133 | MGMT_EV_EXT_INDEX_ADDED, |
| 134 | MGMT_EV_EXT_INDEX_REMOVED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 135 | }; |
| 136 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 137 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 138 | |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 139 | #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ |
| 140 | "\x00\x00\x00\x00\x00\x00\x00\x00" |
| 141 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 142 | struct mgmt_pending_cmd { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 143 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 144 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 145 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 146 | void *param; |
Johan Hedberg | 323b0b8 | 2014-12-05 13:36:01 +0200 | [diff] [blame] | 147 | size_t param_len; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 148 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 149 | void *user_data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 150 | int (*cmd_complete)(struct mgmt_pending_cmd *cmd, u8 status); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 151 | }; |
| 152 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 153 | /* HCI to MGMT error code conversion table */ |
| 154 | static u8 mgmt_status_table[] = { |
| 155 | MGMT_STATUS_SUCCESS, |
| 156 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 157 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 158 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 159 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 160 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 161 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 162 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 163 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 164 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 165 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 166 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 167 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 168 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 169 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 170 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 171 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 172 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 173 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 174 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 175 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 176 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 177 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 178 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 179 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 180 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 181 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 182 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 183 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 184 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 185 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 186 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 187 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 188 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 189 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 190 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 191 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 192 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 193 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 194 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 195 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 196 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 197 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 198 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 199 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 200 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 201 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 202 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 203 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 204 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 205 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 206 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 207 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 208 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 209 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 210 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 211 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 212 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 213 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 214 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 215 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 216 | }; |
| 217 | |
| 218 | static u8 mgmt_status(u8 hci_status) |
| 219 | { |
| 220 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 221 | return mgmt_status_table[hci_status]; |
| 222 | |
| 223 | return MGMT_STATUS_FAILED; |
| 224 | } |
| 225 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 226 | static int mgmt_send_event(u16 event, struct hci_dev *hdev, |
| 227 | unsigned short channel, void *data, u16 data_len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 228 | int flag, struct sock *skip_sk) |
Marcel Holtmann | 04c60f05 | 2014-07-04 19:06:22 +0200 | [diff] [blame] | 229 | { |
| 230 | struct sk_buff *skb; |
| 231 | struct mgmt_hdr *hdr; |
| 232 | |
| 233 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
| 234 | if (!skb) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 238 | hdr->opcode = cpu_to_le16(event); |
| 239 | if (hdev) |
| 240 | hdr->index = cpu_to_le16(hdev->id); |
| 241 | else |
| 242 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 243 | hdr->len = cpu_to_le16(data_len); |
| 244 | |
| 245 | if (data) |
| 246 | memcpy(skb_put(skb, data_len), data, data_len); |
| 247 | |
| 248 | /* Time stamp */ |
| 249 | __net_timestamp(skb); |
| 250 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 251 | hci_send_to_channel(channel, skb, flag, skip_sk); |
Marcel Holtmann | 04c60f05 | 2014-07-04 19:06:22 +0200 | [diff] [blame] | 252 | kfree_skb(skb); |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 257 | static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data, |
| 258 | u16 len, int flag) |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 259 | { |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 260 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 261 | flag, NULL); |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 264 | static int mgmt_generic_event(u16 event, struct hci_dev *hdev, void *data, |
| 265 | u16 len, struct sock *skip_sk) |
| 266 | { |
| 267 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
| 268 | HCI_MGMT_GENERIC_EVENTS, skip_sk); |
| 269 | } |
| 270 | |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 271 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len, |
| 272 | struct sock *skip_sk) |
| 273 | { |
| 274 | return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len, |
Marcel Holtmann | c08b1a1 | 2015-03-14 19:27:59 -0700 | [diff] [blame] | 275 | HCI_SOCK_TRUSTED, skip_sk); |
Johan Hedberg | 7a00ff4 | 2015-03-06 21:08:56 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 278 | static int mgmt_cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 279 | { |
| 280 | struct sk_buff *skb; |
| 281 | struct mgmt_hdr *hdr; |
| 282 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 283 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 284 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 285 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 286 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 287 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 288 | if (!skb) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 292 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 293 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 294 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 295 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 296 | |
| 297 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 298 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 299 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 300 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 301 | err = sock_queue_rcv_skb(sk, skb); |
| 302 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 303 | kfree_skb(skb); |
| 304 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 305 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 308 | static int mgmt_cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status, |
| 309 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 310 | { |
| 311 | struct sk_buff *skb; |
| 312 | struct mgmt_hdr *hdr; |
| 313 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 314 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 315 | |
| 316 | BT_DBG("sock %p", sk); |
| 317 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 318 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 319 | if (!skb) |
| 320 | return -ENOMEM; |
| 321 | |
| 322 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 323 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 324 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 325 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 326 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 327 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 328 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 329 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 330 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 331 | |
| 332 | if (rp) |
| 333 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 334 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 335 | err = sock_queue_rcv_skb(sk, skb); |
| 336 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 337 | kfree_skb(skb); |
| 338 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 339 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 340 | } |
| 341 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 342 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 343 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 344 | { |
| 345 | struct mgmt_rp_read_version rp; |
| 346 | |
| 347 | BT_DBG("sock %p", sk); |
| 348 | |
| 349 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 350 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 351 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 352 | return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, |
| 353 | &rp, sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 356 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 357 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 358 | { |
| 359 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 360 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 361 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 362 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 363 | size_t rp_size; |
| 364 | int i, err; |
| 365 | |
| 366 | BT_DBG("sock %p", sk); |
| 367 | |
| 368 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 369 | |
| 370 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 371 | if (!rp) |
| 372 | return -ENOMEM; |
| 373 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 374 | rp->num_commands = cpu_to_le16(num_commands); |
| 375 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 376 | |
| 377 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 378 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 379 | |
| 380 | for (i = 0; i < num_events; i++, opcode++) |
| 381 | put_unaligned_le16(mgmt_events[i], opcode); |
| 382 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 383 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, |
| 384 | rp, rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 385 | kfree(rp); |
| 386 | |
| 387 | return err; |
| 388 | } |
| 389 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 390 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 391 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 392 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 393 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 394 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 395 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 396 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 397 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 398 | |
| 399 | BT_DBG("sock %p", sk); |
| 400 | |
| 401 | read_lock(&hci_dev_list_lock); |
| 402 | |
| 403 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 404 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 405 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 406 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 407 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 408 | } |
| 409 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 410 | rp_len = sizeof(*rp) + (2 * count); |
| 411 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 412 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 413 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 414 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 415 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 416 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 417 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 418 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 419 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 420 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 421 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 422 | continue; |
| 423 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 424 | /* Devices marked as raw-only are neither configured |
| 425 | * nor unconfigured controllers. |
| 426 | */ |
| 427 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 428 | continue; |
| 429 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 430 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 431 | !hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 432 | rp->index[count++] = cpu_to_le16(d->id); |
| 433 | BT_DBG("Added hci%u", d->id); |
| 434 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 437 | rp->num_controllers = cpu_to_le16(count); |
| 438 | rp_len = sizeof(*rp) + (2 * count); |
| 439 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 440 | read_unlock(&hci_dev_list_lock); |
| 441 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 442 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, |
| 443 | 0, rp, rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 444 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 445 | kfree(rp); |
| 446 | |
| 447 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 450 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 451 | void *data, u16 data_len) |
| 452 | { |
| 453 | struct mgmt_rp_read_unconf_index_list *rp; |
| 454 | struct hci_dev *d; |
| 455 | size_t rp_len; |
| 456 | u16 count; |
| 457 | int err; |
| 458 | |
| 459 | BT_DBG("sock %p", sk); |
| 460 | |
| 461 | read_lock(&hci_dev_list_lock); |
| 462 | |
| 463 | count = 0; |
| 464 | list_for_each_entry(d, &hci_dev_list, list) { |
| 465 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 466 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 467 | count++; |
| 468 | } |
| 469 | |
| 470 | rp_len = sizeof(*rp) + (2 * count); |
| 471 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 472 | if (!rp) { |
| 473 | read_unlock(&hci_dev_list_lock); |
| 474 | return -ENOMEM; |
| 475 | } |
| 476 | |
| 477 | count = 0; |
| 478 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 479 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 480 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 481 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 482 | continue; |
| 483 | |
| 484 | /* Devices marked as raw-only are neither configured |
| 485 | * nor unconfigured controllers. |
| 486 | */ |
| 487 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 488 | continue; |
| 489 | |
| 490 | if (d->dev_type == HCI_BREDR && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 491 | hci_dev_test_flag(d, HCI_UNCONFIGURED)) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 492 | rp->index[count++] = cpu_to_le16(d->id); |
| 493 | BT_DBG("Added hci%u", d->id); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | rp->num_controllers = cpu_to_le16(count); |
| 498 | rp_len = sizeof(*rp) + (2 * count); |
| 499 | |
| 500 | read_unlock(&hci_dev_list_lock); |
| 501 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 502 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 503 | MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 504 | |
| 505 | kfree(rp); |
| 506 | |
| 507 | return err; |
| 508 | } |
| 509 | |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 510 | static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev, |
| 511 | void *data, u16 data_len) |
| 512 | { |
| 513 | struct mgmt_rp_read_ext_index_list *rp; |
| 514 | struct hci_dev *d; |
| 515 | size_t rp_len; |
| 516 | u16 count; |
| 517 | int err; |
| 518 | |
| 519 | BT_DBG("sock %p", sk); |
| 520 | |
| 521 | read_lock(&hci_dev_list_lock); |
| 522 | |
| 523 | count = 0; |
| 524 | list_for_each_entry(d, &hci_dev_list, list) { |
| 525 | if (d->dev_type == HCI_BREDR || d->dev_type == HCI_AMP) |
| 526 | count++; |
| 527 | } |
| 528 | |
| 529 | rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count); |
| 530 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 531 | if (!rp) { |
| 532 | read_unlock(&hci_dev_list_lock); |
| 533 | return -ENOMEM; |
| 534 | } |
| 535 | |
| 536 | count = 0; |
| 537 | list_for_each_entry(d, &hci_dev_list, list) { |
| 538 | if (hci_dev_test_flag(d, HCI_SETUP) || |
| 539 | hci_dev_test_flag(d, HCI_CONFIG) || |
| 540 | hci_dev_test_flag(d, HCI_USER_CHANNEL)) |
| 541 | continue; |
| 542 | |
| 543 | /* Devices marked as raw-only are neither configured |
| 544 | * nor unconfigured controllers. |
| 545 | */ |
| 546 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 547 | continue; |
| 548 | |
| 549 | if (d->dev_type == HCI_BREDR) { |
| 550 | if (hci_dev_test_flag(d, HCI_UNCONFIGURED)) |
| 551 | rp->entry[count].type = 0x01; |
| 552 | else |
| 553 | rp->entry[count].type = 0x00; |
| 554 | } else if (d->dev_type == HCI_AMP) { |
| 555 | rp->entry[count].type = 0x02; |
| 556 | } else { |
| 557 | continue; |
| 558 | } |
| 559 | |
| 560 | rp->entry[count].bus = d->bus; |
| 561 | rp->entry[count++].index = cpu_to_le16(d->id); |
| 562 | BT_DBG("Added hci%u", d->id); |
| 563 | } |
| 564 | |
| 565 | rp->num_controllers = cpu_to_le16(count); |
| 566 | rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count); |
| 567 | |
| 568 | read_unlock(&hci_dev_list_lock); |
| 569 | |
| 570 | /* If this command is called at least once, then all the |
| 571 | * default index and unconfigured index events are disabled |
| 572 | * and from now on only extended index events are used. |
| 573 | */ |
| 574 | hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS); |
| 575 | hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS); |
| 576 | hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS); |
| 577 | |
| 578 | err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, |
| 579 | MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len); |
| 580 | |
| 581 | kfree(rp); |
| 582 | |
| 583 | return err; |
| 584 | } |
| 585 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 586 | static bool is_configured(struct hci_dev *hdev) |
| 587 | { |
| 588 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 589 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 590 | return false; |
| 591 | |
| 592 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 593 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 594 | return false; |
| 595 | |
| 596 | return true; |
| 597 | } |
| 598 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 599 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 600 | { |
| 601 | u32 options = 0; |
| 602 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 603 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 604 | !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 605 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 606 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 607 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 608 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 609 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 610 | |
| 611 | return cpu_to_le32(options); |
| 612 | } |
| 613 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 614 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 615 | { |
| 616 | __le32 options = get_missing_options(hdev); |
| 617 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 618 | return mgmt_generic_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 619 | sizeof(options), skip); |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 622 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 623 | { |
| 624 | __le32 options = get_missing_options(hdev); |
| 625 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 626 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 627 | sizeof(options)); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 628 | } |
| 629 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 630 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 631 | void *data, u16 data_len) |
| 632 | { |
| 633 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 634 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 635 | |
| 636 | BT_DBG("sock %p %s", sk, hdev->name); |
| 637 | |
| 638 | hci_dev_lock(hdev); |
| 639 | |
| 640 | memset(&rp, 0, sizeof(rp)); |
| 641 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 642 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 643 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 644 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 645 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 646 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 647 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 648 | |
| 649 | rp.supported_options = cpu_to_le32(options); |
| 650 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 651 | |
| 652 | hci_dev_unlock(hdev); |
| 653 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 654 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, |
| 655 | &rp, sizeof(rp)); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 656 | } |
| 657 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 658 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 659 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 660 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 661 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 662 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 663 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 664 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 665 | settings |= MGMT_SETTING_CONNECTABLE; |
| 666 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 667 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 668 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 669 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 670 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 671 | settings |= MGMT_SETTING_BREDR; |
| 672 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 673 | |
| 674 | if (lmp_ssp_capable(hdev)) { |
| 675 | settings |= MGMT_SETTING_SSP; |
| 676 | settings |= MGMT_SETTING_HS; |
| 677 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 678 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 679 | if (lmp_sc_capable(hdev)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 680 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 681 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 682 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 683 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 684 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 685 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 686 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 687 | settings |= MGMT_SETTING_PRIVACY; |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 688 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 689 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 690 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 691 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 692 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 693 | settings |= MGMT_SETTING_CONFIGURATION; |
| 694 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 695 | return settings; |
| 696 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 697 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 698 | static u32 get_current_settings(struct hci_dev *hdev) |
| 699 | { |
| 700 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 701 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 702 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 703 | settings |= MGMT_SETTING_POWERED; |
| 704 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 705 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 706 | settings |= MGMT_SETTING_CONNECTABLE; |
| 707 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 708 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 709 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 710 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 711 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 712 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 713 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 714 | if (hci_dev_test_flag(hdev, HCI_BONDABLE)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 715 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 716 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 717 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 718 | settings |= MGMT_SETTING_BREDR; |
| 719 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 720 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 721 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 722 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 723 | if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 724 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 725 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 726 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 727 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 728 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 729 | if (hci_dev_test_flag(hdev, HCI_HS_ENABLED)) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 730 | settings |= MGMT_SETTING_HS; |
| 731 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 732 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 733 | settings |= MGMT_SETTING_ADVERTISING; |
| 734 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 735 | if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 736 | settings |= MGMT_SETTING_SECURE_CONN; |
| 737 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 738 | if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 739 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 740 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 741 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 742 | settings |= MGMT_SETTING_PRIVACY; |
| 743 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 744 | /* The current setting for static address has two purposes. The |
| 745 | * first is to indicate if the static address will be used and |
| 746 | * the second is to indicate if it is actually set. |
| 747 | * |
| 748 | * This means if the static address is not configured, this flag |
| 749 | * will never bet set. If the address is configured, then if the |
| 750 | * address is actually used decides if the flag is set or not. |
| 751 | * |
| 752 | * For single mode LE only controllers and dual-mode controllers |
| 753 | * with BR/EDR disabled, the existence of the static address will |
| 754 | * be evaluated. |
| 755 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 756 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 757 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 758 | !bacmp(&hdev->bdaddr, BDADDR_ANY)) { |
| 759 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 760 | settings |= MGMT_SETTING_STATIC_ADDRESS; |
| 761 | } |
| 762 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 763 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 766 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 767 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 768 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 769 | { |
| 770 | u8 *ptr = data, *uuids_start = NULL; |
| 771 | struct bt_uuid *uuid; |
| 772 | |
| 773 | if (len < 4) |
| 774 | return ptr; |
| 775 | |
| 776 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 777 | u16 uuid16; |
| 778 | |
| 779 | if (uuid->size != 16) |
| 780 | continue; |
| 781 | |
| 782 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 783 | if (uuid16 < 0x1100) |
| 784 | continue; |
| 785 | |
| 786 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 787 | continue; |
| 788 | |
| 789 | if (!uuids_start) { |
| 790 | uuids_start = ptr; |
| 791 | uuids_start[0] = 1; |
| 792 | uuids_start[1] = EIR_UUID16_ALL; |
| 793 | ptr += 2; |
| 794 | } |
| 795 | |
| 796 | /* Stop if not enough space to put next UUID */ |
| 797 | if ((ptr - data) + sizeof(u16) > len) { |
| 798 | uuids_start[1] = EIR_UUID16_SOME; |
| 799 | break; |
| 800 | } |
| 801 | |
| 802 | *ptr++ = (uuid16 & 0x00ff); |
| 803 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 804 | uuids_start[0] += sizeof(uuid16); |
| 805 | } |
| 806 | |
| 807 | return ptr; |
| 808 | } |
| 809 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 810 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 811 | { |
| 812 | u8 *ptr = data, *uuids_start = NULL; |
| 813 | struct bt_uuid *uuid; |
| 814 | |
| 815 | if (len < 6) |
| 816 | return ptr; |
| 817 | |
| 818 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 819 | if (uuid->size != 32) |
| 820 | continue; |
| 821 | |
| 822 | if (!uuids_start) { |
| 823 | uuids_start = ptr; |
| 824 | uuids_start[0] = 1; |
| 825 | uuids_start[1] = EIR_UUID32_ALL; |
| 826 | ptr += 2; |
| 827 | } |
| 828 | |
| 829 | /* Stop if not enough space to put next UUID */ |
| 830 | if ((ptr - data) + sizeof(u32) > len) { |
| 831 | uuids_start[1] = EIR_UUID32_SOME; |
| 832 | break; |
| 833 | } |
| 834 | |
| 835 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 836 | ptr += sizeof(u32); |
| 837 | uuids_start[0] += sizeof(u32); |
| 838 | } |
| 839 | |
| 840 | return ptr; |
| 841 | } |
| 842 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 843 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 844 | { |
| 845 | u8 *ptr = data, *uuids_start = NULL; |
| 846 | struct bt_uuid *uuid; |
| 847 | |
| 848 | if (len < 18) |
| 849 | return ptr; |
| 850 | |
| 851 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 852 | if (uuid->size != 128) |
| 853 | continue; |
| 854 | |
| 855 | if (!uuids_start) { |
| 856 | uuids_start = ptr; |
| 857 | uuids_start[0] = 1; |
| 858 | uuids_start[1] = EIR_UUID128_ALL; |
| 859 | ptr += 2; |
| 860 | } |
| 861 | |
| 862 | /* Stop if not enough space to put next UUID */ |
| 863 | if ((ptr - data) + 16 > len) { |
| 864 | uuids_start[1] = EIR_UUID128_SOME; |
| 865 | break; |
| 866 | } |
| 867 | |
| 868 | memcpy(ptr, uuid->uuid, 16); |
| 869 | ptr += 16; |
| 870 | uuids_start[0] += 16; |
| 871 | } |
| 872 | |
| 873 | return ptr; |
| 874 | } |
| 875 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 876 | static struct mgmt_pending_cmd *mgmt_pending_find(u16 opcode, |
| 877 | struct hci_dev *hdev) |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 878 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 879 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 880 | |
| 881 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 882 | if (cmd->opcode == opcode) |
| 883 | return cmd; |
| 884 | } |
| 885 | |
| 886 | return NULL; |
| 887 | } |
| 888 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 889 | static struct mgmt_pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 890 | struct hci_dev *hdev, |
| 891 | const void *data) |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 892 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 893 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 894 | |
| 895 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 896 | if (cmd->user_data != data) |
| 897 | continue; |
| 898 | if (cmd->opcode == opcode) |
| 899 | return cmd; |
| 900 | } |
| 901 | |
| 902 | return NULL; |
| 903 | } |
| 904 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 905 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 906 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 907 | u8 ad_len = 0; |
| 908 | size_t name_len; |
| 909 | |
| 910 | name_len = strlen(hdev->dev_name); |
| 911 | if (name_len > 0) { |
| 912 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 913 | |
| 914 | if (name_len > max_len) { |
| 915 | name_len = max_len; |
| 916 | ptr[1] = EIR_NAME_SHORT; |
| 917 | } else |
| 918 | ptr[1] = EIR_NAME_COMPLETE; |
| 919 | |
| 920 | ptr[0] = name_len + 1; |
| 921 | |
| 922 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 923 | |
| 924 | ad_len += (name_len + 2); |
| 925 | ptr += (name_len + 2); |
| 926 | } |
| 927 | |
| 928 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | static void update_scan_rsp_data(struct hci_request *req) |
| 932 | { |
| 933 | struct hci_dev *hdev = req->hdev; |
| 934 | struct hci_cp_le_set_scan_rsp_data cp; |
| 935 | u8 len; |
| 936 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 937 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 938 | return; |
| 939 | |
| 940 | memset(&cp, 0, sizeof(cp)); |
| 941 | |
| 942 | len = create_scan_rsp_data(hdev, cp.data); |
| 943 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 944 | if (hdev->scan_rsp_data_len == len && |
| 945 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 946 | return; |
| 947 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 948 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 949 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 950 | |
| 951 | cp.length = len; |
| 952 | |
| 953 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 954 | } |
| 955 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 956 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 957 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 958 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 959 | |
| 960 | /* If there's a pending mgmt command the flags will not yet have |
| 961 | * their final values, so check for this first. |
| 962 | */ |
| 963 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 964 | if (cmd) { |
| 965 | struct mgmt_mode *cp = cmd->param; |
| 966 | if (cp->val == 0x01) |
| 967 | return LE_AD_GENERAL; |
| 968 | else if (cp->val == 0x02) |
| 969 | return LE_AD_LIMITED; |
| 970 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 971 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 972 | return LE_AD_LIMITED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 973 | else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 974 | return LE_AD_GENERAL; |
| 975 | } |
| 976 | |
| 977 | return 0; |
| 978 | } |
| 979 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 980 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 981 | { |
| 982 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 983 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 984 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 985 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 986 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 987 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 988 | |
| 989 | if (flags) { |
| 990 | BT_DBG("adv flags 0x%02x", flags); |
| 991 | |
| 992 | ptr[0] = 2; |
| 993 | ptr[1] = EIR_FLAGS; |
| 994 | ptr[2] = flags; |
| 995 | |
| 996 | ad_len += 3; |
| 997 | ptr += 3; |
| 998 | } |
| 999 | |
| 1000 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 1001 | ptr[0] = 2; |
| 1002 | ptr[1] = EIR_TX_POWER; |
| 1003 | ptr[2] = (u8) hdev->adv_tx_power; |
| 1004 | |
| 1005 | ad_len += 3; |
| 1006 | ptr += 3; |
| 1007 | } |
| 1008 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1009 | return ad_len; |
| 1010 | } |
| 1011 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 1012 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1013 | { |
| 1014 | struct hci_dev *hdev = req->hdev; |
| 1015 | struct hci_cp_le_set_adv_data cp; |
| 1016 | u8 len; |
| 1017 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1018 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1019 | return; |
| 1020 | |
| 1021 | memset(&cp, 0, sizeof(cp)); |
| 1022 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 1023 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1024 | |
| 1025 | if (hdev->adv_data_len == len && |
| 1026 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 1027 | return; |
| 1028 | |
| 1029 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 1030 | hdev->adv_data_len = len; |
| 1031 | |
| 1032 | cp.length = len; |
| 1033 | |
| 1034 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 1035 | } |
| 1036 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1037 | int mgmt_update_adv_data(struct hci_dev *hdev) |
| 1038 | { |
| 1039 | struct hci_request req; |
| 1040 | |
| 1041 | hci_req_init(&req, hdev); |
| 1042 | update_adv_data(&req); |
| 1043 | |
| 1044 | return hci_req_run(&req, NULL); |
| 1045 | } |
| 1046 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1047 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 1048 | { |
| 1049 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1050 | size_t name_len; |
| 1051 | |
| 1052 | name_len = strlen(hdev->dev_name); |
| 1053 | |
| 1054 | if (name_len > 0) { |
| 1055 | /* EIR Data type */ |
| 1056 | if (name_len > 48) { |
| 1057 | name_len = 48; |
| 1058 | ptr[1] = EIR_NAME_SHORT; |
| 1059 | } else |
| 1060 | ptr[1] = EIR_NAME_COMPLETE; |
| 1061 | |
| 1062 | /* EIR Data length */ |
| 1063 | ptr[0] = name_len + 1; |
| 1064 | |
| 1065 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 1066 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1067 | ptr += (name_len + 2); |
| 1068 | } |
| 1069 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 1070 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 1071 | ptr[0] = 2; |
| 1072 | ptr[1] = EIR_TX_POWER; |
| 1073 | ptr[2] = (u8) hdev->inq_tx_power; |
| 1074 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 1075 | ptr += 3; |
| 1076 | } |
| 1077 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 1078 | if (hdev->devid_source > 0) { |
| 1079 | ptr[0] = 9; |
| 1080 | ptr[1] = EIR_DEVICE_ID; |
| 1081 | |
| 1082 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 1083 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 1084 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 1085 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 1086 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 1087 | ptr += 10; |
| 1088 | } |
| 1089 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 1090 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 1091 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 1092 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1093 | } |
| 1094 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1095 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1096 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1097 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1098 | struct hci_cp_write_eir cp; |
| 1099 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 1100 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1101 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 1102 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 1103 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1104 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1105 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1106 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1107 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1108 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1109 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1110 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1111 | |
| 1112 | memset(&cp, 0, sizeof(cp)); |
| 1113 | |
| 1114 | create_eir(hdev, cp.data); |
| 1115 | |
| 1116 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1117 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1118 | |
| 1119 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 1120 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1121 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 1122 | } |
| 1123 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1124 | static u8 get_service_classes(struct hci_dev *hdev) |
| 1125 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 1126 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1127 | u8 val = 0; |
| 1128 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 1129 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1130 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1131 | |
| 1132 | return val; |
| 1133 | } |
| 1134 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1135 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1136 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1137 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1138 | u8 cod[3]; |
| 1139 | |
| 1140 | BT_DBG("%s", hdev->name); |
| 1141 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 1142 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1143 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 1144 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1145 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 1146 | return; |
| 1147 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1148 | if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1149 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1150 | |
| 1151 | cod[0] = hdev->minor_class; |
| 1152 | cod[1] = hdev->major_class; |
| 1153 | cod[2] = get_service_classes(hdev); |
| 1154 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1155 | if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 1156 | cod[1] |= 0x20; |
| 1157 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1158 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1159 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1160 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1161 | hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1164 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1165 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1166 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1167 | |
| 1168 | /* If there's a pending mgmt command the flag will not yet have |
| 1169 | * it's final value, so check for this first. |
| 1170 | */ |
| 1171 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1172 | if (cmd) { |
| 1173 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1174 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1175 | } |
| 1176 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1177 | return hci_dev_test_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1178 | } |
| 1179 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1180 | static void disable_advertising(struct hci_request *req) |
| 1181 | { |
| 1182 | u8 enable = 0x00; |
| 1183 | |
| 1184 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1185 | } |
| 1186 | |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1187 | static void enable_advertising(struct hci_request *req) |
| 1188 | { |
| 1189 | struct hci_dev *hdev = req->hdev; |
| 1190 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1191 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1192 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1193 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1194 | if (hci_conn_num(hdev, LE_LINK) > 0) |
| 1195 | return; |
| 1196 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1197 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1198 | disable_advertising(req); |
| 1199 | |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1200 | /* Clear the HCI_LE_ADV bit temporarily so that the |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1201 | * hci_update_random_address knows that it's safe to go ahead |
| 1202 | * and write a new random address. The flag will be set back on |
| 1203 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 1204 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1205 | hci_dev_clear_flag(hdev, HCI_LE_ADV); |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1206 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1207 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 1208 | connectable = true; |
| 1209 | else |
| 1210 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1211 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1212 | /* Set require_privacy to true only when non-connectable |
| 1213 | * advertising is used. In that case it is fine to use a |
| 1214 | * non-resolvable private address. |
| 1215 | */ |
| 1216 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1217 | return; |
| 1218 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 1219 | memset(&cp, 0, sizeof(cp)); |
Georg Lukas | 628531c | 2014-07-26 13:59:57 +0200 | [diff] [blame] | 1220 | cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval); |
| 1221 | cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1222 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1223 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1224 | cp.channel_map = hdev->le_adv_channel_map; |
| 1225 | |
| 1226 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1227 | |
| 1228 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1229 | } |
| 1230 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1231 | static void service_cache_off(struct work_struct *work) |
| 1232 | { |
| 1233 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1234 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1235 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1236 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1237 | if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1238 | return; |
| 1239 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1240 | hci_req_init(&req, hdev); |
| 1241 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1242 | hci_dev_lock(hdev); |
| 1243 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1244 | update_eir(&req); |
| 1245 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1246 | |
| 1247 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1248 | |
| 1249 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1252 | static void rpa_expired(struct work_struct *work) |
| 1253 | { |
| 1254 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1255 | rpa_expired.work); |
| 1256 | struct hci_request req; |
| 1257 | |
| 1258 | BT_DBG(""); |
| 1259 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1260 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1261 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1262 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1263 | return; |
| 1264 | |
| 1265 | /* The generation of a new RPA and programming it into the |
| 1266 | * controller happens in the enable_advertising() function. |
| 1267 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1268 | hci_req_init(&req, hdev); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1269 | enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1270 | hci_req_run(&req, NULL); |
| 1271 | } |
| 1272 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1273 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1274 | { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1275 | if (hci_dev_test_and_set_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1276 | return; |
| 1277 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1278 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1279 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1280 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1281 | /* Non-mgmt controlled devices get this bit set |
| 1282 | * implicitly so that pairing works for them, however |
| 1283 | * for mgmt we require user-space to explicitly enable |
| 1284 | * it |
| 1285 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1286 | hci_dev_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1287 | } |
| 1288 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1289 | static int read_controller_info(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1290 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1291 | { |
| 1292 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1293 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1294 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1295 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1296 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1297 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1298 | memset(&rp, 0, sizeof(rp)); |
| 1299 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1300 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1301 | |
| 1302 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1303 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1304 | |
| 1305 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1306 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1307 | |
| 1308 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1309 | |
| 1310 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1311 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1312 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1313 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1314 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1315 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
| 1316 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1317 | } |
| 1318 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1319 | static void mgmt_pending_free(struct mgmt_pending_cmd *cmd) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1320 | { |
| 1321 | sock_put(cmd->sk); |
| 1322 | kfree(cmd->param); |
| 1323 | kfree(cmd); |
| 1324 | } |
| 1325 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1326 | static struct mgmt_pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
| 1327 | struct hci_dev *hdev, |
| 1328 | void *data, u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1329 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1330 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1331 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1332 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1333 | if (!cmd) |
| 1334 | return NULL; |
| 1335 | |
| 1336 | cmd->opcode = opcode; |
| 1337 | cmd->index = hdev->id; |
| 1338 | |
Johan Hedberg | 323b0b8 | 2014-12-05 13:36:01 +0200 | [diff] [blame] | 1339 | cmd->param = kmemdup(data, len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1340 | if (!cmd->param) { |
| 1341 | kfree(cmd); |
| 1342 | return NULL; |
| 1343 | } |
| 1344 | |
Johan Hedberg | 323b0b8 | 2014-12-05 13:36:01 +0200 | [diff] [blame] | 1345 | cmd->param_len = len; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1346 | |
| 1347 | cmd->sk = sk; |
| 1348 | sock_hold(sk); |
| 1349 | |
| 1350 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1351 | |
| 1352 | return cmd; |
| 1353 | } |
| 1354 | |
| 1355 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1356 | void (*cb)(struct mgmt_pending_cmd *cmd, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1357 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1358 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1359 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1360 | struct mgmt_pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1361 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1362 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1363 | if (opcode > 0 && cmd->opcode != opcode) |
| 1364 | continue; |
| 1365 | |
| 1366 | cb(cmd, data); |
| 1367 | } |
| 1368 | } |
| 1369 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1370 | static void mgmt_pending_remove(struct mgmt_pending_cmd *cmd) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1371 | { |
| 1372 | list_del(&cmd->list); |
| 1373 | mgmt_pending_free(cmd); |
| 1374 | } |
| 1375 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1376 | static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1377 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1378 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1379 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1380 | return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings, |
| 1381 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1384 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1385 | { |
| 1386 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1387 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1388 | if (hci_conn_count(hdev) == 0) { |
| 1389 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1390 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1391 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1392 | } |
| 1393 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1394 | static bool hci_stop_discovery(struct hci_request *req) |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1395 | { |
| 1396 | struct hci_dev *hdev = req->hdev; |
| 1397 | struct hci_cp_remote_name_req_cancel cp; |
| 1398 | struct inquiry_entry *e; |
| 1399 | |
| 1400 | switch (hdev->discovery.state) { |
| 1401 | case DISCOVERY_FINDING: |
| 1402 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1403 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1404 | } else { |
| 1405 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1406 | hci_req_add_le_scan_disable(req); |
| 1407 | } |
| 1408 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1409 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1410 | |
| 1411 | case DISCOVERY_RESOLVING: |
| 1412 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1413 | NAME_PENDING); |
| 1414 | if (!e) |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1415 | break; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1416 | |
| 1417 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1418 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1419 | &cp); |
| 1420 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1421 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1422 | |
| 1423 | default: |
| 1424 | /* Passive scanning */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1425 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) { |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1426 | hci_req_add_le_scan_disable(req); |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1427 | return true; |
| 1428 | } |
| 1429 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1430 | break; |
| 1431 | } |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1432 | |
| 1433 | return false; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1434 | } |
| 1435 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1436 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1437 | { |
| 1438 | struct hci_request req; |
| 1439 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1440 | bool discov_stopped; |
| 1441 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1442 | |
| 1443 | hci_req_init(&req, hdev); |
| 1444 | |
| 1445 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1446 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1447 | u8 scan = 0x00; |
| 1448 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1449 | } |
| 1450 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1451 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1452 | disable_advertising(&req); |
| 1453 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1454 | discov_stopped = hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1455 | |
| 1456 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1457 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1458 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1459 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1460 | switch (conn->state) { |
| 1461 | case BT_CONNECTED: |
| 1462 | case BT_CONFIG: |
| 1463 | dc.handle = cpu_to_le16(conn->handle); |
| 1464 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1465 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1466 | break; |
| 1467 | case BT_CONNECT: |
| 1468 | if (conn->type == LE_LINK) |
| 1469 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1470 | 0, NULL); |
| 1471 | else if (conn->type == ACL_LINK) |
| 1472 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1473 | 6, &conn->dst); |
| 1474 | break; |
| 1475 | case BT_CONNECT2: |
| 1476 | bacpy(&rej.bdaddr, &conn->dst); |
| 1477 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1478 | if (conn->type == ACL_LINK) |
| 1479 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1480 | sizeof(rej), &rej); |
| 1481 | else if (conn->type == SCO_LINK) |
| 1482 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1483 | sizeof(rej), &rej); |
| 1484 | break; |
| 1485 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1486 | } |
| 1487 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1488 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1489 | if (!err && discov_stopped) |
| 1490 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1491 | |
| 1492 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1493 | } |
| 1494 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1495 | static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1496 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1497 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1498 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1499 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1500 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1501 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1502 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1503 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1504 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1505 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1506 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1507 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1508 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1509 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1510 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1511 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1512 | MGMT_STATUS_BUSY); |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1513 | goto failed; |
| 1514 | } |
| 1515 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1516 | if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) { |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1517 | cancel_delayed_work(&hdev->power_off); |
| 1518 | |
| 1519 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1520 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1521 | data, len); |
| 1522 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1523 | goto failed; |
| 1524 | } |
| 1525 | } |
| 1526 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1527 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1528 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1529 | goto failed; |
| 1530 | } |
| 1531 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1532 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1533 | if (!cmd) { |
| 1534 | err = -ENOMEM; |
| 1535 | goto failed; |
| 1536 | } |
| 1537 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1538 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1539 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1540 | err = 0; |
| 1541 | } else { |
| 1542 | /* Disconnect connections, stop scans, etc */ |
| 1543 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1544 | if (!err) |
| 1545 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1546 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1547 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1548 | /* ENODATA means there were no HCI commands queued */ |
| 1549 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1550 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1551 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1552 | err = 0; |
| 1553 | } |
| 1554 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1555 | |
| 1556 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1557 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1558 | return err; |
| 1559 | } |
| 1560 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1561 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1562 | { |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1563 | __le32 ev = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1564 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 1565 | return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, |
| 1566 | sizeof(ev), skip); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1567 | } |
| 1568 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1569 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1570 | { |
| 1571 | return new_settings(hdev, NULL); |
| 1572 | } |
| 1573 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1574 | struct cmd_lookup { |
| 1575 | struct sock *sk; |
| 1576 | struct hci_dev *hdev; |
| 1577 | u8 mgmt_status; |
| 1578 | }; |
| 1579 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1580 | static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1581 | { |
| 1582 | struct cmd_lookup *match = data; |
| 1583 | |
| 1584 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1585 | |
| 1586 | list_del(&cmd->list); |
| 1587 | |
| 1588 | if (match->sk == NULL) { |
| 1589 | match->sk = cmd->sk; |
| 1590 | sock_hold(match->sk); |
| 1591 | } |
| 1592 | |
| 1593 | mgmt_pending_free(cmd); |
| 1594 | } |
| 1595 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1596 | static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1597 | { |
| 1598 | u8 *status = data; |
| 1599 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1600 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1601 | mgmt_pending_remove(cmd); |
| 1602 | } |
| 1603 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1604 | static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 1b9b5ee | 2014-12-05 13:36:00 +0200 | [diff] [blame] | 1605 | { |
| 1606 | if (cmd->cmd_complete) { |
| 1607 | u8 *status = data; |
| 1608 | |
| 1609 | cmd->cmd_complete(cmd, *status); |
| 1610 | mgmt_pending_remove(cmd); |
| 1611 | |
| 1612 | return; |
| 1613 | } |
| 1614 | |
| 1615 | cmd_status_rsp(cmd, data); |
| 1616 | } |
| 1617 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1618 | static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1619 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1620 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1621 | cmd->param, cmd->param_len); |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 1622 | } |
| 1623 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1624 | static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1625 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 1626 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 1627 | cmd->param, sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 1628 | } |
| 1629 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1630 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1631 | { |
| 1632 | if (!lmp_bredr_capable(hdev)) |
| 1633 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1634 | else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1635 | return MGMT_STATUS_REJECTED; |
| 1636 | else |
| 1637 | return MGMT_STATUS_SUCCESS; |
| 1638 | } |
| 1639 | |
| 1640 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1641 | { |
| 1642 | if (!lmp_le_capable(hdev)) |
| 1643 | return MGMT_STATUS_NOT_SUPPORTED; |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1644 | else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1645 | return MGMT_STATUS_REJECTED; |
| 1646 | else |
| 1647 | return MGMT_STATUS_SUCCESS; |
| 1648 | } |
| 1649 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1650 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status, |
| 1651 | u16 opcode) |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1652 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1653 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1654 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1655 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1656 | bool changed; |
| 1657 | |
| 1658 | BT_DBG("status 0x%02x", status); |
| 1659 | |
| 1660 | hci_dev_lock(hdev); |
| 1661 | |
| 1662 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1663 | if (!cmd) |
| 1664 | goto unlock; |
| 1665 | |
| 1666 | if (status) { |
| 1667 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1668 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1669 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1670 | goto remove_cmd; |
| 1671 | } |
| 1672 | |
| 1673 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1674 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1675 | changed = !hci_dev_test_and_set_flag(hdev, HCI_DISCOVERABLE); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1676 | |
| 1677 | if (hdev->discov_timeout > 0) { |
| 1678 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1679 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1680 | to); |
| 1681 | } |
| 1682 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1683 | changed = hci_dev_test_and_clear_flag(hdev, HCI_DISCOVERABLE); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1684 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1685 | |
| 1686 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1687 | |
| 1688 | if (changed) |
| 1689 | new_settings(hdev, cmd->sk); |
| 1690 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1691 | /* When the discoverable mode gets changed, make sure |
| 1692 | * that class of device has the limited discoverable |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1693 | * bit correctly set. Also update page scan based on whitelist |
| 1694 | * entries. |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1695 | */ |
| 1696 | hci_req_init(&req, hdev); |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 1697 | __hci_update_page_scan(&req); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1698 | update_class(&req); |
| 1699 | hci_req_run(&req, NULL); |
| 1700 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1701 | remove_cmd: |
| 1702 | mgmt_pending_remove(cmd); |
| 1703 | |
| 1704 | unlock: |
| 1705 | hci_dev_unlock(hdev); |
| 1706 | } |
| 1707 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1708 | static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1709 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1710 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1711 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1712 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1713 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1714 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1715 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1716 | int err; |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1717 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1718 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1719 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1720 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1721 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1722 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1723 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1724 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1725 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1726 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1727 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1728 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1729 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1730 | |
| 1731 | /* Disabling discoverable requires that no timeout is set, |
| 1732 | * and enabling limited discoverable requires a timeout. |
| 1733 | */ |
| 1734 | if ((cp->val == 0x00 && timeout > 0) || |
| 1735 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1736 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1737 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1738 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1739 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1740 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1741 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1742 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1743 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1744 | goto failed; |
| 1745 | } |
| 1746 | |
| 1747 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1748 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1749 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1750 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1751 | goto failed; |
| 1752 | } |
| 1753 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1754 | if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1755 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1756 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1757 | goto failed; |
| 1758 | } |
| 1759 | |
| 1760 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1761 | bool changed = false; |
| 1762 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1763 | /* Setting limited discoverable when powered off is |
| 1764 | * not a valid operation since it requires a timeout |
| 1765 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1766 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1767 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 1768 | hci_dev_change_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1769 | changed = true; |
| 1770 | } |
| 1771 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1772 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1773 | if (err < 0) |
| 1774 | goto failed; |
| 1775 | |
| 1776 | if (changed) |
| 1777 | err = new_settings(hdev, sk); |
| 1778 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1779 | goto failed; |
| 1780 | } |
| 1781 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1782 | /* If the current mode is the same, then just update the timeout |
| 1783 | * value with the new value. And if only the timeout gets updated, |
| 1784 | * then no need for any HCI transactions. |
| 1785 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1786 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) && |
| 1787 | (cp->val == 0x02) == hci_dev_test_flag(hdev, |
| 1788 | HCI_LIMITED_DISCOVERABLE)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1789 | cancel_delayed_work(&hdev->discov_off); |
| 1790 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1791 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1792 | if (cp->val && hdev->discov_timeout > 0) { |
| 1793 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1794 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1795 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1796 | } |
| 1797 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1798 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1799 | goto failed; |
| 1800 | } |
| 1801 | |
| 1802 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1803 | if (!cmd) { |
| 1804 | err = -ENOMEM; |
| 1805 | goto failed; |
| 1806 | } |
| 1807 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1808 | /* Cancel any potential discoverable timeout that might be |
| 1809 | * still active and store new timeout value. The arming of |
| 1810 | * the timeout happens in the complete handler. |
| 1811 | */ |
| 1812 | cancel_delayed_work(&hdev->discov_off); |
| 1813 | hdev->discov_timeout = timeout; |
| 1814 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1815 | /* Limited discoverable mode */ |
| 1816 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1817 | hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1818 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1819 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1820 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1821 | hci_req_init(&req, hdev); |
| 1822 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1823 | /* The procedure for LE-only controllers is much simpler - just |
| 1824 | * update the advertising data. |
| 1825 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1826 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1827 | goto update_ad; |
| 1828 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1829 | scan = SCAN_PAGE; |
| 1830 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1831 | if (cp->val) { |
| 1832 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1833 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1834 | if (cp->val == 0x02) { |
| 1835 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1836 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1837 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1838 | hci_cp.iac_lap[1] = 0x8b; |
| 1839 | hci_cp.iac_lap[2] = 0x9e; |
| 1840 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1841 | hci_cp.iac_lap[4] = 0x8b; |
| 1842 | hci_cp.iac_lap[5] = 0x9e; |
| 1843 | } else { |
| 1844 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1845 | hci_cp.num_iac = 1; |
| 1846 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1847 | hci_cp.iac_lap[1] = 0x8b; |
| 1848 | hci_cp.iac_lap[2] = 0x9e; |
| 1849 | } |
| 1850 | |
| 1851 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1852 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1853 | |
| 1854 | scan |= SCAN_INQUIRY; |
| 1855 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1856 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1860 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1861 | update_ad: |
| 1862 | update_adv_data(&req); |
| 1863 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1864 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1865 | if (err < 0) |
| 1866 | mgmt_pending_remove(cmd); |
| 1867 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1868 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1869 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1870 | return err; |
| 1871 | } |
| 1872 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1873 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1874 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1875 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1876 | struct hci_cp_write_page_scan_activity acp; |
| 1877 | u8 type; |
| 1878 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1879 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1880 | return; |
| 1881 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1882 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1883 | return; |
| 1884 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1885 | if (enable) { |
| 1886 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1887 | |
| 1888 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1889 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1890 | } else { |
| 1891 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1892 | |
| 1893 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1894 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1895 | } |
| 1896 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1897 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1898 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1899 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1900 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1901 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1902 | sizeof(acp), &acp); |
| 1903 | |
| 1904 | if (hdev->page_scan_type != type) |
| 1905 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1906 | } |
| 1907 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 1908 | static void set_connectable_complete(struct hci_dev *hdev, u8 status, |
| 1909 | u16 opcode) |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1910 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1911 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1912 | struct mgmt_mode *cp; |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1913 | bool conn_changed, discov_changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1914 | |
| 1915 | BT_DBG("status 0x%02x", status); |
| 1916 | |
| 1917 | hci_dev_lock(hdev); |
| 1918 | |
| 1919 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1920 | if (!cmd) |
| 1921 | goto unlock; |
| 1922 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1923 | if (status) { |
| 1924 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 1925 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1926 | goto remove_cmd; |
| 1927 | } |
| 1928 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1929 | cp = cmd->param; |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1930 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 1931 | conn_changed = !hci_dev_test_and_set_flag(hdev, |
| 1932 | HCI_CONNECTABLE); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1933 | discov_changed = false; |
| 1934 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 1935 | conn_changed = hci_dev_test_and_clear_flag(hdev, |
| 1936 | HCI_CONNECTABLE); |
| 1937 | discov_changed = hci_dev_test_and_clear_flag(hdev, |
| 1938 | HCI_DISCOVERABLE); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1939 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1940 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1941 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1942 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1943 | if (conn_changed || discov_changed) { |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1944 | new_settings(hdev, cmd->sk); |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 1945 | hci_update_page_scan(hdev); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1946 | if (discov_changed) |
| 1947 | mgmt_update_adv_data(hdev); |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1948 | hci_update_background_scan(hdev); |
| 1949 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1950 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1951 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1952 | mgmt_pending_remove(cmd); |
| 1953 | |
| 1954 | unlock: |
| 1955 | hci_dev_unlock(hdev); |
| 1956 | } |
| 1957 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1958 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1959 | struct sock *sk, u8 val) |
| 1960 | { |
| 1961 | bool changed = false; |
| 1962 | int err; |
| 1963 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1964 | if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE)) |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1965 | changed = true; |
| 1966 | |
| 1967 | if (val) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 1968 | hci_dev_set_flag(hdev, HCI_CONNECTABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1969 | } else { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 1970 | hci_dev_clear_flag(hdev, HCI_CONNECTABLE); |
| 1971 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1975 | if (err < 0) |
| 1976 | return err; |
| 1977 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1978 | if (changed) { |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 1979 | hci_update_page_scan(hdev); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1980 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1981 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1982 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1983 | |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1987 | static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1988 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1989 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1990 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 1991 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1992 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1993 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1994 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1995 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1996 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1997 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 1998 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
| 1999 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2000 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 2001 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 2002 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2003 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2004 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 2005 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2006 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2007 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2008 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2009 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 2010 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2011 | goto failed; |
| 2012 | } |
| 2013 | |
| 2014 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2015 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2016 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 2017 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2018 | goto failed; |
| 2019 | } |
| 2020 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2021 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 2022 | if (!cmd) { |
| 2023 | err = -ENOMEM; |
| 2024 | goto failed; |
| 2025 | } |
| 2026 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 2027 | hci_req_init(&req, hdev); |
| 2028 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 2029 | /* If BR/EDR is not enabled and we disable advertising as a |
| 2030 | * by-product of disabling connectable, we need to update the |
| 2031 | * advertising flags. |
| 2032 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2033 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 2034 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 2035 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 2036 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 2037 | } |
| 2038 | update_adv_data(&req); |
| 2039 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 2040 | if (cp->val) { |
| 2041 | scan = SCAN_PAGE; |
| 2042 | } else { |
Johan Hedberg | 3bd2724 | 2014-07-28 20:53:58 +0300 | [diff] [blame] | 2043 | /* If we don't have any whitelist entries just |
| 2044 | * disable all scanning. If there are entries |
| 2045 | * and we had both page and inquiry scanning |
| 2046 | * enabled then fall back to only page scanning. |
| 2047 | * Otherwise no changes are needed. |
| 2048 | */ |
| 2049 | if (list_empty(&hdev->whitelist)) |
| 2050 | scan = SCAN_DISABLED; |
| 2051 | else if (test_bit(HCI_ISCAN, &hdev->flags)) |
| 2052 | scan = SCAN_PAGE; |
| 2053 | else |
| 2054 | goto no_scan_update; |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 2055 | |
| 2056 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 2057 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 2058 | cancel_delayed_work(&hdev->discov_off); |
| 2059 | } |
| 2060 | |
| 2061 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 2062 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 2063 | |
Johan Hedberg | 3bd2724 | 2014-07-28 20:53:58 +0300 | [diff] [blame] | 2064 | no_scan_update: |
Johan Hedberg | e8b1202 | 2014-07-10 10:51:27 +0300 | [diff] [blame] | 2065 | /* Update the advertising parameters if necessary */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2066 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 2067 | enable_advertising(&req); |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 2068 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 2069 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 2070 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2071 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 2072 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 2073 | err = set_connectable_update_settings(hdev, sk, |
| 2074 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 2075 | goto failed; |
| 2076 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2077 | |
| 2078 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2079 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2080 | return err; |
| 2081 | } |
| 2082 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 2083 | static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2084 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2085 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2086 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 2087 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 2088 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2089 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2090 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2091 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2092 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2093 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
| 2094 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2095 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2096 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2097 | |
| 2098 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2099 | changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2100 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2101 | changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2102 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 2103 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2104 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 2105 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2106 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 2107 | if (changed) |
| 2108 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2109 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 2110 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2111 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 2112 | return err; |
| 2113 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 2114 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2115 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2116 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2117 | { |
| 2118 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2119 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2120 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2121 | int err; |
| 2122 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2123 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2124 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2125 | status = mgmt_bredr_support(hdev); |
| 2126 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2127 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 2128 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 2129 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2130 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2131 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 2132 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2133 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2134 | hci_dev_lock(hdev); |
| 2135 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2136 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 2137 | bool changed = false; |
| 2138 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2139 | if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 2140 | hci_dev_change_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 2141 | changed = true; |
| 2142 | } |
| 2143 | |
| 2144 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 2145 | if (err < 0) |
| 2146 | goto failed; |
| 2147 | |
| 2148 | if (changed) |
| 2149 | err = new_settings(hdev, sk); |
| 2150 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2151 | goto failed; |
| 2152 | } |
| 2153 | |
| 2154 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2155 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 2156 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2157 | goto failed; |
| 2158 | } |
| 2159 | |
| 2160 | val = !!cp->val; |
| 2161 | |
| 2162 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 2163 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 2164 | goto failed; |
| 2165 | } |
| 2166 | |
| 2167 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 2168 | if (!cmd) { |
| 2169 | err = -ENOMEM; |
| 2170 | goto failed; |
| 2171 | } |
| 2172 | |
| 2173 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 2174 | if (err < 0) { |
| 2175 | mgmt_pending_remove(cmd); |
| 2176 | goto failed; |
| 2177 | } |
| 2178 | |
| 2179 | failed: |
| 2180 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2181 | return err; |
| 2182 | } |
| 2183 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2184 | static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2185 | { |
| 2186 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2187 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2188 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2189 | int err; |
| 2190 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2191 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2192 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 2193 | status = mgmt_bredr_support(hdev); |
| 2194 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2195 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 2196 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2197 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2198 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2199 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2200 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2201 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2202 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2203 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2204 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2205 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 2206 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2207 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2208 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2209 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2210 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2211 | changed = !hci_dev_test_and_set_flag(hdev, |
| 2212 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2213 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2214 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2215 | HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2216 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2217 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2218 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2219 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 2220 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2224 | if (err < 0) |
| 2225 | goto failed; |
| 2226 | |
| 2227 | if (changed) |
| 2228 | err = new_settings(hdev, sk); |
| 2229 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2230 | goto failed; |
| 2231 | } |
| 2232 | |
Johan Hedberg | 94d52da | 2015-02-19 17:38:06 +0200 | [diff] [blame] | 2233 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2234 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2235 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2236 | goto failed; |
| 2237 | } |
| 2238 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2239 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2240 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2241 | goto failed; |
| 2242 | } |
| 2243 | |
| 2244 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 2245 | if (!cmd) { |
| 2246 | err = -ENOMEM; |
| 2247 | goto failed; |
| 2248 | } |
| 2249 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2250 | if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 2251 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2252 | sizeof(cp->val), &cp->val); |
| 2253 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2254 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2255 | if (err < 0) { |
| 2256 | mgmt_pending_remove(cmd); |
| 2257 | goto failed; |
| 2258 | } |
| 2259 | |
| 2260 | failed: |
| 2261 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2262 | return err; |
| 2263 | } |
| 2264 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2265 | static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2266 | { |
| 2267 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2268 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2269 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2270 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2271 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2272 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2273 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2274 | status = mgmt_bredr_support(hdev); |
| 2275 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2276 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2277 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2278 | if (!lmp_ssp_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2279 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2280 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2281 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2282 | if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2283 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2284 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2285 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2286 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2287 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2288 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2289 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2290 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2291 | |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 2292 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2293 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2294 | MGMT_STATUS_BUSY); |
Johan Hedberg | a2cb01d | 2015-02-19 17:38:07 +0200 | [diff] [blame] | 2295 | goto unlock; |
| 2296 | } |
| 2297 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2298 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2299 | changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2300 | } else { |
| 2301 | if (hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2302 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2303 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2304 | goto unlock; |
| 2305 | } |
| 2306 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2307 | changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2308 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2309 | |
| 2310 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2311 | if (err < 0) |
| 2312 | goto unlock; |
| 2313 | |
| 2314 | if (changed) |
| 2315 | err = new_settings(hdev, sk); |
| 2316 | |
| 2317 | unlock: |
| 2318 | hci_dev_unlock(hdev); |
| 2319 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2320 | } |
| 2321 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2322 | static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2323 | { |
| 2324 | struct cmd_lookup match = { NULL, hdev }; |
| 2325 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 2326 | hci_dev_lock(hdev); |
| 2327 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2328 | if (status) { |
| 2329 | u8 mgmt_err = mgmt_status(status); |
| 2330 | |
| 2331 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2332 | &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 2333 | goto unlock; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2337 | |
| 2338 | new_settings(hdev, match.sk); |
| 2339 | |
| 2340 | if (match.sk) |
| 2341 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2342 | |
| 2343 | /* Make sure the controller has a good default for |
| 2344 | * advertising data. Restrict the update to when LE |
| 2345 | * has actually been enabled. During power on, the |
| 2346 | * update in powered_update_hci will take care of it. |
| 2347 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2348 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2349 | struct hci_request req; |
| 2350 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2351 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2352 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2353 | update_scan_rsp_data(&req); |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 2354 | __hci_update_background_scan(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2355 | hci_req_run(&req, NULL); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2356 | } |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 2357 | |
| 2358 | unlock: |
| 2359 | hci_dev_unlock(hdev); |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2360 | } |
| 2361 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2362 | static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2363 | { |
| 2364 | struct mgmt_mode *cp = data; |
| 2365 | struct hci_cp_write_le_host_supported hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2366 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2367 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2368 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2369 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2370 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2371 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2372 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2373 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2374 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2375 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2376 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2377 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2378 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2379 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2380 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2381 | /* LE-only devices do not allow toggling LE on/off */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2382 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2383 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2384 | MGMT_STATUS_REJECTED); |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2385 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2386 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2387 | |
| 2388 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2389 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2390 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2391 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2392 | bool changed = false; |
| 2393 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2394 | if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 2395 | hci_dev_change_flag(hdev, HCI_LE_ENABLED); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2396 | changed = true; |
| 2397 | } |
| 2398 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2399 | if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 2400 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2401 | changed = true; |
| 2402 | } |
| 2403 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2404 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2405 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2406 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2407 | |
| 2408 | if (changed) |
| 2409 | err = new_settings(hdev, sk); |
| 2410 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2411 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2412 | } |
| 2413 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2414 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2415 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2416 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2417 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2418 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2419 | } |
| 2420 | |
| 2421 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2422 | if (!cmd) { |
| 2423 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2424 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2427 | hci_req_init(&req, hdev); |
| 2428 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2429 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2430 | |
| 2431 | if (val) { |
| 2432 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 2433 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2434 | } else { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 2435 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2436 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2437 | } |
| 2438 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2439 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2440 | &hci_cp); |
| 2441 | |
| 2442 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2443 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2444 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2445 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2446 | unlock: |
| 2447 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2448 | return err; |
| 2449 | } |
| 2450 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2451 | /* This is a helper function to test for pending mgmt commands that can |
| 2452 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2453 | * mgmt command at a time since otherwise we cannot easily track what |
| 2454 | * the current values are, will be, and based on that calculate if a new |
| 2455 | * HCI command needs to be sent and if yes with what value. |
| 2456 | */ |
| 2457 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2458 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2459 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2460 | |
| 2461 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2462 | switch (cmd->opcode) { |
| 2463 | case MGMT_OP_ADD_UUID: |
| 2464 | case MGMT_OP_REMOVE_UUID: |
| 2465 | case MGMT_OP_SET_DEV_CLASS: |
| 2466 | case MGMT_OP_SET_POWERED: |
| 2467 | return true; |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | return false; |
| 2472 | } |
| 2473 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2474 | static const u8 bluetooth_base_uuid[] = { |
| 2475 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2476 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2477 | }; |
| 2478 | |
| 2479 | static u8 get_uuid_size(const u8 *uuid) |
| 2480 | { |
| 2481 | u32 val; |
| 2482 | |
| 2483 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2484 | return 128; |
| 2485 | |
| 2486 | val = get_unaligned_le32(&uuid[12]); |
| 2487 | if (val > 0xffff) |
| 2488 | return 32; |
| 2489 | |
| 2490 | return 16; |
| 2491 | } |
| 2492 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2493 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2494 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2495 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2496 | |
| 2497 | hci_dev_lock(hdev); |
| 2498 | |
| 2499 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2500 | if (!cmd) |
| 2501 | goto unlock; |
| 2502 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2503 | mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, |
| 2504 | mgmt_status(status), hdev->dev_class, 3); |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2505 | |
| 2506 | mgmt_pending_remove(cmd); |
| 2507 | |
| 2508 | unlock: |
| 2509 | hci_dev_unlock(hdev); |
| 2510 | } |
| 2511 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2512 | static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2513 | { |
| 2514 | BT_DBG("status 0x%02x", status); |
| 2515 | |
| 2516 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2517 | } |
| 2518 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2519 | static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2520 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2521 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2522 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2523 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2524 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2525 | int err; |
| 2526 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2527 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2528 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2529 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2530 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2531 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2532 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
| 2533 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2534 | goto failed; |
| 2535 | } |
| 2536 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2537 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2538 | if (!uuid) { |
| 2539 | err = -ENOMEM; |
| 2540 | goto failed; |
| 2541 | } |
| 2542 | |
| 2543 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2544 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2545 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2546 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2547 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2548 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2549 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2550 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2551 | update_class(&req); |
| 2552 | update_eir(&req); |
| 2553 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2554 | err = hci_req_run(&req, add_uuid_complete); |
| 2555 | if (err < 0) { |
| 2556 | if (err != -ENODATA) |
| 2557 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2558 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2559 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
| 2560 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2561 | goto failed; |
| 2562 | } |
| 2563 | |
| 2564 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2565 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2566 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2567 | goto failed; |
| 2568 | } |
| 2569 | |
| 2570 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2571 | |
| 2572 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2573 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2574 | return err; |
| 2575 | } |
| 2576 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2577 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2578 | { |
| 2579 | if (!hdev_is_powered(hdev)) |
| 2580 | return false; |
| 2581 | |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2582 | if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2583 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2584 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2585 | return true; |
| 2586 | } |
| 2587 | |
| 2588 | return false; |
| 2589 | } |
| 2590 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2591 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2592 | { |
| 2593 | BT_DBG("status 0x%02x", status); |
| 2594 | |
| 2595 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2596 | } |
| 2597 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2598 | static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2599 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2600 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2601 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2602 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2603 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2604 | u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2605 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2606 | int err, found; |
| 2607 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2608 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2609 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2610 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2611 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2612 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2613 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2614 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2615 | goto unlock; |
| 2616 | } |
| 2617 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2618 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2619 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2620 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2621 | if (enable_service_cache(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2622 | err = mgmt_cmd_complete(sk, hdev->id, |
| 2623 | MGMT_OP_REMOVE_UUID, |
| 2624 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2625 | goto unlock; |
| 2626 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2627 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2628 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | found = 0; |
| 2632 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2633 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2634 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2635 | continue; |
| 2636 | |
| 2637 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2638 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2639 | found++; |
| 2640 | } |
| 2641 | |
| 2642 | if (found == 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2643 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
| 2644 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2645 | goto unlock; |
| 2646 | } |
| 2647 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2648 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2649 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2650 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2651 | update_class(&req); |
| 2652 | update_eir(&req); |
| 2653 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2654 | err = hci_req_run(&req, remove_uuid_complete); |
| 2655 | if (err < 0) { |
| 2656 | if (err != -ENODATA) |
| 2657 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2658 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2659 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
| 2660 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2661 | goto unlock; |
| 2662 | } |
| 2663 | |
| 2664 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2665 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2666 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2667 | goto unlock; |
| 2668 | } |
| 2669 | |
| 2670 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2671 | |
| 2672 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2673 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2674 | return err; |
| 2675 | } |
| 2676 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 2677 | static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2678 | { |
| 2679 | BT_DBG("status 0x%02x", status); |
| 2680 | |
| 2681 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2682 | } |
| 2683 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2684 | static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2685 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2686 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2687 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2688 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2689 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2690 | int err; |
| 2691 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2692 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2693 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2694 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2695 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2696 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2697 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2698 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2699 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2700 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2701 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2702 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2703 | goto unlock; |
| 2704 | } |
| 2705 | |
| 2706 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2707 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2708 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2709 | goto unlock; |
| 2710 | } |
| 2711 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2712 | hdev->major_class = cp->major; |
| 2713 | hdev->minor_class = cp->minor; |
| 2714 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2715 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2716 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2717 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2718 | goto unlock; |
| 2719 | } |
| 2720 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2721 | hci_req_init(&req, hdev); |
| 2722 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2723 | if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2724 | hci_dev_unlock(hdev); |
| 2725 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2726 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2727 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2728 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2729 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2730 | update_class(&req); |
| 2731 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2732 | err = hci_req_run(&req, set_class_complete); |
| 2733 | if (err < 0) { |
| 2734 | if (err != -ENODATA) |
| 2735 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2736 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2737 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
| 2738 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2739 | goto unlock; |
| 2740 | } |
| 2741 | |
| 2742 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2743 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2744 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2745 | goto unlock; |
| 2746 | } |
| 2747 | |
| 2748 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2749 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2750 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2751 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2752 | return err; |
| 2753 | } |
| 2754 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2755 | static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2756 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2757 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2758 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2759 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2760 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2761 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2762 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2763 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2764 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2765 | BT_DBG("request for %s", hdev->name); |
| 2766 | |
| 2767 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2768 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2769 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2770 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2771 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2772 | if (key_count > max_key_count) { |
| 2773 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2774 | key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2775 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2776 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2777 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2778 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2779 | expected_len = sizeof(*cp) + key_count * |
| 2780 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2781 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2782 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2783 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2784 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2785 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2786 | } |
| 2787 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2788 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2789 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2790 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2791 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2792 | BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2793 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2794 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2795 | for (i = 0; i < key_count; i++) { |
| 2796 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2797 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2798 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 2799 | return mgmt_cmd_status(sk, hdev->id, |
| 2800 | MGMT_OP_LOAD_LINK_KEYS, |
| 2801 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2802 | } |
| 2803 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2804 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2805 | |
| 2806 | hci_link_keys_clear(hdev); |
| 2807 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2808 | if (cp->debug_keys) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 2809 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2810 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 2811 | changed = hci_dev_test_and_clear_flag(hdev, |
| 2812 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2813 | |
| 2814 | if (changed) |
| 2815 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2816 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2817 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2818 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2819 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2820 | /* Always ignore debug keys and require a new pairing if |
| 2821 | * the user wants to use them. |
| 2822 | */ |
| 2823 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2824 | continue; |
| 2825 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2826 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2827 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2828 | } |
| 2829 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2830 | mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0); |
Johan Hedberg | 0e5f875 | 2011-11-11 16:18:54 +0200 | [diff] [blame] | 2831 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2832 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2833 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2834 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2835 | } |
| 2836 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2837 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2838 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2839 | { |
| 2840 | struct mgmt_ev_device_unpaired ev; |
| 2841 | |
| 2842 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2843 | ev.addr.type = addr_type; |
| 2844 | |
| 2845 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2846 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2847 | } |
| 2848 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2849 | static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2850 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2851 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2852 | struct mgmt_cp_unpair_device *cp = data; |
| 2853 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2854 | struct hci_cp_disconnect dc; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2855 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2856 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2857 | int err; |
| 2858 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2859 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2860 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2861 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2862 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2863 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2864 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2865 | MGMT_STATUS_INVALID_PARAMS, |
| 2866 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2867 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2868 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2869 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2870 | MGMT_STATUS_INVALID_PARAMS, |
| 2871 | &rp, sizeof(rp)); |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2872 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2873 | hci_dev_lock(hdev); |
| 2874 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2875 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2876 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2877 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2878 | sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2879 | goto unlock; |
| 2880 | } |
| 2881 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2882 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2883 | /* If disconnection is requested, then look up the |
| 2884 | * connection. If the remote device is connected, it |
| 2885 | * will be later used to terminate the link. |
| 2886 | * |
| 2887 | * Setting it to NULL explicitly will cause no |
| 2888 | * termination of the link. |
| 2889 | */ |
| 2890 | if (cp->disconnect) |
| 2891 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2892 | &cp->addr.bdaddr); |
| 2893 | else |
| 2894 | conn = NULL; |
| 2895 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2896 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2897 | } else { |
| 2898 | u8 addr_type; |
| 2899 | |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2900 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
| 2901 | &cp->addr.bdaddr); |
| 2902 | if (conn) { |
| 2903 | /* Defer clearing up the connection parameters |
| 2904 | * until closing to give a chance of keeping |
| 2905 | * them if a repairing happens. |
| 2906 | */ |
| 2907 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2908 | |
| 2909 | /* If disconnection is not requested, then |
| 2910 | * clear the connection variable so that the |
| 2911 | * link is not terminated. |
| 2912 | */ |
| 2913 | if (!cp->disconnect) |
| 2914 | conn = NULL; |
| 2915 | } |
| 2916 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2917 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2918 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2919 | else |
| 2920 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2921 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2922 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2923 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2924 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2925 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2926 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2927 | if (err < 0) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2928 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2929 | MGMT_STATUS_NOT_PAIRED, &rp, |
| 2930 | sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2931 | goto unlock; |
| 2932 | } |
| 2933 | |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2934 | /* If the connection variable is set, then termination of the |
| 2935 | * link is requested. |
| 2936 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2937 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2938 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
| 2939 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2940 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2941 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2942 | } |
| 2943 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2944 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2945 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2946 | if (!cmd) { |
| 2947 | err = -ENOMEM; |
| 2948 | goto unlock; |
| 2949 | } |
| 2950 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 2951 | cmd->cmd_complete = addr_cmd_complete; |
| 2952 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2953 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2954 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2955 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2956 | if (err < 0) |
| 2957 | mgmt_pending_remove(cmd); |
| 2958 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2959 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2960 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2961 | return err; |
| 2962 | } |
| 2963 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2964 | static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2965 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2966 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2967 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2968 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 2969 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2970 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2971 | int err; |
| 2972 | |
| 2973 | BT_DBG(""); |
| 2974 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2975 | memset(&rp, 0, sizeof(rp)); |
| 2976 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2977 | rp.addr.type = cp->addr.type; |
| 2978 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2979 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2980 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2981 | MGMT_STATUS_INVALID_PARAMS, |
| 2982 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2983 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2984 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2985 | |
| 2986 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2987 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2988 | MGMT_STATUS_NOT_POWERED, &rp, |
| 2989 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2990 | goto failed; |
| 2991 | } |
| 2992 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2993 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 2994 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2995 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2996 | goto failed; |
| 2997 | } |
| 2998 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2999 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 3000 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 3001 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 3002 | else |
| 3003 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
Vinicius Costa Gomes | 365227e | 2011-05-06 18:41:44 -0300 | [diff] [blame] | 3004 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 3005 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3006 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 3007 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 3008 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3009 | goto failed; |
| 3010 | } |
| 3011 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3012 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 3013 | if (!cmd) { |
| 3014 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3015 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 3016 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3017 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 3018 | cmd->cmd_complete = generic_cmd_complete; |
| 3019 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 3020 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3021 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3022 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3023 | |
| 3024 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3025 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 3026 | return err; |
| 3027 | } |
| 3028 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3029 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3030 | { |
| 3031 | switch (link_type) { |
| 3032 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3033 | switch (addr_type) { |
| 3034 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 3035 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 3036 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3037 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 3038 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 3039 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 3040 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 3041 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3042 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 3043 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 3044 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3045 | } |
| 3046 | } |
| 3047 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3048 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3049 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3050 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3051 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3052 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 3053 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 3054 | int err; |
| 3055 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3056 | |
| 3057 | BT_DBG(""); |
| 3058 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3059 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3060 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3061 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3062 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
| 3063 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3064 | goto unlock; |
| 3065 | } |
| 3066 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 3067 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3068 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 3069 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 3070 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3071 | } |
| 3072 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 3073 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 3074 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 3075 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3076 | err = -ENOMEM; |
| 3077 | goto unlock; |
| 3078 | } |
| 3079 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3080 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3081 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 3082 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 3083 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3084 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 3085 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 3086 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3087 | continue; |
| 3088 | i++; |
| 3089 | } |
| 3090 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 3091 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 3092 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 3093 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 3094 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3095 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3096 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
| 3097 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3098 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 3099 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3100 | |
| 3101 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3102 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 3103 | return err; |
| 3104 | } |
| 3105 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3106 | static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3107 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3108 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3109 | struct mgmt_pending_cmd *cmd; |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3110 | int err; |
| 3111 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3112 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3113 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3114 | if (!cmd) |
| 3115 | return -ENOMEM; |
| 3116 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3117 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3118 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3119 | if (err < 0) |
| 3120 | mgmt_pending_remove(cmd); |
| 3121 | |
| 3122 | return err; |
| 3123 | } |
| 3124 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3125 | static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3126 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3127 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3128 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3129 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3130 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3131 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3132 | int err; |
| 3133 | |
| 3134 | BT_DBG(""); |
| 3135 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3136 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3137 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3138 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3139 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 3140 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3141 | goto failed; |
| 3142 | } |
| 3143 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3144 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3145 | if (!conn) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3146 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 3147 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3148 | goto failed; |
| 3149 | } |
| 3150 | |
| 3151 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3152 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 3153 | |
| 3154 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3155 | |
| 3156 | BT_ERR("PIN code is not 16 bytes long"); |
| 3157 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3158 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3159 | if (err >= 0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3160 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 3161 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3162 | |
| 3163 | goto failed; |
| 3164 | } |
| 3165 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 3166 | cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 3167 | if (!cmd) { |
| 3168 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3169 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 3170 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3171 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3172 | cmd->cmd_complete = addr_cmd_complete; |
| 3173 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3174 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3175 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 3176 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3177 | |
| 3178 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 3179 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3180 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3181 | |
| 3182 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3183 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3184 | return err; |
| 3185 | } |
| 3186 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3187 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3188 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3189 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3190 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3191 | |
| 3192 | BT_DBG(""); |
| 3193 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3194 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3195 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 3196 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3197 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3198 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3199 | |
| 3200 | hdev->io_capability = cp->io_capability; |
| 3201 | |
| 3202 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3203 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3204 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3205 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3206 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3207 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, |
| 3208 | NULL, 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3209 | } |
| 3210 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3211 | static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3212 | { |
| 3213 | struct hci_dev *hdev = conn->hdev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3214 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3215 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3216 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3217 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 3218 | continue; |
| 3219 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3220 | if (cmd->user_data != conn) |
| 3221 | continue; |
| 3222 | |
| 3223 | return cmd; |
| 3224 | } |
| 3225 | |
| 3226 | return NULL; |
| 3227 | } |
| 3228 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3229 | static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3230 | { |
| 3231 | struct mgmt_rp_pair_device rp; |
| 3232 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 3233 | int err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3234 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 3235 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 3236 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3237 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3238 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, |
| 3239 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3240 | |
| 3241 | /* So we don't get further callbacks for this connection */ |
| 3242 | conn->connect_cfm_cb = NULL; |
| 3243 | conn->security_cfm_cb = NULL; |
| 3244 | conn->disconn_cfm_cb = NULL; |
| 3245 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3246 | hci_conn_drop(conn); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 3247 | |
| 3248 | /* The device is paired so there is no need to remove |
| 3249 | * its connection parameters anymore. |
| 3250 | */ |
| 3251 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | 15013ae | 2014-12-11 21:45:44 +0200 | [diff] [blame] | 3252 | |
| 3253 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 3254 | |
| 3255 | return err; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3256 | } |
| 3257 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3258 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 3259 | { |
| 3260 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3261 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3262 | |
| 3263 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3264 | if (cmd) { |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 3265 | cmd->cmd_complete(cmd, status); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3266 | mgmt_pending_remove(cmd); |
| 3267 | } |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3268 | } |
| 3269 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3270 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 3271 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3272 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3273 | |
| 3274 | BT_DBG("status %u", status); |
| 3275 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3276 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3277 | if (!cmd) { |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3278 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3279 | return; |
| 3280 | } |
| 3281 | |
| 3282 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 3283 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3284 | } |
| 3285 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3286 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3287 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3288 | struct mgmt_pending_cmd *cmd; |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3289 | |
| 3290 | BT_DBG("status %u", status); |
| 3291 | |
| 3292 | if (!status) |
| 3293 | return; |
| 3294 | |
| 3295 | cmd = find_pairing(conn); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3296 | if (!cmd) { |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3297 | BT_DBG("Unable to find a pending command"); |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3298 | return; |
| 3299 | } |
| 3300 | |
| 3301 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 3302 | mgmt_pending_remove(cmd); |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3303 | } |
| 3304 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3305 | static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3306 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3307 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3308 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3309 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3310 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3311 | u8 sec_level, auth_type; |
| 3312 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3313 | int err; |
| 3314 | |
| 3315 | BT_DBG(""); |
| 3316 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3317 | memset(&rp, 0, sizeof(rp)); |
| 3318 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3319 | rp.addr.type = cp->addr.type; |
| 3320 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3321 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3322 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3323 | MGMT_STATUS_INVALID_PARAMS, |
| 3324 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3325 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3326 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3327 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3328 | MGMT_STATUS_INVALID_PARAMS, |
| 3329 | &rp, sizeof(rp)); |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3330 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3331 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3332 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3333 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3334 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3335 | MGMT_STATUS_NOT_POWERED, &rp, |
| 3336 | sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3337 | goto unlock; |
| 3338 | } |
| 3339 | |
Johan Hedberg | 55e76b3 | 2015-03-10 22:34:40 +0200 | [diff] [blame] | 3340 | if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) { |
| 3341 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3342 | MGMT_STATUS_ALREADY_PAIRED, &rp, |
| 3343 | sizeof(rp)); |
| 3344 | goto unlock; |
| 3345 | } |
| 3346 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3347 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3348 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3349 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3350 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3351 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3352 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3353 | } else { |
| 3354 | u8 addr_type; |
| 3355 | |
| 3356 | /* Convert from L2CAP channel address type to HCI address type |
| 3357 | */ |
| 3358 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3359 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3360 | else |
| 3361 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3362 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3363 | /* When pairing a new device, it is expected to remember |
| 3364 | * this device for future connections. Adding the connection |
| 3365 | * parameter information ahead of time allows tracking |
| 3366 | * of the slave preferred values and will speed up any |
| 3367 | * further connection establishment. |
| 3368 | * |
| 3369 | * If connection parameters already exist, then they |
| 3370 | * will be kept and this function does nothing. |
| 3371 | */ |
| 3372 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3373 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3374 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 3375 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 3376 | HCI_ROLE_MASTER); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3377 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3378 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3379 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3380 | int status; |
| 3381 | |
| 3382 | if (PTR_ERR(conn) == -EBUSY) |
| 3383 | status = MGMT_STATUS_BUSY; |
Lukasz Rymanowski | faa8103 | 2015-02-11 12:31:42 +0100 | [diff] [blame] | 3384 | else if (PTR_ERR(conn) == -EOPNOTSUPP) |
| 3385 | status = MGMT_STATUS_NOT_SUPPORTED; |
| 3386 | else if (PTR_ERR(conn) == -ECONNREFUSED) |
| 3387 | status = MGMT_STATUS_REJECTED; |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3388 | else |
| 3389 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3390 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3391 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3392 | status, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3393 | goto unlock; |
| 3394 | } |
| 3395 | |
| 3396 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3397 | hci_conn_drop(conn); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3398 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3399 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3400 | goto unlock; |
| 3401 | } |
| 3402 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3403 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3404 | if (!cmd) { |
| 3405 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3406 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3407 | goto unlock; |
| 3408 | } |
| 3409 | |
Johan Hedberg | 04ab274 | 2014-12-05 13:36:04 +0200 | [diff] [blame] | 3410 | cmd->cmd_complete = pairing_complete; |
| 3411 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3412 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3413 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3414 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3415 | conn->security_cfm_cb = pairing_complete_cb; |
| 3416 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3417 | } else { |
| 3418 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3419 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3420 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3421 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3422 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3423 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3424 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3425 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3426 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3427 | hci_conn_security(conn, sec_level, auth_type, true)) { |
| 3428 | cmd->cmd_complete(cmd, 0); |
| 3429 | mgmt_pending_remove(cmd); |
| 3430 | } |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3431 | |
| 3432 | err = 0; |
| 3433 | |
| 3434 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3435 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3436 | return err; |
| 3437 | } |
| 3438 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3439 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3440 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3441 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3442 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3443 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3444 | struct hci_conn *conn; |
| 3445 | int err; |
| 3446 | |
| 3447 | BT_DBG(""); |
| 3448 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3449 | hci_dev_lock(hdev); |
| 3450 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3451 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3452 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3453 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3454 | goto unlock; |
| 3455 | } |
| 3456 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3457 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3458 | if (!cmd) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3459 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3460 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3461 | goto unlock; |
| 3462 | } |
| 3463 | |
| 3464 | conn = cmd->user_data; |
| 3465 | |
| 3466 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3467 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
| 3468 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3469 | goto unlock; |
| 3470 | } |
| 3471 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 3472 | cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3473 | mgmt_pending_remove(cmd); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3474 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3475 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
| 3476 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3477 | unlock: |
| 3478 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3479 | return err; |
| 3480 | } |
| 3481 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3482 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3483 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3484 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3485 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3486 | struct mgmt_pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3487 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3488 | int err; |
| 3489 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3490 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3491 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3492 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3493 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3494 | MGMT_STATUS_NOT_POWERED, addr, |
| 3495 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3496 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3497 | } |
| 3498 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3499 | if (addr->type == BDADDR_BREDR) |
| 3500 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3501 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3502 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3503 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3504 | if (!conn) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3505 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3506 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3507 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3508 | goto done; |
| 3509 | } |
| 3510 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3511 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3512 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3513 | if (!err) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3514 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3515 | MGMT_STATUS_SUCCESS, addr, |
| 3516 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3517 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3518 | err = mgmt_cmd_complete(sk, hdev->id, mgmt_op, |
| 3519 | MGMT_STATUS_FAILED, addr, |
| 3520 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3521 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3522 | goto done; |
| 3523 | } |
| 3524 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3525 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3526 | if (!cmd) { |
| 3527 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3528 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3529 | } |
| 3530 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 3531 | cmd->cmd_complete = addr_cmd_complete; |
| 3532 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3533 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3534 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3535 | struct hci_cp_user_passkey_reply cp; |
| 3536 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3537 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3538 | cp.passkey = passkey; |
| 3539 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3540 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3541 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3542 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3543 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3544 | if (err < 0) |
| 3545 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3546 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3547 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3548 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3549 | return err; |
| 3550 | } |
| 3551 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3552 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3553 | void *data, u16 len) |
| 3554 | { |
| 3555 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3556 | |
| 3557 | BT_DBG(""); |
| 3558 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3559 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3560 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3561 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3562 | } |
| 3563 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3564 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3565 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3566 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3567 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3568 | |
| 3569 | BT_DBG(""); |
| 3570 | |
| 3571 | if (len != sizeof(*cp)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3572 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
| 3573 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3574 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3575 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3576 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3577 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3578 | } |
| 3579 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3580 | static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3581 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3582 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3583 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3584 | |
| 3585 | BT_DBG(""); |
| 3586 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3587 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3588 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3589 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3590 | } |
| 3591 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3592 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3593 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3594 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3595 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3596 | |
| 3597 | BT_DBG(""); |
| 3598 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3599 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3600 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3601 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3602 | } |
| 3603 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3604 | static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3605 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3606 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3607 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3608 | |
| 3609 | BT_DBG(""); |
| 3610 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3611 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3612 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3613 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3614 | } |
| 3615 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3616 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3617 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3618 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3619 | struct hci_cp_write_local_name cp; |
| 3620 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3621 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3622 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3623 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3624 | } |
| 3625 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 3626 | static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3627 | { |
| 3628 | struct mgmt_cp_set_local_name *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3629 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3630 | |
| 3631 | BT_DBG("status 0x%02x", status); |
| 3632 | |
| 3633 | hci_dev_lock(hdev); |
| 3634 | |
| 3635 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3636 | if (!cmd) |
| 3637 | goto unlock; |
| 3638 | |
| 3639 | cp = cmd->param; |
| 3640 | |
| 3641 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3642 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3643 | mgmt_status(status)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3644 | else |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3645 | mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3646 | cp, sizeof(*cp)); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3647 | |
| 3648 | mgmt_pending_remove(cmd); |
| 3649 | |
| 3650 | unlock: |
| 3651 | hci_dev_unlock(hdev); |
| 3652 | } |
| 3653 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3654 | static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3655 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3656 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3657 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3658 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3659 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3660 | int err; |
| 3661 | |
| 3662 | BT_DBG(""); |
| 3663 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3664 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3665 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3666 | /* If the old values are the same as the new ones just return a |
| 3667 | * direct command complete event. |
| 3668 | */ |
| 3669 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3670 | !memcmp(hdev->short_name, cp->short_name, |
| 3671 | sizeof(hdev->short_name))) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3672 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3673 | data, len); |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3674 | goto failed; |
| 3675 | } |
| 3676 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3677 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3678 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3679 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3680 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3681 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3682 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3683 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3684 | if (err < 0) |
| 3685 | goto failed; |
| 3686 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 3687 | err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, |
| 3688 | data, len, sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3689 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3690 | goto failed; |
| 3691 | } |
| 3692 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3693 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3694 | if (!cmd) { |
| 3695 | err = -ENOMEM; |
| 3696 | goto failed; |
| 3697 | } |
| 3698 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3699 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3700 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3701 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3702 | |
| 3703 | if (lmp_bredr_capable(hdev)) { |
| 3704 | update_name(&req); |
| 3705 | update_eir(&req); |
| 3706 | } |
| 3707 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3708 | /* The name is stored in the scan response data and so |
| 3709 | * no need to udpate the advertising data here. |
| 3710 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3711 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3712 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3713 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3714 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3715 | if (err < 0) |
| 3716 | mgmt_pending_remove(cmd); |
| 3717 | |
| 3718 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3719 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3720 | return err; |
| 3721 | } |
| 3722 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3723 | static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3724 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3725 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 3726 | struct mgmt_pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3727 | int err; |
| 3728 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3729 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3730 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3731 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3732 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3733 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3734 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3735 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3736 | goto unlock; |
| 3737 | } |
| 3738 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3739 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3740 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3741 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3742 | goto unlock; |
| 3743 | } |
| 3744 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3745 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3746 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 3747 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3748 | goto unlock; |
| 3749 | } |
| 3750 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3751 | cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3752 | if (!cmd) { |
| 3753 | err = -ENOMEM; |
| 3754 | goto unlock; |
| 3755 | } |
| 3756 | |
Johan Hedberg | 710f11c | 2014-05-26 11:21:22 +0300 | [diff] [blame] | 3757 | if (bredr_sc_enabled(hdev)) |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3758 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3759 | 0, NULL); |
| 3760 | else |
| 3761 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3762 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3763 | if (err < 0) |
| 3764 | mgmt_pending_remove(cmd); |
| 3765 | |
| 3766 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3767 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3768 | return err; |
| 3769 | } |
| 3770 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3771 | static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3772 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3773 | { |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3774 | struct mgmt_addr_info *addr = data; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3775 | int err; |
| 3776 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3777 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3778 | |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3779 | if (!bdaddr_type_is_valid(addr->type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3780 | return mgmt_cmd_complete(sk, hdev->id, |
| 3781 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3782 | MGMT_STATUS_INVALID_PARAMS, |
| 3783 | addr, sizeof(*addr)); |
Johan Hedberg | 5d57e79 | 2015-01-23 10:10:38 +0200 | [diff] [blame] | 3784 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3785 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3786 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3787 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3788 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3789 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3790 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3791 | if (cp->addr.type != BDADDR_BREDR) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3792 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3793 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3794 | MGMT_STATUS_INVALID_PARAMS, |
| 3795 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3796 | goto unlock; |
| 3797 | } |
| 3798 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3799 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3800 | cp->addr.type, cp->hash, |
| 3801 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3802 | if (err < 0) |
| 3803 | status = MGMT_STATUS_FAILED; |
| 3804 | else |
| 3805 | status = MGMT_STATUS_SUCCESS; |
| 3806 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3807 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3808 | MGMT_OP_ADD_REMOTE_OOB_DATA, status, |
| 3809 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3810 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3811 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3812 | u8 *rand192, *hash192, *rand256, *hash256; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3813 | u8 status; |
| 3814 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3815 | if (bdaddr_type_is_le(cp->addr.type)) { |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3816 | /* Enforce zero-valued 192-bit parameters as |
| 3817 | * long as legacy SMP OOB isn't implemented. |
| 3818 | */ |
| 3819 | if (memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3820 | memcmp(cp->hash192, ZERO_KEY, 16)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3821 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3822 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3823 | MGMT_STATUS_INVALID_PARAMS, |
| 3824 | addr, sizeof(*addr)); |
Johan Hedberg | d25b78e | 2015-01-27 12:55:52 +0200 | [diff] [blame] | 3825 | goto unlock; |
| 3826 | } |
| 3827 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3828 | rand192 = NULL; |
| 3829 | hash192 = NULL; |
| 3830 | } else { |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3831 | /* In case one of the P-192 values is set to zero, |
| 3832 | * then just disable OOB data for P-192. |
| 3833 | */ |
| 3834 | if (!memcmp(cp->rand192, ZERO_KEY, 16) || |
| 3835 | !memcmp(cp->hash192, ZERO_KEY, 16)) { |
| 3836 | rand192 = NULL; |
| 3837 | hash192 = NULL; |
| 3838 | } else { |
| 3839 | rand192 = cp->rand192; |
| 3840 | hash192 = cp->hash192; |
| 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | /* In case one of the P-256 values is set to zero, then just |
| 3845 | * disable OOB data for P-256. |
| 3846 | */ |
| 3847 | if (!memcmp(cp->rand256, ZERO_KEY, 16) || |
| 3848 | !memcmp(cp->hash256, ZERO_KEY, 16)) { |
| 3849 | rand256 = NULL; |
| 3850 | hash256 = NULL; |
| 3851 | } else { |
| 3852 | rand256 = cp->rand256; |
| 3853 | hash256 = cp->hash256; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3854 | } |
| 3855 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 3856 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3857 | cp->addr.type, hash192, rand192, |
Marcel Holtmann | 41bcfd5 | 2015-01-31 00:37:02 -0800 | [diff] [blame] | 3858 | hash256, rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3859 | if (err < 0) |
| 3860 | status = MGMT_STATUS_FAILED; |
| 3861 | else |
| 3862 | status = MGMT_STATUS_SUCCESS; |
| 3863 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3864 | err = mgmt_cmd_complete(sk, hdev->id, |
| 3865 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3866 | status, &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3867 | } else { |
| 3868 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 3869 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3870 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3871 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3872 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3873 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3874 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3875 | return err; |
| 3876 | } |
| 3877 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3878 | static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3879 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3880 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3881 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3882 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3883 | int err; |
| 3884 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3885 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3886 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3887 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3888 | return mgmt_cmd_complete(sk, hdev->id, |
| 3889 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3890 | MGMT_STATUS_INVALID_PARAMS, |
| 3891 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3892 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3893 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3894 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3895 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 3896 | hci_remote_oob_data_clear(hdev); |
| 3897 | status = MGMT_STATUS_SUCCESS; |
| 3898 | goto done; |
| 3899 | } |
| 3900 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3901 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3902 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3903 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3904 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3905 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3906 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3907 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 3908 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3909 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3910 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3911 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3912 | return err; |
| 3913 | } |
| 3914 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3915 | static bool trigger_discovery(struct hci_request *req, u8 *status) |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3916 | { |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3917 | struct hci_dev *hdev = req->hdev; |
| 3918 | struct hci_cp_le_set_scan_param param_cp; |
| 3919 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3920 | struct hci_cp_inquiry inq_cp; |
| 3921 | /* General inquiry access code (GIAC) */ |
| 3922 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 3923 | u8 own_addr_type; |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3924 | int err; |
| 3925 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3926 | switch (hdev->discovery.type) { |
| 3927 | case DISCOV_TYPE_BREDR: |
| 3928 | *status = mgmt_bredr_support(hdev); |
| 3929 | if (*status) |
| 3930 | return false; |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3931 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3932 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3933 | *status = MGMT_STATUS_BUSY; |
| 3934 | return false; |
| 3935 | } |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3936 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3937 | hci_inquiry_cache_flush(hdev); |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3938 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3939 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3940 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
| 3941 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
| 3942 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
| 3943 | break; |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3944 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3945 | case DISCOV_TYPE_LE: |
| 3946 | case DISCOV_TYPE_INTERLEAVED: |
| 3947 | *status = mgmt_le_support(hdev); |
| 3948 | if (*status) |
| 3949 | return false; |
| 3950 | |
| 3951 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3952 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3953 | *status = MGMT_STATUS_NOT_SUPPORTED; |
| 3954 | return false; |
| 3955 | } |
| 3956 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3957 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) { |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3958 | /* Don't let discovery abort an outgoing |
| 3959 | * connection attempt that's using directed |
| 3960 | * advertising. |
| 3961 | */ |
| 3962 | if (hci_conn_hash_lookup_state(hdev, LE_LINK, |
| 3963 | BT_CONNECT)) { |
| 3964 | *status = MGMT_STATUS_REJECTED; |
| 3965 | return false; |
| 3966 | } |
| 3967 | |
| 3968 | disable_advertising(req); |
| 3969 | } |
| 3970 | |
| 3971 | /* If controller is scanning, it means the background scanning |
| 3972 | * is running. Thus, we should temporarily stop it in order to |
| 3973 | * set the discovery scanning parameters. |
| 3974 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 3975 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3976 | hci_req_add_le_scan_disable(req); |
| 3977 | |
| 3978 | memset(¶m_cp, 0, sizeof(param_cp)); |
| 3979 | |
| 3980 | /* All active scans will be done with either a resolvable |
| 3981 | * private address (when privacy feature has been enabled) |
Marcel Holtmann | 9437d2e | 2014-12-07 20:13:17 +0100 | [diff] [blame] | 3982 | * or non-resolvable private address. |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3983 | */ |
| 3984 | err = hci_update_random_address(req, true, &own_addr_type); |
| 3985 | if (err < 0) { |
| 3986 | *status = MGMT_STATUS_FAILED; |
| 3987 | return false; |
| 3988 | } |
| 3989 | |
| 3990 | param_cp.type = LE_SCAN_ACTIVE; |
| 3991 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3992 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
| 3993 | param_cp.own_address_type = own_addr_type; |
| 3994 | hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3995 | ¶m_cp); |
| 3996 | |
| 3997 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3998 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3999 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 4000 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 4001 | &enable_cp); |
| 4002 | break; |
| 4003 | |
| 4004 | default: |
| 4005 | *status = MGMT_STATUS_INVALID_PARAMS; |
| 4006 | return false; |
| 4007 | } |
| 4008 | |
| 4009 | return true; |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 4010 | } |
| 4011 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4012 | static void start_discovery_complete(struct hci_dev *hdev, u8 status, |
| 4013 | u16 opcode) |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4014 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4015 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4016 | unsigned long timeout; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 4017 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4018 | BT_DBG("status %d", status); |
| 4019 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4020 | hci_dev_lock(hdev); |
| 4021 | |
| 4022 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4023 | if (!cmd) |
| 4024 | cmd = mgmt_pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
| 4025 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4026 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4027 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4028 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4029 | } |
| 4030 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4031 | if (status) { |
| 4032 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 4033 | goto unlock; |
| 4034 | } |
| 4035 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4036 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4037 | |
Jakub Pawlowski | 2d28cfe | 2015-02-01 23:07:54 -0800 | [diff] [blame] | 4038 | /* If the scan involves LE scan, pick proper timeout to schedule |
| 4039 | * hdev->le_scan_disable that will stop it. |
| 4040 | */ |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4041 | switch (hdev->discovery.type) { |
| 4042 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 4043 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4044 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4045 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 4046 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4047 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4048 | case DISCOV_TYPE_BREDR: |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4049 | timeout = 0; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4050 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4051 | default: |
| 4052 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4053 | timeout = 0; |
| 4054 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4055 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 4056 | |
Jakub Pawlowski | 2d28cfe | 2015-02-01 23:07:54 -0800 | [diff] [blame] | 4057 | if (timeout) { |
| 4058 | /* When service discovery is used and the controller has |
| 4059 | * a strict duplicate filter, it is important to remember |
| 4060 | * the start and duration of the scan. This is required |
| 4061 | * for restarting scanning during the discovery phase. |
| 4062 | */ |
| 4063 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, |
| 4064 | &hdev->quirks) && |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 4065 | hdev->discovery.result_filtering) { |
Jakub Pawlowski | 2d28cfe | 2015-02-01 23:07:54 -0800 | [diff] [blame] | 4066 | hdev->discovery.scan_start = jiffies; |
| 4067 | hdev->discovery.scan_duration = timeout; |
| 4068 | } |
| 4069 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4070 | queue_delayed_work(hdev->workqueue, |
| 4071 | &hdev->le_scan_disable, timeout); |
Jakub Pawlowski | 2d28cfe | 2015-02-01 23:07:54 -0800 | [diff] [blame] | 4072 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 4073 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4074 | unlock: |
| 4075 | hci_dev_unlock(hdev); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4076 | } |
| 4077 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4078 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4079 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4080 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4081 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4082 | struct mgmt_pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4083 | struct hci_request req; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 4084 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4085 | int err; |
| 4086 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4087 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4088 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4089 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4090 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 4091 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4092 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 4093 | MGMT_STATUS_NOT_POWERED, |
| 4094 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 4095 | goto failed; |
| 4096 | } |
| 4097 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4098 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4099 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4100 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 4101 | MGMT_STATUS_BUSY, &cp->type, |
| 4102 | sizeof(cp->type)); |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 4103 | goto failed; |
| 4104 | } |
| 4105 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4106 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4107 | if (!cmd) { |
| 4108 | err = -ENOMEM; |
| 4109 | goto failed; |
| 4110 | } |
| 4111 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4112 | cmd->cmd_complete = generic_cmd_complete; |
| 4113 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4114 | /* Clear the discovery filter first to free any previously |
| 4115 | * allocated memory for the UUID list. |
| 4116 | */ |
| 4117 | hci_discovery_filter_clear(hdev); |
| 4118 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4119 | hdev->discovery.type = cp->type; |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 4120 | hdev->discovery.report_invalid_rssi = false; |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 4121 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4122 | hci_req_init(&req, hdev); |
| 4123 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 4124 | if (!trigger_discovery(&req, &status)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4125 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 4126 | status, &cp->type, sizeof(cp->type)); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 4127 | mgmt_pending_remove(cmd); |
| 4128 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 4129 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 4130 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 4131 | err = hci_req_run(&req, start_discovery_complete); |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4132 | if (err < 0) { |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4133 | mgmt_pending_remove(cmd); |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 4134 | goto failed; |
| 4135 | } |
| 4136 | |
| 4137 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4138 | |
| 4139 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4140 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4141 | return err; |
| 4142 | } |
| 4143 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4144 | static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd, |
| 4145 | u8 status) |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4146 | { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4147 | return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, |
| 4148 | cmd->param, 1); |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4149 | } |
| 4150 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4151 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 4152 | void *data, u16 len) |
| 4153 | { |
| 4154 | struct mgmt_cp_start_service_discovery *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4155 | struct mgmt_pending_cmd *cmd; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4156 | struct hci_request req; |
| 4157 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 4158 | u16 uuid_count, expected_len; |
| 4159 | u8 status; |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4160 | int err; |
| 4161 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4162 | BT_DBG("%s", hdev->name); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4163 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4164 | hci_dev_lock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4165 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4166 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4167 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4168 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4169 | MGMT_STATUS_NOT_POWERED, |
| 4170 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4171 | goto failed; |
| 4172 | } |
| 4173 | |
| 4174 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4175 | hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4176 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4177 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4178 | MGMT_STATUS_BUSY, &cp->type, |
| 4179 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4180 | goto failed; |
| 4181 | } |
| 4182 | |
| 4183 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 4184 | if (uuid_count > max_uuid_count) { |
| 4185 | BT_ERR("service_discovery: too big uuid_count value %u", |
| 4186 | uuid_count); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4187 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4188 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4189 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4190 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4191 | goto failed; |
| 4192 | } |
| 4193 | |
| 4194 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 4195 | if (expected_len != len) { |
| 4196 | BT_ERR("service_discovery: expected %u bytes, got %u bytes", |
| 4197 | expected_len, len); |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4198 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4199 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4200 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 4201 | sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4202 | goto failed; |
| 4203 | } |
| 4204 | |
| 4205 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4206 | hdev, data, len); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4207 | if (!cmd) { |
| 4208 | err = -ENOMEM; |
| 4209 | goto failed; |
| 4210 | } |
| 4211 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4212 | cmd->cmd_complete = service_discovery_cmd_complete; |
| 4213 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame] | 4214 | /* Clear the discovery filter first to free any previously |
| 4215 | * allocated memory for the UUID list. |
| 4216 | */ |
| 4217 | hci_discovery_filter_clear(hdev); |
| 4218 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 4219 | hdev->discovery.result_filtering = true; |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4220 | hdev->discovery.type = cp->type; |
| 4221 | hdev->discovery.rssi = cp->rssi; |
| 4222 | hdev->discovery.uuid_count = uuid_count; |
| 4223 | |
| 4224 | if (uuid_count > 0) { |
| 4225 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 4226 | GFP_KERNEL); |
| 4227 | if (!hdev->discovery.uuids) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4228 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4229 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4230 | MGMT_STATUS_FAILED, |
| 4231 | &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4232 | mgmt_pending_remove(cmd); |
| 4233 | goto failed; |
| 4234 | } |
| 4235 | } |
| 4236 | |
| 4237 | hci_req_init(&req, hdev); |
| 4238 | |
| 4239 | if (!trigger_discovery(&req, &status)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4240 | err = mgmt_cmd_complete(sk, hdev->id, |
| 4241 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 4242 | status, &cp->type, sizeof(cp->type)); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 4243 | mgmt_pending_remove(cmd); |
| 4244 | goto failed; |
| 4245 | } |
| 4246 | |
| 4247 | err = hci_req_run(&req, start_discovery_complete); |
| 4248 | if (err < 0) { |
| 4249 | mgmt_pending_remove(cmd); |
| 4250 | goto failed; |
| 4251 | } |
| 4252 | |
| 4253 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
| 4254 | |
| 4255 | failed: |
| 4256 | hci_dev_unlock(hdev); |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 4257 | return err; |
| 4258 | } |
| 4259 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4260 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4261 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4262 | struct mgmt_pending_cmd *cmd; |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4263 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4264 | BT_DBG("status %d", status); |
| 4265 | |
| 4266 | hci_dev_lock(hdev); |
| 4267 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4268 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 4269 | if (cmd) { |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4270 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4271 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4272 | } |
| 4273 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4274 | if (!status) |
| 4275 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4276 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4277 | hci_dev_unlock(hdev); |
| 4278 | } |
| 4279 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4280 | static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4281 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4282 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4283 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4284 | struct mgmt_pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4285 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4286 | int err; |
| 4287 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4288 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4289 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4290 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4291 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4292 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4293 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4294 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 4295 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4296 | goto unlock; |
| 4297 | } |
| 4298 | |
| 4299 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4300 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 4301 | MGMT_STATUS_INVALID_PARAMS, |
| 4302 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4303 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 4304 | } |
| 4305 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4306 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4307 | if (!cmd) { |
| 4308 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4309 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4310 | } |
| 4311 | |
Johan Hedberg | 2922a94 | 2014-12-05 13:36:06 +0200 | [diff] [blame] | 4312 | cmd->cmd_complete = generic_cmd_complete; |
| 4313 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4314 | hci_req_init(&req, hdev); |
| 4315 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4316 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4317 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4318 | err = hci_req_run(&req, stop_discovery_complete); |
| 4319 | if (!err) { |
| 4320 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4321 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4322 | } |
| 4323 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4324 | mgmt_pending_remove(cmd); |
| 4325 | |
| 4326 | /* If no HCI commands were sent we're done */ |
| 4327 | if (err == -ENODATA) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4328 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 4329 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4330 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 4331 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4332 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4333 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4334 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4335 | return err; |
| 4336 | } |
| 4337 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4338 | static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4339 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4340 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4341 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4342 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4343 | int err; |
| 4344 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4345 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4346 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4347 | hci_dev_lock(hdev); |
| 4348 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4349 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4350 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4351 | MGMT_STATUS_FAILED, &cp->addr, |
| 4352 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4353 | goto failed; |
| 4354 | } |
| 4355 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 4356 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4357 | if (!e) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4358 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4359 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 4360 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4361 | goto failed; |
| 4362 | } |
| 4363 | |
| 4364 | if (cp->name_known) { |
| 4365 | e->name_state = NAME_KNOWN; |
| 4366 | list_del(&e->list); |
| 4367 | } else { |
| 4368 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 4369 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4370 | } |
| 4371 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4372 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, |
| 4373 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4374 | |
| 4375 | failed: |
| 4376 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4377 | return err; |
| 4378 | } |
| 4379 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4380 | static int block_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4381 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4382 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4383 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4384 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4385 | int err; |
| 4386 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4387 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4388 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4389 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4390 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 4391 | MGMT_STATUS_INVALID_PARAMS, |
| 4392 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4393 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4394 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4395 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4396 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 4397 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4398 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4399 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4400 | goto done; |
| 4401 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4402 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4403 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4404 | sk); |
| 4405 | status = MGMT_STATUS_SUCCESS; |
| 4406 | |
| 4407 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4408 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
| 4409 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4410 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4411 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4412 | |
| 4413 | return err; |
| 4414 | } |
| 4415 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4416 | static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4417 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4418 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4419 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4420 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4421 | int err; |
| 4422 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4423 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4424 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4425 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4426 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 4427 | MGMT_STATUS_INVALID_PARAMS, |
| 4428 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4429 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4430 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4431 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4432 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 4433 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4434 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4435 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4436 | goto done; |
| 4437 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4438 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4439 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4440 | sk); |
| 4441 | status = MGMT_STATUS_SUCCESS; |
| 4442 | |
| 4443 | done: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4444 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
| 4445 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4446 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4447 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4448 | |
| 4449 | return err; |
| 4450 | } |
| 4451 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4452 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4453 | u16 len) |
| 4454 | { |
| 4455 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4456 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4457 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4458 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4459 | |
| 4460 | BT_DBG("%s", hdev->name); |
| 4461 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4462 | source = __le16_to_cpu(cp->source); |
| 4463 | |
| 4464 | if (source > 0x0002) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4465 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4466 | MGMT_STATUS_INVALID_PARAMS); |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4467 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4468 | hci_dev_lock(hdev); |
| 4469 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4470 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4471 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4472 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4473 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4474 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4475 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, |
| 4476 | NULL, 0); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4477 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4478 | hci_req_init(&req, hdev); |
| 4479 | update_eir(&req); |
| 4480 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4481 | |
| 4482 | hci_dev_unlock(hdev); |
| 4483 | |
| 4484 | return err; |
| 4485 | } |
| 4486 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4487 | static void set_advertising_complete(struct hci_dev *hdev, u8 status, |
| 4488 | u16 opcode) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4489 | { |
| 4490 | struct cmd_lookup match = { NULL, hdev }; |
| 4491 | |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4492 | hci_dev_lock(hdev); |
| 4493 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4494 | if (status) { |
| 4495 | u8 mgmt_err = mgmt_status(status); |
| 4496 | |
| 4497 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4498 | cmd_status_rsp, &mgmt_err); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4499 | goto unlock; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4500 | } |
| 4501 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4502 | if (hci_dev_test_flag(hdev, HCI_LE_ADV)) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4503 | hci_dev_set_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4504 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4505 | hci_dev_clear_flag(hdev, HCI_ADVERTISING); |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4506 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4507 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4508 | &match); |
| 4509 | |
| 4510 | new_settings(hdev, match.sk); |
| 4511 | |
| 4512 | if (match.sk) |
| 4513 | sock_put(match.sk); |
Jaganath Kanakkassery | 3ad6758 | 2014-12-11 11:43:12 +0530 | [diff] [blame] | 4514 | |
| 4515 | unlock: |
| 4516 | hci_dev_unlock(hdev); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4517 | } |
| 4518 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4519 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4520 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4521 | { |
| 4522 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4523 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4524 | struct hci_request req; |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4525 | u8 val, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4526 | int err; |
| 4527 | |
| 4528 | BT_DBG("request for %s", hdev->name); |
| 4529 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4530 | status = mgmt_le_support(hdev); |
| 4531 | if (status) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4532 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4533 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4534 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4535 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4536 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4537 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4538 | |
| 4539 | hci_dev_lock(hdev); |
| 4540 | |
| 4541 | val = !!cp->val; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4542 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4543 | /* The following conditions are ones which mean that we should |
| 4544 | * not do any HCI communication but directly send a mgmt |
| 4545 | * response to user space (after toggling the flag if |
| 4546 | * necessary). |
| 4547 | */ |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4548 | if (!hdev_is_powered(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4549 | (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) && |
| 4550 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4551 | hci_conn_num(hdev, LE_LINK) > 0 || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4552 | (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4553 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4554 | bool changed; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4555 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4556 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 4557 | changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4558 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4559 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4560 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4561 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4562 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 4563 | changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4564 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4568 | if (err < 0) |
| 4569 | goto unlock; |
| 4570 | |
| 4571 | if (changed) |
| 4572 | err = new_settings(hdev, sk); |
| 4573 | |
| 4574 | goto unlock; |
| 4575 | } |
| 4576 | |
| 4577 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4578 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4579 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4580 | MGMT_STATUS_BUSY); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4581 | goto unlock; |
| 4582 | } |
| 4583 | |
| 4584 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4585 | if (!cmd) { |
| 4586 | err = -ENOMEM; |
| 4587 | goto unlock; |
| 4588 | } |
| 4589 | |
| 4590 | hci_req_init(&req, hdev); |
| 4591 | |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4592 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4593 | hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4594 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4595 | hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE); |
Marcel Holtmann | cc91cb0 | 2015-03-12 22:30:58 -0700 | [diff] [blame] | 4596 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4597 | if (val) |
| 4598 | enable_advertising(&req); |
| 4599 | else |
| 4600 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4601 | |
| 4602 | err = hci_req_run(&req, set_advertising_complete); |
| 4603 | if (err < 0) |
| 4604 | mgmt_pending_remove(cmd); |
| 4605 | |
| 4606 | unlock: |
| 4607 | hci_dev_unlock(hdev); |
| 4608 | return err; |
| 4609 | } |
| 4610 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4611 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4612 | void *data, u16 len) |
| 4613 | { |
| 4614 | struct mgmt_cp_set_static_address *cp = data; |
| 4615 | int err; |
| 4616 | |
| 4617 | BT_DBG("%s", hdev->name); |
| 4618 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4619 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4620 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4621 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4622 | |
| 4623 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4624 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4625 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4626 | |
| 4627 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4628 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4629 | return mgmt_cmd_status(sk, hdev->id, |
| 4630 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4631 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4632 | |
| 4633 | /* Two most significant bits shall be set */ |
| 4634 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4635 | return mgmt_cmd_status(sk, hdev->id, |
| 4636 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4637 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | hci_dev_lock(hdev); |
| 4641 | |
| 4642 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4643 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4644 | err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev); |
| 4645 | if (err < 0) |
| 4646 | goto unlock; |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4647 | |
Marcel Holtmann | 93690c2 | 2015-03-06 10:11:21 -0800 | [diff] [blame] | 4648 | err = new_settings(hdev, sk); |
| 4649 | |
| 4650 | unlock: |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4651 | hci_dev_unlock(hdev); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4652 | return err; |
| 4653 | } |
| 4654 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4655 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4656 | void *data, u16 len) |
| 4657 | { |
| 4658 | struct mgmt_cp_set_scan_params *cp = data; |
| 4659 | __u16 interval, window; |
| 4660 | int err; |
| 4661 | |
| 4662 | BT_DBG("%s", hdev->name); |
| 4663 | |
| 4664 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4665 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4666 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4667 | |
| 4668 | interval = __le16_to_cpu(cp->interval); |
| 4669 | |
| 4670 | if (interval < 0x0004 || interval > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4671 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4672 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4673 | |
| 4674 | window = __le16_to_cpu(cp->window); |
| 4675 | |
| 4676 | if (window < 0x0004 || window > 0x4000) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4677 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4678 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4679 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4680 | if (window > interval) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4681 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4682 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4683 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4684 | hci_dev_lock(hdev); |
| 4685 | |
| 4686 | hdev->le_scan_interval = interval; |
| 4687 | hdev->le_scan_window = window; |
| 4688 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 4689 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, |
| 4690 | NULL, 0); |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4691 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4692 | /* If background scan is running, restart it so new parameters are |
| 4693 | * loaded. |
| 4694 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4695 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN) && |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4696 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4697 | struct hci_request req; |
| 4698 | |
| 4699 | hci_req_init(&req, hdev); |
| 4700 | |
| 4701 | hci_req_add_le_scan_disable(&req); |
| 4702 | hci_req_add_le_passive_scan(&req); |
| 4703 | |
| 4704 | hci_req_run(&req, NULL); |
| 4705 | } |
| 4706 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4707 | hci_dev_unlock(hdev); |
| 4708 | |
| 4709 | return err; |
| 4710 | } |
| 4711 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4712 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status, |
| 4713 | u16 opcode) |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4714 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4715 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4716 | |
| 4717 | BT_DBG("status 0x%02x", status); |
| 4718 | |
| 4719 | hci_dev_lock(hdev); |
| 4720 | |
| 4721 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4722 | if (!cmd) |
| 4723 | goto unlock; |
| 4724 | |
| 4725 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4726 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4727 | mgmt_status(status)); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4728 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4729 | struct mgmt_mode *cp = cmd->param; |
| 4730 | |
| 4731 | if (cp->val) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4732 | hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4733 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4734 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4735 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4736 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4737 | new_settings(hdev, cmd->sk); |
| 4738 | } |
| 4739 | |
| 4740 | mgmt_pending_remove(cmd); |
| 4741 | |
| 4742 | unlock: |
| 4743 | hci_dev_unlock(hdev); |
| 4744 | } |
| 4745 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4746 | static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4747 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4748 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4749 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4750 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4751 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4752 | int err; |
| 4753 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4754 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4755 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4756 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) || |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4757 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4758 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4759 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4760 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4761 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4762 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4763 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4764 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4765 | hci_dev_lock(hdev); |
| 4766 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4767 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4768 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4769 | MGMT_STATUS_BUSY); |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4770 | goto unlock; |
| 4771 | } |
| 4772 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4773 | if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4774 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4775 | hdev); |
| 4776 | goto unlock; |
| 4777 | } |
| 4778 | |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4779 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4780 | hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE); |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 4781 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4782 | hdev); |
| 4783 | new_settings(hdev, sk); |
| 4784 | goto unlock; |
| 4785 | } |
| 4786 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4787 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4788 | data, len); |
| 4789 | if (!cmd) { |
| 4790 | err = -ENOMEM; |
| 4791 | goto unlock; |
| 4792 | } |
| 4793 | |
| 4794 | hci_req_init(&req, hdev); |
| 4795 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4796 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4797 | |
| 4798 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4799 | if (err < 0) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4800 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4801 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4802 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4803 | } |
| 4804 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4805 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4806 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4807 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4808 | return err; |
| 4809 | } |
| 4810 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 4811 | static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4812 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4813 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4814 | |
| 4815 | BT_DBG("status 0x%02x", status); |
| 4816 | |
| 4817 | hci_dev_lock(hdev); |
| 4818 | |
| 4819 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4820 | if (!cmd) |
| 4821 | goto unlock; |
| 4822 | |
| 4823 | if (status) { |
| 4824 | u8 mgmt_err = mgmt_status(status); |
| 4825 | |
| 4826 | /* We need to restore the flag if related HCI commands |
| 4827 | * failed. |
| 4828 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4829 | hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4830 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4831 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4832 | } else { |
| 4833 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4834 | new_settings(hdev, cmd->sk); |
| 4835 | } |
| 4836 | |
| 4837 | mgmt_pending_remove(cmd); |
| 4838 | |
| 4839 | unlock: |
| 4840 | hci_dev_unlock(hdev); |
| 4841 | } |
| 4842 | |
| 4843 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4844 | { |
| 4845 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4846 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4847 | struct hci_request req; |
| 4848 | int err; |
| 4849 | |
| 4850 | BT_DBG("request for %s", hdev->name); |
| 4851 | |
| 4852 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4853 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4854 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4855 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4856 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4857 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4858 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4859 | |
| 4860 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4861 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4862 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4863 | |
| 4864 | hci_dev_lock(hdev); |
| 4865 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4866 | if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4867 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4868 | goto unlock; |
| 4869 | } |
| 4870 | |
| 4871 | if (!hdev_is_powered(hdev)) { |
| 4872 | if (!cp->val) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4873 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
| 4874 | hci_dev_clear_flag(hdev, HCI_SSP_ENABLED); |
| 4875 | hci_dev_clear_flag(hdev, HCI_LINK_SECURITY); |
| 4876 | hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE); |
| 4877 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4878 | } |
| 4879 | |
Marcel Holtmann | ce05d60 | 2015-03-13 02:11:03 -0700 | [diff] [blame] | 4880 | hci_dev_change_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4881 | |
| 4882 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4883 | if (err < 0) |
| 4884 | goto unlock; |
| 4885 | |
| 4886 | err = new_settings(hdev, sk); |
| 4887 | goto unlock; |
| 4888 | } |
| 4889 | |
| 4890 | /* Reject disabling when powered on */ |
| 4891 | if (!cp->val) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4892 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4893 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4894 | goto unlock; |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4895 | } else { |
| 4896 | /* When configuring a dual-mode controller to operate |
| 4897 | * with LE only and using a static address, then switching |
| 4898 | * BR/EDR back on is not allowed. |
| 4899 | * |
| 4900 | * Dual-mode controllers shall operate with the public |
| 4901 | * address as its identity address for BR/EDR and LE. So |
| 4902 | * reject the attempt to create an invalid configuration. |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4903 | * |
| 4904 | * The same restrictions applies when secure connections |
| 4905 | * has been enabled. For BR/EDR this is a controller feature |
| 4906 | * while for LE it is a host stack feature. This means that |
| 4907 | * switching BR/EDR back on when secure connections has been |
| 4908 | * enabled is not a supported transaction. |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4909 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4910 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 3a5486e | 2015-01-22 11:15:21 -0800 | [diff] [blame] | 4911 | (bacmp(&hdev->static_addr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 4912 | hci_dev_test_flag(hdev, HCI_SC_ENABLED))) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4913 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4914 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 111e4bc | 2015-01-14 14:40:42 -0800 | [diff] [blame] | 4915 | goto unlock; |
| 4916 | } |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4920 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4921 | MGMT_STATUS_BUSY); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4922 | goto unlock; |
| 4923 | } |
| 4924 | |
| 4925 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4926 | if (!cmd) { |
| 4927 | err = -ENOMEM; |
| 4928 | goto unlock; |
| 4929 | } |
| 4930 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4931 | /* We need to flip the bit already here so that update_adv_data |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4932 | * generates the correct flags. |
| 4933 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4934 | hci_dev_set_flag(hdev, HCI_BREDR_ENABLED); |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4935 | |
| 4936 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4937 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 4938 | write_fast_connectable(&req, false); |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 4939 | __hci_update_page_scan(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4940 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4941 | /* Since only the advertising data flags will change, there |
| 4942 | * is no need to update the scan response data. |
| 4943 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4944 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4945 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4946 | err = hci_req_run(&req, set_bredr_complete); |
| 4947 | if (err < 0) |
| 4948 | mgmt_pending_remove(cmd); |
| 4949 | |
| 4950 | unlock: |
| 4951 | hci_dev_unlock(hdev); |
| 4952 | return err; |
| 4953 | } |
| 4954 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4955 | static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 4956 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 4957 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4958 | struct mgmt_mode *cp; |
| 4959 | |
| 4960 | BT_DBG("%s status %u", hdev->name, status); |
| 4961 | |
| 4962 | hci_dev_lock(hdev); |
| 4963 | |
| 4964 | cmd = mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev); |
| 4965 | if (!cmd) |
| 4966 | goto unlock; |
| 4967 | |
| 4968 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 4969 | mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, |
| 4970 | mgmt_status(status)); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4971 | goto remove; |
| 4972 | } |
| 4973 | |
| 4974 | cp = cmd->param; |
| 4975 | |
| 4976 | switch (cp->val) { |
| 4977 | case 0x00: |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4978 | hci_dev_clear_flag(hdev, HCI_SC_ENABLED); |
| 4979 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4980 | break; |
| 4981 | case 0x01: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4982 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 4983 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4984 | break; |
| 4985 | case 0x02: |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 4986 | hci_dev_set_flag(hdev, HCI_SC_ENABLED); |
| 4987 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 4988 | break; |
| 4989 | } |
| 4990 | |
| 4991 | send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4992 | new_settings(hdev, cmd->sk); |
| 4993 | |
| 4994 | remove: |
| 4995 | mgmt_pending_remove(cmd); |
| 4996 | unlock: |
| 4997 | hci_dev_unlock(hdev); |
| 4998 | } |
| 4999 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5000 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 5001 | void *data, u16 len) |
| 5002 | { |
| 5003 | struct mgmt_mode *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5004 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5005 | struct hci_request req; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 5006 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5007 | int err; |
| 5008 | |
| 5009 | BT_DBG("request for %s", hdev->name); |
| 5010 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5011 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5012 | !hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5013 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5014 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5015 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5016 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Johan Hedberg | 5920028 | 2015-01-28 19:56:00 +0200 | [diff] [blame] | 5017 | lmp_sc_capable(hdev) && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5018 | !hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5019 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5020 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | ed93ec6 | 2015-01-22 11:15:22 -0800 | [diff] [blame] | 5021 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5022 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5023 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5024 | MGMT_STATUS_INVALID_PARAMS); |
| 5025 | |
| 5026 | hci_dev_lock(hdev); |
| 5027 | |
Marcel Holtmann | 05b3c3e | 2014-12-31 14:43:18 -0800 | [diff] [blame] | 5028 | if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5029 | !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5030 | bool changed; |
| 5031 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5032 | if (cp->val) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5033 | changed = !hci_dev_test_and_set_flag(hdev, |
| 5034 | HCI_SC_ENABLED); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5035 | if (cp->val == 0x02) |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5036 | hci_dev_set_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5037 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5038 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5039 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5040 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5041 | HCI_SC_ENABLED); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5042 | hci_dev_clear_flag(hdev, HCI_SC_ONLY); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5043 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5044 | |
| 5045 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5046 | if (err < 0) |
| 5047 | goto failed; |
| 5048 | |
| 5049 | if (changed) |
| 5050 | err = new_settings(hdev, sk); |
| 5051 | |
| 5052 | goto failed; |
| 5053 | } |
| 5054 | |
| 5055 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5056 | err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 5057 | MGMT_STATUS_BUSY); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5058 | goto failed; |
| 5059 | } |
| 5060 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5061 | val = !!cp->val; |
| 5062 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5063 | if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) && |
| 5064 | (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5065 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 5066 | goto failed; |
| 5067 | } |
| 5068 | |
| 5069 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 5070 | if (!cmd) { |
| 5071 | err = -ENOMEM; |
| 5072 | goto failed; |
| 5073 | } |
| 5074 | |
Johan Hedberg | a1443f5 | 2015-01-23 15:42:46 +0200 | [diff] [blame] | 5075 | hci_req_init(&req, hdev); |
| 5076 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
| 5077 | err = hci_req_run(&req, sc_enable_complete); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5078 | if (err < 0) { |
| 5079 | mgmt_pending_remove(cmd); |
| 5080 | goto failed; |
| 5081 | } |
| 5082 | |
| 5083 | failed: |
| 5084 | hci_dev_unlock(hdev); |
| 5085 | return err; |
| 5086 | } |
| 5087 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5088 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 5089 | void *data, u16 len) |
| 5090 | { |
| 5091 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5092 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5093 | int err; |
| 5094 | |
| 5095 | BT_DBG("request for %s", hdev->name); |
| 5096 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5097 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5098 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 5099 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5100 | |
| 5101 | hci_dev_lock(hdev); |
| 5102 | |
| 5103 | if (cp->val) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5104 | changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5105 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5106 | changed = hci_dev_test_and_clear_flag(hdev, |
| 5107 | HCI_KEEP_DEBUG_KEYS); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5108 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5109 | if (cp->val == 0x02) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5110 | use_changed = !hci_dev_test_and_set_flag(hdev, |
| 5111 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5112 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5113 | use_changed = hci_dev_test_and_clear_flag(hdev, |
| 5114 | HCI_USE_DEBUG_KEYS); |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5115 | |
| 5116 | if (hdev_is_powered(hdev) && use_changed && |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 5117 | hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 5118 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 5119 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 5120 | sizeof(mode), &mode); |
| 5121 | } |
| 5122 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5123 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 5124 | if (err < 0) |
| 5125 | goto unlock; |
| 5126 | |
| 5127 | if (changed) |
| 5128 | err = new_settings(hdev, sk); |
| 5129 | |
| 5130 | unlock: |
| 5131 | hci_dev_unlock(hdev); |
| 5132 | return err; |
| 5133 | } |
| 5134 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5135 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5136 | u16 len) |
| 5137 | { |
| 5138 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 5139 | bool changed; |
| 5140 | int err; |
| 5141 | |
| 5142 | BT_DBG("request for %s", hdev->name); |
| 5143 | |
| 5144 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5145 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5146 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5147 | |
| 5148 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5149 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5150 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5151 | |
| 5152 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5153 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 5154 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5155 | |
| 5156 | hci_dev_lock(hdev); |
| 5157 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5158 | /* If user space supports this command it is also expected to |
| 5159 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 5160 | */ |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5161 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 5162 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5163 | if (cp->privacy) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 5164 | changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5165 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5166 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5167 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 5168 | changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5169 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 5170 | hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5171 | } |
| 5172 | |
| 5173 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 5174 | if (err < 0) |
| 5175 | goto unlock; |
| 5176 | |
| 5177 | if (changed) |
| 5178 | err = new_settings(hdev, sk); |
| 5179 | |
| 5180 | unlock: |
| 5181 | hci_dev_unlock(hdev); |
| 5182 | return err; |
| 5183 | } |
| 5184 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5185 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 5186 | { |
| 5187 | switch (irk->addr.type) { |
| 5188 | case BDADDR_LE_PUBLIC: |
| 5189 | return true; |
| 5190 | |
| 5191 | case BDADDR_LE_RANDOM: |
| 5192 | /* Two most significant bits shall be set */ |
| 5193 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5194 | return false; |
| 5195 | return true; |
| 5196 | } |
| 5197 | |
| 5198 | return false; |
| 5199 | } |
| 5200 | |
| 5201 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 5202 | u16 len) |
| 5203 | { |
| 5204 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5205 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 5206 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5207 | u16 irk_count, expected_len; |
| 5208 | int i, err; |
| 5209 | |
| 5210 | BT_DBG("request for %s", hdev->name); |
| 5211 | |
| 5212 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5213 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5214 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5215 | |
| 5216 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5217 | if (irk_count > max_irk_count) { |
| 5218 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5219 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5220 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5221 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5222 | |
| 5223 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 5224 | if (expected_len != len) { |
| 5225 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 5226 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5227 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 5228 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 5232 | |
| 5233 | for (i = 0; i < irk_count; i++) { |
| 5234 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 5235 | |
| 5236 | if (!irk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5237 | return mgmt_cmd_status(sk, hdev->id, |
| 5238 | MGMT_OP_LOAD_IRKS, |
| 5239 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5240 | } |
| 5241 | |
| 5242 | hci_dev_lock(hdev); |
| 5243 | |
| 5244 | hci_smp_irks_clear(hdev); |
| 5245 | |
| 5246 | for (i = 0; i < irk_count; i++) { |
| 5247 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 5248 | u8 addr_type; |
| 5249 | |
| 5250 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 5251 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5252 | else |
| 5253 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5254 | |
| 5255 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 5256 | BDADDR_ANY); |
| 5257 | } |
| 5258 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 5259 | hci_dev_set_flag(hdev, HCI_RPA_RESOLVING); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5260 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5261 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5262 | |
| 5263 | hci_dev_unlock(hdev); |
| 5264 | |
| 5265 | return err; |
| 5266 | } |
| 5267 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5268 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 5269 | { |
| 5270 | if (key->master != 0x00 && key->master != 0x01) |
| 5271 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 5272 | |
| 5273 | switch (key->addr.type) { |
| 5274 | case BDADDR_LE_PUBLIC: |
| 5275 | return true; |
| 5276 | |
| 5277 | case BDADDR_LE_RANDOM: |
| 5278 | /* Two most significant bits shall be set */ |
| 5279 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 5280 | return false; |
| 5281 | return true; |
| 5282 | } |
| 5283 | |
| 5284 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5285 | } |
| 5286 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5287 | static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5288 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5289 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5290 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5291 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 5292 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5293 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5294 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5295 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5296 | BT_DBG("request for %s", hdev->name); |
| 5297 | |
| 5298 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5299 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5300 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 5301 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5302 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5303 | if (key_count > max_key_count) { |
| 5304 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5305 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5306 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5307 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5308 | |
| 5309 | expected_len = sizeof(*cp) + key_count * |
| 5310 | sizeof(struct mgmt_ltk_info); |
| 5311 | if (expected_len != len) { |
| 5312 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 5313 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5314 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5315 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5316 | } |
| 5317 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5318 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5319 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5320 | for (i = 0; i < key_count; i++) { |
| 5321 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 5322 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 5323 | if (!ltk_is_valid(key)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 5324 | return mgmt_cmd_status(sk, hdev->id, |
| 5325 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 5326 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 5327 | } |
| 5328 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5329 | hci_dev_lock(hdev); |
| 5330 | |
| 5331 | hci_smp_ltks_clear(hdev); |
| 5332 | |
| 5333 | for (i = 0; i < key_count; i++) { |
| 5334 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5335 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 5336 | |
| 5337 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 5338 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5339 | else |
| 5340 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5341 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5342 | switch (key->type) { |
| 5343 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5344 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5345 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5346 | break; |
| 5347 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5348 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5349 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5350 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5351 | case MGMT_LTK_P256_UNAUTH: |
| 5352 | authenticated = 0x00; |
| 5353 | type = SMP_LTK_P256; |
| 5354 | break; |
| 5355 | case MGMT_LTK_P256_AUTH: |
| 5356 | authenticated = 0x01; |
| 5357 | type = SMP_LTK_P256; |
| 5358 | break; |
| 5359 | case MGMT_LTK_P256_DEBUG: |
| 5360 | authenticated = 0x00; |
| 5361 | type = SMP_LTK_P256_DEBUG; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5362 | default: |
| 5363 | continue; |
| 5364 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5365 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 5366 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5367 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 5368 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5369 | } |
| 5370 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5371 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5372 | NULL, 0); |
| 5373 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5374 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5375 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5376 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5377 | } |
| 5378 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5379 | static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5380 | { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5381 | struct hci_conn *conn = cmd->user_data; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5382 | struct mgmt_rp_get_conn_info rp; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5383 | int err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5384 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5385 | memcpy(&rp.addr, cmd->param, sizeof(rp.addr)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5386 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5387 | if (status == MGMT_STATUS_SUCCESS) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5388 | rp.rssi = conn->rssi; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5389 | rp.tx_power = conn->tx_power; |
| 5390 | rp.max_tx_power = conn->max_tx_power; |
| 5391 | } else { |
| 5392 | rp.rssi = HCI_RSSI_INVALID; |
| 5393 | rp.tx_power = HCI_TX_POWER_INVALID; |
| 5394 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5395 | } |
| 5396 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5397 | err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 5398 | status, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5399 | |
| 5400 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5401 | hci_conn_put(conn); |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5402 | |
| 5403 | return err; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5404 | } |
| 5405 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5406 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status, |
| 5407 | u16 opcode) |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5408 | { |
| 5409 | struct hci_cp_read_rssi *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5410 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5411 | struct hci_conn *conn; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5412 | u16 handle; |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5413 | u8 status; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5414 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5415 | BT_DBG("status 0x%02x", hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5416 | |
| 5417 | hci_dev_lock(hdev); |
| 5418 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5419 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 5420 | * Level so we check which one was last sent to retrieve connection |
| 5421 | * handle. Both commands have handle as first parameter so it's safe to |
| 5422 | * cast data on the same command struct. |
| 5423 | * |
| 5424 | * First command sent is always Read RSSI and we fail only if it fails. |
| 5425 | * In other case we simply override error to indicate success as we |
| 5426 | * already remembered if TX power value is actually valid. |
| 5427 | */ |
| 5428 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 5429 | if (!cp) { |
| 5430 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5431 | status = MGMT_STATUS_SUCCESS; |
| 5432 | } else { |
| 5433 | status = mgmt_status(hci_status); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5434 | } |
| 5435 | |
| 5436 | if (!cp) { |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5437 | BT_ERR("invalid sent_cmd in conn_info response"); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5438 | goto unlock; |
| 5439 | } |
| 5440 | |
| 5441 | handle = __le16_to_cpu(cp->handle); |
| 5442 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5443 | if (!conn) { |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5444 | BT_ERR("unknown handle (%d) in conn_info response", handle); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5445 | goto unlock; |
| 5446 | } |
| 5447 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5448 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); |
| 5449 | if (!cmd) |
| 5450 | goto unlock; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5451 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5452 | cmd->cmd_complete(cmd, status); |
| 5453 | mgmt_pending_remove(cmd); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5454 | |
| 5455 | unlock: |
| 5456 | hci_dev_unlock(hdev); |
| 5457 | } |
| 5458 | |
| 5459 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5460 | u16 len) |
| 5461 | { |
| 5462 | struct mgmt_cp_get_conn_info *cp = data; |
| 5463 | struct mgmt_rp_get_conn_info rp; |
| 5464 | struct hci_conn *conn; |
| 5465 | unsigned long conn_info_age; |
| 5466 | int err = 0; |
| 5467 | |
| 5468 | BT_DBG("%s", hdev->name); |
| 5469 | |
| 5470 | memset(&rp, 0, sizeof(rp)); |
| 5471 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5472 | rp.addr.type = cp->addr.type; |
| 5473 | |
| 5474 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5475 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5476 | MGMT_STATUS_INVALID_PARAMS, |
| 5477 | &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5478 | |
| 5479 | hci_dev_lock(hdev); |
| 5480 | |
| 5481 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5482 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5483 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5484 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5485 | goto unlock; |
| 5486 | } |
| 5487 | |
| 5488 | if (cp->addr.type == BDADDR_BREDR) |
| 5489 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5490 | &cp->addr.bdaddr); |
| 5491 | else |
| 5492 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 5493 | |
| 5494 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5495 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5496 | MGMT_STATUS_NOT_CONNECTED, &rp, |
| 5497 | sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5498 | goto unlock; |
| 5499 | } |
| 5500 | |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5501 | if (mgmt_pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5502 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5503 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5504 | goto unlock; |
| 5505 | } |
| 5506 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5507 | /* To avoid client trying to guess when to poll again for information we |
| 5508 | * calculate conn info age as random value between min/max set in hdev. |
| 5509 | */ |
| 5510 | conn_info_age = hdev->conn_info_min_age + |
| 5511 | prandom_u32_max(hdev->conn_info_max_age - |
| 5512 | hdev->conn_info_min_age); |
| 5513 | |
| 5514 | /* Query controller to refresh cached values if they are too old or were |
| 5515 | * never read. |
| 5516 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5517 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5518 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5519 | !conn->conn_info_timestamp) { |
| 5520 | struct hci_request req; |
| 5521 | struct hci_cp_read_tx_power req_txp_cp; |
| 5522 | struct hci_cp_read_rssi req_rssi_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5523 | struct mgmt_pending_cmd *cmd; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5524 | |
| 5525 | hci_req_init(&req, hdev); |
| 5526 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5527 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5528 | &req_rssi_cp); |
| 5529 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5530 | /* For LE links TX power does not change thus we don't need to |
| 5531 | * query for it once value is known. |
| 5532 | */ |
| 5533 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5534 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5535 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5536 | req_txp_cp.type = 0x00; |
| 5537 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5538 | sizeof(req_txp_cp), &req_txp_cp); |
| 5539 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5540 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5541 | /* Max TX power needs to be read only once per connection */ |
| 5542 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5543 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5544 | req_txp_cp.type = 0x01; |
| 5545 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5546 | sizeof(req_txp_cp), &req_txp_cp); |
| 5547 | } |
| 5548 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5549 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5550 | if (err < 0) |
| 5551 | goto unlock; |
| 5552 | |
| 5553 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5554 | data, len); |
| 5555 | if (!cmd) { |
| 5556 | err = -ENOMEM; |
| 5557 | goto unlock; |
| 5558 | } |
| 5559 | |
| 5560 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5561 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9981bdb | 2014-12-05 13:42:57 +0200 | [diff] [blame] | 5562 | cmd->cmd_complete = conn_info_cmd_complete; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5563 | |
| 5564 | conn->conn_info_timestamp = jiffies; |
| 5565 | } else { |
| 5566 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5567 | rp.rssi = conn->rssi; |
| 5568 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5569 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5570 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5571 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5572 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5573 | } |
| 5574 | |
| 5575 | unlock: |
| 5576 | hci_dev_unlock(hdev); |
| 5577 | return err; |
| 5578 | } |
| 5579 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5580 | static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status) |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5581 | { |
| 5582 | struct hci_conn *conn = cmd->user_data; |
| 5583 | struct mgmt_rp_get_clock_info rp; |
| 5584 | struct hci_dev *hdev; |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5585 | int err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5586 | |
| 5587 | memset(&rp, 0, sizeof(rp)); |
| 5588 | memcpy(&rp.addr, &cmd->param, sizeof(rp.addr)); |
| 5589 | |
| 5590 | if (status) |
| 5591 | goto complete; |
| 5592 | |
| 5593 | hdev = hci_dev_get(cmd->index); |
| 5594 | if (hdev) { |
| 5595 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5596 | hci_dev_put(hdev); |
| 5597 | } |
| 5598 | |
| 5599 | if (conn) { |
| 5600 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5601 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5602 | } |
| 5603 | |
| 5604 | complete: |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5605 | err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, |
| 5606 | sizeof(rp)); |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5607 | |
| 5608 | if (conn) { |
| 5609 | hci_conn_drop(conn); |
| 5610 | hci_conn_put(conn); |
| 5611 | } |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5612 | |
| 5613 | return err; |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5614 | } |
| 5615 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5616 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5617 | { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5618 | struct hci_cp_read_clock *hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5619 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5620 | struct hci_conn *conn; |
| 5621 | |
| 5622 | BT_DBG("%s status %u", hdev->name, status); |
| 5623 | |
| 5624 | hci_dev_lock(hdev); |
| 5625 | |
| 5626 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5627 | if (!hci_cp) |
| 5628 | goto unlock; |
| 5629 | |
| 5630 | if (hci_cp->which) { |
| 5631 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5632 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5633 | } else { |
| 5634 | conn = NULL; |
| 5635 | } |
| 5636 | |
| 5637 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5638 | if (!cmd) |
| 5639 | goto unlock; |
| 5640 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5641 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5642 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5643 | |
| 5644 | unlock: |
| 5645 | hci_dev_unlock(hdev); |
| 5646 | } |
| 5647 | |
| 5648 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5649 | u16 len) |
| 5650 | { |
| 5651 | struct mgmt_cp_get_clock_info *cp = data; |
| 5652 | struct mgmt_rp_get_clock_info rp; |
| 5653 | struct hci_cp_read_clock hci_cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5654 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5655 | struct hci_request req; |
| 5656 | struct hci_conn *conn; |
| 5657 | int err; |
| 5658 | |
| 5659 | BT_DBG("%s", hdev->name); |
| 5660 | |
| 5661 | memset(&rp, 0, sizeof(rp)); |
| 5662 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5663 | rp.addr.type = cp->addr.type; |
| 5664 | |
| 5665 | if (cp->addr.type != BDADDR_BREDR) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5666 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5667 | MGMT_STATUS_INVALID_PARAMS, |
| 5668 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5669 | |
| 5670 | hci_dev_lock(hdev); |
| 5671 | |
| 5672 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5673 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5674 | MGMT_STATUS_NOT_POWERED, &rp, |
| 5675 | sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5676 | goto unlock; |
| 5677 | } |
| 5678 | |
| 5679 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5680 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5681 | &cp->addr.bdaddr); |
| 5682 | if (!conn || conn->state != BT_CONNECTED) { |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5683 | err = mgmt_cmd_complete(sk, hdev->id, |
| 5684 | MGMT_OP_GET_CLOCK_INFO, |
| 5685 | MGMT_STATUS_NOT_CONNECTED, |
| 5686 | &rp, sizeof(rp)); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5687 | goto unlock; |
| 5688 | } |
| 5689 | } else { |
| 5690 | conn = NULL; |
| 5691 | } |
| 5692 | |
| 5693 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5694 | if (!cmd) { |
| 5695 | err = -ENOMEM; |
| 5696 | goto unlock; |
| 5697 | } |
| 5698 | |
Johan Hedberg | 6948737 | 2014-12-05 13:36:07 +0200 | [diff] [blame] | 5699 | cmd->cmd_complete = clock_info_cmd_complete; |
| 5700 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5701 | hci_req_init(&req, hdev); |
| 5702 | |
| 5703 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5704 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5705 | |
| 5706 | if (conn) { |
| 5707 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5708 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5709 | |
| 5710 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5711 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5712 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5713 | } |
| 5714 | |
| 5715 | err = hci_req_run(&req, get_clock_info_complete); |
| 5716 | if (err < 0) |
| 5717 | mgmt_pending_remove(cmd); |
| 5718 | |
| 5719 | unlock: |
| 5720 | hci_dev_unlock(hdev); |
| 5721 | return err; |
| 5722 | } |
| 5723 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5724 | static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type) |
| 5725 | { |
| 5726 | struct hci_conn *conn; |
| 5727 | |
| 5728 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 5729 | if (!conn) |
| 5730 | return false; |
| 5731 | |
| 5732 | if (conn->dst_type != type) |
| 5733 | return false; |
| 5734 | |
| 5735 | if (conn->state != BT_CONNECTED) |
| 5736 | return false; |
| 5737 | |
| 5738 | return true; |
| 5739 | } |
| 5740 | |
| 5741 | /* This function requires the caller holds hdev->lock */ |
| 5742 | static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr, |
| 5743 | u8 addr_type, u8 auto_connect) |
| 5744 | { |
| 5745 | struct hci_dev *hdev = req->hdev; |
| 5746 | struct hci_conn_params *params; |
| 5747 | |
| 5748 | params = hci_conn_params_add(hdev, addr, addr_type); |
| 5749 | if (!params) |
| 5750 | return -EIO; |
| 5751 | |
| 5752 | if (params->auto_connect == auto_connect) |
| 5753 | return 0; |
| 5754 | |
| 5755 | list_del_init(¶ms->action); |
| 5756 | |
| 5757 | switch (auto_connect) { |
| 5758 | case HCI_AUTO_CONN_DISABLED: |
| 5759 | case HCI_AUTO_CONN_LINK_LOSS: |
| 5760 | __hci_update_background_scan(req); |
| 5761 | break; |
| 5762 | case HCI_AUTO_CONN_REPORT: |
| 5763 | list_add(¶ms->action, &hdev->pend_le_reports); |
| 5764 | __hci_update_background_scan(req); |
| 5765 | break; |
| 5766 | case HCI_AUTO_CONN_DIRECT: |
| 5767 | case HCI_AUTO_CONN_ALWAYS: |
| 5768 | if (!is_connected(hdev, addr, addr_type)) { |
| 5769 | list_add(¶ms->action, &hdev->pend_le_conns); |
| 5770 | __hci_update_background_scan(req); |
| 5771 | } |
| 5772 | break; |
| 5773 | } |
| 5774 | |
| 5775 | params->auto_connect = auto_connect; |
| 5776 | |
| 5777 | BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type, |
| 5778 | auto_connect); |
| 5779 | |
| 5780 | return 0; |
| 5781 | } |
| 5782 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5783 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5784 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5785 | { |
| 5786 | struct mgmt_ev_device_added ev; |
| 5787 | |
| 5788 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5789 | ev.addr.type = type; |
| 5790 | ev.action = action; |
| 5791 | |
| 5792 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5793 | } |
| 5794 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5795 | static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5796 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5797 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5798 | |
| 5799 | BT_DBG("status 0x%02x", status); |
| 5800 | |
| 5801 | hci_dev_lock(hdev); |
| 5802 | |
| 5803 | cmd = mgmt_pending_find(MGMT_OP_ADD_DEVICE, hdev); |
| 5804 | if (!cmd) |
| 5805 | goto unlock; |
| 5806 | |
| 5807 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 5808 | mgmt_pending_remove(cmd); |
| 5809 | |
| 5810 | unlock: |
| 5811 | hci_dev_unlock(hdev); |
| 5812 | } |
| 5813 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5814 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5815 | void *data, u16 len) |
| 5816 | { |
| 5817 | struct mgmt_cp_add_device *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5818 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5819 | struct hci_request req; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5820 | u8 auto_conn, addr_type; |
| 5821 | int err; |
| 5822 | |
| 5823 | BT_DBG("%s", hdev->name); |
| 5824 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5825 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5826 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5827 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5828 | MGMT_STATUS_INVALID_PARAMS, |
| 5829 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5830 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5831 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 5832 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5833 | MGMT_STATUS_INVALID_PARAMS, |
| 5834 | &cp->addr, sizeof(cp->addr)); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5835 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5836 | hci_req_init(&req, hdev); |
| 5837 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5838 | hci_dev_lock(hdev); |
| 5839 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5840 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_DEVICE, hdev, data, len); |
| 5841 | if (!cmd) { |
| 5842 | err = -ENOMEM; |
| 5843 | goto unlock; |
| 5844 | } |
| 5845 | |
| 5846 | cmd->cmd_complete = addr_cmd_complete; |
| 5847 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5848 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5849 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5850 | if (cp->action != 0x01) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5851 | err = cmd->cmd_complete(cmd, |
| 5852 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5853 | mgmt_pending_remove(cmd); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5854 | goto unlock; |
| 5855 | } |
| 5856 | |
| 5857 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5858 | cp->addr.type); |
| 5859 | if (err) |
| 5860 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5861 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5862 | __hci_update_page_scan(&req); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5863 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5864 | goto added; |
| 5865 | } |
| 5866 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5867 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5868 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5869 | else |
| 5870 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5871 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5872 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5873 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5874 | else if (cp->action == 0x01) |
| 5875 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5876 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5877 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5878 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5879 | /* If the connection parameters don't exist for this device, |
| 5880 | * they will be created and configured with defaults. |
| 5881 | */ |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5882 | if (hci_conn_params_set(&req, &cp->addr.bdaddr, addr_type, |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5883 | auto_conn) < 0) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5884 | err = cmd->cmd_complete(cmd, MGMT_STATUS_FAILED); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5885 | mgmt_pending_remove(cmd); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5886 | goto unlock; |
| 5887 | } |
| 5888 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5889 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5890 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5891 | |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5892 | err = hci_req_run(&req, add_device_complete); |
| 5893 | if (err < 0) { |
| 5894 | /* ENODATA means no HCI commands were needed (e.g. if |
| 5895 | * the adapter is powered off). |
| 5896 | */ |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5897 | if (err == -ENODATA) |
| 5898 | err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS); |
Johan Hedberg | 5a154e6 | 2014-12-19 22:26:02 +0200 | [diff] [blame] | 5899 | mgmt_pending_remove(cmd); |
| 5900 | } |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5901 | |
| 5902 | unlock: |
| 5903 | hci_dev_unlock(hdev); |
| 5904 | return err; |
| 5905 | } |
| 5906 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5907 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5908 | bdaddr_t *bdaddr, u8 type) |
| 5909 | { |
| 5910 | struct mgmt_ev_device_removed ev; |
| 5911 | |
| 5912 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5913 | ev.addr.type = type; |
| 5914 | |
| 5915 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5916 | } |
| 5917 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 5918 | static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5919 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5920 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5921 | |
| 5922 | BT_DBG("status 0x%02x", status); |
| 5923 | |
| 5924 | hci_dev_lock(hdev); |
| 5925 | |
| 5926 | cmd = mgmt_pending_find(MGMT_OP_REMOVE_DEVICE, hdev); |
| 5927 | if (!cmd) |
| 5928 | goto unlock; |
| 5929 | |
| 5930 | cmd->cmd_complete(cmd, mgmt_status(status)); |
| 5931 | mgmt_pending_remove(cmd); |
| 5932 | |
| 5933 | unlock: |
| 5934 | hci_dev_unlock(hdev); |
| 5935 | } |
| 5936 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5937 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5938 | void *data, u16 len) |
| 5939 | { |
| 5940 | struct mgmt_cp_remove_device *cp = data; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 5941 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5942 | struct hci_request req; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5943 | int err; |
| 5944 | |
| 5945 | BT_DBG("%s", hdev->name); |
| 5946 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5947 | hci_req_init(&req, hdev); |
| 5948 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5949 | hci_dev_lock(hdev); |
| 5950 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5951 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_DEVICE, hdev, data, len); |
| 5952 | if (!cmd) { |
| 5953 | err = -ENOMEM; |
| 5954 | goto unlock; |
| 5955 | } |
| 5956 | |
| 5957 | cmd->cmd_complete = addr_cmd_complete; |
| 5958 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5959 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5960 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5961 | u8 addr_type; |
| 5962 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5963 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5964 | err = cmd->cmd_complete(cmd, |
| 5965 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5966 | mgmt_pending_remove(cmd); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5967 | goto unlock; |
| 5968 | } |
| 5969 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5970 | if (cp->addr.type == BDADDR_BREDR) { |
| 5971 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5972 | &cp->addr.bdaddr, |
| 5973 | cp->addr.type); |
| 5974 | if (err) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5975 | err = cmd->cmd_complete(cmd, |
| 5976 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5977 | mgmt_pending_remove(cmd); |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5978 | goto unlock; |
| 5979 | } |
| 5980 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5981 | __hci_update_page_scan(&req); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5982 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5983 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5984 | cp->addr.type); |
| 5985 | goto complete; |
| 5986 | } |
| 5987 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5988 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5989 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5990 | else |
| 5991 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5992 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5993 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5994 | addr_type); |
| 5995 | if (!params) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 5996 | err = cmd->cmd_complete(cmd, |
| 5997 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 5998 | mgmt_pending_remove(cmd); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5999 | goto unlock; |
| 6000 | } |
| 6001 | |
| 6002 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6003 | err = cmd->cmd_complete(cmd, |
| 6004 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 6005 | mgmt_pending_remove(cmd); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6006 | goto unlock; |
| 6007 | } |
| 6008 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 6009 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 6010 | list_del(¶ms->list); |
| 6011 | kfree(params); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 6012 | __hci_update_background_scan(&req); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 6013 | |
| 6014 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6015 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6016 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6017 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6018 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6019 | if (cp->addr.type) { |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6020 | err = cmd->cmd_complete(cmd, |
| 6021 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 6022 | mgmt_pending_remove(cmd); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6023 | goto unlock; |
| 6024 | } |
| 6025 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6026 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 6027 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 6028 | list_del(&b->list); |
| 6029 | kfree(b); |
| 6030 | } |
| 6031 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 6032 | __hci_update_page_scan(&req); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 6033 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 6034 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 6035 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 6036 | continue; |
| 6037 | device_removed(sk, hdev, &p->addr, p->addr_type); |
| 6038 | list_del(&p->action); |
| 6039 | list_del(&p->list); |
| 6040 | kfree(p); |
| 6041 | } |
| 6042 | |
| 6043 | BT_DBG("All LE connection parameters were removed"); |
| 6044 | |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 6045 | __hci_update_background_scan(&req); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6046 | } |
| 6047 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 6048 | complete: |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 6049 | err = hci_req_run(&req, remove_device_complete); |
| 6050 | if (err < 0) { |
| 6051 | /* ENODATA means no HCI commands were needed (e.g. if |
| 6052 | * the adapter is powered off). |
| 6053 | */ |
Johan Hedberg | 9df7465 | 2014-12-19 22:26:03 +0200 | [diff] [blame] | 6054 | if (err == -ENODATA) |
| 6055 | err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS); |
Johan Hedberg | 51ef3eb | 2014-12-19 22:26:01 +0200 | [diff] [blame] | 6056 | mgmt_pending_remove(cmd); |
| 6057 | } |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 6058 | |
| 6059 | unlock: |
| 6060 | hci_dev_unlock(hdev); |
| 6061 | return err; |
| 6062 | } |
| 6063 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6064 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 6065 | u16 len) |
| 6066 | { |
| 6067 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6068 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 6069 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6070 | u16 param_count, expected_len; |
| 6071 | int i; |
| 6072 | |
| 6073 | if (!lmp_le_capable(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6074 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6075 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6076 | |
| 6077 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6078 | if (param_count > max_param_count) { |
| 6079 | BT_ERR("load_conn_param: too big param_count value %u", |
| 6080 | param_count); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6081 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6082 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 6083 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6084 | |
| 6085 | expected_len = sizeof(*cp) + param_count * |
| 6086 | sizeof(struct mgmt_conn_param); |
| 6087 | if (expected_len != len) { |
| 6088 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 6089 | expected_len, len); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6090 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 6091 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 6095 | |
| 6096 | hci_dev_lock(hdev); |
| 6097 | |
| 6098 | hci_conn_params_clear_disabled(hdev); |
| 6099 | |
| 6100 | for (i = 0; i < param_count; i++) { |
| 6101 | struct mgmt_conn_param *param = &cp->params[i]; |
| 6102 | struct hci_conn_params *hci_param; |
| 6103 | u16 min, max, latency, timeout; |
| 6104 | u8 addr_type; |
| 6105 | |
| 6106 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 6107 | param->addr.type); |
| 6108 | |
| 6109 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 6110 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 6111 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 6112 | addr_type = ADDR_LE_DEV_RANDOM; |
| 6113 | } else { |
| 6114 | BT_ERR("Ignoring invalid connection parameters"); |
| 6115 | continue; |
| 6116 | } |
| 6117 | |
| 6118 | min = le16_to_cpu(param->min_interval); |
| 6119 | max = le16_to_cpu(param->max_interval); |
| 6120 | latency = le16_to_cpu(param->latency); |
| 6121 | timeout = le16_to_cpu(param->timeout); |
| 6122 | |
| 6123 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 6124 | min, max, latency, timeout); |
| 6125 | |
| 6126 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 6127 | BT_ERR("Ignoring invalid connection parameters"); |
| 6128 | continue; |
| 6129 | } |
| 6130 | |
| 6131 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 6132 | addr_type); |
| 6133 | if (!hci_param) { |
| 6134 | BT_ERR("Failed to add connection parameters"); |
| 6135 | continue; |
| 6136 | } |
| 6137 | |
| 6138 | hci_param->conn_min_interval = min; |
| 6139 | hci_param->conn_max_interval = max; |
| 6140 | hci_param->conn_latency = latency; |
| 6141 | hci_param->supervision_timeout = timeout; |
| 6142 | } |
| 6143 | |
| 6144 | hci_dev_unlock(hdev); |
| 6145 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 6146 | return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, |
| 6147 | NULL, 0); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 6148 | } |
| 6149 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6150 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 6151 | void *data, u16 len) |
| 6152 | { |
| 6153 | struct mgmt_cp_set_external_config *cp = data; |
| 6154 | bool changed; |
| 6155 | int err; |
| 6156 | |
| 6157 | BT_DBG("%s", hdev->name); |
| 6158 | |
| 6159 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6160 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6161 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6162 | |
| 6163 | if (cp->config != 0x00 && cp->config != 0x01) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6164 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6165 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6166 | |
| 6167 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6168 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 6169 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6170 | |
| 6171 | hci_dev_lock(hdev); |
| 6172 | |
| 6173 | if (cp->config) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 6174 | changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6175 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 6176 | changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED); |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6177 | |
| 6178 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 6179 | if (err < 0) |
| 6180 | goto unlock; |
| 6181 | |
| 6182 | if (!changed) |
| 6183 | goto unlock; |
| 6184 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 6185 | err = new_options(hdev, sk); |
| 6186 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6187 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) { |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6188 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6189 | |
Marcel Holtmann | 516018a | 2015-03-13 02:11:04 -0700 | [diff] [blame] | 6190 | if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) { |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6191 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6192 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6193 | |
| 6194 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6195 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 6196 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 6197 | mgmt_index_added(hdev); |
| 6198 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 6199 | } |
| 6200 | |
| 6201 | unlock: |
| 6202 | hci_dev_unlock(hdev); |
| 6203 | return err; |
| 6204 | } |
| 6205 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6206 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 6207 | void *data, u16 len) |
| 6208 | { |
| 6209 | struct mgmt_cp_set_public_address *cp = data; |
| 6210 | bool changed; |
| 6211 | int err; |
| 6212 | |
| 6213 | BT_DBG("%s", hdev->name); |
| 6214 | |
| 6215 | if (hdev_is_powered(hdev)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6216 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6217 | MGMT_STATUS_REJECTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6218 | |
| 6219 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6220 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6221 | MGMT_STATUS_INVALID_PARAMS); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6222 | |
| 6223 | if (!hdev->set_bdaddr) |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6224 | return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 6225 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6226 | |
| 6227 | hci_dev_lock(hdev); |
| 6228 | |
| 6229 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 6230 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 6231 | |
| 6232 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 6233 | if (err < 0) |
| 6234 | goto unlock; |
| 6235 | |
| 6236 | if (!changed) |
| 6237 | goto unlock; |
| 6238 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6239 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6240 | err = new_options(hdev, sk); |
| 6241 | |
| 6242 | if (is_configured(hdev)) { |
| 6243 | mgmt_index_removed(hdev); |
| 6244 | |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6245 | hci_dev_clear_flag(hdev, HCI_UNCONFIGURED); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6246 | |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 6247 | hci_dev_set_flag(hdev, HCI_CONFIG); |
| 6248 | hci_dev_set_flag(hdev, HCI_AUTO_OFF); |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 6249 | |
| 6250 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 6251 | } |
| 6252 | |
| 6253 | unlock: |
| 6254 | hci_dev_unlock(hdev); |
| 6255 | return err; |
| 6256 | } |
| 6257 | |
Marcel Holtmann | bea4160 | 2015-03-14 22:43:17 -0700 | [diff] [blame^] | 6258 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 6259 | u8 data_len) |
| 6260 | { |
| 6261 | eir[eir_len++] = sizeof(type) + data_len; |
| 6262 | eir[eir_len++] = type; |
| 6263 | memcpy(&eir[eir_len], data, data_len); |
| 6264 | eir_len += data_len; |
| 6265 | |
| 6266 | return eir_len; |
| 6267 | } |
| 6268 | |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6269 | static int read_adv_features(struct sock *sk, struct hci_dev *hdev, |
| 6270 | void *data, u16 data_len) |
| 6271 | { |
| 6272 | struct mgmt_rp_read_adv_features *rp; |
| 6273 | size_t rp_len; |
| 6274 | int err; |
| 6275 | |
| 6276 | BT_DBG("%s", hdev->name); |
| 6277 | |
| 6278 | hci_dev_lock(hdev); |
| 6279 | |
| 6280 | rp_len = sizeof(*rp); |
| 6281 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 6282 | if (!rp) { |
| 6283 | hci_dev_unlock(hdev); |
| 6284 | return -ENOMEM; |
| 6285 | } |
| 6286 | |
| 6287 | rp->supported_flags = cpu_to_le32(0); |
| 6288 | rp->max_adv_data_len = 31; |
| 6289 | rp->max_scan_rsp_len = 31; |
| 6290 | rp->max_instances = 0; |
| 6291 | rp->num_instances = 0; |
| 6292 | |
| 6293 | hci_dev_unlock(hdev); |
| 6294 | |
| 6295 | err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES, |
| 6296 | MGMT_STATUS_SUCCESS, rp, rp_len); |
| 6297 | |
| 6298 | kfree(rp); |
| 6299 | |
| 6300 | return err; |
| 6301 | } |
| 6302 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6303 | static const struct hci_mgmt_handler mgmt_handlers[] = { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6304 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6305 | { read_version, MGMT_READ_VERSION_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6306 | HCI_MGMT_NO_HDEV | |
| 6307 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6308 | { read_commands, MGMT_READ_COMMANDS_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6309 | HCI_MGMT_NO_HDEV | |
| 6310 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6311 | { read_index_list, MGMT_READ_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6312 | HCI_MGMT_NO_HDEV | |
| 6313 | HCI_MGMT_UNTRUSTED }, |
| 6314 | { read_controller_info, MGMT_READ_INFO_SIZE, |
| 6315 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6316 | { set_powered, MGMT_SETTING_SIZE }, |
| 6317 | { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE }, |
| 6318 | { set_connectable, MGMT_SETTING_SIZE }, |
| 6319 | { set_fast_connectable, MGMT_SETTING_SIZE }, |
| 6320 | { set_bondable, MGMT_SETTING_SIZE }, |
| 6321 | { set_link_security, MGMT_SETTING_SIZE }, |
| 6322 | { set_ssp, MGMT_SETTING_SIZE }, |
| 6323 | { set_hs, MGMT_SETTING_SIZE }, |
| 6324 | { set_le, MGMT_SETTING_SIZE }, |
| 6325 | { set_dev_class, MGMT_SET_DEV_CLASS_SIZE }, |
| 6326 | { set_local_name, MGMT_SET_LOCAL_NAME_SIZE }, |
| 6327 | { add_uuid, MGMT_ADD_UUID_SIZE }, |
| 6328 | { remove_uuid, MGMT_REMOVE_UUID_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6329 | { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE, |
| 6330 | HCI_MGMT_VAR_LEN }, |
| 6331 | { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE, |
| 6332 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6333 | { disconnect, MGMT_DISCONNECT_SIZE }, |
| 6334 | { get_connections, MGMT_GET_CONNECTIONS_SIZE }, |
| 6335 | { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE }, |
| 6336 | { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 6337 | { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 6338 | { pair_device, MGMT_PAIR_DEVICE_SIZE }, |
| 6339 | { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 6340 | { unpair_device, MGMT_UNPAIR_DEVICE_SIZE }, |
| 6341 | { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 6342 | { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 6343 | { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 6344 | { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6345 | { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
| 6346 | { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE, |
| 6347 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6348 | { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 6349 | { start_discovery, MGMT_START_DISCOVERY_SIZE }, |
| 6350 | { stop_discovery, MGMT_STOP_DISCOVERY_SIZE }, |
| 6351 | { confirm_name, MGMT_CONFIRM_NAME_SIZE }, |
| 6352 | { block_device, MGMT_BLOCK_DEVICE_SIZE }, |
| 6353 | { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE }, |
| 6354 | { set_device_id, MGMT_SET_DEVICE_ID_SIZE }, |
| 6355 | { set_advertising, MGMT_SETTING_SIZE }, |
| 6356 | { set_bredr, MGMT_SETTING_SIZE }, |
| 6357 | { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE }, |
| 6358 | { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE }, |
| 6359 | { set_secure_conn, MGMT_SETTING_SIZE }, |
| 6360 | { set_debug_keys, MGMT_SETTING_SIZE }, |
| 6361 | { set_privacy, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6362 | { load_irks, MGMT_LOAD_IRKS_SIZE, |
| 6363 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 7aea8616 | 2015-03-14 19:28:02 -0700 | [diff] [blame] | 6364 | { get_conn_info, MGMT_GET_CONN_INFO_SIZE }, |
| 6365 | { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE }, |
| 6366 | { add_device, MGMT_ADD_DEVICE_SIZE }, |
| 6367 | { remove_device, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6368 | { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE, |
| 6369 | HCI_MGMT_VAR_LEN }, |
| 6370 | { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6371 | HCI_MGMT_NO_HDEV | |
| 6372 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6373 | { read_config_info, MGMT_READ_CONFIG_INFO_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6374 | HCI_MGMT_UNCONFIGURED | |
| 6375 | HCI_MGMT_UNTRUSTED }, |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6376 | { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE, |
| 6377 | HCI_MGMT_UNCONFIGURED }, |
| 6378 | { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE, |
| 6379 | HCI_MGMT_UNCONFIGURED }, |
| 6380 | { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE, |
| 6381 | HCI_MGMT_VAR_LEN }, |
Marcel Holtmann | 96f1474 | 2015-03-14 19:27:57 -0700 | [diff] [blame] | 6382 | { NULL }, |
| 6383 | { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE, |
Marcel Holtmann | c91041d | 2015-03-14 19:28:01 -0700 | [diff] [blame] | 6384 | HCI_MGMT_NO_HDEV | |
| 6385 | HCI_MGMT_UNTRUSTED }, |
Marcel Holtmann | d3d5305 | 2015-03-14 20:53:25 -0700 | [diff] [blame] | 6386 | { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6387 | }; |
| 6388 | |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6389 | int mgmt_control(struct hci_mgmt_chan *chan, struct sock *sk, |
| 6390 | struct msghdr *msg, size_t msglen) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6391 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 6392 | void *buf; |
| 6393 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6394 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 6395 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6396 | struct hci_dev *hdev = NULL; |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 6397 | const struct hci_mgmt_handler *handler; |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6398 | bool var_len, no_hdev; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6399 | int err; |
| 6400 | |
| 6401 | BT_DBG("got %zu bytes", msglen); |
| 6402 | |
| 6403 | if (msglen < sizeof(*hdr)) |
| 6404 | return -EINVAL; |
| 6405 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 6406 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6407 | if (!buf) |
| 6408 | return -ENOMEM; |
| 6409 | |
Al Viro | 6ce8e9c | 2014-04-06 21:25:44 -0400 | [diff] [blame] | 6410 | if (memcpy_from_msg(buf, msg, msglen)) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6411 | err = -EFAULT; |
| 6412 | goto done; |
| 6413 | } |
| 6414 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 6415 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 6416 | opcode = __le16_to_cpu(hdr->opcode); |
| 6417 | index = __le16_to_cpu(hdr->index); |
| 6418 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6419 | |
| 6420 | if (len != msglen - sizeof(*hdr)) { |
| 6421 | err = -EINVAL; |
| 6422 | goto done; |
| 6423 | } |
| 6424 | |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6425 | if (opcode >= chan->handler_count || |
| 6426 | chan->handlers[opcode].func == NULL) { |
| 6427 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6428 | err = mgmt_cmd_status(sk, index, opcode, |
| 6429 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6430 | goto done; |
| 6431 | } |
| 6432 | |
| 6433 | handler = &chan->handlers[opcode]; |
| 6434 | |
Marcel Holtmann | c927a10 | 2015-03-14 19:28:03 -0700 | [diff] [blame] | 6435 | if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) && |
| 6436 | !(handler->flags & HCI_MGMT_UNTRUSTED)) { |
| 6437 | err = mgmt_cmd_status(sk, index, opcode, |
| 6438 | MGMT_STATUS_PERMISSION_DENIED); |
| 6439 | goto done; |
| 6440 | } |
| 6441 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6442 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6443 | hdev = hci_dev_get(index); |
| 6444 | if (!hdev) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6445 | err = mgmt_cmd_status(sk, index, opcode, |
| 6446 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6447 | goto done; |
| 6448 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 6449 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6450 | if (hci_dev_test_flag(hdev, HCI_SETUP) || |
| 6451 | hci_dev_test_flag(hdev, HCI_CONFIG) || |
| 6452 | hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6453 | err = mgmt_cmd_status(sk, index, opcode, |
| 6454 | MGMT_STATUS_INVALID_INDEX); |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 6455 | goto done; |
| 6456 | } |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 6457 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6458 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) && |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6459 | !(handler->flags & HCI_MGMT_UNCONFIGURED)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6460 | err = mgmt_cmd_status(sk, index, opcode, |
| 6461 | MGMT_STATUS_INVALID_INDEX); |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 6462 | goto done; |
| 6463 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6464 | } |
| 6465 | |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6466 | no_hdev = (handler->flags & HCI_MGMT_NO_HDEV); |
| 6467 | if (no_hdev != !hdev) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6468 | err = mgmt_cmd_status(sk, index, opcode, |
| 6469 | MGMT_STATUS_INVALID_INDEX); |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 6470 | goto done; |
| 6471 | } |
| 6472 | |
Johan Hedberg | b9a245fb | 2015-03-06 21:08:52 +0200 | [diff] [blame] | 6473 | var_len = (handler->flags & HCI_MGMT_VAR_LEN); |
| 6474 | if ((var_len && len < handler->data_len) || |
| 6475 | (!var_len && len != handler->data_len)) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6476 | err = mgmt_cmd_status(sk, index, opcode, |
| 6477 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 6478 | goto done; |
| 6479 | } |
| 6480 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 6481 | if (hdev) |
| 6482 | mgmt_init_hdev(sk, hdev); |
| 6483 | |
| 6484 | cp = buf + sizeof(*hdr); |
| 6485 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 6486 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 6487 | if (err < 0) |
| 6488 | goto done; |
| 6489 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6490 | err = msglen; |
| 6491 | |
| 6492 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 6493 | if (hdev) |
| 6494 | hci_dev_put(hdev); |
| 6495 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6496 | kfree(buf); |
| 6497 | return err; |
| 6498 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6499 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6500 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6501 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6502 | struct mgmt_ev_ext_index ev; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 6503 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6504 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6505 | return; |
| 6506 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6507 | switch (hdev->dev_type) { |
| 6508 | case HCI_BREDR: |
| 6509 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6510 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, |
| 6511 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6512 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6513 | } else { |
| 6514 | mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, |
| 6515 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6516 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6517 | } |
| 6518 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6519 | case HCI_AMP: |
| 6520 | ev.type = 0x02; |
| 6521 | break; |
| 6522 | default: |
| 6523 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6524 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6525 | |
| 6526 | ev.bus = hdev->bus; |
| 6527 | |
| 6528 | mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev), |
| 6529 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6530 | } |
| 6531 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 6532 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 6533 | { |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6534 | struct mgmt_ev_ext_index ev; |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 6535 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6536 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 6537 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 6538 | return; |
| 6539 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6540 | switch (hdev->dev_type) { |
| 6541 | case HCI_BREDR: |
| 6542 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6543 | |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6544 | if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) { |
| 6545 | mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, |
| 6546 | NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6547 | ev.type = 0x01; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6548 | } else { |
| 6549 | mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, |
| 6550 | HCI_MGMT_INDEX_EVENTS); |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6551 | ev.type = 0x00; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6552 | } |
| 6553 | break; |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6554 | case HCI_AMP: |
| 6555 | ev.type = 0x02; |
| 6556 | break; |
| 6557 | default: |
| 6558 | return; |
Marcel Holtmann | f920733 | 2015-03-14 19:27:55 -0700 | [diff] [blame] | 6559 | } |
Marcel Holtmann | ced8554 | 2015-03-14 19:27:56 -0700 | [diff] [blame] | 6560 | |
| 6561 | ev.bus = hdev->bus; |
| 6562 | |
| 6563 | mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev), |
| 6564 | HCI_MGMT_EXT_INDEX_EVENTS); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6565 | } |
| 6566 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6567 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 6568 | static void restart_le_actions(struct hci_request *req) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6569 | { |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 6570 | struct hci_dev *hdev = req->hdev; |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6571 | struct hci_conn_params *p; |
| 6572 | |
| 6573 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6574 | /* Needed for AUTO_OFF case where might not "really" |
| 6575 | * have been powered off. |
| 6576 | */ |
| 6577 | list_del_init(&p->action); |
| 6578 | |
| 6579 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 6580 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6581 | case HCI_AUTO_CONN_ALWAYS: |
| 6582 | list_add(&p->action, &hdev->pend_le_conns); |
| 6583 | break; |
| 6584 | case HCI_AUTO_CONN_REPORT: |
| 6585 | list_add(&p->action, &hdev->pend_le_reports); |
| 6586 | break; |
| 6587 | default: |
| 6588 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6589 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6590 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6591 | |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 6592 | __hci_update_background_scan(req); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6593 | } |
| 6594 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 6595 | static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6596 | { |
| 6597 | struct cmd_lookup match = { NULL, hdev }; |
| 6598 | |
| 6599 | BT_DBG("status 0x%02x", status); |
| 6600 | |
Marcel Holtmann | 162a3ba | 2015-01-14 15:43:11 -0800 | [diff] [blame] | 6601 | if (!status) { |
| 6602 | /* Register the available SMP channels (BR/EDR and LE) only |
| 6603 | * when successfully powering on the controller. This late |
| 6604 | * registration is required so that LE SMP can clearly |
| 6605 | * decide if the public address or static address is used. |
| 6606 | */ |
| 6607 | smp_register(hdev); |
| 6608 | } |
| 6609 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6610 | hci_dev_lock(hdev); |
| 6611 | |
| 6612 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 6613 | |
| 6614 | new_settings(hdev, match.sk); |
| 6615 | |
| 6616 | hci_dev_unlock(hdev); |
| 6617 | |
| 6618 | if (match.sk) |
| 6619 | sock_put(match.sk); |
| 6620 | } |
| 6621 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6622 | static int powered_update_hci(struct hci_dev *hdev) |
| 6623 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6624 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6625 | u8 link_sec; |
| 6626 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6627 | hci_req_init(&req, hdev); |
| 6628 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6629 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) && |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6630 | !lmp_host_ssp_capable(hdev)) { |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 6631 | u8 mode = 0x01; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6632 | |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 6633 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6634 | |
Marcel Holtmann | 574ea3c | 2015-01-22 11:15:20 -0800 | [diff] [blame] | 6635 | if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) { |
| 6636 | u8 support = 0x01; |
| 6637 | |
| 6638 | hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, |
| 6639 | sizeof(support), &support); |
| 6640 | } |
Johan Hedberg | ec6f99b | 2014-12-12 13:30:11 +0200 | [diff] [blame] | 6641 | } |
| 6642 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6643 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) && |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 6644 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6645 | struct hci_cp_write_le_host_supported cp; |
| 6646 | |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 6647 | cp.le = 0x01; |
| 6648 | cp.simul = 0x00; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6649 | |
| 6650 | /* Check first if we already have the right |
| 6651 | * host state (host features set) |
| 6652 | */ |
| 6653 | if (cp.le != lmp_host_le_capable(hdev) || |
| 6654 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6655 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 6656 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6657 | } |
| 6658 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 6659 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 6660 | /* Make sure the controller has a good default for |
| 6661 | * advertising data. This also applies to the case |
| 6662 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 6663 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6664 | if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 6665 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 6666 | update_scan_rsp_data(&req); |
| 6667 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 6668 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6669 | if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 6670 | enable_advertising(&req); |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 6671 | |
| 6672 | restart_le_actions(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 6673 | } |
| 6674 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6675 | link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6676 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6677 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 6678 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6679 | |
| 6680 | if (lmp_bredr_capable(hdev)) { |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6681 | if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) |
Johan Hedberg | 406ef2a | 2015-03-10 20:14:27 +0200 | [diff] [blame] | 6682 | write_fast_connectable(&req, true); |
| 6683 | else |
| 6684 | write_fast_connectable(&req, false); |
Johan Hedberg | 1d2dc5b | 2014-12-19 13:40:19 +0200 | [diff] [blame] | 6685 | __hci_update_page_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6686 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6687 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6688 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6689 | } |
| 6690 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6691 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6692 | } |
| 6693 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6694 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6695 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 6696 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6697 | u8 status, zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 6698 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6699 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6700 | if (!hci_dev_test_flag(hdev, HCI_MGMT)) |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 6701 | return 0; |
| 6702 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 6703 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6704 | if (powered_update_hci(hdev) == 0) |
| 6705 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 6706 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6707 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 6708 | &match); |
| 6709 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6710 | } |
| 6711 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6712 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6713 | |
| 6714 | /* If the power off is because of hdev unregistration let |
| 6715 | * use the appropriate INVALID_INDEX status. Otherwise use |
| 6716 | * NOT_POWERED. We cover both scenarios here since later in |
| 6717 | * mgmt_index_removed() any hci_conn callbacks will have already |
| 6718 | * been triggered, potentially causing misleading DISCONNECTED |
| 6719 | * status responses. |
| 6720 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6721 | if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 9845904 | 2014-12-12 11:15:21 +0200 | [diff] [blame] | 6722 | status = MGMT_STATUS_INVALID_INDEX; |
| 6723 | else |
| 6724 | status = MGMT_STATUS_NOT_POWERED; |
| 6725 | |
| 6726 | mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6727 | |
| 6728 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 6729 | mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 6730 | zero_cod, sizeof(zero_cod), NULL); |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6731 | |
| 6732 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 6733 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6734 | |
| 6735 | if (match.sk) |
| 6736 | sock_put(match.sk); |
| 6737 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 6738 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6739 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6740 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6741 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6742 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6743 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6744 | u8 status; |
| 6745 | |
| 6746 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6747 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6748 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6749 | |
| 6750 | if (err == -ERFKILL) |
| 6751 | status = MGMT_STATUS_RFKILLED; |
| 6752 | else |
| 6753 | status = MGMT_STATUS_FAILED; |
| 6754 | |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 6755 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6756 | |
| 6757 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6758 | } |
| 6759 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6760 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 6761 | { |
| 6762 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6763 | |
| 6764 | hci_dev_lock(hdev); |
| 6765 | |
| 6766 | /* When discoverable timeout triggers, then just make sure |
| 6767 | * the limited discoverable flag is cleared. Even in the case |
| 6768 | * of a timeout triggered from general discoverable, it is |
| 6769 | * safe to unconditionally clear the flag. |
| 6770 | */ |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 6771 | hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE); |
| 6772 | hci_dev_clear_flag(hdev, HCI_DISCOVERABLE); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6773 | |
| 6774 | hci_req_init(&req, hdev); |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 6775 | if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) { |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 6776 | u8 scan = SCAN_PAGE; |
| 6777 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 6778 | sizeof(scan), &scan); |
| 6779 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6780 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6781 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6782 | hci_req_run(&req, NULL); |
| 6783 | |
| 6784 | hdev->discov_timeout = 0; |
| 6785 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6786 | new_settings(hdev, NULL); |
| 6787 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6788 | hci_dev_unlock(hdev); |
| 6789 | } |
| 6790 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6791 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 6792 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6793 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 6794 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6795 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6796 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6797 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6798 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 6799 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6800 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6801 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 6802 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6803 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6804 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6805 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6806 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6807 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6808 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 6809 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6810 | switch (ltk->type) { |
| 6811 | case SMP_LTK: |
| 6812 | case SMP_LTK_SLAVE: |
| 6813 | if (ltk->authenticated) |
| 6814 | return MGMT_LTK_AUTHENTICATED; |
| 6815 | return MGMT_LTK_UNAUTHENTICATED; |
| 6816 | case SMP_LTK_P256: |
| 6817 | if (ltk->authenticated) |
| 6818 | return MGMT_LTK_P256_AUTH; |
| 6819 | return MGMT_LTK_P256_UNAUTH; |
| 6820 | case SMP_LTK_P256_DEBUG: |
| 6821 | return MGMT_LTK_P256_DEBUG; |
| 6822 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6823 | |
| 6824 | return MGMT_LTK_UNAUTHENTICATED; |
| 6825 | } |
| 6826 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6827 | void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6828 | { |
| 6829 | struct mgmt_ev_new_long_term_key ev; |
| 6830 | |
| 6831 | memset(&ev, 0, sizeof(ev)); |
| 6832 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6833 | /* Devices using resolvable or non-resolvable random addresses |
| 6834 | * without providing an indentity resolving key don't require |
| 6835 | * to store long term keys. Their addresses will change the |
| 6836 | * next time around. |
| 6837 | * |
| 6838 | * Only when a remote device provides an identity address |
| 6839 | * make sure the long term key is stored. If the remote |
| 6840 | * identity is known, the long term keys are internally |
| 6841 | * mapped to the identity address. So allow static random |
| 6842 | * and public addresses here. |
| 6843 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6844 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6845 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6846 | ev.store_hint = 0x00; |
| 6847 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6848 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6849 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6850 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6851 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6852 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6853 | ev.key.enc_size = key->enc_size; |
| 6854 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 6855 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6856 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 6857 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6858 | ev.key.master = 1; |
| 6859 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6860 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 6861 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 6862 | mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6863 | } |
| 6864 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6865 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 6866 | { |
| 6867 | struct mgmt_ev_new_irk ev; |
| 6868 | |
| 6869 | memset(&ev, 0, sizeof(ev)); |
| 6870 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6871 | /* For identity resolving keys from devices that are already |
| 6872 | * using a public address or static random address, do not |
| 6873 | * ask for storing this key. The identity resolving key really |
| 6874 | * is only mandatory for devices using resovlable random |
| 6875 | * addresses. |
| 6876 | * |
| 6877 | * Storing all identity resolving keys has the downside that |
| 6878 | * they will be also loaded on next boot of they system. More |
| 6879 | * identity resolving keys, means more time during scanning is |
| 6880 | * needed to actually resolve these addresses. |
| 6881 | */ |
| 6882 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 6883 | ev.store_hint = 0x01; |
| 6884 | else |
| 6885 | ev.store_hint = 0x00; |
| 6886 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6887 | bacpy(&ev.rpa, &irk->rpa); |
| 6888 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6889 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6890 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6891 | |
| 6892 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6893 | } |
| 6894 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6895 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6896 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6897 | { |
| 6898 | struct mgmt_ev_new_csrk ev; |
| 6899 | |
| 6900 | memset(&ev, 0, sizeof(ev)); |
| 6901 | |
| 6902 | /* Devices using resolvable or non-resolvable random addresses |
| 6903 | * without providing an indentity resolving key don't require |
| 6904 | * to store signature resolving keys. Their addresses will change |
| 6905 | * the next time around. |
| 6906 | * |
| 6907 | * Only when a remote device provides an identity address |
| 6908 | * make sure the signature resolving key is stored. So allow |
| 6909 | * static random and public addresses here. |
| 6910 | */ |
| 6911 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6912 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6913 | ev.store_hint = 0x00; |
| 6914 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6915 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6916 | |
| 6917 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6918 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
Johan Hedberg | 4cd3928 | 2015-02-27 10:11:13 +0200 | [diff] [blame] | 6919 | ev.key.type = csrk->type; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6920 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6921 | |
| 6922 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6923 | } |
| 6924 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6925 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6926 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6927 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6928 | { |
| 6929 | struct mgmt_ev_new_conn_param ev; |
| 6930 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6931 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6932 | return; |
| 6933 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6934 | memset(&ev, 0, sizeof(ev)); |
| 6935 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6936 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6937 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6938 | ev.min_interval = cpu_to_le16(min_interval); |
| 6939 | ev.max_interval = cpu_to_le16(max_interval); |
| 6940 | ev.latency = cpu_to_le16(latency); |
| 6941 | ev.timeout = cpu_to_le16(timeout); |
| 6942 | |
| 6943 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6944 | } |
| 6945 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6946 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 6947 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6948 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6949 | char buf[512]; |
| 6950 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6951 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6952 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6953 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 6954 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6955 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6956 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6957 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6958 | /* We must ensure that the EIR Data fields are ordered and |
| 6959 | * unique. Keep it simple for now and avoid the problem by not |
| 6960 | * adding any BR/EDR data to the LE adv. |
| 6961 | */ |
| 6962 | if (conn->le_adv_data_len > 0) { |
| 6963 | memcpy(&ev->eir[eir_len], |
| 6964 | conn->le_adv_data, conn->le_adv_data_len); |
| 6965 | eir_len = conn->le_adv_data_len; |
| 6966 | } else { |
| 6967 | if (name_len > 0) |
| 6968 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 6969 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6970 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 6971 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6972 | eir_len = eir_append_data(ev->eir, eir_len, |
| 6973 | EIR_CLASS_OF_DEV, |
| 6974 | conn->dev_class, 3); |
| 6975 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6976 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6977 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6978 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6979 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6980 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6981 | } |
| 6982 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6983 | static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6984 | { |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6985 | struct sock **sk = data; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6986 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 6987 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6988 | |
| 6989 | *sk = cmd->sk; |
| 6990 | sock_hold(*sk); |
| 6991 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6992 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6993 | } |
| 6994 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 6995 | static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6996 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6997 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6998 | struct mgmt_cp_unpair_device *cp = cmd->param; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6999 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 7000 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 7001 | |
Johan Hedberg | d8b7b1e | 2014-12-05 13:36:05 +0200 | [diff] [blame] | 7002 | cmd->cmd_complete(cmd, 0); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 7003 | mgmt_pending_remove(cmd); |
| 7004 | } |
| 7005 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7006 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 7007 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7008 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7009 | struct mgmt_mode *cp; |
| 7010 | |
| 7011 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 7012 | if (!cmd) |
| 7013 | return false; |
| 7014 | |
| 7015 | cp = cmd->param; |
| 7016 | if (!cp->val) |
| 7017 | return true; |
| 7018 | |
| 7019 | return false; |
| 7020 | } |
| 7021 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 7022 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7023 | u8 link_type, u8 addr_type, u8 reason, |
| 7024 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7025 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 7026 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7027 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7028 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7029 | /* The connection is still in hci_conn_hash so test for 1 |
| 7030 | * instead of 0 to know if this is the last one. |
| 7031 | */ |
| 7032 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7033 | cancel_delayed_work(&hdev->power_off); |
| 7034 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 7035 | } |
| 7036 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 7037 | if (!mgmt_connected) |
| 7038 | return; |
| 7039 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 7040 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 7041 | return; |
| 7042 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7043 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7044 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 7045 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7046 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7047 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7048 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 7049 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7050 | |
| 7051 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 7052 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7053 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 7054 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7055 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7056 | } |
| 7057 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 7058 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7059 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7060 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 7061 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 7062 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7063 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7064 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 7065 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 7066 | hdev); |
| 7067 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7068 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7069 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 7070 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 7071 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 7072 | cp = cmd->param; |
| 7073 | |
| 7074 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 7075 | return; |
| 7076 | |
| 7077 | if (cp->addr.type != bdaddr_type) |
| 7078 | return; |
| 7079 | |
Johan Hedberg | f5818c2 | 2014-12-05 13:36:02 +0200 | [diff] [blame] | 7080 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7081 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 7082 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7083 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7084 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7085 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7086 | { |
| 7087 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7088 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 7089 | /* The connection is still in hci_conn_hash so test for 1 |
| 7090 | * instead of 0 to know if this is the last one. |
| 7091 | */ |
| 7092 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 7093 | cancel_delayed_work(&hdev->power_off); |
| 7094 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 7095 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7096 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 7097 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7098 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 7099 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7100 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 7101 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 7102 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7103 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7104 | void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7105 | { |
| 7106 | struct mgmt_ev_pin_code_request ev; |
| 7107 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 7108 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 7109 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 7110 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7111 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 7112 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7113 | } |
| 7114 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7115 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7116 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7117 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7118 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7119 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7120 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7121 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 7122 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7123 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7124 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7125 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7126 | } |
| 7127 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7128 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7129 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7130 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7131 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7132 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7133 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7134 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 7135 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7136 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7137 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7138 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 7139 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7140 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7141 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7142 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7143 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7144 | { |
| 7145 | struct mgmt_ev_user_confirm_request ev; |
| 7146 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7147 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7148 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7149 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7150 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 7151 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 7152 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7153 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7154 | return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7155 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7156 | } |
| 7157 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7158 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 7159 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7160 | { |
| 7161 | struct mgmt_ev_user_passkey_request ev; |
| 7162 | |
| 7163 | BT_DBG("%s", hdev->name); |
| 7164 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7165 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7166 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7167 | |
| 7168 | return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7169 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7170 | } |
| 7171 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 7172 | static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 7173 | u8 link_type, u8 addr_type, u8 status, |
| 7174 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7175 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7176 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7177 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7178 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7179 | if (!cmd) |
| 7180 | return -ENOENT; |
| 7181 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7182 | cmd->cmd_complete(cmd, mgmt_status(status)); |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 7183 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7184 | |
Johan Hedberg | 7776d1d | 2014-12-05 13:36:03 +0200 | [diff] [blame] | 7185 | return 0; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7186 | } |
| 7187 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7188 | int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7189 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7190 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7191 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7192 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7193 | } |
| 7194 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7195 | int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7196 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7197 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7198 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7199 | status, |
| 7200 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 7201 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7202 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7203 | int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7204 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7205 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7206 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7207 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7208 | } |
| 7209 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7210 | int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7211 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7212 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 7213 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 7214 | status, |
| 7215 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 7216 | } |
| 7217 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 7218 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 7219 | u8 link_type, u8 addr_type, u32 passkey, |
| 7220 | u8 entered) |
| 7221 | { |
| 7222 | struct mgmt_ev_passkey_notify ev; |
| 7223 | |
| 7224 | BT_DBG("%s", hdev->name); |
| 7225 | |
| 7226 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 7227 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 7228 | ev.passkey = __cpu_to_le32(passkey); |
| 7229 | ev.entered = entered; |
| 7230 | |
| 7231 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 7232 | } |
| 7233 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7234 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7235 | { |
| 7236 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7237 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7238 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7239 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7240 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 7241 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 7242 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7243 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 7244 | cmd = find_pairing(conn); |
| 7245 | |
| 7246 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 7247 | cmd ? cmd->sk : NULL); |
| 7248 | |
Johan Hedberg | a511b35 | 2014-12-11 21:45:45 +0200 | [diff] [blame] | 7249 | if (cmd) { |
| 7250 | cmd->cmd_complete(cmd, status); |
| 7251 | mgmt_pending_remove(cmd); |
| 7252 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 7253 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7254 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7255 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7256 | { |
| 7257 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7258 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7259 | |
| 7260 | if (status) { |
| 7261 | u8 mgmt_err = mgmt_status(status); |
| 7262 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7263 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7264 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7265 | } |
| 7266 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7267 | if (test_bit(HCI_AUTH, &hdev->flags)) |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7268 | changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7269 | else |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7270 | changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7271 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7272 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7273 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7274 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 7275 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 7276 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7277 | |
| 7278 | if (match.sk) |
| 7279 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 7280 | } |
| 7281 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7282 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7283 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7284 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7285 | struct hci_cp_write_eir cp; |
| 7286 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 7287 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7288 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7289 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 7290 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 7291 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7292 | memset(&cp, 0, sizeof(cp)); |
| 7293 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7294 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 7295 | } |
| 7296 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7297 | void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7298 | { |
| 7299 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7300 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7301 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7302 | |
| 7303 | if (status) { |
| 7304 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7305 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7306 | if (enable && hci_dev_test_and_clear_flag(hdev, |
| 7307 | HCI_SSP_ENABLED)) { |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7308 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7309 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7310 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7311 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7312 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 7313 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7314 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7315 | } |
| 7316 | |
| 7317 | if (enable) { |
Marcel Holtmann | 238be78 | 2015-03-13 02:11:06 -0700 | [diff] [blame] | 7318 | changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7319 | } else { |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7320 | changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7321 | if (!changed) |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 7322 | changed = hci_dev_test_and_clear_flag(hdev, |
| 7323 | HCI_HS_ENABLED); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 7324 | else |
Marcel Holtmann | a358dc1 | 2015-03-13 02:11:02 -0700 | [diff] [blame] | 7325 | hci_dev_clear_flag(hdev, HCI_HS_ENABLED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7326 | } |
| 7327 | |
| 7328 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 7329 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 7330 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 7331 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7332 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 7333 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7334 | sock_put(match.sk); |
| 7335 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7336 | hci_req_init(&req, hdev); |
| 7337 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7338 | if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) { |
| 7339 | if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7340 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 7341 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7342 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7343 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7344 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 7345 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 7346 | |
| 7347 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 7348 | } |
| 7349 | |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7350 | static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7351 | { |
| 7352 | struct cmd_lookup *match = data; |
| 7353 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7354 | if (match->sk == NULL) { |
| 7355 | match->sk = cmd->sk; |
| 7356 | sock_hold(match->sk); |
| 7357 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7358 | } |
| 7359 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 7360 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 7361 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7362 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7363 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7364 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 7365 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 7366 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 7367 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7368 | |
| 7369 | if (!status) |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 7370 | mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 7371 | dev_class, 3, NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 7372 | |
| 7373 | if (match.sk) |
| 7374 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 7375 | } |
| 7376 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7377 | void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7378 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7379 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7380 | struct mgmt_pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7381 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7382 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7383 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7384 | |
| 7385 | memset(&ev, 0, sizeof(ev)); |
| 7386 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 7387 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7388 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7389 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7390 | if (!cmd) { |
| 7391 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7392 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 7393 | /* If this is a HCI command related to powering on the |
| 7394 | * HCI dev don't send any mgmt signals. |
| 7395 | */ |
| 7396 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 7397 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7398 | } |
| 7399 | |
Marcel Holtmann | f6b7712 | 2015-03-14 19:28:05 -0700 | [diff] [blame] | 7400 | mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 7401 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 7402 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7403 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 7404 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 7405 | u8 *rand192, u8 *hash256, u8 *rand256, |
| 7406 | u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7407 | { |
Johan Hedberg | 3b0602c | 2015-03-06 21:08:55 +0200 | [diff] [blame] | 7408 | struct mgmt_pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7409 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 7410 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7411 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 7412 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7413 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 7414 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7415 | |
| 7416 | if (status) { |
Johan Hedberg | a69e837 | 2015-03-06 21:08:53 +0200 | [diff] [blame] | 7417 | mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 7418 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7419 | } else { |
Johan Hedberg | 66f096f | 2015-02-02 13:23:42 +0200 | [diff] [blame] | 7420 | struct mgmt_rp_read_local_oob_data rp; |
| 7421 | size_t rp_size = sizeof(rp); |
| 7422 | |
| 7423 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 7424 | memcpy(rp.rand192, rand192, sizeof(rp.rand192)); |
| 7425 | |
Johan Hedberg | 710f11c | 2014-05-26 11:21:22 +0300 | [diff] [blame] | 7426 | if (bredr_sc_enabled(hdev) && hash256 && rand256) { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 7427 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 7428 | memcpy(rp.rand256, rand256, sizeof(rp.rand256)); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 7429 | } else { |
Johan Hedberg | 66f096f | 2015-02-02 13:23:42 +0200 | [diff] [blame] | 7430 | rp_size -= sizeof(rp.hash256) + sizeof(rp.rand256); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 7431 | } |
Johan Hedberg | 66f096f | 2015-02-02 13:23:42 +0200 | [diff] [blame] | 7432 | |
Johan Hedberg | 2a1afb5 | 2015-03-06 21:08:54 +0200 | [diff] [blame] | 7433 | mgmt_cmd_complete(cmd->sk, hdev->id, |
| 7434 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 7435 | &rp, rp_size); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7436 | } |
| 7437 | |
| 7438 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 7439 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7440 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7441 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 7442 | { |
| 7443 | int i; |
| 7444 | |
| 7445 | for (i = 0; i < uuid_count; i++) { |
| 7446 | if (!memcmp(uuid, uuids[i], 16)) |
| 7447 | return true; |
| 7448 | } |
| 7449 | |
| 7450 | return false; |
| 7451 | } |
| 7452 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7453 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 7454 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7455 | u16 parsed = 0; |
| 7456 | |
| 7457 | while (parsed < eir_len) { |
| 7458 | u8 field_len = eir[0]; |
| 7459 | u8 uuid[16]; |
| 7460 | int i; |
| 7461 | |
| 7462 | if (field_len == 0) |
| 7463 | break; |
| 7464 | |
| 7465 | if (eir_len - parsed < field_len + 1) |
| 7466 | break; |
| 7467 | |
| 7468 | switch (eir[1]) { |
| 7469 | case EIR_UUID16_ALL: |
| 7470 | case EIR_UUID16_SOME: |
| 7471 | for (i = 0; i + 3 <= field_len; i += 2) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7472 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7473 | uuid[13] = eir[i + 3]; |
| 7474 | uuid[12] = eir[i + 2]; |
| 7475 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7476 | return true; |
| 7477 | } |
| 7478 | break; |
| 7479 | case EIR_UUID32_ALL: |
| 7480 | case EIR_UUID32_SOME: |
| 7481 | for (i = 0; i + 5 <= field_len; i += 4) { |
Johan Hedberg | 189f6ad | 2014-12-05 13:40:01 +0200 | [diff] [blame] | 7482 | memcpy(uuid, bluetooth_base_uuid, 16); |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 7483 | uuid[15] = eir[i + 5]; |
| 7484 | uuid[14] = eir[i + 4]; |
| 7485 | uuid[13] = eir[i + 3]; |
| 7486 | uuid[12] = eir[i + 2]; |
| 7487 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7488 | return true; |
| 7489 | } |
| 7490 | break; |
| 7491 | case EIR_UUID128_ALL: |
| 7492 | case EIR_UUID128_SOME: |
| 7493 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 7494 | memcpy(uuid, eir + i + 2, 16); |
| 7495 | if (has_uuid(uuid, uuid_count, uuids)) |
| 7496 | return true; |
| 7497 | } |
| 7498 | break; |
| 7499 | } |
| 7500 | |
| 7501 | parsed += field_len + 1; |
| 7502 | eir += field_len + 1; |
| 7503 | } |
| 7504 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7505 | return false; |
| 7506 | } |
| 7507 | |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7508 | static void restart_le_scan(struct hci_dev *hdev) |
| 7509 | { |
| 7510 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7511 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7512 | return; |
| 7513 | |
| 7514 | if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, |
| 7515 | hdev->discovery.scan_start + |
| 7516 | hdev->discovery.scan_duration)) |
| 7517 | return; |
| 7518 | |
| 7519 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart, |
| 7520 | DISCOV_LE_RESTART_DELAY); |
| 7521 | } |
| 7522 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7523 | static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir, |
| 7524 | u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
| 7525 | { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7526 | /* If a RSSI threshold has been specified, and |
| 7527 | * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with |
| 7528 | * a RSSI smaller than the RSSI threshold will be dropped. If the quirk |
| 7529 | * is set, let it through for further processing, as we might need to |
| 7530 | * restart the scan. |
| 7531 | * |
| 7532 | * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry, |
| 7533 | * the results are also dropped. |
| 7534 | */ |
| 7535 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7536 | (rssi == HCI_RSSI_INVALID || |
| 7537 | (rssi < hdev->discovery.rssi && |
| 7538 | !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)))) |
| 7539 | return false; |
| 7540 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7541 | if (hdev->discovery.uuid_count != 0) { |
| 7542 | /* If a list of UUIDs is provided in filter, results with no |
| 7543 | * matching UUID should be dropped. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7544 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7545 | if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count, |
| 7546 | hdev->discovery.uuids) && |
| 7547 | !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 7548 | hdev->discovery.uuid_count, |
| 7549 | hdev->discovery.uuids)) |
| 7550 | return false; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7551 | } |
| 7552 | |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7553 | /* If duplicate filtering does not report RSSI changes, then restart |
| 7554 | * scanning to ensure updated result with updated RSSI values. |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7555 | */ |
Jakub Pawlowski | 2976cde | 2015-03-04 16:24:25 -0800 | [diff] [blame] | 7556 | if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) { |
| 7557 | restart_le_scan(hdev); |
| 7558 | |
| 7559 | /* Validate RSSI value against the RSSI threshold once more. */ |
| 7560 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7561 | rssi < hdev->discovery.rssi) |
| 7562 | return false; |
| 7563 | } |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7564 | |
| 7565 | return true; |
| 7566 | } |
| 7567 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7568 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 7569 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 7570 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7571 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7572 | char buf[512]; |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7573 | struct mgmt_ev_device_found *ev = (void *)buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7574 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7575 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7576 | /* Don't send events for a non-kernel initiated discovery. With |
| 7577 | * LE one exception is if we have pend_le_reports > 0 in which |
| 7578 | * case we're doing passive scanning and want these events. |
| 7579 | */ |
| 7580 | if (!hci_discovery_active(hdev)) { |
| 7581 | if (link_type == ACL_LINK) |
| 7582 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 7583 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7584 | return; |
| 7585 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 7586 | |
Jakub Pawlowski | 82f8b65 | 2015-03-04 16:24:26 -0800 | [diff] [blame] | 7587 | if (hdev->discovery.result_filtering) { |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7588 | /* We are using service discovery */ |
| 7589 | if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp, |
| 7590 | scan_rsp_len)) |
| 7591 | return; |
| 7592 | } |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 7593 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7594 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 7595 | * are for the potential CoD field. |
| 7596 | */ |
| 7597 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7598 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 7599 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7600 | memset(buf, 0, sizeof(buf)); |
| 7601 | |
Marcel Holtmann | da25cf6 | 2014-12-05 13:03:35 +0100 | [diff] [blame] | 7602 | /* In case of device discovery with BR/EDR devices (pre 1.2), the |
| 7603 | * RSSI value was reported as 0 when not available. This behavior |
| 7604 | * is kept when using device discovery. This is required for full |
| 7605 | * backwards compatibility with the API. |
| 7606 | * |
| 7607 | * However when using service discovery, the value 127 will be |
| 7608 | * returned when the RSSI is not available. |
| 7609 | */ |
Szymon Janc | 91200e9 | 2015-01-22 16:57:05 +0100 | [diff] [blame] | 7610 | if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi && |
| 7611 | link_type == ACL_LINK) |
Marcel Holtmann | efb2513 | 2014-12-05 13:03:34 +0100 | [diff] [blame] | 7612 | rssi = 0; |
| 7613 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 7614 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 7615 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7616 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 7617 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7618 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7619 | if (eir_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7620 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7621 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7622 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7623 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 7624 | eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7625 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7626 | |
Jakub Pawlowski | 48f86b7 | 2015-03-04 16:24:24 -0800 | [diff] [blame] | 7627 | if (scan_rsp_len > 0) |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7628 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7629 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Jakub Pawlowski | 4b0e0ce | 2015-02-01 23:07:55 -0800 | [diff] [blame] | 7630 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7631 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 7632 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 7633 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7634 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7635 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7636 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7637 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7638 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7639 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7640 | struct mgmt_ev_device_found *ev; |
| 7641 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 7642 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7643 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7644 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7645 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7646 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7647 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7648 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7649 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7650 | ev->rssi = rssi; |
| 7651 | |
| 7652 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 7653 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7654 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7655 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7656 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7657 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7658 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7659 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7660 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7661 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7662 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 7663 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 7664 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 7665 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7666 | memset(&ev, 0, sizeof(ev)); |
| 7667 | ev.type = hdev->discovery.type; |
| 7668 | ev.discovering = discovering; |
| 7669 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7670 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7671 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 7672 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 7673 | static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7674 | { |
| 7675 | BT_DBG("%s status %u", hdev->name, status); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7676 | } |
| 7677 | |
| 7678 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 7679 | { |
| 7680 | struct hci_request req; |
| 7681 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 7682 | if (!hci_dev_test_flag(hdev, HCI_ADVERTISING)) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7683 | return; |
| 7684 | |
| 7685 | hci_req_init(&req, hdev); |
| 7686 | enable_advertising(&req); |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 7687 | hci_req_run(&req, adv_enable_complete); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7688 | } |
Johan Hedberg | 6d785aa3 | 2015-03-06 21:08:51 +0200 | [diff] [blame] | 7689 | |
| 7690 | static struct hci_mgmt_chan chan = { |
| 7691 | .channel = HCI_CHANNEL_CONTROL, |
| 7692 | .handler_count = ARRAY_SIZE(mgmt_handlers), |
| 7693 | .handlers = mgmt_handlers, |
| 7694 | }; |
| 7695 | |
| 7696 | int mgmt_init(void) |
| 7697 | { |
| 7698 | return hci_mgmt_chan_register(&chan); |
| 7699 | } |
| 7700 | |
| 7701 | void mgmt_exit(void) |
| 7702 | { |
| 7703 | hci_mgmt_chan_unregister(&chan); |
| 7704 | } |