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 | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 32 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 33 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 34 | |
| 35 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 36 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 37 | #define MGMT_VERSION 1 |
Marcel Holtmann | 854bda1 | 2014-12-03 19:52:43 +0100 | [diff] [blame] | 38 | #define MGMT_REVISION 8 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 39 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 40 | static const u16 mgmt_commands[] = { |
| 41 | MGMT_OP_READ_INDEX_LIST, |
| 42 | MGMT_OP_READ_INFO, |
| 43 | MGMT_OP_SET_POWERED, |
| 44 | MGMT_OP_SET_DISCOVERABLE, |
| 45 | MGMT_OP_SET_CONNECTABLE, |
| 46 | MGMT_OP_SET_FAST_CONNECTABLE, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 47 | MGMT_OP_SET_BONDABLE, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 48 | MGMT_OP_SET_LINK_SECURITY, |
| 49 | MGMT_OP_SET_SSP, |
| 50 | MGMT_OP_SET_HS, |
| 51 | MGMT_OP_SET_LE, |
| 52 | MGMT_OP_SET_DEV_CLASS, |
| 53 | MGMT_OP_SET_LOCAL_NAME, |
| 54 | MGMT_OP_ADD_UUID, |
| 55 | MGMT_OP_REMOVE_UUID, |
| 56 | MGMT_OP_LOAD_LINK_KEYS, |
| 57 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 58 | MGMT_OP_DISCONNECT, |
| 59 | MGMT_OP_GET_CONNECTIONS, |
| 60 | MGMT_OP_PIN_CODE_REPLY, |
| 61 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 62 | MGMT_OP_SET_IO_CAPABILITY, |
| 63 | MGMT_OP_PAIR_DEVICE, |
| 64 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 65 | MGMT_OP_UNPAIR_DEVICE, |
| 66 | MGMT_OP_USER_CONFIRM_REPLY, |
| 67 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 68 | MGMT_OP_USER_PASSKEY_REPLY, |
| 69 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 70 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 71 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 72 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 73 | MGMT_OP_START_DISCOVERY, |
| 74 | MGMT_OP_STOP_DISCOVERY, |
| 75 | MGMT_OP_CONFIRM_NAME, |
| 76 | MGMT_OP_BLOCK_DEVICE, |
| 77 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 78 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 79 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 80 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 82 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 83 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 84 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 85 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 86 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 87 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 88 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 89 | MGMT_OP_ADD_DEVICE, |
| 90 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 91 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 92 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 93 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 94 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 95 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 96 | MGMT_OP_START_SERVICE_DISCOVERY, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | static const u16 mgmt_events[] = { |
| 100 | MGMT_EV_CONTROLLER_ERROR, |
| 101 | MGMT_EV_INDEX_ADDED, |
| 102 | MGMT_EV_INDEX_REMOVED, |
| 103 | MGMT_EV_NEW_SETTINGS, |
| 104 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 105 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 106 | MGMT_EV_NEW_LINK_KEY, |
| 107 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 108 | MGMT_EV_DEVICE_CONNECTED, |
| 109 | MGMT_EV_DEVICE_DISCONNECTED, |
| 110 | MGMT_EV_CONNECT_FAILED, |
| 111 | MGMT_EV_PIN_CODE_REQUEST, |
| 112 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 113 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 114 | MGMT_EV_AUTH_FAILED, |
| 115 | MGMT_EV_DEVICE_FOUND, |
| 116 | MGMT_EV_DISCOVERING, |
| 117 | MGMT_EV_DEVICE_BLOCKED, |
| 118 | MGMT_EV_DEVICE_UNBLOCKED, |
| 119 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 120 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 121 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 122 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 123 | MGMT_EV_DEVICE_ADDED, |
| 124 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 125 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 126 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 127 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 128 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 131 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 132 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 133 | struct pending_cmd { |
| 134 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 135 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 136 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 137 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 138 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 139 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 140 | }; |
| 141 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 142 | /* HCI to MGMT error code conversion table */ |
| 143 | static u8 mgmt_status_table[] = { |
| 144 | MGMT_STATUS_SUCCESS, |
| 145 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 146 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 147 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 148 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 149 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 150 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 151 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 152 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 153 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 154 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 155 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 156 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 157 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 158 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 159 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 160 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 161 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 162 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 163 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 164 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 165 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 166 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 167 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 168 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 169 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 170 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 171 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 172 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 173 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 174 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 175 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 176 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 177 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 178 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 179 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 180 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 181 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 182 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 183 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 184 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 185 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 186 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 187 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 188 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 189 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 190 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 191 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 192 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 193 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 194 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 195 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 196 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 197 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 198 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 199 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 200 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 201 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 202 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 203 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 204 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 205 | }; |
| 206 | |
| 207 | static u8 mgmt_status(u8 hci_status) |
| 208 | { |
| 209 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 210 | return mgmt_status_table[hci_status]; |
| 211 | |
| 212 | return MGMT_STATUS_FAILED; |
| 213 | } |
| 214 | |
Marcel Holtmann | 04c60f0 | 2014-07-04 19:06:22 +0200 | [diff] [blame] | 215 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 216 | struct sock *skip_sk) |
| 217 | { |
| 218 | struct sk_buff *skb; |
| 219 | struct mgmt_hdr *hdr; |
| 220 | |
| 221 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
| 222 | if (!skb) |
| 223 | return -ENOMEM; |
| 224 | |
| 225 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 226 | hdr->opcode = cpu_to_le16(event); |
| 227 | if (hdev) |
| 228 | hdr->index = cpu_to_le16(hdev->id); |
| 229 | else |
| 230 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 231 | hdr->len = cpu_to_le16(data_len); |
| 232 | |
| 233 | if (data) |
| 234 | memcpy(skb_put(skb, data_len), data, data_len); |
| 235 | |
| 236 | /* Time stamp */ |
| 237 | __net_timestamp(skb); |
| 238 | |
| 239 | hci_send_to_control(skb, skip_sk); |
| 240 | kfree_skb(skb); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 245 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 246 | { |
| 247 | struct sk_buff *skb; |
| 248 | struct mgmt_hdr *hdr; |
| 249 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 250 | int err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 251 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 252 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 253 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 254 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 255 | if (!skb) |
| 256 | return -ENOMEM; |
| 257 | |
| 258 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 259 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 260 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 261 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 262 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 263 | |
| 264 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 265 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 266 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 267 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 268 | err = sock_queue_rcv_skb(sk, skb); |
| 269 | if (err < 0) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 270 | kfree_skb(skb); |
| 271 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 272 | return err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 275 | static int cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 276 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 277 | { |
| 278 | struct sk_buff *skb; |
| 279 | struct mgmt_hdr *hdr; |
| 280 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 281 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 282 | |
| 283 | BT_DBG("sock %p", sk); |
| 284 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 285 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 286 | if (!skb) |
| 287 | return -ENOMEM; |
| 288 | |
| 289 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 290 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 291 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 292 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 293 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 294 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 295 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 296 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 297 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 298 | |
| 299 | if (rp) |
| 300 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 301 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 302 | err = sock_queue_rcv_skb(sk, skb); |
| 303 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 304 | kfree_skb(skb); |
| 305 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 306 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 307 | } |
| 308 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 309 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 310 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 311 | { |
| 312 | struct mgmt_rp_read_version rp; |
| 313 | |
| 314 | BT_DBG("sock %p", sk); |
| 315 | |
| 316 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 317 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 318 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 319 | return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 320 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 323 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 324 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 325 | { |
| 326 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 327 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 328 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 329 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 330 | size_t rp_size; |
| 331 | int i, err; |
| 332 | |
| 333 | BT_DBG("sock %p", sk); |
| 334 | |
| 335 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 336 | |
| 337 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 338 | if (!rp) |
| 339 | return -ENOMEM; |
| 340 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 341 | rp->num_commands = cpu_to_le16(num_commands); |
| 342 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 343 | |
| 344 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 345 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 346 | |
| 347 | for (i = 0; i < num_events; i++, opcode++) |
| 348 | put_unaligned_le16(mgmt_events[i], opcode); |
| 349 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 350 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 351 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 352 | kfree(rp); |
| 353 | |
| 354 | return err; |
| 355 | } |
| 356 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 357 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 358 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 359 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 360 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 361 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 362 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 363 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 364 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 365 | |
| 366 | BT_DBG("sock %p", sk); |
| 367 | |
| 368 | read_lock(&hci_dev_list_lock); |
| 369 | |
| 370 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 371 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 372 | if (d->dev_type == HCI_BREDR && |
| 373 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 374 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 375 | } |
| 376 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 377 | rp_len = sizeof(*rp) + (2 * count); |
| 378 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 379 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 380 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 381 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 382 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 383 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 384 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 385 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 386 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 387 | test_bit(HCI_CONFIG, &d->dev_flags) || |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 388 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 389 | continue; |
| 390 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 391 | /* Devices marked as raw-only are neither configured |
| 392 | * nor unconfigured controllers. |
| 393 | */ |
| 394 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 395 | continue; |
| 396 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 397 | if (d->dev_type == HCI_BREDR && |
| 398 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 399 | rp->index[count++] = cpu_to_le16(d->id); |
| 400 | BT_DBG("Added hci%u", d->id); |
| 401 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 404 | rp->num_controllers = cpu_to_le16(count); |
| 405 | rp_len = sizeof(*rp) + (2 * count); |
| 406 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 407 | read_unlock(&hci_dev_list_lock); |
| 408 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 409 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 410 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 411 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 412 | kfree(rp); |
| 413 | |
| 414 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 415 | } |
| 416 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 417 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 418 | void *data, u16 data_len) |
| 419 | { |
| 420 | struct mgmt_rp_read_unconf_index_list *rp; |
| 421 | struct hci_dev *d; |
| 422 | size_t rp_len; |
| 423 | u16 count; |
| 424 | int err; |
| 425 | |
| 426 | BT_DBG("sock %p", sk); |
| 427 | |
| 428 | read_lock(&hci_dev_list_lock); |
| 429 | |
| 430 | count = 0; |
| 431 | list_for_each_entry(d, &hci_dev_list, list) { |
| 432 | if (d->dev_type == HCI_BREDR && |
| 433 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
| 434 | count++; |
| 435 | } |
| 436 | |
| 437 | rp_len = sizeof(*rp) + (2 * count); |
| 438 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 439 | if (!rp) { |
| 440 | read_unlock(&hci_dev_list_lock); |
| 441 | return -ENOMEM; |
| 442 | } |
| 443 | |
| 444 | count = 0; |
| 445 | list_for_each_entry(d, &hci_dev_list, list) { |
| 446 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 447 | test_bit(HCI_CONFIG, &d->dev_flags) || |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 448 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 449 | continue; |
| 450 | |
| 451 | /* Devices marked as raw-only are neither configured |
| 452 | * nor unconfigured controllers. |
| 453 | */ |
| 454 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 455 | continue; |
| 456 | |
| 457 | if (d->dev_type == HCI_BREDR && |
| 458 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
| 459 | rp->index[count++] = cpu_to_le16(d->id); |
| 460 | BT_DBG("Added hci%u", d->id); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | rp->num_controllers = cpu_to_le16(count); |
| 465 | rp_len = sizeof(*rp) + (2 * count); |
| 466 | |
| 467 | read_unlock(&hci_dev_list_lock); |
| 468 | |
| 469 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 470 | 0, rp, rp_len); |
| 471 | |
| 472 | kfree(rp); |
| 473 | |
| 474 | return err; |
| 475 | } |
| 476 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 477 | static bool is_configured(struct hci_dev *hdev) |
| 478 | { |
| 479 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 480 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
| 481 | return false; |
| 482 | |
| 483 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 484 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 485 | return false; |
| 486 | |
| 487 | return true; |
| 488 | } |
| 489 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 490 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 491 | { |
| 492 | u32 options = 0; |
| 493 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 494 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 495 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 496 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 497 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 498 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 499 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 500 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 501 | |
| 502 | return cpu_to_le32(options); |
| 503 | } |
| 504 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 505 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 506 | { |
| 507 | __le32 options = get_missing_options(hdev); |
| 508 | |
| 509 | return mgmt_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 510 | sizeof(options), skip); |
| 511 | } |
| 512 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 513 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 514 | { |
| 515 | __le32 options = get_missing_options(hdev); |
| 516 | |
| 517 | return cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 518 | sizeof(options)); |
| 519 | } |
| 520 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 521 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 522 | void *data, u16 data_len) |
| 523 | { |
| 524 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 525 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 526 | |
| 527 | BT_DBG("sock %p %s", sk, hdev->name); |
| 528 | |
| 529 | hci_dev_lock(hdev); |
| 530 | |
| 531 | memset(&rp, 0, sizeof(rp)); |
| 532 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 533 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 534 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 535 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 536 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 537 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 538 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 539 | |
| 540 | rp.supported_options = cpu_to_le32(options); |
| 541 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 542 | |
| 543 | hci_dev_unlock(hdev); |
| 544 | |
| 545 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp, |
| 546 | sizeof(rp)); |
| 547 | } |
| 548 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 549 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 550 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 551 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 552 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 553 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 554 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 555 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 556 | settings |= MGMT_SETTING_CONNECTABLE; |
| 557 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 558 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 559 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 560 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 561 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 562 | settings |= MGMT_SETTING_BREDR; |
| 563 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 564 | |
| 565 | if (lmp_ssp_capable(hdev)) { |
| 566 | settings |= MGMT_SETTING_SSP; |
| 567 | settings |= MGMT_SETTING_HS; |
| 568 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 569 | |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 570 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 571 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 572 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 573 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 574 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 575 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 576 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 577 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 578 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 579 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 580 | } |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 581 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 582 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 583 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 584 | settings |= MGMT_SETTING_CONFIGURATION; |
| 585 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 586 | return settings; |
| 587 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 588 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 589 | static u32 get_current_settings(struct hci_dev *hdev) |
| 590 | { |
| 591 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 592 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 593 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 594 | settings |= MGMT_SETTING_POWERED; |
| 595 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 596 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 597 | settings |= MGMT_SETTING_CONNECTABLE; |
| 598 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 599 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 600 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 601 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 602 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 603 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 604 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 605 | if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 606 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 607 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 608 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 609 | settings |= MGMT_SETTING_BREDR; |
| 610 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 611 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 612 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 613 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 614 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 615 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 616 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 617 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 618 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 619 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 620 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 621 | settings |= MGMT_SETTING_HS; |
| 622 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 623 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 624 | settings |= MGMT_SETTING_ADVERTISING; |
| 625 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 626 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 627 | settings |= MGMT_SETTING_SECURE_CONN; |
| 628 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 629 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 630 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 631 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 632 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 633 | settings |= MGMT_SETTING_PRIVACY; |
| 634 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 635 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 636 | } |
| 637 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 638 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 639 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 640 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 641 | { |
| 642 | u8 *ptr = data, *uuids_start = NULL; |
| 643 | struct bt_uuid *uuid; |
| 644 | |
| 645 | if (len < 4) |
| 646 | return ptr; |
| 647 | |
| 648 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 649 | u16 uuid16; |
| 650 | |
| 651 | if (uuid->size != 16) |
| 652 | continue; |
| 653 | |
| 654 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 655 | if (uuid16 < 0x1100) |
| 656 | continue; |
| 657 | |
| 658 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 659 | continue; |
| 660 | |
| 661 | if (!uuids_start) { |
| 662 | uuids_start = ptr; |
| 663 | uuids_start[0] = 1; |
| 664 | uuids_start[1] = EIR_UUID16_ALL; |
| 665 | ptr += 2; |
| 666 | } |
| 667 | |
| 668 | /* Stop if not enough space to put next UUID */ |
| 669 | if ((ptr - data) + sizeof(u16) > len) { |
| 670 | uuids_start[1] = EIR_UUID16_SOME; |
| 671 | break; |
| 672 | } |
| 673 | |
| 674 | *ptr++ = (uuid16 & 0x00ff); |
| 675 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 676 | uuids_start[0] += sizeof(uuid16); |
| 677 | } |
| 678 | |
| 679 | return ptr; |
| 680 | } |
| 681 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 682 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 683 | { |
| 684 | u8 *ptr = data, *uuids_start = NULL; |
| 685 | struct bt_uuid *uuid; |
| 686 | |
| 687 | if (len < 6) |
| 688 | return ptr; |
| 689 | |
| 690 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 691 | if (uuid->size != 32) |
| 692 | continue; |
| 693 | |
| 694 | if (!uuids_start) { |
| 695 | uuids_start = ptr; |
| 696 | uuids_start[0] = 1; |
| 697 | uuids_start[1] = EIR_UUID32_ALL; |
| 698 | ptr += 2; |
| 699 | } |
| 700 | |
| 701 | /* Stop if not enough space to put next UUID */ |
| 702 | if ((ptr - data) + sizeof(u32) > len) { |
| 703 | uuids_start[1] = EIR_UUID32_SOME; |
| 704 | break; |
| 705 | } |
| 706 | |
| 707 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 708 | ptr += sizeof(u32); |
| 709 | uuids_start[0] += sizeof(u32); |
| 710 | } |
| 711 | |
| 712 | return ptr; |
| 713 | } |
| 714 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 715 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 716 | { |
| 717 | u8 *ptr = data, *uuids_start = NULL; |
| 718 | struct bt_uuid *uuid; |
| 719 | |
| 720 | if (len < 18) |
| 721 | return ptr; |
| 722 | |
| 723 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 724 | if (uuid->size != 128) |
| 725 | continue; |
| 726 | |
| 727 | if (!uuids_start) { |
| 728 | uuids_start = ptr; |
| 729 | uuids_start[0] = 1; |
| 730 | uuids_start[1] = EIR_UUID128_ALL; |
| 731 | ptr += 2; |
| 732 | } |
| 733 | |
| 734 | /* Stop if not enough space to put next UUID */ |
| 735 | if ((ptr - data) + 16 > len) { |
| 736 | uuids_start[1] = EIR_UUID128_SOME; |
| 737 | break; |
| 738 | } |
| 739 | |
| 740 | memcpy(ptr, uuid->uuid, 16); |
| 741 | ptr += 16; |
| 742 | uuids_start[0] += 16; |
| 743 | } |
| 744 | |
| 745 | return ptr; |
| 746 | } |
| 747 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 748 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 749 | { |
| 750 | struct pending_cmd *cmd; |
| 751 | |
| 752 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 753 | if (cmd->opcode == opcode) |
| 754 | return cmd; |
| 755 | } |
| 756 | |
| 757 | return NULL; |
| 758 | } |
| 759 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 760 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 761 | struct hci_dev *hdev, |
| 762 | const void *data) |
| 763 | { |
| 764 | struct pending_cmd *cmd; |
| 765 | |
| 766 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 767 | if (cmd->user_data != data) |
| 768 | continue; |
| 769 | if (cmd->opcode == opcode) |
| 770 | return cmd; |
| 771 | } |
| 772 | |
| 773 | return NULL; |
| 774 | } |
| 775 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 776 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 777 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 778 | u8 ad_len = 0; |
| 779 | size_t name_len; |
| 780 | |
| 781 | name_len = strlen(hdev->dev_name); |
| 782 | if (name_len > 0) { |
| 783 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 784 | |
| 785 | if (name_len > max_len) { |
| 786 | name_len = max_len; |
| 787 | ptr[1] = EIR_NAME_SHORT; |
| 788 | } else |
| 789 | ptr[1] = EIR_NAME_COMPLETE; |
| 790 | |
| 791 | ptr[0] = name_len + 1; |
| 792 | |
| 793 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 794 | |
| 795 | ad_len += (name_len + 2); |
| 796 | ptr += (name_len + 2); |
| 797 | } |
| 798 | |
| 799 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | static void update_scan_rsp_data(struct hci_request *req) |
| 803 | { |
| 804 | struct hci_dev *hdev = req->hdev; |
| 805 | struct hci_cp_le_set_scan_rsp_data cp; |
| 806 | u8 len; |
| 807 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 808 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 809 | return; |
| 810 | |
| 811 | memset(&cp, 0, sizeof(cp)); |
| 812 | |
| 813 | len = create_scan_rsp_data(hdev, cp.data); |
| 814 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 815 | if (hdev->scan_rsp_data_len == len && |
| 816 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 817 | return; |
| 818 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 819 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 820 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 821 | |
| 822 | cp.length = len; |
| 823 | |
| 824 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 825 | } |
| 826 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 827 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 828 | { |
| 829 | struct pending_cmd *cmd; |
| 830 | |
| 831 | /* If there's a pending mgmt command the flags will not yet have |
| 832 | * their final values, so check for this first. |
| 833 | */ |
| 834 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 835 | if (cmd) { |
| 836 | struct mgmt_mode *cp = cmd->param; |
| 837 | if (cp->val == 0x01) |
| 838 | return LE_AD_GENERAL; |
| 839 | else if (cp->val == 0x02) |
| 840 | return LE_AD_LIMITED; |
| 841 | } else { |
| 842 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 843 | return LE_AD_LIMITED; |
| 844 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 845 | return LE_AD_GENERAL; |
| 846 | } |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 851 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 852 | { |
| 853 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 854 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 855 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 856 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 857 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 858 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 859 | |
| 860 | if (flags) { |
| 861 | BT_DBG("adv flags 0x%02x", flags); |
| 862 | |
| 863 | ptr[0] = 2; |
| 864 | ptr[1] = EIR_FLAGS; |
| 865 | ptr[2] = flags; |
| 866 | |
| 867 | ad_len += 3; |
| 868 | ptr += 3; |
| 869 | } |
| 870 | |
| 871 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 872 | ptr[0] = 2; |
| 873 | ptr[1] = EIR_TX_POWER; |
| 874 | ptr[2] = (u8) hdev->adv_tx_power; |
| 875 | |
| 876 | ad_len += 3; |
| 877 | ptr += 3; |
| 878 | } |
| 879 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 880 | return ad_len; |
| 881 | } |
| 882 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 883 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 884 | { |
| 885 | struct hci_dev *hdev = req->hdev; |
| 886 | struct hci_cp_le_set_adv_data cp; |
| 887 | u8 len; |
| 888 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 889 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 890 | return; |
| 891 | |
| 892 | memset(&cp, 0, sizeof(cp)); |
| 893 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 894 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 895 | |
| 896 | if (hdev->adv_data_len == len && |
| 897 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 898 | return; |
| 899 | |
| 900 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 901 | hdev->adv_data_len = len; |
| 902 | |
| 903 | cp.length = len; |
| 904 | |
| 905 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 906 | } |
| 907 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 908 | int mgmt_update_adv_data(struct hci_dev *hdev) |
| 909 | { |
| 910 | struct hci_request req; |
| 911 | |
| 912 | hci_req_init(&req, hdev); |
| 913 | update_adv_data(&req); |
| 914 | |
| 915 | return hci_req_run(&req, NULL); |
| 916 | } |
| 917 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 918 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 919 | { |
| 920 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 921 | size_t name_len; |
| 922 | |
| 923 | name_len = strlen(hdev->dev_name); |
| 924 | |
| 925 | if (name_len > 0) { |
| 926 | /* EIR Data type */ |
| 927 | if (name_len > 48) { |
| 928 | name_len = 48; |
| 929 | ptr[1] = EIR_NAME_SHORT; |
| 930 | } else |
| 931 | ptr[1] = EIR_NAME_COMPLETE; |
| 932 | |
| 933 | /* EIR Data length */ |
| 934 | ptr[0] = name_len + 1; |
| 935 | |
| 936 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 937 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 938 | ptr += (name_len + 2); |
| 939 | } |
| 940 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 941 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 942 | ptr[0] = 2; |
| 943 | ptr[1] = EIR_TX_POWER; |
| 944 | ptr[2] = (u8) hdev->inq_tx_power; |
| 945 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 946 | ptr += 3; |
| 947 | } |
| 948 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 949 | if (hdev->devid_source > 0) { |
| 950 | ptr[0] = 9; |
| 951 | ptr[1] = EIR_DEVICE_ID; |
| 952 | |
| 953 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 954 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 955 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 956 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 957 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 958 | ptr += 10; |
| 959 | } |
| 960 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 961 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 962 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 963 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 964 | } |
| 965 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 966 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 967 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 968 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 969 | struct hci_cp_write_eir cp; |
| 970 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 971 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 972 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 973 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 974 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 975 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 976 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 977 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 978 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 979 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 980 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 981 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 982 | |
| 983 | memset(&cp, 0, sizeof(cp)); |
| 984 | |
| 985 | create_eir(hdev, cp.data); |
| 986 | |
| 987 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 988 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 989 | |
| 990 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 991 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 992 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 993 | } |
| 994 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 995 | static u8 get_service_classes(struct hci_dev *hdev) |
| 996 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 997 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 998 | u8 val = 0; |
| 999 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 1000 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1001 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1002 | |
| 1003 | return val; |
| 1004 | } |
| 1005 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1006 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1007 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1008 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1009 | u8 cod[3]; |
| 1010 | |
| 1011 | BT_DBG("%s", hdev->name); |
| 1012 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 1013 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1014 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 1015 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 1016 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1017 | return; |
| 1018 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1019 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1020 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1021 | |
| 1022 | cod[0] = hdev->minor_class; |
| 1023 | cod[1] = hdev->major_class; |
| 1024 | cod[2] = get_service_classes(hdev); |
| 1025 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 1026 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 1027 | cod[1] |= 0x20; |
| 1028 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1029 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1030 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1031 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1032 | 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] | 1033 | } |
| 1034 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1035 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1036 | { |
| 1037 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1038 | |
| 1039 | /* If there's a pending mgmt command the flag will not yet have |
| 1040 | * it's final value, so check for this first. |
| 1041 | */ |
| 1042 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1043 | if (cmd) { |
| 1044 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1045 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1048 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1051 | static void disable_advertising(struct hci_request *req) |
| 1052 | { |
| 1053 | u8 enable = 0x00; |
| 1054 | |
| 1055 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1056 | } |
| 1057 | |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1058 | static void enable_advertising(struct hci_request *req) |
| 1059 | { |
| 1060 | struct hci_dev *hdev = req->hdev; |
| 1061 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1062 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1063 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1064 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1065 | if (hci_conn_num(hdev, LE_LINK) > 0) |
| 1066 | return; |
| 1067 | |
| 1068 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
| 1069 | disable_advertising(req); |
| 1070 | |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1071 | /* Clear the HCI_LE_ADV bit temporarily so that the |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1072 | * hci_update_random_address knows that it's safe to go ahead |
| 1073 | * and write a new random address. The flag will be set back on |
| 1074 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 1075 | */ |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1076 | clear_bit(HCI_LE_ADV, &hdev->dev_flags); |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1077 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1078 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1079 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1080 | /* Set require_privacy to true only when non-connectable |
| 1081 | * advertising is used. In that case it is fine to use a |
| 1082 | * non-resolvable private address. |
| 1083 | */ |
| 1084 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1085 | return; |
| 1086 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 1087 | memset(&cp, 0, sizeof(cp)); |
Georg Lukas | 628531c | 2014-07-26 13:59:57 +0200 | [diff] [blame] | 1088 | cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval); |
| 1089 | cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1090 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1091 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1092 | cp.channel_map = hdev->le_adv_channel_map; |
| 1093 | |
| 1094 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1095 | |
| 1096 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1097 | } |
| 1098 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1099 | static void service_cache_off(struct work_struct *work) |
| 1100 | { |
| 1101 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1102 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1103 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1104 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1105 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1106 | return; |
| 1107 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1108 | hci_req_init(&req, hdev); |
| 1109 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1110 | hci_dev_lock(hdev); |
| 1111 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1112 | update_eir(&req); |
| 1113 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1114 | |
| 1115 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1116 | |
| 1117 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1120 | static void rpa_expired(struct work_struct *work) |
| 1121 | { |
| 1122 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1123 | rpa_expired.work); |
| 1124 | struct hci_request req; |
| 1125 | |
| 1126 | BT_DBG(""); |
| 1127 | |
| 1128 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 1129 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1130 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1131 | return; |
| 1132 | |
| 1133 | /* The generation of a new RPA and programming it into the |
| 1134 | * controller happens in the enable_advertising() function. |
| 1135 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1136 | hci_req_init(&req, hdev); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1137 | enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1138 | hci_req_run(&req, NULL); |
| 1139 | } |
| 1140 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1141 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1142 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1143 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1144 | return; |
| 1145 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1146 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1147 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1148 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1149 | /* Non-mgmt controlled devices get this bit set |
| 1150 | * implicitly so that pairing works for them, however |
| 1151 | * for mgmt we require user-space to explicitly enable |
| 1152 | * it |
| 1153 | */ |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1154 | clear_bit(HCI_BONDABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1157 | 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] | 1158 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1159 | { |
| 1160 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1161 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1162 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1163 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1164 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1165 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1166 | memset(&rp, 0, sizeof(rp)); |
| 1167 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1168 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1169 | |
| 1170 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1171 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1172 | |
| 1173 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1174 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1175 | |
| 1176 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1177 | |
| 1178 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1179 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1180 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1181 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1182 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1183 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1184 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
| 1187 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1188 | { |
| 1189 | sock_put(cmd->sk); |
| 1190 | kfree(cmd->param); |
| 1191 | kfree(cmd); |
| 1192 | } |
| 1193 | |
| 1194 | static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1195 | struct hci_dev *hdev, void *data, |
| 1196 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1197 | { |
| 1198 | struct pending_cmd *cmd; |
| 1199 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1200 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1201 | if (!cmd) |
| 1202 | return NULL; |
| 1203 | |
| 1204 | cmd->opcode = opcode; |
| 1205 | cmd->index = hdev->id; |
| 1206 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1207 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1208 | if (!cmd->param) { |
| 1209 | kfree(cmd); |
| 1210 | return NULL; |
| 1211 | } |
| 1212 | |
| 1213 | if (data) |
| 1214 | memcpy(cmd->param, data, len); |
| 1215 | |
| 1216 | cmd->sk = sk; |
| 1217 | sock_hold(sk); |
| 1218 | |
| 1219 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1220 | |
| 1221 | return cmd; |
| 1222 | } |
| 1223 | |
| 1224 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1225 | void (*cb)(struct pending_cmd *cmd, |
| 1226 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1227 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1228 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1229 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1230 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1231 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1232 | if (opcode > 0 && cmd->opcode != opcode) |
| 1233 | continue; |
| 1234 | |
| 1235 | cb(cmd, data); |
| 1236 | } |
| 1237 | } |
| 1238 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1239 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1240 | { |
| 1241 | list_del(&cmd->list); |
| 1242 | mgmt_pending_free(cmd); |
| 1243 | } |
| 1244 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1245 | 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] | 1246 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1247 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1248 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1249 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1250 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1253 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1254 | { |
| 1255 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1256 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1257 | if (hci_conn_count(hdev) == 0) { |
| 1258 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1259 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1260 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1261 | } |
| 1262 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1263 | static bool hci_stop_discovery(struct hci_request *req) |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1264 | { |
| 1265 | struct hci_dev *hdev = req->hdev; |
| 1266 | struct hci_cp_remote_name_req_cancel cp; |
| 1267 | struct inquiry_entry *e; |
| 1268 | |
| 1269 | switch (hdev->discovery.state) { |
| 1270 | case DISCOVERY_FINDING: |
| 1271 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1272 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1273 | } else { |
| 1274 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1275 | hci_req_add_le_scan_disable(req); |
| 1276 | } |
| 1277 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1278 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1279 | |
| 1280 | case DISCOVERY_RESOLVING: |
| 1281 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1282 | NAME_PENDING); |
| 1283 | if (!e) |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1284 | break; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1285 | |
| 1286 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1287 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1288 | &cp); |
| 1289 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1290 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1291 | |
| 1292 | default: |
| 1293 | /* Passive scanning */ |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1294 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1295 | hci_req_add_le_scan_disable(req); |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1296 | return true; |
| 1297 | } |
| 1298 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1299 | break; |
| 1300 | } |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1301 | |
| 1302 | return false; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1303 | } |
| 1304 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1305 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1306 | { |
| 1307 | struct hci_request req; |
| 1308 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1309 | bool discov_stopped; |
| 1310 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1311 | |
| 1312 | hci_req_init(&req, hdev); |
| 1313 | |
| 1314 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1315 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1316 | u8 scan = 0x00; |
| 1317 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1318 | } |
| 1319 | |
Johan Hedberg | 73e082f | 2014-07-08 15:07:51 +0300 | [diff] [blame] | 1320 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1321 | disable_advertising(&req); |
| 1322 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1323 | discov_stopped = hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1324 | |
| 1325 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1326 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1327 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1328 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1329 | switch (conn->state) { |
| 1330 | case BT_CONNECTED: |
| 1331 | case BT_CONFIG: |
| 1332 | dc.handle = cpu_to_le16(conn->handle); |
| 1333 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1334 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1335 | break; |
| 1336 | case BT_CONNECT: |
| 1337 | if (conn->type == LE_LINK) |
| 1338 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1339 | 0, NULL); |
| 1340 | else if (conn->type == ACL_LINK) |
| 1341 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1342 | 6, &conn->dst); |
| 1343 | break; |
| 1344 | case BT_CONNECT2: |
| 1345 | bacpy(&rej.bdaddr, &conn->dst); |
| 1346 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1347 | if (conn->type == ACL_LINK) |
| 1348 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1349 | sizeof(rej), &rej); |
| 1350 | else if (conn->type == SCO_LINK) |
| 1351 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1352 | sizeof(rej), &rej); |
| 1353 | break; |
| 1354 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1355 | } |
| 1356 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1357 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1358 | if (!err && discov_stopped) |
| 1359 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1360 | |
| 1361 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1362 | } |
| 1363 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1364 | 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] | 1365 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1366 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1367 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1368 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1369 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1370 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1371 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1372 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1373 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1374 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1375 | MGMT_STATUS_INVALID_PARAMS); |
| 1376 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1377 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1378 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1379 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1380 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1381 | MGMT_STATUS_BUSY); |
| 1382 | goto failed; |
| 1383 | } |
| 1384 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1385 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1386 | cancel_delayed_work(&hdev->power_off); |
| 1387 | |
| 1388 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1389 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1390 | data, len); |
| 1391 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1392 | goto failed; |
| 1393 | } |
| 1394 | } |
| 1395 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1396 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1397 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1398 | goto failed; |
| 1399 | } |
| 1400 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1401 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1402 | if (!cmd) { |
| 1403 | err = -ENOMEM; |
| 1404 | goto failed; |
| 1405 | } |
| 1406 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1407 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1408 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1409 | err = 0; |
| 1410 | } else { |
| 1411 | /* Disconnect connections, stop scans, etc */ |
| 1412 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1413 | if (!err) |
| 1414 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1415 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1416 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1417 | /* ENODATA means there were no HCI commands queued */ |
| 1418 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1419 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1420 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1421 | err = 0; |
| 1422 | } |
| 1423 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1424 | |
| 1425 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1426 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1427 | return err; |
| 1428 | } |
| 1429 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1430 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1431 | { |
| 1432 | __le32 ev; |
| 1433 | |
| 1434 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1435 | |
| 1436 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1437 | } |
| 1438 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1439 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1440 | { |
| 1441 | return new_settings(hdev, NULL); |
| 1442 | } |
| 1443 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1444 | struct cmd_lookup { |
| 1445 | struct sock *sk; |
| 1446 | struct hci_dev *hdev; |
| 1447 | u8 mgmt_status; |
| 1448 | }; |
| 1449 | |
| 1450 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1451 | { |
| 1452 | struct cmd_lookup *match = data; |
| 1453 | |
| 1454 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1455 | |
| 1456 | list_del(&cmd->list); |
| 1457 | |
| 1458 | if (match->sk == NULL) { |
| 1459 | match->sk = cmd->sk; |
| 1460 | sock_hold(match->sk); |
| 1461 | } |
| 1462 | |
| 1463 | mgmt_pending_free(cmd); |
| 1464 | } |
| 1465 | |
| 1466 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1467 | { |
| 1468 | u8 *status = data; |
| 1469 | |
| 1470 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1471 | mgmt_pending_remove(cmd); |
| 1472 | } |
| 1473 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1474 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1475 | { |
| 1476 | if (!lmp_bredr_capable(hdev)) |
| 1477 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1478 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1479 | return MGMT_STATUS_REJECTED; |
| 1480 | else |
| 1481 | return MGMT_STATUS_SUCCESS; |
| 1482 | } |
| 1483 | |
| 1484 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1485 | { |
| 1486 | if (!lmp_le_capable(hdev)) |
| 1487 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1488 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1489 | return MGMT_STATUS_REJECTED; |
| 1490 | else |
| 1491 | return MGMT_STATUS_SUCCESS; |
| 1492 | } |
| 1493 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1494 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1495 | { |
| 1496 | struct pending_cmd *cmd; |
| 1497 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1498 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1499 | bool changed; |
| 1500 | |
| 1501 | BT_DBG("status 0x%02x", status); |
| 1502 | |
| 1503 | hci_dev_lock(hdev); |
| 1504 | |
| 1505 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1506 | if (!cmd) |
| 1507 | goto unlock; |
| 1508 | |
| 1509 | if (status) { |
| 1510 | u8 mgmt_err = mgmt_status(status); |
| 1511 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1512 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1513 | goto remove_cmd; |
| 1514 | } |
| 1515 | |
| 1516 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1517 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1518 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1519 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1520 | |
| 1521 | if (hdev->discov_timeout > 0) { |
| 1522 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1523 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1524 | to); |
| 1525 | } |
| 1526 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1527 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1528 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1529 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1530 | |
| 1531 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1532 | |
| 1533 | if (changed) |
| 1534 | new_settings(hdev, cmd->sk); |
| 1535 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1536 | /* When the discoverable mode gets changed, make sure |
| 1537 | * that class of device has the limited discoverable |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1538 | * bit correctly set. Also update page scan based on whitelist |
| 1539 | * entries. |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1540 | */ |
| 1541 | hci_req_init(&req, hdev); |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1542 | hci_update_page_scan(hdev, &req); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1543 | update_class(&req); |
| 1544 | hci_req_run(&req, NULL); |
| 1545 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1546 | remove_cmd: |
| 1547 | mgmt_pending_remove(cmd); |
| 1548 | |
| 1549 | unlock: |
| 1550 | hci_dev_unlock(hdev); |
| 1551 | } |
| 1552 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1553 | 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] | 1554 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1555 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1556 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1557 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1558 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1559 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1560 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1561 | int err; |
| 1562 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1563 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1564 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1565 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1566 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1567 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1568 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1569 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1570 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1571 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1572 | MGMT_STATUS_INVALID_PARAMS); |
| 1573 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1574 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1575 | |
| 1576 | /* Disabling discoverable requires that no timeout is set, |
| 1577 | * and enabling limited discoverable requires a timeout. |
| 1578 | */ |
| 1579 | if ((cp->val == 0x00 && timeout > 0) || |
| 1580 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1581 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1582 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1583 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1584 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1585 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1586 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1587 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1588 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1589 | goto failed; |
| 1590 | } |
| 1591 | |
| 1592 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1593 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1594 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1595 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1596 | goto failed; |
| 1597 | } |
| 1598 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1599 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1600 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1601 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1602 | goto failed; |
| 1603 | } |
| 1604 | |
| 1605 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1606 | bool changed = false; |
| 1607 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1608 | /* Setting limited discoverable when powered off is |
| 1609 | * not a valid operation since it requires a timeout |
| 1610 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1611 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1612 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1613 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1614 | changed = true; |
| 1615 | } |
| 1616 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1617 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1618 | if (err < 0) |
| 1619 | goto failed; |
| 1620 | |
| 1621 | if (changed) |
| 1622 | err = new_settings(hdev, sk); |
| 1623 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1624 | goto failed; |
| 1625 | } |
| 1626 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1627 | /* If the current mode is the same, then just update the timeout |
| 1628 | * value with the new value. And if only the timeout gets updated, |
| 1629 | * then no need for any HCI transactions. |
| 1630 | */ |
| 1631 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1632 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1633 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1634 | cancel_delayed_work(&hdev->discov_off); |
| 1635 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1636 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1637 | if (cp->val && hdev->discov_timeout > 0) { |
| 1638 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1639 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1640 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1641 | } |
| 1642 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1643 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1644 | goto failed; |
| 1645 | } |
| 1646 | |
| 1647 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1648 | if (!cmd) { |
| 1649 | err = -ENOMEM; |
| 1650 | goto failed; |
| 1651 | } |
| 1652 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1653 | /* Cancel any potential discoverable timeout that might be |
| 1654 | * still active and store new timeout value. The arming of |
| 1655 | * the timeout happens in the complete handler. |
| 1656 | */ |
| 1657 | cancel_delayed_work(&hdev->discov_off); |
| 1658 | hdev->discov_timeout = timeout; |
| 1659 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1660 | /* Limited discoverable mode */ |
| 1661 | if (cp->val == 0x02) |
| 1662 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1663 | else |
| 1664 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1665 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1666 | hci_req_init(&req, hdev); |
| 1667 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1668 | /* The procedure for LE-only controllers is much simpler - just |
| 1669 | * update the advertising data. |
| 1670 | */ |
| 1671 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1672 | goto update_ad; |
| 1673 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1674 | scan = SCAN_PAGE; |
| 1675 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1676 | if (cp->val) { |
| 1677 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1678 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1679 | if (cp->val == 0x02) { |
| 1680 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1681 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1682 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1683 | hci_cp.iac_lap[1] = 0x8b; |
| 1684 | hci_cp.iac_lap[2] = 0x9e; |
| 1685 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1686 | hci_cp.iac_lap[4] = 0x8b; |
| 1687 | hci_cp.iac_lap[5] = 0x9e; |
| 1688 | } else { |
| 1689 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1690 | hci_cp.num_iac = 1; |
| 1691 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1692 | hci_cp.iac_lap[1] = 0x8b; |
| 1693 | hci_cp.iac_lap[2] = 0x9e; |
| 1694 | } |
| 1695 | |
| 1696 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1697 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1698 | |
| 1699 | scan |= SCAN_INQUIRY; |
| 1700 | } else { |
| 1701 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1702 | } |
| 1703 | |
| 1704 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1705 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1706 | update_ad: |
| 1707 | update_adv_data(&req); |
| 1708 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1709 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1710 | if (err < 0) |
| 1711 | mgmt_pending_remove(cmd); |
| 1712 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1713 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1714 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1715 | return err; |
| 1716 | } |
| 1717 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1718 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1719 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1720 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1721 | struct hci_cp_write_page_scan_activity acp; |
| 1722 | u8 type; |
| 1723 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1724 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1725 | return; |
| 1726 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1727 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1728 | return; |
| 1729 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1730 | if (enable) { |
| 1731 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1732 | |
| 1733 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1734 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1735 | } else { |
| 1736 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1737 | |
| 1738 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1739 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1740 | } |
| 1741 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1742 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1743 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1744 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1745 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1746 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1747 | sizeof(acp), &acp); |
| 1748 | |
| 1749 | if (hdev->page_scan_type != type) |
| 1750 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1751 | } |
| 1752 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1753 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1754 | { |
| 1755 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1756 | struct mgmt_mode *cp; |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1757 | bool conn_changed, discov_changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1758 | |
| 1759 | BT_DBG("status 0x%02x", status); |
| 1760 | |
| 1761 | hci_dev_lock(hdev); |
| 1762 | |
| 1763 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1764 | if (!cmd) |
| 1765 | goto unlock; |
| 1766 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1767 | if (status) { |
| 1768 | u8 mgmt_err = mgmt_status(status); |
| 1769 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1770 | goto remove_cmd; |
| 1771 | } |
| 1772 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1773 | cp = cmd->param; |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1774 | if (cp->val) { |
| 1775 | conn_changed = !test_and_set_bit(HCI_CONNECTABLE, |
| 1776 | &hdev->dev_flags); |
| 1777 | discov_changed = false; |
| 1778 | } else { |
| 1779 | conn_changed = test_and_clear_bit(HCI_CONNECTABLE, |
| 1780 | &hdev->dev_flags); |
| 1781 | discov_changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1782 | &hdev->dev_flags); |
| 1783 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1784 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1785 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1786 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1787 | if (conn_changed || discov_changed) { |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1788 | new_settings(hdev, cmd->sk); |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1789 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1790 | if (discov_changed) |
| 1791 | mgmt_update_adv_data(hdev); |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1792 | hci_update_background_scan(hdev); |
| 1793 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1794 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1795 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1796 | mgmt_pending_remove(cmd); |
| 1797 | |
| 1798 | unlock: |
| 1799 | hci_dev_unlock(hdev); |
| 1800 | } |
| 1801 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1802 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1803 | struct sock *sk, u8 val) |
| 1804 | { |
| 1805 | bool changed = false; |
| 1806 | int err; |
| 1807 | |
| 1808 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1809 | changed = true; |
| 1810 | |
| 1811 | if (val) { |
| 1812 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1813 | } else { |
| 1814 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1815 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1816 | } |
| 1817 | |
| 1818 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1819 | if (err < 0) |
| 1820 | return err; |
| 1821 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1822 | if (changed) { |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1823 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1824 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1825 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1826 | } |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1827 | |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1831 | 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] | 1832 | u16 len) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1833 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1834 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1835 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1836 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1837 | u8 scan; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1838 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1839 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1840 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1841 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1842 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1843 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1844 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1845 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1846 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1847 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1848 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1849 | MGMT_STATUS_INVALID_PARAMS); |
| 1850 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1851 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1852 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1853 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1854 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1855 | goto failed; |
| 1856 | } |
| 1857 | |
| 1858 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1859 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1860 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1861 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1862 | goto failed; |
| 1863 | } |
| 1864 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1865 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1866 | if (!cmd) { |
| 1867 | err = -ENOMEM; |
| 1868 | goto failed; |
| 1869 | } |
| 1870 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1871 | hci_req_init(&req, hdev); |
| 1872 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1873 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1874 | * by-product of disabling connectable, we need to update the |
| 1875 | * advertising flags. |
| 1876 | */ |
| 1877 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1878 | if (!cp->val) { |
| 1879 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1880 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1881 | } |
| 1882 | update_adv_data(&req); |
| 1883 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1884 | if (cp->val) { |
| 1885 | scan = SCAN_PAGE; |
| 1886 | } else { |
Johan Hedberg | 3bd2724 | 2014-07-28 20:53:58 +0300 | [diff] [blame] | 1887 | /* If we don't have any whitelist entries just |
| 1888 | * disable all scanning. If there are entries |
| 1889 | * and we had both page and inquiry scanning |
| 1890 | * enabled then fall back to only page scanning. |
| 1891 | * Otherwise no changes are needed. |
| 1892 | */ |
| 1893 | if (list_empty(&hdev->whitelist)) |
| 1894 | scan = SCAN_DISABLED; |
| 1895 | else if (test_bit(HCI_ISCAN, &hdev->flags)) |
| 1896 | scan = SCAN_PAGE; |
| 1897 | else |
| 1898 | goto no_scan_update; |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1899 | |
| 1900 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1901 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1902 | cancel_delayed_work(&hdev->discov_off); |
| 1903 | } |
| 1904 | |
| 1905 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1906 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1907 | |
Johan Hedberg | 3bd2724 | 2014-07-28 20:53:58 +0300 | [diff] [blame] | 1908 | no_scan_update: |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1909 | /* If we're going from non-connectable to connectable or |
| 1910 | * vice-versa when fast connectable is enabled ensure that fast |
| 1911 | * connectable gets disabled. write_fast_connectable won't do |
| 1912 | * anything if the page scan parameters are already what they |
| 1913 | * should be. |
| 1914 | */ |
| 1915 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1916 | write_fast_connectable(&req, false); |
| 1917 | |
Johan Hedberg | e8b1202 | 2014-07-10 10:51:27 +0300 | [diff] [blame] | 1918 | /* Update the advertising parameters if necessary */ |
| 1919 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1920 | enable_advertising(&req); |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1921 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1922 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1923 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1924 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1925 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1926 | err = set_connectable_update_settings(hdev, sk, |
| 1927 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1928 | goto failed; |
| 1929 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1930 | |
| 1931 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1932 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1933 | return err; |
| 1934 | } |
| 1935 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1936 | 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] | 1937 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1938 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1939 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1940 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1941 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1942 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1943 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1944 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1945 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1946 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1947 | MGMT_STATUS_INVALID_PARAMS); |
| 1948 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1949 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1950 | |
| 1951 | if (cp->val) |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1952 | changed = !test_and_set_bit(HCI_BONDABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1953 | else |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1954 | changed = test_and_clear_bit(HCI_BONDABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1955 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1956 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1957 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1958 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1959 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1960 | if (changed) |
| 1961 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1962 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1963 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1964 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1965 | return err; |
| 1966 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1967 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1968 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1969 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1970 | { |
| 1971 | struct mgmt_mode *cp = data; |
| 1972 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1973 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1974 | int err; |
| 1975 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1976 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1977 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1978 | status = mgmt_bredr_support(hdev); |
| 1979 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1980 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1981 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1982 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1983 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1984 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1985 | MGMT_STATUS_INVALID_PARAMS); |
| 1986 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1987 | hci_dev_lock(hdev); |
| 1988 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1989 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1990 | bool changed = false; |
| 1991 | |
| 1992 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1993 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1994 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1995 | changed = true; |
| 1996 | } |
| 1997 | |
| 1998 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1999 | if (err < 0) |
| 2000 | goto failed; |
| 2001 | |
| 2002 | if (changed) |
| 2003 | err = new_settings(hdev, sk); |
| 2004 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2005 | goto failed; |
| 2006 | } |
| 2007 | |
| 2008 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2009 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2010 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2011 | goto failed; |
| 2012 | } |
| 2013 | |
| 2014 | val = !!cp->val; |
| 2015 | |
| 2016 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 2017 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 2018 | goto failed; |
| 2019 | } |
| 2020 | |
| 2021 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 2022 | if (!cmd) { |
| 2023 | err = -ENOMEM; |
| 2024 | goto failed; |
| 2025 | } |
| 2026 | |
| 2027 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 2028 | if (err < 0) { |
| 2029 | mgmt_pending_remove(cmd); |
| 2030 | goto failed; |
| 2031 | } |
| 2032 | |
| 2033 | failed: |
| 2034 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2035 | return err; |
| 2036 | } |
| 2037 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2038 | 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] | 2039 | { |
| 2040 | struct mgmt_mode *cp = data; |
| 2041 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2042 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2043 | int err; |
| 2044 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2045 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2046 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 2047 | status = mgmt_bredr_support(hdev); |
| 2048 | if (status) |
| 2049 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 2050 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2051 | if (!lmp_ssp_capable(hdev)) |
| 2052 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2053 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2054 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2055 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2056 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2057 | MGMT_STATUS_INVALID_PARAMS); |
| 2058 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2059 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 2060 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2061 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2062 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2063 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2064 | if (cp->val) { |
| 2065 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 2066 | &hdev->dev_flags); |
| 2067 | } else { |
| 2068 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 2069 | &hdev->dev_flags); |
| 2070 | if (!changed) |
| 2071 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 2072 | &hdev->dev_flags); |
| 2073 | else |
| 2074 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2078 | if (err < 0) |
| 2079 | goto failed; |
| 2080 | |
| 2081 | if (changed) |
| 2082 | err = new_settings(hdev, sk); |
| 2083 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2084 | goto failed; |
| 2085 | } |
| 2086 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2087 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 2088 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 2089 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2090 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2091 | goto failed; |
| 2092 | } |
| 2093 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2094 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2095 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2096 | goto failed; |
| 2097 | } |
| 2098 | |
| 2099 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 2100 | if (!cmd) { |
| 2101 | err = -ENOMEM; |
| 2102 | goto failed; |
| 2103 | } |
| 2104 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 2105 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 2106 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2107 | sizeof(cp->val), &cp->val); |
| 2108 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2109 | 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] | 2110 | if (err < 0) { |
| 2111 | mgmt_pending_remove(cmd); |
| 2112 | goto failed; |
| 2113 | } |
| 2114 | |
| 2115 | failed: |
| 2116 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2117 | return err; |
| 2118 | } |
| 2119 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2120 | 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] | 2121 | { |
| 2122 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2123 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2124 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2125 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2126 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2127 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2128 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2129 | status = mgmt_bredr_support(hdev); |
| 2130 | if (status) |
| 2131 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2132 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2133 | if (!lmp_ssp_capable(hdev)) |
| 2134 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2135 | MGMT_STATUS_NOT_SUPPORTED); |
| 2136 | |
| 2137 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 2138 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2139 | MGMT_STATUS_REJECTED); |
| 2140 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2141 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2142 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2143 | MGMT_STATUS_INVALID_PARAMS); |
| 2144 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2145 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2146 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2147 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2148 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2149 | } else { |
| 2150 | if (hdev_is_powered(hdev)) { |
| 2151 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2152 | MGMT_STATUS_REJECTED); |
| 2153 | goto unlock; |
| 2154 | } |
| 2155 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2156 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2157 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2158 | |
| 2159 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2160 | if (err < 0) |
| 2161 | goto unlock; |
| 2162 | |
| 2163 | if (changed) |
| 2164 | err = new_settings(hdev, sk); |
| 2165 | |
| 2166 | unlock: |
| 2167 | hci_dev_unlock(hdev); |
| 2168 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2169 | } |
| 2170 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2171 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 2172 | { |
| 2173 | struct cmd_lookup match = { NULL, hdev }; |
| 2174 | |
| 2175 | if (status) { |
| 2176 | u8 mgmt_err = mgmt_status(status); |
| 2177 | |
| 2178 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2179 | &mgmt_err); |
| 2180 | return; |
| 2181 | } |
| 2182 | |
| 2183 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2184 | |
| 2185 | new_settings(hdev, match.sk); |
| 2186 | |
| 2187 | if (match.sk) |
| 2188 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2189 | |
| 2190 | /* Make sure the controller has a good default for |
| 2191 | * advertising data. Restrict the update to when LE |
| 2192 | * has actually been enabled. During power on, the |
| 2193 | * update in powered_update_hci will take care of it. |
| 2194 | */ |
| 2195 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2196 | struct hci_request req; |
| 2197 | |
| 2198 | hci_dev_lock(hdev); |
| 2199 | |
| 2200 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2201 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2202 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2203 | hci_req_run(&req, NULL); |
| 2204 | |
Johan Hedberg | a70f4b5 | 2014-07-07 15:19:50 +0300 | [diff] [blame] | 2205 | hci_update_background_scan(hdev); |
| 2206 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2207 | hci_dev_unlock(hdev); |
| 2208 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2209 | } |
| 2210 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2211 | 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] | 2212 | { |
| 2213 | struct mgmt_mode *cp = data; |
| 2214 | struct hci_cp_write_le_host_supported hci_cp; |
| 2215 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2216 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2217 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2218 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2219 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2220 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2221 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2222 | if (!lmp_le_capable(hdev)) |
| 2223 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2224 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2225 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2226 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2227 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2228 | MGMT_STATUS_INVALID_PARAMS); |
| 2229 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2230 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2231 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2232 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2233 | MGMT_STATUS_REJECTED); |
| 2234 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2235 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2236 | |
| 2237 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2238 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2239 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2240 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2241 | bool changed = false; |
| 2242 | |
| 2243 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2244 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2245 | changed = true; |
| 2246 | } |
| 2247 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2248 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2249 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2250 | changed = true; |
| 2251 | } |
| 2252 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2253 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2254 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2255 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2256 | |
| 2257 | if (changed) |
| 2258 | err = new_settings(hdev, sk); |
| 2259 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2260 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2263 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2264 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2265 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2266 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2267 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2268 | } |
| 2269 | |
| 2270 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2271 | if (!cmd) { |
| 2272 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2273 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2274 | } |
| 2275 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2276 | hci_req_init(&req, hdev); |
| 2277 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2278 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2279 | |
| 2280 | if (val) { |
| 2281 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 2282 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2283 | } else { |
Johan Hedberg | 73e082f | 2014-07-08 15:07:51 +0300 | [diff] [blame] | 2284 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2285 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2286 | } |
| 2287 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2288 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2289 | &hci_cp); |
| 2290 | |
| 2291 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2292 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2293 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2294 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2295 | unlock: |
| 2296 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2297 | return err; |
| 2298 | } |
| 2299 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2300 | /* This is a helper function to test for pending mgmt commands that can |
| 2301 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2302 | * mgmt command at a time since otherwise we cannot easily track what |
| 2303 | * the current values are, will be, and based on that calculate if a new |
| 2304 | * HCI command needs to be sent and if yes with what value. |
| 2305 | */ |
| 2306 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2307 | { |
| 2308 | struct pending_cmd *cmd; |
| 2309 | |
| 2310 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2311 | switch (cmd->opcode) { |
| 2312 | case MGMT_OP_ADD_UUID: |
| 2313 | case MGMT_OP_REMOVE_UUID: |
| 2314 | case MGMT_OP_SET_DEV_CLASS: |
| 2315 | case MGMT_OP_SET_POWERED: |
| 2316 | return true; |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | return false; |
| 2321 | } |
| 2322 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2323 | static const u8 bluetooth_base_uuid[] = { |
| 2324 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2325 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2326 | }; |
| 2327 | |
| 2328 | static u8 get_uuid_size(const u8 *uuid) |
| 2329 | { |
| 2330 | u32 val; |
| 2331 | |
| 2332 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2333 | return 128; |
| 2334 | |
| 2335 | val = get_unaligned_le32(&uuid[12]); |
| 2336 | if (val > 0xffff) |
| 2337 | return 32; |
| 2338 | |
| 2339 | return 16; |
| 2340 | } |
| 2341 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2342 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2343 | { |
| 2344 | struct pending_cmd *cmd; |
| 2345 | |
| 2346 | hci_dev_lock(hdev); |
| 2347 | |
| 2348 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2349 | if (!cmd) |
| 2350 | goto unlock; |
| 2351 | |
| 2352 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2353 | hdev->dev_class, 3); |
| 2354 | |
| 2355 | mgmt_pending_remove(cmd); |
| 2356 | |
| 2357 | unlock: |
| 2358 | hci_dev_unlock(hdev); |
| 2359 | } |
| 2360 | |
| 2361 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2362 | { |
| 2363 | BT_DBG("status 0x%02x", status); |
| 2364 | |
| 2365 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2366 | } |
| 2367 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2368 | 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] | 2369 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2370 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2371 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2372 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2373 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2374 | int err; |
| 2375 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2376 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2377 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2378 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2379 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2380 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2381 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2382 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2383 | goto failed; |
| 2384 | } |
| 2385 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2386 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2387 | if (!uuid) { |
| 2388 | err = -ENOMEM; |
| 2389 | goto failed; |
| 2390 | } |
| 2391 | |
| 2392 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2393 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2394 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2395 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2396 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2397 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2398 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2399 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2400 | update_class(&req); |
| 2401 | update_eir(&req); |
| 2402 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2403 | err = hci_req_run(&req, add_uuid_complete); |
| 2404 | if (err < 0) { |
| 2405 | if (err != -ENODATA) |
| 2406 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2407 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2408 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2409 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2410 | goto failed; |
| 2411 | } |
| 2412 | |
| 2413 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2414 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2415 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2416 | goto failed; |
| 2417 | } |
| 2418 | |
| 2419 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2420 | |
| 2421 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2422 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2423 | return err; |
| 2424 | } |
| 2425 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2426 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2427 | { |
| 2428 | if (!hdev_is_powered(hdev)) |
| 2429 | return false; |
| 2430 | |
| 2431 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2432 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2433 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2434 | return true; |
| 2435 | } |
| 2436 | |
| 2437 | return false; |
| 2438 | } |
| 2439 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2440 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2441 | { |
| 2442 | BT_DBG("status 0x%02x", status); |
| 2443 | |
| 2444 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2445 | } |
| 2446 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2447 | 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] | 2448 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2449 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2450 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2451 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2452 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2453 | 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] | 2454 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2455 | int err, found; |
| 2456 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2457 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2458 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2459 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2460 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2461 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2462 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2463 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2464 | goto unlock; |
| 2465 | } |
| 2466 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2467 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2468 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2469 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2470 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2471 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2472 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2473 | goto unlock; |
| 2474 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2475 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2476 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | found = 0; |
| 2480 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2481 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2482 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2483 | continue; |
| 2484 | |
| 2485 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2486 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2487 | found++; |
| 2488 | } |
| 2489 | |
| 2490 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2491 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2492 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2493 | goto unlock; |
| 2494 | } |
| 2495 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2496 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2497 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2498 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2499 | update_class(&req); |
| 2500 | update_eir(&req); |
| 2501 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2502 | err = hci_req_run(&req, remove_uuid_complete); |
| 2503 | if (err < 0) { |
| 2504 | if (err != -ENODATA) |
| 2505 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2506 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2507 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2508 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2509 | goto unlock; |
| 2510 | } |
| 2511 | |
| 2512 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2513 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2514 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2515 | goto unlock; |
| 2516 | } |
| 2517 | |
| 2518 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2519 | |
| 2520 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2521 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2522 | return err; |
| 2523 | } |
| 2524 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2525 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2526 | { |
| 2527 | BT_DBG("status 0x%02x", status); |
| 2528 | |
| 2529 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2530 | } |
| 2531 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2532 | 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] | 2533 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2534 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2535 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2536 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2537 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2538 | int err; |
| 2539 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2540 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2541 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2542 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2543 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2544 | MGMT_STATUS_NOT_SUPPORTED); |
| 2545 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2546 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2547 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2548 | if (pending_eir_or_class(hdev)) { |
| 2549 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2550 | MGMT_STATUS_BUSY); |
| 2551 | goto unlock; |
| 2552 | } |
| 2553 | |
| 2554 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2555 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2556 | MGMT_STATUS_INVALID_PARAMS); |
| 2557 | goto unlock; |
| 2558 | } |
| 2559 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2560 | hdev->major_class = cp->major; |
| 2561 | hdev->minor_class = cp->minor; |
| 2562 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2563 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2564 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2565 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2566 | goto unlock; |
| 2567 | } |
| 2568 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2569 | hci_req_init(&req, hdev); |
| 2570 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2571 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2572 | hci_dev_unlock(hdev); |
| 2573 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2574 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2575 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2576 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2577 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2578 | update_class(&req); |
| 2579 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2580 | err = hci_req_run(&req, set_class_complete); |
| 2581 | if (err < 0) { |
| 2582 | if (err != -ENODATA) |
| 2583 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2584 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2585 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2586 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2587 | goto unlock; |
| 2588 | } |
| 2589 | |
| 2590 | 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] | 2591 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2592 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2593 | goto unlock; |
| 2594 | } |
| 2595 | |
| 2596 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2597 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2598 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2599 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2600 | return err; |
| 2601 | } |
| 2602 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2603 | 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] | 2604 | u16 len) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2605 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2606 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2607 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2608 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2609 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2610 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2611 | int i; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2612 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2613 | BT_DBG("request for %s", hdev->name); |
| 2614 | |
| 2615 | if (!lmp_bredr_capable(hdev)) |
| 2616 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2617 | MGMT_STATUS_NOT_SUPPORTED); |
| 2618 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2619 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2620 | if (key_count > max_key_count) { |
| 2621 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2622 | key_count); |
| 2623 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2624 | MGMT_STATUS_INVALID_PARAMS); |
| 2625 | } |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2626 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2627 | expected_len = sizeof(*cp) + key_count * |
| 2628 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2629 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2630 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2631 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2632 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2633 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2634 | } |
| 2635 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2636 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2637 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2638 | MGMT_STATUS_INVALID_PARAMS); |
| 2639 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2640 | 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] | 2641 | key_count); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2642 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2643 | for (i = 0; i < key_count; i++) { |
| 2644 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2645 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2646 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2647 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2648 | MGMT_STATUS_INVALID_PARAMS); |
| 2649 | } |
| 2650 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2651 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2652 | |
| 2653 | hci_link_keys_clear(hdev); |
| 2654 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2655 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2656 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2657 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2658 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2659 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2660 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2661 | |
| 2662 | if (changed) |
| 2663 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2664 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2665 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2666 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2667 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2668 | /* Always ignore debug keys and require a new pairing if |
| 2669 | * the user wants to use them. |
| 2670 | */ |
| 2671 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2672 | continue; |
| 2673 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2674 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2675 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2676 | } |
| 2677 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2678 | 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] | 2679 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2680 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2681 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2682 | return 0; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2683 | } |
| 2684 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2685 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2686 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2687 | { |
| 2688 | struct mgmt_ev_device_unpaired ev; |
| 2689 | |
| 2690 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2691 | ev.addr.type = addr_type; |
| 2692 | |
| 2693 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2694 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2695 | } |
| 2696 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2697 | 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] | 2698 | u16 len) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2699 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2700 | struct mgmt_cp_unpair_device *cp = data; |
| 2701 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2702 | struct hci_cp_disconnect dc; |
| 2703 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2704 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2705 | int err; |
| 2706 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2707 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2708 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2709 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2710 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2711 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2712 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2713 | MGMT_STATUS_INVALID_PARAMS, |
| 2714 | &rp, sizeof(rp)); |
| 2715 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2716 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2717 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2718 | MGMT_STATUS_INVALID_PARAMS, |
| 2719 | &rp, sizeof(rp)); |
| 2720 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2721 | hci_dev_lock(hdev); |
| 2722 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2723 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2724 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2725 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2726 | goto unlock; |
| 2727 | } |
| 2728 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2729 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2730 | /* If disconnection is requested, then look up the |
| 2731 | * connection. If the remote device is connected, it |
| 2732 | * will be later used to terminate the link. |
| 2733 | * |
| 2734 | * Setting it to NULL explicitly will cause no |
| 2735 | * termination of the link. |
| 2736 | */ |
| 2737 | if (cp->disconnect) |
| 2738 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2739 | &cp->addr.bdaddr); |
| 2740 | else |
| 2741 | conn = NULL; |
| 2742 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2743 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2744 | } else { |
| 2745 | u8 addr_type; |
| 2746 | |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2747 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
| 2748 | &cp->addr.bdaddr); |
| 2749 | if (conn) { |
| 2750 | /* Defer clearing up the connection parameters |
| 2751 | * until closing to give a chance of keeping |
| 2752 | * them if a repairing happens. |
| 2753 | */ |
| 2754 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2755 | |
| 2756 | /* If disconnection is not requested, then |
| 2757 | * clear the connection variable so that the |
| 2758 | * link is not terminated. |
| 2759 | */ |
| 2760 | if (!cp->disconnect) |
| 2761 | conn = NULL; |
| 2762 | } |
| 2763 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2764 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2765 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2766 | else |
| 2767 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2768 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2769 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2770 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2771 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2772 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2773 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2774 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2775 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2776 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2777 | goto unlock; |
| 2778 | } |
| 2779 | |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2780 | /* If the connection variable is set, then termination of the |
| 2781 | * link is requested. |
| 2782 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2783 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2784 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2785 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2786 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2787 | goto unlock; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2788 | } |
| 2789 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2790 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2791 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2792 | if (!cmd) { |
| 2793 | err = -ENOMEM; |
| 2794 | goto unlock; |
| 2795 | } |
| 2796 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2797 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2798 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2799 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2800 | if (err < 0) |
| 2801 | mgmt_pending_remove(cmd); |
| 2802 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2803 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2804 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2805 | return err; |
| 2806 | } |
| 2807 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2808 | 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] | 2809 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2810 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2811 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2812 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2813 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2814 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2815 | int err; |
| 2816 | |
| 2817 | BT_DBG(""); |
| 2818 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2819 | memset(&rp, 0, sizeof(rp)); |
| 2820 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2821 | rp.addr.type = cp->addr.type; |
| 2822 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2823 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2824 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2825 | MGMT_STATUS_INVALID_PARAMS, |
| 2826 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2827 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2828 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2829 | |
| 2830 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2831 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2832 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2833 | goto failed; |
| 2834 | } |
| 2835 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2836 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2837 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2838 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2839 | goto failed; |
| 2840 | } |
| 2841 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2842 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2843 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2844 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2845 | else |
| 2846 | 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] | 2847 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2848 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2849 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2850 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2851 | goto failed; |
| 2852 | } |
| 2853 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2854 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2855 | if (!cmd) { |
| 2856 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2857 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2858 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2859 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2860 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2861 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2862 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2863 | |
| 2864 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2865 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2866 | return err; |
| 2867 | } |
| 2868 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2869 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2870 | { |
| 2871 | switch (link_type) { |
| 2872 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2873 | switch (addr_type) { |
| 2874 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2875 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2876 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2877 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2878 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2879 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2880 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2881 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2882 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2883 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2884 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2885 | } |
| 2886 | } |
| 2887 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2888 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2889 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2890 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2891 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2892 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2893 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2894 | int err; |
| 2895 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2896 | |
| 2897 | BT_DBG(""); |
| 2898 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2899 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2900 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2901 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2902 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2903 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2904 | goto unlock; |
| 2905 | } |
| 2906 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2907 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2908 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2909 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2910 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2911 | } |
| 2912 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2913 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2914 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2915 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2916 | err = -ENOMEM; |
| 2917 | goto unlock; |
| 2918 | } |
| 2919 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2920 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2921 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2922 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2923 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2924 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2925 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2926 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2927 | continue; |
| 2928 | i++; |
| 2929 | } |
| 2930 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2931 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2932 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2933 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2934 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2935 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2936 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2937 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2938 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2939 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2940 | |
| 2941 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2942 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2943 | return err; |
| 2944 | } |
| 2945 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2946 | 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] | 2947 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2948 | { |
| 2949 | struct pending_cmd *cmd; |
| 2950 | int err; |
| 2951 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2952 | 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] | 2953 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2954 | if (!cmd) |
| 2955 | return -ENOMEM; |
| 2956 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2957 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2958 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2959 | if (err < 0) |
| 2960 | mgmt_pending_remove(cmd); |
| 2961 | |
| 2962 | return err; |
| 2963 | } |
| 2964 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2965 | 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] | 2966 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2967 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2968 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2969 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2970 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2971 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2972 | int err; |
| 2973 | |
| 2974 | BT_DBG(""); |
| 2975 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2976 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2977 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2978 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2979 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2980 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2981 | goto failed; |
| 2982 | } |
| 2983 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2984 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2985 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2986 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2987 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2988 | goto failed; |
| 2989 | } |
| 2990 | |
| 2991 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2992 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2993 | |
| 2994 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2995 | |
| 2996 | BT_ERR("PIN code is not 16 bytes long"); |
| 2997 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2998 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2999 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3000 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3001 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3002 | |
| 3003 | goto failed; |
| 3004 | } |
| 3005 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 3006 | 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] | 3007 | if (!cmd) { |
| 3008 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3009 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 3010 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3011 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3012 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3013 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 3014 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3015 | |
| 3016 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 3017 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3018 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3019 | |
| 3020 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3021 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3022 | return err; |
| 3023 | } |
| 3024 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3025 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3026 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3027 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3028 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3029 | |
| 3030 | BT_DBG(""); |
| 3031 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3032 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 3033 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 3034 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 3035 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3036 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3037 | |
| 3038 | hdev->io_capability = cp->io_capability; |
| 3039 | |
| 3040 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3041 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3042 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3043 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3044 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3045 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 3046 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3047 | } |
| 3048 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3049 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3050 | { |
| 3051 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3052 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3053 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3054 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3055 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 3056 | continue; |
| 3057 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3058 | if (cmd->user_data != conn) |
| 3059 | continue; |
| 3060 | |
| 3061 | return cmd; |
| 3062 | } |
| 3063 | |
| 3064 | return NULL; |
| 3065 | } |
| 3066 | |
| 3067 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 3068 | { |
| 3069 | struct mgmt_rp_pair_device rp; |
| 3070 | struct hci_conn *conn = cmd->user_data; |
| 3071 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 3072 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 3073 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3074 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3075 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3076 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3077 | |
| 3078 | /* So we don't get further callbacks for this connection */ |
| 3079 | conn->connect_cfm_cb = NULL; |
| 3080 | conn->security_cfm_cb = NULL; |
| 3081 | conn->disconn_cfm_cb = NULL; |
| 3082 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3083 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3084 | hci_conn_put(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3085 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3086 | mgmt_pending_remove(cmd); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 3087 | |
| 3088 | /* The device is paired so there is no need to remove |
| 3089 | * its connection parameters anymore. |
| 3090 | */ |
| 3091 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3092 | } |
| 3093 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3094 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 3095 | { |
| 3096 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 3097 | struct pending_cmd *cmd; |
| 3098 | |
| 3099 | cmd = find_pairing(conn); |
| 3100 | if (cmd) |
| 3101 | pairing_complete(cmd, status); |
| 3102 | } |
| 3103 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3104 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 3105 | { |
| 3106 | struct pending_cmd *cmd; |
| 3107 | |
| 3108 | BT_DBG("status %u", status); |
| 3109 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3110 | cmd = find_pairing(conn); |
| 3111 | if (!cmd) |
| 3112 | BT_DBG("Unable to find a pending command"); |
| 3113 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 3114 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3115 | } |
| 3116 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3117 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3118 | { |
| 3119 | struct pending_cmd *cmd; |
| 3120 | |
| 3121 | BT_DBG("status %u", status); |
| 3122 | |
| 3123 | if (!status) |
| 3124 | return; |
| 3125 | |
| 3126 | cmd = find_pairing(conn); |
| 3127 | if (!cmd) |
| 3128 | BT_DBG("Unable to find a pending command"); |
| 3129 | else |
| 3130 | pairing_complete(cmd, mgmt_status(status)); |
| 3131 | } |
| 3132 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3133 | 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] | 3134 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3135 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3136 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3137 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3138 | struct pending_cmd *cmd; |
| 3139 | u8 sec_level, auth_type; |
| 3140 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3141 | int err; |
| 3142 | |
| 3143 | BT_DBG(""); |
| 3144 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3145 | memset(&rp, 0, sizeof(rp)); |
| 3146 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3147 | rp.addr.type = cp->addr.type; |
| 3148 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3149 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 3150 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3151 | MGMT_STATUS_INVALID_PARAMS, |
| 3152 | &rp, sizeof(rp)); |
| 3153 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3154 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 3155 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3156 | MGMT_STATUS_INVALID_PARAMS, |
| 3157 | &rp, sizeof(rp)); |
| 3158 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3159 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3160 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3161 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3162 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3163 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3164 | goto unlock; |
| 3165 | } |
| 3166 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3167 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3168 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3169 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3170 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3171 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3172 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3173 | } else { |
| 3174 | u8 addr_type; |
| 3175 | |
| 3176 | /* Convert from L2CAP channel address type to HCI address type |
| 3177 | */ |
| 3178 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3179 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3180 | else |
| 3181 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3182 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3183 | /* When pairing a new device, it is expected to remember |
| 3184 | * this device for future connections. Adding the connection |
| 3185 | * parameter information ahead of time allows tracking |
| 3186 | * of the slave preferred values and will speed up any |
| 3187 | * further connection establishment. |
| 3188 | * |
| 3189 | * If connection parameters already exist, then they |
| 3190 | * will be kept and this function does nothing. |
| 3191 | */ |
| 3192 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3193 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3194 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 3195 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 3196 | HCI_ROLE_MASTER); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3197 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3198 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3199 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3200 | int status; |
| 3201 | |
| 3202 | if (PTR_ERR(conn) == -EBUSY) |
| 3203 | status = MGMT_STATUS_BUSY; |
| 3204 | else |
| 3205 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3206 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3207 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3208 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3209 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3210 | goto unlock; |
| 3211 | } |
| 3212 | |
| 3213 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3214 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3215 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3216 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3217 | goto unlock; |
| 3218 | } |
| 3219 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3220 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3221 | if (!cmd) { |
| 3222 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3223 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3224 | goto unlock; |
| 3225 | } |
| 3226 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3227 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3228 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3229 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3230 | conn->security_cfm_cb = pairing_complete_cb; |
| 3231 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3232 | } else { |
| 3233 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3234 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3235 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3236 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3237 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3238 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3239 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3240 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3241 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 3242 | hci_conn_security(conn, sec_level, auth_type, true)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3243 | pairing_complete(cmd, 0); |
| 3244 | |
| 3245 | err = 0; |
| 3246 | |
| 3247 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3248 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3249 | return err; |
| 3250 | } |
| 3251 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3252 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3253 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3254 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3255 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3256 | struct pending_cmd *cmd; |
| 3257 | struct hci_conn *conn; |
| 3258 | int err; |
| 3259 | |
| 3260 | BT_DBG(""); |
| 3261 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3262 | hci_dev_lock(hdev); |
| 3263 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3264 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3265 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3266 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3267 | goto unlock; |
| 3268 | } |
| 3269 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3270 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3271 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3272 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3273 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3274 | goto unlock; |
| 3275 | } |
| 3276 | |
| 3277 | conn = cmd->user_data; |
| 3278 | |
| 3279 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3280 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3281 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3282 | goto unlock; |
| 3283 | } |
| 3284 | |
| 3285 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3286 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3287 | err = cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3288 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3289 | unlock: |
| 3290 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3291 | return err; |
| 3292 | } |
| 3293 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3294 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3295 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3296 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3297 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3298 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3299 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3300 | int err; |
| 3301 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3302 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3303 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3304 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3305 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3306 | MGMT_STATUS_NOT_POWERED, addr, |
| 3307 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3308 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3309 | } |
| 3310 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3311 | if (addr->type == BDADDR_BREDR) |
| 3312 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3313 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3314 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3315 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3316 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3317 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3318 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3319 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3320 | goto done; |
| 3321 | } |
| 3322 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3323 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3324 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3325 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3326 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3327 | MGMT_STATUS_SUCCESS, addr, |
| 3328 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3329 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3330 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3331 | MGMT_STATUS_FAILED, addr, |
| 3332 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3333 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3334 | goto done; |
| 3335 | } |
| 3336 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3337 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3338 | if (!cmd) { |
| 3339 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3340 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3341 | } |
| 3342 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3343 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3344 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3345 | struct hci_cp_user_passkey_reply cp; |
| 3346 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3347 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3348 | cp.passkey = passkey; |
| 3349 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3350 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3351 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3352 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3353 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3354 | if (err < 0) |
| 3355 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3356 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3357 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3358 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3359 | return err; |
| 3360 | } |
| 3361 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3362 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3363 | void *data, u16 len) |
| 3364 | { |
| 3365 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3366 | |
| 3367 | BT_DBG(""); |
| 3368 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3369 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3370 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3371 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3372 | } |
| 3373 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3374 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3375 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3376 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3377 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3378 | |
| 3379 | BT_DBG(""); |
| 3380 | |
| 3381 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3382 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3383 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3384 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3385 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3386 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3387 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3388 | } |
| 3389 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3390 | 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] | 3391 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3392 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3393 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3394 | |
| 3395 | BT_DBG(""); |
| 3396 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3397 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3398 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3399 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3400 | } |
| 3401 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3402 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3403 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3404 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3405 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3406 | |
| 3407 | BT_DBG(""); |
| 3408 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3409 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3410 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3411 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3412 | } |
| 3413 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3414 | 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] | 3415 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3416 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3417 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3418 | |
| 3419 | BT_DBG(""); |
| 3420 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3421 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3422 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3423 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3424 | } |
| 3425 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3426 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3427 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3428 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3429 | struct hci_cp_write_local_name cp; |
| 3430 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3431 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3432 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3433 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3434 | } |
| 3435 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3436 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3437 | { |
| 3438 | struct mgmt_cp_set_local_name *cp; |
| 3439 | struct pending_cmd *cmd; |
| 3440 | |
| 3441 | BT_DBG("status 0x%02x", status); |
| 3442 | |
| 3443 | hci_dev_lock(hdev); |
| 3444 | |
| 3445 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3446 | if (!cmd) |
| 3447 | goto unlock; |
| 3448 | |
| 3449 | cp = cmd->param; |
| 3450 | |
| 3451 | if (status) |
| 3452 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3453 | mgmt_status(status)); |
| 3454 | else |
| 3455 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3456 | cp, sizeof(*cp)); |
| 3457 | |
| 3458 | mgmt_pending_remove(cmd); |
| 3459 | |
| 3460 | unlock: |
| 3461 | hci_dev_unlock(hdev); |
| 3462 | } |
| 3463 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3464 | 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] | 3465 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3466 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3467 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3468 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3469 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3470 | int err; |
| 3471 | |
| 3472 | BT_DBG(""); |
| 3473 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3474 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3475 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3476 | /* If the old values are the same as the new ones just return a |
| 3477 | * direct command complete event. |
| 3478 | */ |
| 3479 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3480 | !memcmp(hdev->short_name, cp->short_name, |
| 3481 | sizeof(hdev->short_name))) { |
| 3482 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3483 | data, len); |
| 3484 | goto failed; |
| 3485 | } |
| 3486 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3487 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3488 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3489 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3490 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3491 | |
| 3492 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3493 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3494 | if (err < 0) |
| 3495 | goto failed; |
| 3496 | |
| 3497 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3498 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3499 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3500 | goto failed; |
| 3501 | } |
| 3502 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3503 | 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] | 3504 | if (!cmd) { |
| 3505 | err = -ENOMEM; |
| 3506 | goto failed; |
| 3507 | } |
| 3508 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3509 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3510 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3511 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3512 | |
| 3513 | if (lmp_bredr_capable(hdev)) { |
| 3514 | update_name(&req); |
| 3515 | update_eir(&req); |
| 3516 | } |
| 3517 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3518 | /* The name is stored in the scan response data and so |
| 3519 | * no need to udpate the advertising data here. |
| 3520 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3521 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3522 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3523 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3524 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3525 | if (err < 0) |
| 3526 | mgmt_pending_remove(cmd); |
| 3527 | |
| 3528 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3529 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3530 | return err; |
| 3531 | } |
| 3532 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3533 | 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] | 3534 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3535 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3536 | struct pending_cmd *cmd; |
| 3537 | int err; |
| 3538 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3539 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3540 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3541 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3542 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3543 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3544 | err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3545 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3546 | goto unlock; |
| 3547 | } |
| 3548 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3549 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3550 | err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3551 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3552 | goto unlock; |
| 3553 | } |
| 3554 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3555 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3556 | err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3557 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3558 | goto unlock; |
| 3559 | } |
| 3560 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3561 | 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] | 3562 | if (!cmd) { |
| 3563 | err = -ENOMEM; |
| 3564 | goto unlock; |
| 3565 | } |
| 3566 | |
Johan Hedberg | 710f11c | 2014-05-26 11:21:22 +0300 | [diff] [blame] | 3567 | if (bredr_sc_enabled(hdev)) |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3568 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3569 | 0, NULL); |
| 3570 | else |
| 3571 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3572 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3573 | if (err < 0) |
| 3574 | mgmt_pending_remove(cmd); |
| 3575 | |
| 3576 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3577 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3578 | return err; |
| 3579 | } |
| 3580 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3581 | 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] | 3582 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3583 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3584 | int err; |
| 3585 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3586 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3587 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3588 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3589 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3590 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3591 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3592 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3593 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3594 | if (cp->addr.type != BDADDR_BREDR) { |
| 3595 | err = cmd_complete(sk, hdev->id, |
| 3596 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3597 | MGMT_STATUS_INVALID_PARAMS, |
| 3598 | &cp->addr, sizeof(cp->addr)); |
| 3599 | goto unlock; |
| 3600 | } |
| 3601 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3602 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3603 | cp->addr.type, cp->hash, |
| 3604 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3605 | if (err < 0) |
| 3606 | status = MGMT_STATUS_FAILED; |
| 3607 | else |
| 3608 | status = MGMT_STATUS_SUCCESS; |
| 3609 | |
| 3610 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3611 | status, &cp->addr, sizeof(cp->addr)); |
| 3612 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3613 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3614 | u8 *rand192, *hash192; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3615 | u8 status; |
| 3616 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3617 | if (cp->addr.type != BDADDR_BREDR) { |
| 3618 | err = cmd_complete(sk, hdev->id, |
| 3619 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3620 | MGMT_STATUS_INVALID_PARAMS, |
| 3621 | &cp->addr, sizeof(cp->addr)); |
| 3622 | goto unlock; |
| 3623 | } |
| 3624 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3625 | if (bdaddr_type_is_le(cp->addr.type)) { |
| 3626 | rand192 = NULL; |
| 3627 | hash192 = NULL; |
| 3628 | } else { |
| 3629 | rand192 = cp->rand192; |
| 3630 | hash192 = cp->hash192; |
| 3631 | } |
| 3632 | |
Johan Hedberg | 81328d5 | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 3633 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3634 | cp->addr.type, hash192, rand192, |
| 3635 | cp->hash256, cp->rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3636 | if (err < 0) |
| 3637 | status = MGMT_STATUS_FAILED; |
| 3638 | else |
| 3639 | status = MGMT_STATUS_SUCCESS; |
| 3640 | |
| 3641 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3642 | status, &cp->addr, sizeof(cp->addr)); |
| 3643 | } else { |
| 3644 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3645 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3646 | MGMT_STATUS_INVALID_PARAMS); |
| 3647 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3648 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3649 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3650 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3651 | return err; |
| 3652 | } |
| 3653 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3654 | 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] | 3655 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3656 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3657 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3658 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3659 | int err; |
| 3660 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3661 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3662 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3663 | if (cp->addr.type != BDADDR_BREDR) |
| 3664 | return cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3665 | MGMT_STATUS_INVALID_PARAMS, |
| 3666 | &cp->addr, sizeof(cp->addr)); |
| 3667 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3668 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3669 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3670 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 3671 | hci_remote_oob_data_clear(hdev); |
| 3672 | status = MGMT_STATUS_SUCCESS; |
| 3673 | goto done; |
| 3674 | } |
| 3675 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3676 | 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] | 3677 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3678 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3679 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3680 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3681 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3682 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3683 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3684 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3685 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3686 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3687 | return err; |
| 3688 | } |
| 3689 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3690 | static bool trigger_discovery(struct hci_request *req, u8 *status) |
| 3691 | { |
| 3692 | struct hci_dev *hdev = req->hdev; |
| 3693 | struct hci_cp_le_set_scan_param param_cp; |
| 3694 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3695 | struct hci_cp_inquiry inq_cp; |
| 3696 | /* General inquiry access code (GIAC) */ |
| 3697 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 3698 | u8 own_addr_type; |
| 3699 | int err; |
| 3700 | |
| 3701 | switch (hdev->discovery.type) { |
| 3702 | case DISCOV_TYPE_BREDR: |
| 3703 | *status = mgmt_bredr_support(hdev); |
| 3704 | if (*status) |
| 3705 | return false; |
| 3706 | |
| 3707 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3708 | *status = MGMT_STATUS_BUSY; |
| 3709 | return false; |
| 3710 | } |
| 3711 | |
| 3712 | hci_inquiry_cache_flush(hdev); |
| 3713 | |
| 3714 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3715 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
| 3716 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
| 3717 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
| 3718 | break; |
| 3719 | |
| 3720 | case DISCOV_TYPE_LE: |
| 3721 | case DISCOV_TYPE_INTERLEAVED: |
| 3722 | *status = mgmt_le_support(hdev); |
| 3723 | if (*status) |
| 3724 | return false; |
| 3725 | |
| 3726 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
| 3727 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 3728 | *status = MGMT_STATUS_NOT_SUPPORTED; |
| 3729 | return false; |
| 3730 | } |
| 3731 | |
| 3732 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) { |
| 3733 | /* Don't let discovery abort an outgoing |
| 3734 | * connection attempt that's using directed |
| 3735 | * advertising. |
| 3736 | */ |
| 3737 | if (hci_conn_hash_lookup_state(hdev, LE_LINK, |
| 3738 | BT_CONNECT)) { |
| 3739 | *status = MGMT_STATUS_REJECTED; |
| 3740 | return false; |
| 3741 | } |
| 3742 | |
| 3743 | disable_advertising(req); |
| 3744 | } |
| 3745 | |
| 3746 | /* If controller is scanning, it means the background scanning |
| 3747 | * is running. Thus, we should temporarily stop it in order to |
| 3748 | * set the discovery scanning parameters. |
| 3749 | */ |
| 3750 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3751 | hci_req_add_le_scan_disable(req); |
| 3752 | |
| 3753 | memset(¶m_cp, 0, sizeof(param_cp)); |
| 3754 | |
| 3755 | /* All active scans will be done with either a resolvable |
| 3756 | * private address (when privacy feature has been enabled) |
| 3757 | * or unresolvable private address. |
| 3758 | */ |
| 3759 | err = hci_update_random_address(req, true, &own_addr_type); |
| 3760 | if (err < 0) { |
| 3761 | *status = MGMT_STATUS_FAILED; |
| 3762 | return false; |
| 3763 | } |
| 3764 | |
| 3765 | param_cp.type = LE_SCAN_ACTIVE; |
| 3766 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3767 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
| 3768 | param_cp.own_address_type = own_addr_type; |
| 3769 | hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3770 | ¶m_cp); |
| 3771 | |
| 3772 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3773 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3774 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3775 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3776 | &enable_cp); |
| 3777 | break; |
| 3778 | |
| 3779 | default: |
| 3780 | *status = MGMT_STATUS_INVALID_PARAMS; |
| 3781 | return false; |
| 3782 | } |
| 3783 | |
| 3784 | return true; |
| 3785 | } |
| 3786 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3787 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3788 | { |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3789 | struct pending_cmd *cmd; |
| 3790 | unsigned long timeout; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3791 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3792 | BT_DBG("status %d", status); |
| 3793 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3794 | hci_dev_lock(hdev); |
| 3795 | |
| 3796 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3797 | if (!cmd) |
| 3798 | cmd = mgmt_pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); |
| 3799 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3800 | if (cmd) { |
| 3801 | u8 type = hdev->discovery.type; |
| 3802 | |
| 3803 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, |
| 3804 | mgmt_status(status), &type, sizeof(type)); |
| 3805 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3806 | } |
| 3807 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3808 | if (status) { |
| 3809 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3810 | goto unlock; |
| 3811 | } |
| 3812 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3813 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3814 | |
| 3815 | switch (hdev->discovery.type) { |
| 3816 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3817 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3818 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3819 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3820 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3821 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3822 | case DISCOV_TYPE_BREDR: |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3823 | timeout = 0; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3824 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3825 | default: |
| 3826 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3827 | timeout = 0; |
| 3828 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3829 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3830 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3831 | if (timeout) |
| 3832 | queue_delayed_work(hdev->workqueue, |
| 3833 | &hdev->le_scan_disable, timeout); |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3834 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3835 | unlock: |
| 3836 | hci_dev_unlock(hdev); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3837 | } |
| 3838 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3839 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3840 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3841 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3842 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3843 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3844 | struct hci_request req; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3845 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3846 | int err; |
| 3847 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3848 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3849 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3850 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3851 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3852 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | a736abc | 2014-11-03 14:20:56 +0100 | [diff] [blame] | 3853 | err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3854 | MGMT_STATUS_NOT_POWERED, |
| 3855 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3856 | goto failed; |
| 3857 | } |
| 3858 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3859 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
| 3860 | test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
Szymon Janc | a736abc | 2014-11-03 14:20:56 +0100 | [diff] [blame] | 3861 | err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3862 | MGMT_STATUS_BUSY, &cp->type, |
| 3863 | sizeof(cp->type)); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3864 | goto failed; |
| 3865 | } |
| 3866 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3867 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3868 | if (!cmd) { |
| 3869 | err = -ENOMEM; |
| 3870 | goto failed; |
| 3871 | } |
| 3872 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame^] | 3873 | /* Clear the discovery filter first to free any previously |
| 3874 | * allocated memory for the UUID list. |
| 3875 | */ |
| 3876 | hci_discovery_filter_clear(hdev); |
| 3877 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3878 | hdev->discovery.type = cp->type; |
| 3879 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3880 | hci_req_init(&req, hdev); |
| 3881 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3882 | if (!trigger_discovery(&req, &status)) { |
Szymon Janc | a736abc | 2014-11-03 14:20:56 +0100 | [diff] [blame] | 3883 | err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame] | 3884 | status, &cp->type, sizeof(cp->type)); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3885 | mgmt_pending_remove(cmd); |
| 3886 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3887 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3888 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3889 | err = hci_req_run(&req, start_discovery_complete); |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3890 | if (err < 0) { |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3891 | mgmt_pending_remove(cmd); |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3892 | goto failed; |
| 3893 | } |
| 3894 | |
| 3895 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3896 | |
| 3897 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3898 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3899 | return err; |
| 3900 | } |
| 3901 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3902 | static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, |
| 3903 | void *data, u16 len) |
| 3904 | { |
| 3905 | struct mgmt_cp_start_service_discovery *cp = data; |
| 3906 | struct pending_cmd *cmd; |
| 3907 | struct hci_request req; |
| 3908 | const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); |
| 3909 | u16 uuid_count, expected_len; |
| 3910 | u8 status; |
| 3911 | int err; |
| 3912 | |
| 3913 | BT_DBG("%s", hdev->name); |
| 3914 | |
| 3915 | hci_dev_lock(hdev); |
| 3916 | |
| 3917 | if (!hdev_is_powered(hdev)) { |
| 3918 | err = cmd_complete(sk, hdev->id, |
| 3919 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3920 | MGMT_STATUS_NOT_POWERED, |
| 3921 | &cp->type, sizeof(cp->type)); |
| 3922 | goto failed; |
| 3923 | } |
| 3924 | |
| 3925 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
| 3926 | test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3927 | err = cmd_complete(sk, hdev->id, |
| 3928 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3929 | MGMT_STATUS_BUSY, &cp->type, |
| 3930 | sizeof(cp->type)); |
| 3931 | goto failed; |
| 3932 | } |
| 3933 | |
| 3934 | uuid_count = __le16_to_cpu(cp->uuid_count); |
| 3935 | if (uuid_count > max_uuid_count) { |
| 3936 | BT_ERR("service_discovery: too big uuid_count value %u", |
| 3937 | uuid_count); |
| 3938 | err = cmd_complete(sk, hdev->id, |
| 3939 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3940 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 3941 | sizeof(cp->type)); |
| 3942 | goto failed; |
| 3943 | } |
| 3944 | |
| 3945 | expected_len = sizeof(*cp) + uuid_count * 16; |
| 3946 | if (expected_len != len) { |
| 3947 | BT_ERR("service_discovery: expected %u bytes, got %u bytes", |
| 3948 | expected_len, len); |
| 3949 | err = cmd_complete(sk, hdev->id, |
| 3950 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3951 | MGMT_STATUS_INVALID_PARAMS, &cp->type, |
| 3952 | sizeof(cp->type)); |
| 3953 | goto failed; |
| 3954 | } |
| 3955 | |
| 3956 | cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY, |
| 3957 | hdev, NULL, 0); |
| 3958 | if (!cmd) { |
| 3959 | err = -ENOMEM; |
| 3960 | goto failed; |
| 3961 | } |
| 3962 | |
Marcel Holtmann | 2207880 | 2014-12-05 11:45:22 +0100 | [diff] [blame^] | 3963 | /* Clear the discovery filter first to free any previously |
| 3964 | * allocated memory for the UUID list. |
| 3965 | */ |
| 3966 | hci_discovery_filter_clear(hdev); |
| 3967 | |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 3968 | hdev->discovery.type = cp->type; |
| 3969 | hdev->discovery.rssi = cp->rssi; |
| 3970 | hdev->discovery.uuid_count = uuid_count; |
| 3971 | |
| 3972 | if (uuid_count > 0) { |
| 3973 | hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, |
| 3974 | GFP_KERNEL); |
| 3975 | if (!hdev->discovery.uuids) { |
| 3976 | err = cmd_complete(sk, hdev->id, |
| 3977 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3978 | MGMT_STATUS_FAILED, |
| 3979 | &cp->type, sizeof(cp->type)); |
| 3980 | mgmt_pending_remove(cmd); |
| 3981 | goto failed; |
| 3982 | } |
| 3983 | } |
| 3984 | |
| 3985 | hci_req_init(&req, hdev); |
| 3986 | |
| 3987 | if (!trigger_discovery(&req, &status)) { |
| 3988 | err = cmd_complete(sk, hdev->id, |
| 3989 | MGMT_OP_START_SERVICE_DISCOVERY, |
| 3990 | status, &cp->type, sizeof(cp->type)); |
| 3991 | mgmt_pending_remove(cmd); |
| 3992 | goto failed; |
| 3993 | } |
| 3994 | |
| 3995 | err = hci_req_run(&req, start_discovery_complete); |
| 3996 | if (err < 0) { |
| 3997 | mgmt_pending_remove(cmd); |
| 3998 | goto failed; |
| 3999 | } |
| 4000 | |
| 4001 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
| 4002 | |
| 4003 | failed: |
| 4004 | hci_dev_unlock(hdev); |
| 4005 | return err; |
| 4006 | } |
| 4007 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4008 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 4009 | { |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4010 | struct pending_cmd *cmd; |
| 4011 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4012 | BT_DBG("status %d", status); |
| 4013 | |
| 4014 | hci_dev_lock(hdev); |
| 4015 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4016 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 4017 | if (cmd) { |
| 4018 | u8 type = hdev->discovery.type; |
| 4019 | |
| 4020 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, |
| 4021 | mgmt_status(status), &type, sizeof(type)); |
| 4022 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4023 | } |
| 4024 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 4025 | if (!status) |
| 4026 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4027 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4028 | hci_dev_unlock(hdev); |
| 4029 | } |
| 4030 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4031 | 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] | 4032 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4033 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4034 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4035 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4036 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4037 | int err; |
| 4038 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4039 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4040 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4041 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4042 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4043 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4044 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4045 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 4046 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 4047 | goto unlock; |
| 4048 | } |
| 4049 | |
| 4050 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4051 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4052 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 4053 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4054 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 4055 | } |
| 4056 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4057 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4058 | if (!cmd) { |
| 4059 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4060 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4061 | } |
| 4062 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4063 | hci_req_init(&req, hdev); |
| 4064 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4065 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4066 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4067 | err = hci_req_run(&req, stop_discovery_complete); |
| 4068 | if (!err) { |
| 4069 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 4070 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4071 | } |
| 4072 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 4073 | mgmt_pending_remove(cmd); |
| 4074 | |
| 4075 | /* If no HCI commands were sent we're done */ |
| 4076 | if (err == -ENODATA) { |
| 4077 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 4078 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 4079 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 4080 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4081 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4082 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4083 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 4084 | return err; |
| 4085 | } |
| 4086 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4087 | 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] | 4088 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4089 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4090 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4091 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4092 | int err; |
| 4093 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4094 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4095 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4096 | hci_dev_lock(hdev); |
| 4097 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4098 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 4099 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4100 | MGMT_STATUS_FAILED, &cp->addr, |
| 4101 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 4102 | goto failed; |
| 4103 | } |
| 4104 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 4105 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4106 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 4107 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 4108 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 4109 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4110 | goto failed; |
| 4111 | } |
| 4112 | |
| 4113 | if (cp->name_known) { |
| 4114 | e->name_state = NAME_KNOWN; |
| 4115 | list_del(&e->list); |
| 4116 | } else { |
| 4117 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 4118 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4119 | } |
| 4120 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 4121 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 4122 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4123 | |
| 4124 | failed: |
| 4125 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4126 | return err; |
| 4127 | } |
| 4128 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4129 | 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] | 4130 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4131 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4132 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4133 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4134 | int err; |
| 4135 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4136 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4137 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4138 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 4139 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 4140 | MGMT_STATUS_INVALID_PARAMS, |
| 4141 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4142 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4143 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4144 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4145 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 4146 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4147 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4148 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4149 | goto done; |
| 4150 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4151 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4152 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4153 | sk); |
| 4154 | status = MGMT_STATUS_SUCCESS; |
| 4155 | |
| 4156 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4157 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4158 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4159 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4160 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4161 | |
| 4162 | return err; |
| 4163 | } |
| 4164 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4165 | 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] | 4166 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4167 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4168 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4169 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4170 | int err; |
| 4171 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4172 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4173 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4174 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 4175 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 4176 | MGMT_STATUS_INVALID_PARAMS, |
| 4177 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4178 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4179 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4180 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4181 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 4182 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4183 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4184 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4185 | goto done; |
| 4186 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4187 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4188 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4189 | sk); |
| 4190 | status = MGMT_STATUS_SUCCESS; |
| 4191 | |
| 4192 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4193 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4194 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4195 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4196 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4197 | |
| 4198 | return err; |
| 4199 | } |
| 4200 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4201 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4202 | u16 len) |
| 4203 | { |
| 4204 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4205 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4206 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4207 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4208 | |
| 4209 | BT_DBG("%s", hdev->name); |
| 4210 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4211 | source = __le16_to_cpu(cp->source); |
| 4212 | |
| 4213 | if (source > 0x0002) |
| 4214 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4215 | MGMT_STATUS_INVALID_PARAMS); |
| 4216 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4217 | hci_dev_lock(hdev); |
| 4218 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4219 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4220 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4221 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4222 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4223 | |
| 4224 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 4225 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4226 | hci_req_init(&req, hdev); |
| 4227 | update_eir(&req); |
| 4228 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4229 | |
| 4230 | hci_dev_unlock(hdev); |
| 4231 | |
| 4232 | return err; |
| 4233 | } |
| 4234 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4235 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 4236 | { |
| 4237 | struct cmd_lookup match = { NULL, hdev }; |
| 4238 | |
| 4239 | if (status) { |
| 4240 | u8 mgmt_err = mgmt_status(status); |
| 4241 | |
| 4242 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4243 | cmd_status_rsp, &mgmt_err); |
| 4244 | return; |
| 4245 | } |
| 4246 | |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4247 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
| 4248 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4249 | else |
| 4250 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4251 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4252 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4253 | &match); |
| 4254 | |
| 4255 | new_settings(hdev, match.sk); |
| 4256 | |
| 4257 | if (match.sk) |
| 4258 | sock_put(match.sk); |
| 4259 | } |
| 4260 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4261 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4262 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4263 | { |
| 4264 | struct mgmt_mode *cp = data; |
| 4265 | struct pending_cmd *cmd; |
| 4266 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4267 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4268 | int err; |
| 4269 | |
| 4270 | BT_DBG("request for %s", hdev->name); |
| 4271 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4272 | status = mgmt_le_support(hdev); |
| 4273 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4274 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4275 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4276 | |
| 4277 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4278 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4279 | MGMT_STATUS_INVALID_PARAMS); |
| 4280 | |
| 4281 | hci_dev_lock(hdev); |
| 4282 | |
| 4283 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4284 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4285 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4286 | /* The following conditions are ones which mean that we should |
| 4287 | * not do any HCI communication but directly send a mgmt |
| 4288 | * response to user space (after toggling the flag if |
| 4289 | * necessary). |
| 4290 | */ |
| 4291 | if (!hdev_is_powered(hdev) || val == enabled || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4292 | hci_conn_num(hdev, LE_LINK) > 0 || |
| 4293 | (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4294 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4295 | bool changed = false; |
| 4296 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4297 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 4298 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4299 | changed = true; |
| 4300 | } |
| 4301 | |
| 4302 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4303 | if (err < 0) |
| 4304 | goto unlock; |
| 4305 | |
| 4306 | if (changed) |
| 4307 | err = new_settings(hdev, sk); |
| 4308 | |
| 4309 | goto unlock; |
| 4310 | } |
| 4311 | |
| 4312 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4313 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 4314 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4315 | MGMT_STATUS_BUSY); |
| 4316 | goto unlock; |
| 4317 | } |
| 4318 | |
| 4319 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4320 | if (!cmd) { |
| 4321 | err = -ENOMEM; |
| 4322 | goto unlock; |
| 4323 | } |
| 4324 | |
| 4325 | hci_req_init(&req, hdev); |
| 4326 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4327 | if (val) |
| 4328 | enable_advertising(&req); |
| 4329 | else |
| 4330 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4331 | |
| 4332 | err = hci_req_run(&req, set_advertising_complete); |
| 4333 | if (err < 0) |
| 4334 | mgmt_pending_remove(cmd); |
| 4335 | |
| 4336 | unlock: |
| 4337 | hci_dev_unlock(hdev); |
| 4338 | return err; |
| 4339 | } |
| 4340 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4341 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4342 | void *data, u16 len) |
| 4343 | { |
| 4344 | struct mgmt_cp_set_static_address *cp = data; |
| 4345 | int err; |
| 4346 | |
| 4347 | BT_DBG("%s", hdev->name); |
| 4348 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4349 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4350 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4351 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4352 | |
| 4353 | if (hdev_is_powered(hdev)) |
| 4354 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4355 | MGMT_STATUS_REJECTED); |
| 4356 | |
| 4357 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4358 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 4359 | return cmd_status(sk, hdev->id, |
| 4360 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4361 | MGMT_STATUS_INVALID_PARAMS); |
| 4362 | |
| 4363 | /* Two most significant bits shall be set */ |
| 4364 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4365 | return cmd_status(sk, hdev->id, |
| 4366 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4367 | MGMT_STATUS_INVALID_PARAMS); |
| 4368 | } |
| 4369 | |
| 4370 | hci_dev_lock(hdev); |
| 4371 | |
| 4372 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4373 | |
| 4374 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4375 | |
| 4376 | hci_dev_unlock(hdev); |
| 4377 | |
| 4378 | return err; |
| 4379 | } |
| 4380 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4381 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4382 | void *data, u16 len) |
| 4383 | { |
| 4384 | struct mgmt_cp_set_scan_params *cp = data; |
| 4385 | __u16 interval, window; |
| 4386 | int err; |
| 4387 | |
| 4388 | BT_DBG("%s", hdev->name); |
| 4389 | |
| 4390 | if (!lmp_le_capable(hdev)) |
| 4391 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4392 | MGMT_STATUS_NOT_SUPPORTED); |
| 4393 | |
| 4394 | interval = __le16_to_cpu(cp->interval); |
| 4395 | |
| 4396 | if (interval < 0x0004 || interval > 0x4000) |
| 4397 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4398 | MGMT_STATUS_INVALID_PARAMS); |
| 4399 | |
| 4400 | window = __le16_to_cpu(cp->window); |
| 4401 | |
| 4402 | if (window < 0x0004 || window > 0x4000) |
| 4403 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4404 | MGMT_STATUS_INVALID_PARAMS); |
| 4405 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4406 | if (window > interval) |
| 4407 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4408 | MGMT_STATUS_INVALID_PARAMS); |
| 4409 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4410 | hci_dev_lock(hdev); |
| 4411 | |
| 4412 | hdev->le_scan_interval = interval; |
| 4413 | hdev->le_scan_window = window; |
| 4414 | |
| 4415 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4416 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4417 | /* If background scan is running, restart it so new parameters are |
| 4418 | * loaded. |
| 4419 | */ |
| 4420 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4421 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4422 | struct hci_request req; |
| 4423 | |
| 4424 | hci_req_init(&req, hdev); |
| 4425 | |
| 4426 | hci_req_add_le_scan_disable(&req); |
| 4427 | hci_req_add_le_passive_scan(&req); |
| 4428 | |
| 4429 | hci_req_run(&req, NULL); |
| 4430 | } |
| 4431 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4432 | hci_dev_unlock(hdev); |
| 4433 | |
| 4434 | return err; |
| 4435 | } |
| 4436 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4437 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4438 | { |
| 4439 | struct pending_cmd *cmd; |
| 4440 | |
| 4441 | BT_DBG("status 0x%02x", status); |
| 4442 | |
| 4443 | hci_dev_lock(hdev); |
| 4444 | |
| 4445 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4446 | if (!cmd) |
| 4447 | goto unlock; |
| 4448 | |
| 4449 | if (status) { |
| 4450 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4451 | mgmt_status(status)); |
| 4452 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4453 | struct mgmt_mode *cp = cmd->param; |
| 4454 | |
| 4455 | if (cp->val) |
| 4456 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4457 | else |
| 4458 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4459 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4460 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4461 | new_settings(hdev, cmd->sk); |
| 4462 | } |
| 4463 | |
| 4464 | mgmt_pending_remove(cmd); |
| 4465 | |
| 4466 | unlock: |
| 4467 | hci_dev_unlock(hdev); |
| 4468 | } |
| 4469 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4470 | 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] | 4471 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4472 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4473 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4474 | struct pending_cmd *cmd; |
| 4475 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4476 | int err; |
| 4477 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4478 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4479 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4480 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4481 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4482 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4483 | MGMT_STATUS_NOT_SUPPORTED); |
| 4484 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4485 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4486 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4487 | MGMT_STATUS_INVALID_PARAMS); |
| 4488 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4489 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4490 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4491 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4492 | |
| 4493 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4494 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4495 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4496 | |
| 4497 | hci_dev_lock(hdev); |
| 4498 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4499 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4500 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4501 | MGMT_STATUS_BUSY); |
| 4502 | goto unlock; |
| 4503 | } |
| 4504 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4505 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4506 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4507 | hdev); |
| 4508 | goto unlock; |
| 4509 | } |
| 4510 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4511 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4512 | data, len); |
| 4513 | if (!cmd) { |
| 4514 | err = -ENOMEM; |
| 4515 | goto unlock; |
| 4516 | } |
| 4517 | |
| 4518 | hci_req_init(&req, hdev); |
| 4519 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4520 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4521 | |
| 4522 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4523 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4524 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4525 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4526 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4527 | } |
| 4528 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4529 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4530 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4531 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4532 | return err; |
| 4533 | } |
| 4534 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4535 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4536 | { |
| 4537 | struct pending_cmd *cmd; |
| 4538 | |
| 4539 | BT_DBG("status 0x%02x", status); |
| 4540 | |
| 4541 | hci_dev_lock(hdev); |
| 4542 | |
| 4543 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4544 | if (!cmd) |
| 4545 | goto unlock; |
| 4546 | |
| 4547 | if (status) { |
| 4548 | u8 mgmt_err = mgmt_status(status); |
| 4549 | |
| 4550 | /* We need to restore the flag if related HCI commands |
| 4551 | * failed. |
| 4552 | */ |
| 4553 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4554 | |
| 4555 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4556 | } else { |
| 4557 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4558 | new_settings(hdev, cmd->sk); |
| 4559 | } |
| 4560 | |
| 4561 | mgmt_pending_remove(cmd); |
| 4562 | |
| 4563 | unlock: |
| 4564 | hci_dev_unlock(hdev); |
| 4565 | } |
| 4566 | |
| 4567 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4568 | { |
| 4569 | struct mgmt_mode *cp = data; |
| 4570 | struct pending_cmd *cmd; |
| 4571 | struct hci_request req; |
| 4572 | int err; |
| 4573 | |
| 4574 | BT_DBG("request for %s", hdev->name); |
| 4575 | |
| 4576 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4577 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4578 | MGMT_STATUS_NOT_SUPPORTED); |
| 4579 | |
| 4580 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4581 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4582 | MGMT_STATUS_REJECTED); |
| 4583 | |
| 4584 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4585 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4586 | MGMT_STATUS_INVALID_PARAMS); |
| 4587 | |
| 4588 | hci_dev_lock(hdev); |
| 4589 | |
| 4590 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4591 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4592 | goto unlock; |
| 4593 | } |
| 4594 | |
| 4595 | if (!hdev_is_powered(hdev)) { |
| 4596 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4597 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4598 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4599 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4600 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4601 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4602 | } |
| 4603 | |
| 4604 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4605 | |
| 4606 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4607 | if (err < 0) |
| 4608 | goto unlock; |
| 4609 | |
| 4610 | err = new_settings(hdev, sk); |
| 4611 | goto unlock; |
| 4612 | } |
| 4613 | |
| 4614 | /* Reject disabling when powered on */ |
| 4615 | if (!cp->val) { |
| 4616 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4617 | MGMT_STATUS_REJECTED); |
| 4618 | goto unlock; |
| 4619 | } |
| 4620 | |
| 4621 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4622 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4623 | MGMT_STATUS_BUSY); |
| 4624 | goto unlock; |
| 4625 | } |
| 4626 | |
| 4627 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4628 | if (!cmd) { |
| 4629 | err = -ENOMEM; |
| 4630 | goto unlock; |
| 4631 | } |
| 4632 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4633 | /* 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] | 4634 | * generates the correct flags. |
| 4635 | */ |
| 4636 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4637 | |
| 4638 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4639 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 4640 | write_fast_connectable(&req, false); |
| 4641 | hci_update_page_scan(hdev, &req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4642 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4643 | /* Since only the advertising data flags will change, there |
| 4644 | * is no need to update the scan response data. |
| 4645 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4646 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4647 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4648 | err = hci_req_run(&req, set_bredr_complete); |
| 4649 | if (err < 0) |
| 4650 | mgmt_pending_remove(cmd); |
| 4651 | |
| 4652 | unlock: |
| 4653 | hci_dev_unlock(hdev); |
| 4654 | return err; |
| 4655 | } |
| 4656 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4657 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4658 | void *data, u16 len) |
| 4659 | { |
| 4660 | struct mgmt_mode *cp = data; |
| 4661 | struct pending_cmd *cmd; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4662 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4663 | int err; |
| 4664 | |
| 4665 | BT_DBG("request for %s", hdev->name); |
| 4666 | |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4667 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 4668 | !lmp_sc_capable(hdev) && !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4669 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4670 | MGMT_STATUS_NOT_SUPPORTED); |
| 4671 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4672 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4673 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4674 | MGMT_STATUS_INVALID_PARAMS); |
| 4675 | |
| 4676 | hci_dev_lock(hdev); |
| 4677 | |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4678 | if (!hdev_is_powered(hdev) || |
| 4679 | (!lmp_sc_capable(hdev) && |
| 4680 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) || |
| 4681 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4682 | bool changed; |
| 4683 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4684 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4685 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4686 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4687 | if (cp->val == 0x02) |
| 4688 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4689 | else |
| 4690 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4691 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4692 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4693 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4694 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4695 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4696 | |
| 4697 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4698 | if (err < 0) |
| 4699 | goto failed; |
| 4700 | |
| 4701 | if (changed) |
| 4702 | err = new_settings(hdev, sk); |
| 4703 | |
| 4704 | goto failed; |
| 4705 | } |
| 4706 | |
| 4707 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4708 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4709 | MGMT_STATUS_BUSY); |
| 4710 | goto failed; |
| 4711 | } |
| 4712 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4713 | val = !!cp->val; |
| 4714 | |
| 4715 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4716 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4717 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4718 | goto failed; |
| 4719 | } |
| 4720 | |
| 4721 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4722 | if (!cmd) { |
| 4723 | err = -ENOMEM; |
| 4724 | goto failed; |
| 4725 | } |
| 4726 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4727 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4728 | if (err < 0) { |
| 4729 | mgmt_pending_remove(cmd); |
| 4730 | goto failed; |
| 4731 | } |
| 4732 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4733 | if (cp->val == 0x02) |
| 4734 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4735 | else |
| 4736 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4737 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4738 | failed: |
| 4739 | hci_dev_unlock(hdev); |
| 4740 | return err; |
| 4741 | } |
| 4742 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4743 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4744 | void *data, u16 len) |
| 4745 | { |
| 4746 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4747 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4748 | int err; |
| 4749 | |
| 4750 | BT_DBG("request for %s", hdev->name); |
| 4751 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4752 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4753 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4754 | MGMT_STATUS_INVALID_PARAMS); |
| 4755 | |
| 4756 | hci_dev_lock(hdev); |
| 4757 | |
| 4758 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4759 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4760 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4761 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4762 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4763 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4764 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4765 | if (cp->val == 0x02) |
| 4766 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4767 | &hdev->dev_flags); |
| 4768 | else |
| 4769 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4770 | &hdev->dev_flags); |
| 4771 | |
| 4772 | if (hdev_is_powered(hdev) && use_changed && |
| 4773 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4774 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4775 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4776 | sizeof(mode), &mode); |
| 4777 | } |
| 4778 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4779 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4780 | if (err < 0) |
| 4781 | goto unlock; |
| 4782 | |
| 4783 | if (changed) |
| 4784 | err = new_settings(hdev, sk); |
| 4785 | |
| 4786 | unlock: |
| 4787 | hci_dev_unlock(hdev); |
| 4788 | return err; |
| 4789 | } |
| 4790 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4791 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4792 | u16 len) |
| 4793 | { |
| 4794 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4795 | bool changed; |
| 4796 | int err; |
| 4797 | |
| 4798 | BT_DBG("request for %s", hdev->name); |
| 4799 | |
| 4800 | if (!lmp_le_capable(hdev)) |
| 4801 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4802 | MGMT_STATUS_NOT_SUPPORTED); |
| 4803 | |
| 4804 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4805 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4806 | MGMT_STATUS_INVALID_PARAMS); |
| 4807 | |
| 4808 | if (hdev_is_powered(hdev)) |
| 4809 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4810 | MGMT_STATUS_REJECTED); |
| 4811 | |
| 4812 | hci_dev_lock(hdev); |
| 4813 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4814 | /* If user space supports this command it is also expected to |
| 4815 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4816 | */ |
| 4817 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4818 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4819 | if (cp->privacy) { |
| 4820 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4821 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4822 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4823 | } else { |
| 4824 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4825 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4826 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4827 | } |
| 4828 | |
| 4829 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4830 | if (err < 0) |
| 4831 | goto unlock; |
| 4832 | |
| 4833 | if (changed) |
| 4834 | err = new_settings(hdev, sk); |
| 4835 | |
| 4836 | unlock: |
| 4837 | hci_dev_unlock(hdev); |
| 4838 | return err; |
| 4839 | } |
| 4840 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4841 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4842 | { |
| 4843 | switch (irk->addr.type) { |
| 4844 | case BDADDR_LE_PUBLIC: |
| 4845 | return true; |
| 4846 | |
| 4847 | case BDADDR_LE_RANDOM: |
| 4848 | /* Two most significant bits shall be set */ |
| 4849 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4850 | return false; |
| 4851 | return true; |
| 4852 | } |
| 4853 | |
| 4854 | return false; |
| 4855 | } |
| 4856 | |
| 4857 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4858 | u16 len) |
| 4859 | { |
| 4860 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4861 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4862 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4863 | u16 irk_count, expected_len; |
| 4864 | int i, err; |
| 4865 | |
| 4866 | BT_DBG("request for %s", hdev->name); |
| 4867 | |
| 4868 | if (!lmp_le_capable(hdev)) |
| 4869 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4870 | MGMT_STATUS_NOT_SUPPORTED); |
| 4871 | |
| 4872 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4873 | if (irk_count > max_irk_count) { |
| 4874 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
| 4875 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4876 | MGMT_STATUS_INVALID_PARAMS); |
| 4877 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4878 | |
| 4879 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4880 | if (expected_len != len) { |
| 4881 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4882 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4883 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4884 | MGMT_STATUS_INVALID_PARAMS); |
| 4885 | } |
| 4886 | |
| 4887 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4888 | |
| 4889 | for (i = 0; i < irk_count; i++) { |
| 4890 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4891 | |
| 4892 | if (!irk_is_valid(key)) |
| 4893 | return cmd_status(sk, hdev->id, |
| 4894 | MGMT_OP_LOAD_IRKS, |
| 4895 | MGMT_STATUS_INVALID_PARAMS); |
| 4896 | } |
| 4897 | |
| 4898 | hci_dev_lock(hdev); |
| 4899 | |
| 4900 | hci_smp_irks_clear(hdev); |
| 4901 | |
| 4902 | for (i = 0; i < irk_count; i++) { |
| 4903 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4904 | u8 addr_type; |
| 4905 | |
| 4906 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4907 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4908 | else |
| 4909 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4910 | |
| 4911 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4912 | BDADDR_ANY); |
| 4913 | } |
| 4914 | |
| 4915 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4916 | |
| 4917 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4918 | |
| 4919 | hci_dev_unlock(hdev); |
| 4920 | |
| 4921 | return err; |
| 4922 | } |
| 4923 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4924 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4925 | { |
| 4926 | if (key->master != 0x00 && key->master != 0x01) |
| 4927 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4928 | |
| 4929 | switch (key->addr.type) { |
| 4930 | case BDADDR_LE_PUBLIC: |
| 4931 | return true; |
| 4932 | |
| 4933 | case BDADDR_LE_RANDOM: |
| 4934 | /* Two most significant bits shall be set */ |
| 4935 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4936 | return false; |
| 4937 | return true; |
| 4938 | } |
| 4939 | |
| 4940 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4941 | } |
| 4942 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4943 | 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] | 4944 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4945 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4946 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4947 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4948 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4949 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4950 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4951 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4952 | BT_DBG("request for %s", hdev->name); |
| 4953 | |
| 4954 | if (!lmp_le_capable(hdev)) |
| 4955 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4956 | MGMT_STATUS_NOT_SUPPORTED); |
| 4957 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4958 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4959 | if (key_count > max_key_count) { |
| 4960 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
| 4961 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4962 | MGMT_STATUS_INVALID_PARAMS); |
| 4963 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4964 | |
| 4965 | expected_len = sizeof(*cp) + key_count * |
| 4966 | sizeof(struct mgmt_ltk_info); |
| 4967 | if (expected_len != len) { |
| 4968 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4969 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4970 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4971 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4972 | } |
| 4973 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4974 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4975 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4976 | for (i = 0; i < key_count; i++) { |
| 4977 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4978 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4979 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4980 | return cmd_status(sk, hdev->id, |
| 4981 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4982 | MGMT_STATUS_INVALID_PARAMS); |
| 4983 | } |
| 4984 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4985 | hci_dev_lock(hdev); |
| 4986 | |
| 4987 | hci_smp_ltks_clear(hdev); |
| 4988 | |
| 4989 | for (i = 0; i < key_count; i++) { |
| 4990 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4991 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4992 | |
| 4993 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4994 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4995 | else |
| 4996 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4997 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4998 | switch (key->type) { |
| 4999 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5000 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5001 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5002 | break; |
| 5003 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5004 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5005 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5006 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 5007 | case MGMT_LTK_P256_UNAUTH: |
| 5008 | authenticated = 0x00; |
| 5009 | type = SMP_LTK_P256; |
| 5010 | break; |
| 5011 | case MGMT_LTK_P256_AUTH: |
| 5012 | authenticated = 0x01; |
| 5013 | type = SMP_LTK_P256; |
| 5014 | break; |
| 5015 | case MGMT_LTK_P256_DEBUG: |
| 5016 | authenticated = 0x00; |
| 5017 | type = SMP_LTK_P256_DEBUG; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 5018 | default: |
| 5019 | continue; |
| 5020 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5021 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 5022 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5023 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 5024 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5025 | } |
| 5026 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5027 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 5028 | NULL, 0); |
| 5029 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5030 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5031 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 5032 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5033 | } |
| 5034 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5035 | struct cmd_conn_lookup { |
| 5036 | struct hci_conn *conn; |
| 5037 | bool valid_tx_power; |
| 5038 | u8 mgmt_status; |
| 5039 | }; |
| 5040 | |
| 5041 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 5042 | { |
| 5043 | struct cmd_conn_lookup *match = data; |
| 5044 | struct mgmt_cp_get_conn_info *cp; |
| 5045 | struct mgmt_rp_get_conn_info rp; |
| 5046 | struct hci_conn *conn = cmd->user_data; |
| 5047 | |
| 5048 | if (conn != match->conn) |
| 5049 | return; |
| 5050 | |
| 5051 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 5052 | |
| 5053 | memset(&rp, 0, sizeof(rp)); |
| 5054 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5055 | rp.addr.type = cp->addr.type; |
| 5056 | |
| 5057 | if (!match->mgmt_status) { |
| 5058 | rp.rssi = conn->rssi; |
| 5059 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5060 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5061 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5062 | rp.max_tx_power = conn->max_tx_power; |
| 5063 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5064 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5065 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 5066 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5067 | } |
| 5068 | |
| 5069 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 5070 | match->mgmt_status, &rp, sizeof(rp)); |
| 5071 | |
| 5072 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5073 | hci_conn_put(conn); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5074 | |
| 5075 | mgmt_pending_remove(cmd); |
| 5076 | } |
| 5077 | |
| 5078 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 5079 | { |
| 5080 | struct hci_cp_read_rssi *cp; |
| 5081 | struct hci_conn *conn; |
| 5082 | struct cmd_conn_lookup match; |
| 5083 | u16 handle; |
| 5084 | |
| 5085 | BT_DBG("status 0x%02x", status); |
| 5086 | |
| 5087 | hci_dev_lock(hdev); |
| 5088 | |
| 5089 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5090 | * otherwise we assume it's not valid. At the moment we assume that |
| 5091 | * either both or none of current and max values are valid to keep code |
| 5092 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5093 | */ |
| 5094 | match.valid_tx_power = !status; |
| 5095 | |
| 5096 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 5097 | * Level so we check which one was last sent to retrieve connection |
| 5098 | * handle. Both commands have handle as first parameter so it's safe to |
| 5099 | * cast data on the same command struct. |
| 5100 | * |
| 5101 | * First command sent is always Read RSSI and we fail only if it fails. |
| 5102 | * In other case we simply override error to indicate success as we |
| 5103 | * already remembered if TX power value is actually valid. |
| 5104 | */ |
| 5105 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 5106 | if (!cp) { |
| 5107 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 5108 | status = 0; |
| 5109 | } |
| 5110 | |
| 5111 | if (!cp) { |
| 5112 | BT_ERR("invalid sent_cmd in response"); |
| 5113 | goto unlock; |
| 5114 | } |
| 5115 | |
| 5116 | handle = __le16_to_cpu(cp->handle); |
| 5117 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5118 | if (!conn) { |
| 5119 | BT_ERR("unknown handle (%d) in response", handle); |
| 5120 | goto unlock; |
| 5121 | } |
| 5122 | |
| 5123 | match.conn = conn; |
| 5124 | match.mgmt_status = mgmt_status(status); |
| 5125 | |
| 5126 | /* Cache refresh is complete, now reply for mgmt request for given |
| 5127 | * connection only. |
| 5128 | */ |
| 5129 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 5130 | get_conn_info_complete, &match); |
| 5131 | |
| 5132 | unlock: |
| 5133 | hci_dev_unlock(hdev); |
| 5134 | } |
| 5135 | |
| 5136 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5137 | u16 len) |
| 5138 | { |
| 5139 | struct mgmt_cp_get_conn_info *cp = data; |
| 5140 | struct mgmt_rp_get_conn_info rp; |
| 5141 | struct hci_conn *conn; |
| 5142 | unsigned long conn_info_age; |
| 5143 | int err = 0; |
| 5144 | |
| 5145 | BT_DBG("%s", hdev->name); |
| 5146 | |
| 5147 | memset(&rp, 0, sizeof(rp)); |
| 5148 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5149 | rp.addr.type = cp->addr.type; |
| 5150 | |
| 5151 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 5152 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5153 | MGMT_STATUS_INVALID_PARAMS, |
| 5154 | &rp, sizeof(rp)); |
| 5155 | |
| 5156 | hci_dev_lock(hdev); |
| 5157 | |
| 5158 | if (!hdev_is_powered(hdev)) { |
| 5159 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5160 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5161 | goto unlock; |
| 5162 | } |
| 5163 | |
| 5164 | if (cp->addr.type == BDADDR_BREDR) |
| 5165 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5166 | &cp->addr.bdaddr); |
| 5167 | else |
| 5168 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 5169 | |
| 5170 | if (!conn || conn->state != BT_CONNECTED) { |
| 5171 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5172 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 5173 | goto unlock; |
| 5174 | } |
| 5175 | |
| 5176 | /* To avoid client trying to guess when to poll again for information we |
| 5177 | * calculate conn info age as random value between min/max set in hdev. |
| 5178 | */ |
| 5179 | conn_info_age = hdev->conn_info_min_age + |
| 5180 | prandom_u32_max(hdev->conn_info_max_age - |
| 5181 | hdev->conn_info_min_age); |
| 5182 | |
| 5183 | /* Query controller to refresh cached values if they are too old or were |
| 5184 | * never read. |
| 5185 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5186 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5187 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5188 | !conn->conn_info_timestamp) { |
| 5189 | struct hci_request req; |
| 5190 | struct hci_cp_read_tx_power req_txp_cp; |
| 5191 | struct hci_cp_read_rssi req_rssi_cp; |
| 5192 | struct pending_cmd *cmd; |
| 5193 | |
| 5194 | hci_req_init(&req, hdev); |
| 5195 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5196 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5197 | &req_rssi_cp); |
| 5198 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5199 | /* For LE links TX power does not change thus we don't need to |
| 5200 | * query for it once value is known. |
| 5201 | */ |
| 5202 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5203 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5204 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5205 | req_txp_cp.type = 0x00; |
| 5206 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5207 | sizeof(req_txp_cp), &req_txp_cp); |
| 5208 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5209 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5210 | /* Max TX power needs to be read only once per connection */ |
| 5211 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5212 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5213 | req_txp_cp.type = 0x01; |
| 5214 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5215 | sizeof(req_txp_cp), &req_txp_cp); |
| 5216 | } |
| 5217 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5218 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5219 | if (err < 0) |
| 5220 | goto unlock; |
| 5221 | |
| 5222 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5223 | data, len); |
| 5224 | if (!cmd) { |
| 5225 | err = -ENOMEM; |
| 5226 | goto unlock; |
| 5227 | } |
| 5228 | |
| 5229 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5230 | cmd->user_data = hci_conn_get(conn); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5231 | |
| 5232 | conn->conn_info_timestamp = jiffies; |
| 5233 | } else { |
| 5234 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5235 | rp.rssi = conn->rssi; |
| 5236 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5237 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5238 | |
| 5239 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5240 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 5241 | } |
| 5242 | |
| 5243 | unlock: |
| 5244 | hci_dev_unlock(hdev); |
| 5245 | return err; |
| 5246 | } |
| 5247 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5248 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 5249 | { |
| 5250 | struct mgmt_cp_get_clock_info *cp; |
| 5251 | struct mgmt_rp_get_clock_info rp; |
| 5252 | struct hci_cp_read_clock *hci_cp; |
| 5253 | struct pending_cmd *cmd; |
| 5254 | struct hci_conn *conn; |
| 5255 | |
| 5256 | BT_DBG("%s status %u", hdev->name, status); |
| 5257 | |
| 5258 | hci_dev_lock(hdev); |
| 5259 | |
| 5260 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5261 | if (!hci_cp) |
| 5262 | goto unlock; |
| 5263 | |
| 5264 | if (hci_cp->which) { |
| 5265 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5266 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5267 | } else { |
| 5268 | conn = NULL; |
| 5269 | } |
| 5270 | |
| 5271 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5272 | if (!cmd) |
| 5273 | goto unlock; |
| 5274 | |
| 5275 | cp = cmd->param; |
| 5276 | |
| 5277 | memset(&rp, 0, sizeof(rp)); |
| 5278 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 5279 | |
| 5280 | if (status) |
| 5281 | goto send_rsp; |
| 5282 | |
| 5283 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5284 | |
| 5285 | if (conn) { |
| 5286 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5287 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5288 | } |
| 5289 | |
| 5290 | send_rsp: |
| 5291 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 5292 | &rp, sizeof(rp)); |
| 5293 | mgmt_pending_remove(cmd); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5294 | if (conn) { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5295 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5296 | hci_conn_put(conn); |
| 5297 | } |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5298 | |
| 5299 | unlock: |
| 5300 | hci_dev_unlock(hdev); |
| 5301 | } |
| 5302 | |
| 5303 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5304 | u16 len) |
| 5305 | { |
| 5306 | struct mgmt_cp_get_clock_info *cp = data; |
| 5307 | struct mgmt_rp_get_clock_info rp; |
| 5308 | struct hci_cp_read_clock hci_cp; |
| 5309 | struct pending_cmd *cmd; |
| 5310 | struct hci_request req; |
| 5311 | struct hci_conn *conn; |
| 5312 | int err; |
| 5313 | |
| 5314 | BT_DBG("%s", hdev->name); |
| 5315 | |
| 5316 | memset(&rp, 0, sizeof(rp)); |
| 5317 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5318 | rp.addr.type = cp->addr.type; |
| 5319 | |
| 5320 | if (cp->addr.type != BDADDR_BREDR) |
| 5321 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5322 | MGMT_STATUS_INVALID_PARAMS, |
| 5323 | &rp, sizeof(rp)); |
| 5324 | |
| 5325 | hci_dev_lock(hdev); |
| 5326 | |
| 5327 | if (!hdev_is_powered(hdev)) { |
| 5328 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5329 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5330 | goto unlock; |
| 5331 | } |
| 5332 | |
| 5333 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5334 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5335 | &cp->addr.bdaddr); |
| 5336 | if (!conn || conn->state != BT_CONNECTED) { |
| 5337 | err = cmd_complete(sk, hdev->id, |
| 5338 | MGMT_OP_GET_CLOCK_INFO, |
| 5339 | MGMT_STATUS_NOT_CONNECTED, |
| 5340 | &rp, sizeof(rp)); |
| 5341 | goto unlock; |
| 5342 | } |
| 5343 | } else { |
| 5344 | conn = NULL; |
| 5345 | } |
| 5346 | |
| 5347 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5348 | if (!cmd) { |
| 5349 | err = -ENOMEM; |
| 5350 | goto unlock; |
| 5351 | } |
| 5352 | |
| 5353 | hci_req_init(&req, hdev); |
| 5354 | |
| 5355 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5356 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5357 | |
| 5358 | if (conn) { |
| 5359 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5360 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5361 | |
| 5362 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5363 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5364 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5365 | } |
| 5366 | |
| 5367 | err = hci_req_run(&req, get_clock_info_complete); |
| 5368 | if (err < 0) |
| 5369 | mgmt_pending_remove(cmd); |
| 5370 | |
| 5371 | unlock: |
| 5372 | hci_dev_unlock(hdev); |
| 5373 | return err; |
| 5374 | } |
| 5375 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5376 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5377 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5378 | { |
| 5379 | struct mgmt_ev_device_added ev; |
| 5380 | |
| 5381 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5382 | ev.addr.type = type; |
| 5383 | ev.action = action; |
| 5384 | |
| 5385 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5386 | } |
| 5387 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5388 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5389 | void *data, u16 len) |
| 5390 | { |
| 5391 | struct mgmt_cp_add_device *cp = data; |
| 5392 | u8 auto_conn, addr_type; |
| 5393 | int err; |
| 5394 | |
| 5395 | BT_DBG("%s", hdev->name); |
| 5396 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5397 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5398 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5399 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5400 | MGMT_STATUS_INVALID_PARAMS, |
| 5401 | &cp->addr, sizeof(cp->addr)); |
| 5402 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5403 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5404 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5405 | MGMT_STATUS_INVALID_PARAMS, |
| 5406 | &cp->addr, sizeof(cp->addr)); |
| 5407 | |
| 5408 | hci_dev_lock(hdev); |
| 5409 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5410 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5411 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5412 | if (cp->action != 0x01) { |
| 5413 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5414 | MGMT_STATUS_INVALID_PARAMS, |
| 5415 | &cp->addr, sizeof(cp->addr)); |
| 5416 | goto unlock; |
| 5417 | } |
| 5418 | |
| 5419 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5420 | cp->addr.type); |
| 5421 | if (err) |
| 5422 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5423 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 5424 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5425 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5426 | goto added; |
| 5427 | } |
| 5428 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5429 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5430 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5431 | else |
| 5432 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5433 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5434 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5435 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5436 | else if (cp->action == 0x01) |
| 5437 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5438 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5439 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5440 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5441 | /* If the connection parameters don't exist for this device, |
| 5442 | * they will be created and configured with defaults. |
| 5443 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5444 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5445 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5446 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5447 | MGMT_STATUS_FAILED, |
| 5448 | &cp->addr, sizeof(cp->addr)); |
| 5449 | goto unlock; |
| 5450 | } |
| 5451 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5452 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5453 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5454 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5455 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5456 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5457 | |
| 5458 | unlock: |
| 5459 | hci_dev_unlock(hdev); |
| 5460 | return err; |
| 5461 | } |
| 5462 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5463 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5464 | bdaddr_t *bdaddr, u8 type) |
| 5465 | { |
| 5466 | struct mgmt_ev_device_removed ev; |
| 5467 | |
| 5468 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5469 | ev.addr.type = type; |
| 5470 | |
| 5471 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5472 | } |
| 5473 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5474 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5475 | void *data, u16 len) |
| 5476 | { |
| 5477 | struct mgmt_cp_remove_device *cp = data; |
| 5478 | int err; |
| 5479 | |
| 5480 | BT_DBG("%s", hdev->name); |
| 5481 | |
| 5482 | hci_dev_lock(hdev); |
| 5483 | |
| 5484 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5485 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5486 | u8 addr_type; |
| 5487 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5488 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5489 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5490 | MGMT_STATUS_INVALID_PARAMS, |
| 5491 | &cp->addr, sizeof(cp->addr)); |
| 5492 | goto unlock; |
| 5493 | } |
| 5494 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5495 | if (cp->addr.type == BDADDR_BREDR) { |
| 5496 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5497 | &cp->addr.bdaddr, |
| 5498 | cp->addr.type); |
| 5499 | if (err) { |
| 5500 | err = cmd_complete(sk, hdev->id, |
| 5501 | MGMT_OP_REMOVE_DEVICE, |
| 5502 | MGMT_STATUS_INVALID_PARAMS, |
| 5503 | &cp->addr, sizeof(cp->addr)); |
| 5504 | goto unlock; |
| 5505 | } |
| 5506 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 5507 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5508 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5509 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5510 | cp->addr.type); |
| 5511 | goto complete; |
| 5512 | } |
| 5513 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5514 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5515 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5516 | else |
| 5517 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5518 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5519 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5520 | addr_type); |
| 5521 | if (!params) { |
| 5522 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5523 | MGMT_STATUS_INVALID_PARAMS, |
| 5524 | &cp->addr, sizeof(cp->addr)); |
| 5525 | goto unlock; |
| 5526 | } |
| 5527 | |
| 5528 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5529 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5530 | MGMT_STATUS_INVALID_PARAMS, |
| 5531 | &cp->addr, sizeof(cp->addr)); |
| 5532 | goto unlock; |
| 5533 | } |
| 5534 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5535 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5536 | list_del(¶ms->list); |
| 5537 | kfree(params); |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame] | 5538 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5539 | |
| 5540 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5541 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5542 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5543 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5544 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5545 | if (cp->addr.type) { |
| 5546 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5547 | MGMT_STATUS_INVALID_PARAMS, |
| 5548 | &cp->addr, sizeof(cp->addr)); |
| 5549 | goto unlock; |
| 5550 | } |
| 5551 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5552 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 5553 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 5554 | list_del(&b->list); |
| 5555 | kfree(b); |
| 5556 | } |
| 5557 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 5558 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5559 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5560 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5561 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5562 | continue; |
| 5563 | device_removed(sk, hdev, &p->addr, p->addr_type); |
| 5564 | list_del(&p->action); |
| 5565 | list_del(&p->list); |
| 5566 | kfree(p); |
| 5567 | } |
| 5568 | |
| 5569 | BT_DBG("All LE connection parameters were removed"); |
| 5570 | |
| 5571 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5572 | } |
| 5573 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5574 | complete: |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5575 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5576 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5577 | |
| 5578 | unlock: |
| 5579 | hci_dev_unlock(hdev); |
| 5580 | return err; |
| 5581 | } |
| 5582 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5583 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5584 | u16 len) |
| 5585 | { |
| 5586 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5587 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5588 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5589 | u16 param_count, expected_len; |
| 5590 | int i; |
| 5591 | |
| 5592 | if (!lmp_le_capable(hdev)) |
| 5593 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5594 | MGMT_STATUS_NOT_SUPPORTED); |
| 5595 | |
| 5596 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5597 | if (param_count > max_param_count) { |
| 5598 | BT_ERR("load_conn_param: too big param_count value %u", |
| 5599 | param_count); |
| 5600 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5601 | MGMT_STATUS_INVALID_PARAMS); |
| 5602 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5603 | |
| 5604 | expected_len = sizeof(*cp) + param_count * |
| 5605 | sizeof(struct mgmt_conn_param); |
| 5606 | if (expected_len != len) { |
| 5607 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5608 | expected_len, len); |
| 5609 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5610 | MGMT_STATUS_INVALID_PARAMS); |
| 5611 | } |
| 5612 | |
| 5613 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5614 | |
| 5615 | hci_dev_lock(hdev); |
| 5616 | |
| 5617 | hci_conn_params_clear_disabled(hdev); |
| 5618 | |
| 5619 | for (i = 0; i < param_count; i++) { |
| 5620 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5621 | struct hci_conn_params *hci_param; |
| 5622 | u16 min, max, latency, timeout; |
| 5623 | u8 addr_type; |
| 5624 | |
| 5625 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5626 | param->addr.type); |
| 5627 | |
| 5628 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5629 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5630 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5631 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5632 | } else { |
| 5633 | BT_ERR("Ignoring invalid connection parameters"); |
| 5634 | continue; |
| 5635 | } |
| 5636 | |
| 5637 | min = le16_to_cpu(param->min_interval); |
| 5638 | max = le16_to_cpu(param->max_interval); |
| 5639 | latency = le16_to_cpu(param->latency); |
| 5640 | timeout = le16_to_cpu(param->timeout); |
| 5641 | |
| 5642 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5643 | min, max, latency, timeout); |
| 5644 | |
| 5645 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5646 | BT_ERR("Ignoring invalid connection parameters"); |
| 5647 | continue; |
| 5648 | } |
| 5649 | |
| 5650 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5651 | addr_type); |
| 5652 | if (!hci_param) { |
| 5653 | BT_ERR("Failed to add connection parameters"); |
| 5654 | continue; |
| 5655 | } |
| 5656 | |
| 5657 | hci_param->conn_min_interval = min; |
| 5658 | hci_param->conn_max_interval = max; |
| 5659 | hci_param->conn_latency = latency; |
| 5660 | hci_param->supervision_timeout = timeout; |
| 5661 | } |
| 5662 | |
| 5663 | hci_dev_unlock(hdev); |
| 5664 | |
| 5665 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5666 | } |
| 5667 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5668 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5669 | void *data, u16 len) |
| 5670 | { |
| 5671 | struct mgmt_cp_set_external_config *cp = data; |
| 5672 | bool changed; |
| 5673 | int err; |
| 5674 | |
| 5675 | BT_DBG("%s", hdev->name); |
| 5676 | |
| 5677 | if (hdev_is_powered(hdev)) |
| 5678 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5679 | MGMT_STATUS_REJECTED); |
| 5680 | |
| 5681 | if (cp->config != 0x00 && cp->config != 0x01) |
| 5682 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5683 | MGMT_STATUS_INVALID_PARAMS); |
| 5684 | |
| 5685 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 5686 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5687 | MGMT_STATUS_NOT_SUPPORTED); |
| 5688 | |
| 5689 | hci_dev_lock(hdev); |
| 5690 | |
| 5691 | if (cp->config) |
| 5692 | changed = !test_and_set_bit(HCI_EXT_CONFIGURED, |
| 5693 | &hdev->dev_flags); |
| 5694 | else |
| 5695 | changed = test_and_clear_bit(HCI_EXT_CONFIGURED, |
| 5696 | &hdev->dev_flags); |
| 5697 | |
| 5698 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5699 | if (err < 0) |
| 5700 | goto unlock; |
| 5701 | |
| 5702 | if (!changed) |
| 5703 | goto unlock; |
| 5704 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 5705 | err = new_options(hdev, sk); |
| 5706 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5707 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) { |
| 5708 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5709 | |
| 5710 | if (test_and_change_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { |
| 5711 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5712 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5713 | |
| 5714 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5715 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 5716 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5717 | mgmt_index_added(hdev); |
| 5718 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5719 | } |
| 5720 | |
| 5721 | unlock: |
| 5722 | hci_dev_unlock(hdev); |
| 5723 | return err; |
| 5724 | } |
| 5725 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5726 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 5727 | void *data, u16 len) |
| 5728 | { |
| 5729 | struct mgmt_cp_set_public_address *cp = data; |
| 5730 | bool changed; |
| 5731 | int err; |
| 5732 | |
| 5733 | BT_DBG("%s", hdev->name); |
| 5734 | |
| 5735 | if (hdev_is_powered(hdev)) |
| 5736 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5737 | MGMT_STATUS_REJECTED); |
| 5738 | |
| 5739 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
| 5740 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5741 | MGMT_STATUS_INVALID_PARAMS); |
| 5742 | |
| 5743 | if (!hdev->set_bdaddr) |
| 5744 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5745 | MGMT_STATUS_NOT_SUPPORTED); |
| 5746 | |
| 5747 | hci_dev_lock(hdev); |
| 5748 | |
| 5749 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 5750 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 5751 | |
| 5752 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 5753 | if (err < 0) |
| 5754 | goto unlock; |
| 5755 | |
| 5756 | if (!changed) |
| 5757 | goto unlock; |
| 5758 | |
| 5759 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5760 | err = new_options(hdev, sk); |
| 5761 | |
| 5762 | if (is_configured(hdev)) { |
| 5763 | mgmt_index_removed(hdev); |
| 5764 | |
| 5765 | clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags); |
| 5766 | |
| 5767 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5768 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5769 | |
| 5770 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5771 | } |
| 5772 | |
| 5773 | unlock: |
| 5774 | hci_dev_unlock(hdev); |
| 5775 | return err; |
| 5776 | } |
| 5777 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5778 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5779 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5780 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5781 | bool var_len; |
| 5782 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5783 | } mgmt_handlers[] = { |
| 5784 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5785 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5786 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5787 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5788 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5789 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5790 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5791 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5792 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 5793 | { set_bondable, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5794 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5795 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5796 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5797 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5798 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5799 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5800 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5801 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5802 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5803 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5804 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5805 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5806 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5807 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5808 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5809 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5810 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5811 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5812 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5813 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5814 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5815 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5816 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5817 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5818 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5819 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5820 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5821 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5822 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5823 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5824 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5825 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5826 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5827 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5828 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5829 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5830 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5831 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5832 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5833 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5834 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5835 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5836 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5837 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
| 5838 | { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE }, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 5839 | { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE }, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5840 | { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE }, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5841 | { set_public_address, false, MGMT_SET_PUBLIC_ADDRESS_SIZE }, |
Jakub Pawlowski | 66ea942 | 2014-12-05 10:55:59 +0100 | [diff] [blame] | 5842 | { start_service_discovery,true, MGMT_START_SERVICE_DISCOVERY_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5843 | }; |
| 5844 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5845 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5846 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5847 | void *buf; |
| 5848 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5849 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5850 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5851 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5852 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5853 | int err; |
| 5854 | |
| 5855 | BT_DBG("got %zu bytes", msglen); |
| 5856 | |
| 5857 | if (msglen < sizeof(*hdr)) |
| 5858 | return -EINVAL; |
| 5859 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5860 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5861 | if (!buf) |
| 5862 | return -ENOMEM; |
| 5863 | |
| 5864 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5865 | err = -EFAULT; |
| 5866 | goto done; |
| 5867 | } |
| 5868 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5869 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5870 | opcode = __le16_to_cpu(hdr->opcode); |
| 5871 | index = __le16_to_cpu(hdr->index); |
| 5872 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5873 | |
| 5874 | if (len != msglen - sizeof(*hdr)) { |
| 5875 | err = -EINVAL; |
| 5876 | goto done; |
| 5877 | } |
| 5878 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5879 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5880 | hdev = hci_dev_get(index); |
| 5881 | if (!hdev) { |
| 5882 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5883 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5884 | goto done; |
| 5885 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5886 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5887 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5888 | test_bit(HCI_CONFIG, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5889 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5890 | err = cmd_status(sk, index, opcode, |
| 5891 | MGMT_STATUS_INVALID_INDEX); |
| 5892 | goto done; |
| 5893 | } |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5894 | |
| 5895 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5896 | opcode != MGMT_OP_READ_CONFIG_INFO && |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5897 | opcode != MGMT_OP_SET_EXTERNAL_CONFIG && |
| 5898 | opcode != MGMT_OP_SET_PUBLIC_ADDRESS) { |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5899 | err = cmd_status(sk, index, opcode, |
| 5900 | MGMT_STATUS_INVALID_INDEX); |
| 5901 | goto done; |
| 5902 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5903 | } |
| 5904 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5905 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5906 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5907 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5908 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5909 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5910 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5911 | } |
| 5912 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5913 | if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || |
| 5914 | opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
| 5915 | err = cmd_status(sk, index, opcode, |
| 5916 | MGMT_STATUS_INVALID_INDEX); |
| 5917 | goto done; |
| 5918 | } |
| 5919 | |
| 5920 | if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && |
| 5921 | opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5922 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5923 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5924 | goto done; |
| 5925 | } |
| 5926 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5927 | handler = &mgmt_handlers[opcode]; |
| 5928 | |
| 5929 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5930 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5931 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5932 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5933 | goto done; |
| 5934 | } |
| 5935 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5936 | if (hdev) |
| 5937 | mgmt_init_hdev(sk, hdev); |
| 5938 | |
| 5939 | cp = buf + sizeof(*hdr); |
| 5940 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5941 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5942 | if (err < 0) |
| 5943 | goto done; |
| 5944 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5945 | err = msglen; |
| 5946 | |
| 5947 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5948 | if (hdev) |
| 5949 | hci_dev_put(hdev); |
| 5950 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5951 | kfree(buf); |
| 5952 | return err; |
| 5953 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5954 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5955 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5956 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5957 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5958 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5959 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5960 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5961 | return; |
| 5962 | |
| 5963 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5964 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5965 | else |
| 5966 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5967 | } |
| 5968 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5969 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5970 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5971 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5972 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5973 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5974 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5975 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5976 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5977 | return; |
| 5978 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5979 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5980 | |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 5981 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5982 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5983 | else |
| 5984 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5985 | } |
| 5986 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5987 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5988 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5989 | { |
| 5990 | struct hci_conn_params *p; |
| 5991 | |
| 5992 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5993 | /* Needed for AUTO_OFF case where might not "really" |
| 5994 | * have been powered off. |
| 5995 | */ |
| 5996 | list_del_init(&p->action); |
| 5997 | |
| 5998 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5999 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6000 | case HCI_AUTO_CONN_ALWAYS: |
| 6001 | list_add(&p->action, &hdev->pend_le_conns); |
| 6002 | break; |
| 6003 | case HCI_AUTO_CONN_REPORT: |
| 6004 | list_add(&p->action, &hdev->pend_le_reports); |
| 6005 | break; |
| 6006 | default: |
| 6007 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6008 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6009 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6010 | |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 6011 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6012 | } |
| 6013 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6014 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 6015 | { |
| 6016 | struct cmd_lookup match = { NULL, hdev }; |
| 6017 | |
| 6018 | BT_DBG("status 0x%02x", status); |
| 6019 | |
| 6020 | hci_dev_lock(hdev); |
| 6021 | |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 6022 | restart_le_actions(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 6023 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6024 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 6025 | |
| 6026 | new_settings(hdev, match.sk); |
| 6027 | |
| 6028 | hci_dev_unlock(hdev); |
| 6029 | |
| 6030 | if (match.sk) |
| 6031 | sock_put(match.sk); |
| 6032 | } |
| 6033 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6034 | static int powered_update_hci(struct hci_dev *hdev) |
| 6035 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6036 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6037 | u8 link_sec; |
| 6038 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6039 | hci_req_init(&req, hdev); |
| 6040 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6041 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 6042 | !lmp_host_ssp_capable(hdev)) { |
| 6043 | u8 ssp = 1; |
| 6044 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6045 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6046 | } |
| 6047 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 6048 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 6049 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6050 | struct hci_cp_write_le_host_supported cp; |
| 6051 | |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 6052 | cp.le = 0x01; |
| 6053 | cp.simul = 0x00; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6054 | |
| 6055 | /* Check first if we already have the right |
| 6056 | * host state (host features set) |
| 6057 | */ |
| 6058 | if (cp.le != lmp_host_le_capable(hdev) || |
| 6059 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6060 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 6061 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6062 | } |
| 6063 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 6064 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 6065 | /* Make sure the controller has a good default for |
| 6066 | * advertising data. This also applies to the case |
| 6067 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 6068 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 6069 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 6070 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 6071 | update_scan_rsp_data(&req); |
| 6072 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 6073 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 6074 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6075 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 6076 | } |
| 6077 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6078 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 6079 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6080 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 6081 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6082 | |
| 6083 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 6084 | write_fast_connectable(&req, false); |
| 6085 | hci_update_page_scan(hdev, &req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6086 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6087 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6088 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6089 | } |
| 6090 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6091 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 6092 | } |
| 6093 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6094 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6095 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 6096 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6097 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 6098 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 6099 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6100 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 6101 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 6102 | return 0; |
| 6103 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 6104 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6105 | if (powered_update_hci(hdev) == 0) |
| 6106 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 6107 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6108 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 6109 | &match); |
| 6110 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 6111 | } |
| 6112 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 6113 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 6114 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 6115 | |
| 6116 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 6117 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 6118 | zero_cod, sizeof(zero_cod), NULL); |
| 6119 | |
| 6120 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 6121 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6122 | |
| 6123 | if (match.sk) |
| 6124 | sock_put(match.sk); |
| 6125 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 6126 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6127 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6128 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6129 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6130 | { |
| 6131 | struct pending_cmd *cmd; |
| 6132 | u8 status; |
| 6133 | |
| 6134 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6135 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6136 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6137 | |
| 6138 | if (err == -ERFKILL) |
| 6139 | status = MGMT_STATUS_RFKILLED; |
| 6140 | else |
| 6141 | status = MGMT_STATUS_FAILED; |
| 6142 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6143 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6144 | |
| 6145 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6146 | } |
| 6147 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6148 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 6149 | { |
| 6150 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6151 | |
| 6152 | hci_dev_lock(hdev); |
| 6153 | |
| 6154 | /* When discoverable timeout triggers, then just make sure |
| 6155 | * the limited discoverable flag is cleared. Even in the case |
| 6156 | * of a timeout triggered from general discoverable, it is |
| 6157 | * safe to unconditionally clear the flag. |
| 6158 | */ |
| 6159 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6160 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6161 | |
| 6162 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 6163 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 6164 | u8 scan = SCAN_PAGE; |
| 6165 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 6166 | sizeof(scan), &scan); |
| 6167 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6168 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6169 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6170 | hci_req_run(&req, NULL); |
| 6171 | |
| 6172 | hdev->discov_timeout = 0; |
| 6173 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6174 | new_settings(hdev, NULL); |
| 6175 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6176 | hci_dev_unlock(hdev); |
| 6177 | } |
| 6178 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6179 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 6180 | bool persistent) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6181 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 6182 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6183 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6184 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6185 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6186 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 6187 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6188 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6189 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 6190 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6191 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6192 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6193 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6194 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6195 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6196 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 6197 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6198 | switch (ltk->type) { |
| 6199 | case SMP_LTK: |
| 6200 | case SMP_LTK_SLAVE: |
| 6201 | if (ltk->authenticated) |
| 6202 | return MGMT_LTK_AUTHENTICATED; |
| 6203 | return MGMT_LTK_UNAUTHENTICATED; |
| 6204 | case SMP_LTK_P256: |
| 6205 | if (ltk->authenticated) |
| 6206 | return MGMT_LTK_P256_AUTH; |
| 6207 | return MGMT_LTK_P256_UNAUTH; |
| 6208 | case SMP_LTK_P256_DEBUG: |
| 6209 | return MGMT_LTK_P256_DEBUG; |
| 6210 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6211 | |
| 6212 | return MGMT_LTK_UNAUTHENTICATED; |
| 6213 | } |
| 6214 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6215 | 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] | 6216 | { |
| 6217 | struct mgmt_ev_new_long_term_key ev; |
| 6218 | |
| 6219 | memset(&ev, 0, sizeof(ev)); |
| 6220 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6221 | /* Devices using resolvable or non-resolvable random addresses |
| 6222 | * without providing an indentity resolving key don't require |
| 6223 | * to store long term keys. Their addresses will change the |
| 6224 | * next time around. |
| 6225 | * |
| 6226 | * Only when a remote device provides an identity address |
| 6227 | * make sure the long term key is stored. If the remote |
| 6228 | * identity is known, the long term keys are internally |
| 6229 | * mapped to the identity address. So allow static random |
| 6230 | * and public addresses here. |
| 6231 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6232 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6233 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6234 | ev.store_hint = 0x00; |
| 6235 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6236 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6237 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6238 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6239 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6240 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6241 | ev.key.enc_size = key->enc_size; |
| 6242 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 6243 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6244 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 6245 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6246 | ev.key.master = 1; |
| 6247 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6248 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 6249 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 6250 | 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] | 6251 | } |
| 6252 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6253 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 6254 | { |
| 6255 | struct mgmt_ev_new_irk ev; |
| 6256 | |
| 6257 | memset(&ev, 0, sizeof(ev)); |
| 6258 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6259 | /* For identity resolving keys from devices that are already |
| 6260 | * using a public address or static random address, do not |
| 6261 | * ask for storing this key. The identity resolving key really |
| 6262 | * is only mandatory for devices using resovlable random |
| 6263 | * addresses. |
| 6264 | * |
| 6265 | * Storing all identity resolving keys has the downside that |
| 6266 | * they will be also loaded on next boot of they system. More |
| 6267 | * identity resolving keys, means more time during scanning is |
| 6268 | * needed to actually resolve these addresses. |
| 6269 | */ |
| 6270 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 6271 | ev.store_hint = 0x01; |
| 6272 | else |
| 6273 | ev.store_hint = 0x00; |
| 6274 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6275 | bacpy(&ev.rpa, &irk->rpa); |
| 6276 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6277 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6278 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6279 | |
| 6280 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6281 | } |
| 6282 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6283 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6284 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6285 | { |
| 6286 | struct mgmt_ev_new_csrk ev; |
| 6287 | |
| 6288 | memset(&ev, 0, sizeof(ev)); |
| 6289 | |
| 6290 | /* Devices using resolvable or non-resolvable random addresses |
| 6291 | * without providing an indentity resolving key don't require |
| 6292 | * to store signature resolving keys. Their addresses will change |
| 6293 | * the next time around. |
| 6294 | * |
| 6295 | * Only when a remote device provides an identity address |
| 6296 | * make sure the signature resolving key is stored. So allow |
| 6297 | * static random and public addresses here. |
| 6298 | */ |
| 6299 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6300 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6301 | ev.store_hint = 0x00; |
| 6302 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6303 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6304 | |
| 6305 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6306 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 6307 | ev.key.master = csrk->master; |
| 6308 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6309 | |
| 6310 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6311 | } |
| 6312 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6313 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6314 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6315 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6316 | { |
| 6317 | struct mgmt_ev_new_conn_param ev; |
| 6318 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6319 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6320 | return; |
| 6321 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6322 | memset(&ev, 0, sizeof(ev)); |
| 6323 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6324 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6325 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6326 | ev.min_interval = cpu_to_le16(min_interval); |
| 6327 | ev.max_interval = cpu_to_le16(max_interval); |
| 6328 | ev.latency = cpu_to_le16(latency); |
| 6329 | ev.timeout = cpu_to_le16(timeout); |
| 6330 | |
| 6331 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6332 | } |
| 6333 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 6334 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 6335 | u8 data_len) |
| 6336 | { |
| 6337 | eir[eir_len++] = sizeof(type) + data_len; |
| 6338 | eir[eir_len++] = type; |
| 6339 | memcpy(&eir[eir_len], data, data_len); |
| 6340 | eir_len += data_len; |
| 6341 | |
| 6342 | return eir_len; |
| 6343 | } |
| 6344 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6345 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 6346 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6347 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6348 | char buf[512]; |
| 6349 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6350 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6351 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6352 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 6353 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6354 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6355 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6356 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6357 | /* We must ensure that the EIR Data fields are ordered and |
| 6358 | * unique. Keep it simple for now and avoid the problem by not |
| 6359 | * adding any BR/EDR data to the LE adv. |
| 6360 | */ |
| 6361 | if (conn->le_adv_data_len > 0) { |
| 6362 | memcpy(&ev->eir[eir_len], |
| 6363 | conn->le_adv_data, conn->le_adv_data_len); |
| 6364 | eir_len = conn->le_adv_data_len; |
| 6365 | } else { |
| 6366 | if (name_len > 0) |
| 6367 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 6368 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6369 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 6370 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6371 | eir_len = eir_append_data(ev->eir, eir_len, |
| 6372 | EIR_CLASS_OF_DEV, |
| 6373 | conn->dev_class, 3); |
| 6374 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6375 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6376 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6377 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6378 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6379 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6380 | } |
| 6381 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6382 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 6383 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 6384 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6385 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 6386 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6387 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6388 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6389 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6390 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6391 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6392 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6393 | |
| 6394 | *sk = cmd->sk; |
| 6395 | sock_hold(*sk); |
| 6396 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6397 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6398 | } |
| 6399 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6400 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6401 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6402 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6403 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 6404 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6405 | |
| 6406 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6407 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6408 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6409 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6410 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6411 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6412 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6413 | |
| 6414 | mgmt_pending_remove(cmd); |
| 6415 | } |
| 6416 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6417 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 6418 | { |
| 6419 | struct pending_cmd *cmd; |
| 6420 | struct mgmt_mode *cp; |
| 6421 | |
| 6422 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6423 | if (!cmd) |
| 6424 | return false; |
| 6425 | |
| 6426 | cp = cmd->param; |
| 6427 | if (!cp->val) |
| 6428 | return true; |
| 6429 | |
| 6430 | return false; |
| 6431 | } |
| 6432 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6433 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6434 | u8 link_type, u8 addr_type, u8 reason, |
| 6435 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6436 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6437 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6438 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6439 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6440 | /* The connection is still in hci_conn_hash so test for 1 |
| 6441 | * instead of 0 to know if this is the last one. |
| 6442 | */ |
| 6443 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 6444 | cancel_delayed_work(&hdev->power_off); |
| 6445 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6446 | } |
| 6447 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6448 | if (!mgmt_connected) |
| 6449 | return; |
| 6450 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6451 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6452 | return; |
| 6453 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6454 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6455 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6456 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6457 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6458 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6459 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6460 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6461 | |
| 6462 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6463 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6464 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6465 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6466 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6467 | } |
| 6468 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6469 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6470 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6471 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6472 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6473 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6474 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6475 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6476 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6477 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6478 | hdev); |
| 6479 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6480 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6481 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6482 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6483 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6484 | cp = cmd->param; |
| 6485 | |
| 6486 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6487 | return; |
| 6488 | |
| 6489 | if (cp->addr.type != bdaddr_type) |
| 6490 | return; |
| 6491 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6492 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6493 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 6494 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6495 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 6496 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6497 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6498 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6499 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6500 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6501 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6502 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6503 | { |
| 6504 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6505 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6506 | /* The connection is still in hci_conn_hash so test for 1 |
| 6507 | * instead of 0 to know if this is the last one. |
| 6508 | */ |
| 6509 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 6510 | cancel_delayed_work(&hdev->power_off); |
| 6511 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6512 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6513 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6514 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6515 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6516 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6517 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6518 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6519 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6520 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6521 | 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] | 6522 | { |
| 6523 | struct mgmt_ev_pin_code_request ev; |
| 6524 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6525 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6526 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6527 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6528 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6529 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6530 | } |
| 6531 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6532 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6533 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6534 | { |
| 6535 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6536 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6537 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6538 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6539 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6540 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6541 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6542 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6543 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6544 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6545 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6546 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6547 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6548 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6549 | } |
| 6550 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6551 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6552 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6553 | { |
| 6554 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6555 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6556 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6557 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6558 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6559 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6560 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6561 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6562 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6563 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6564 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6565 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6566 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6567 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6568 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6569 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6570 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6571 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6572 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6573 | { |
| 6574 | struct mgmt_ev_user_confirm_request ev; |
| 6575 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6576 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6577 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6578 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6579 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6580 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6581 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6582 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6583 | 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] | 6584 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6585 | } |
| 6586 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6587 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6588 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6589 | { |
| 6590 | struct mgmt_ev_user_passkey_request ev; |
| 6591 | |
| 6592 | BT_DBG("%s", hdev->name); |
| 6593 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6594 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6595 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6596 | |
| 6597 | 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] | 6598 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6599 | } |
| 6600 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6601 | 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] | 6602 | u8 link_type, u8 addr_type, u8 status, |
| 6603 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6604 | { |
| 6605 | struct pending_cmd *cmd; |
| 6606 | struct mgmt_rp_user_confirm_reply rp; |
| 6607 | int err; |
| 6608 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6609 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6610 | if (!cmd) |
| 6611 | return -ENOENT; |
| 6612 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6613 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6614 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6615 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6616 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6617 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6618 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6619 | |
| 6620 | return err; |
| 6621 | } |
| 6622 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6623 | 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] | 6624 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6625 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6626 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6627 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6628 | } |
| 6629 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6630 | 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] | 6631 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6632 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6633 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6634 | status, |
| 6635 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6636 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6637 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6638 | 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] | 6639 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6640 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6641 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6642 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6643 | } |
| 6644 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6645 | 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] | 6646 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6647 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6648 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6649 | status, |
| 6650 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6651 | } |
| 6652 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6653 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6654 | u8 link_type, u8 addr_type, u32 passkey, |
| 6655 | u8 entered) |
| 6656 | { |
| 6657 | struct mgmt_ev_passkey_notify ev; |
| 6658 | |
| 6659 | BT_DBG("%s", hdev->name); |
| 6660 | |
| 6661 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6662 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6663 | ev.passkey = __cpu_to_le32(passkey); |
| 6664 | ev.entered = entered; |
| 6665 | |
| 6666 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6667 | } |
| 6668 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 6669 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6670 | { |
| 6671 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 6672 | struct pending_cmd *cmd; |
| 6673 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6674 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 6675 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 6676 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 6677 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6678 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 6679 | cmd = find_pairing(conn); |
| 6680 | |
| 6681 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 6682 | cmd ? cmd->sk : NULL); |
| 6683 | |
| 6684 | if (cmd) |
| 6685 | pairing_complete(cmd, status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6686 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6687 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6688 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6689 | { |
| 6690 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6691 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6692 | |
| 6693 | if (status) { |
| 6694 | u8 mgmt_err = mgmt_status(status); |
| 6695 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6696 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6697 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6698 | } |
| 6699 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6700 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6701 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6702 | &hdev->dev_flags); |
| 6703 | else |
| 6704 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6705 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6706 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6707 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6708 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6709 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6710 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6711 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6712 | |
| 6713 | if (match.sk) |
| 6714 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6715 | } |
| 6716 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6717 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6718 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6719 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6720 | struct hci_cp_write_eir cp; |
| 6721 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6722 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6723 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6724 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6725 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6726 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6727 | memset(&cp, 0, sizeof(cp)); |
| 6728 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6729 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6730 | } |
| 6731 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6732 | 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] | 6733 | { |
| 6734 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6735 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6736 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6737 | |
| 6738 | if (status) { |
| 6739 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6740 | |
| 6741 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6742 | &hdev->dev_flags)) { |
| 6743 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6744 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6745 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6746 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6747 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6748 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6749 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6750 | } |
| 6751 | |
| 6752 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6753 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6754 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6755 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6756 | if (!changed) |
| 6757 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6758 | &hdev->dev_flags); |
| 6759 | else |
| 6760 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6761 | } |
| 6762 | |
| 6763 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6764 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6765 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6766 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6767 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6768 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6769 | sock_put(match.sk); |
| 6770 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6771 | hci_req_init(&req, hdev); |
| 6772 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6773 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6774 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6775 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6776 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6777 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6778 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6779 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6780 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6781 | |
| 6782 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6783 | } |
| 6784 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6785 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6786 | { |
| 6787 | struct cmd_lookup match = { NULL, hdev }; |
| 6788 | bool changed = false; |
| 6789 | |
| 6790 | if (status) { |
| 6791 | u8 mgmt_err = mgmt_status(status); |
| 6792 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6793 | if (enable) { |
| 6794 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6795 | &hdev->dev_flags)) |
| 6796 | new_settings(hdev, NULL); |
| 6797 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6798 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6799 | |
| 6800 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6801 | cmd_status_rsp, &mgmt_err); |
| 6802 | return; |
| 6803 | } |
| 6804 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6805 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6806 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6807 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6808 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6809 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6810 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6811 | |
| 6812 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6813 | settings_rsp, &match); |
| 6814 | |
| 6815 | if (changed) |
| 6816 | new_settings(hdev, match.sk); |
| 6817 | |
| 6818 | if (match.sk) |
| 6819 | sock_put(match.sk); |
| 6820 | } |
| 6821 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6822 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6823 | { |
| 6824 | struct cmd_lookup *match = data; |
| 6825 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6826 | if (match->sk == NULL) { |
| 6827 | match->sk = cmd->sk; |
| 6828 | sock_hold(match->sk); |
| 6829 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6830 | } |
| 6831 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6832 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6833 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6834 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6835 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6836 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6837 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6838 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6839 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6840 | |
| 6841 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6842 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6843 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6844 | |
| 6845 | if (match.sk) |
| 6846 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6847 | } |
| 6848 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6849 | 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] | 6850 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6851 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6852 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6853 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6854 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6855 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6856 | |
| 6857 | memset(&ev, 0, sizeof(ev)); |
| 6858 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6859 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6860 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6861 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6862 | if (!cmd) { |
| 6863 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6864 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6865 | /* If this is a HCI command related to powering on the |
| 6866 | * HCI dev don't send any mgmt signals. |
| 6867 | */ |
| 6868 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6869 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6870 | } |
| 6871 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6872 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6873 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6874 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6875 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6876 | 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] | 6877 | u8 *rand192, u8 *hash256, u8 *rand256, |
| 6878 | u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6879 | { |
| 6880 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6881 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6882 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6883 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6884 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6885 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6886 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6887 | |
| 6888 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6889 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6890 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6891 | } else { |
Johan Hedberg | 710f11c | 2014-05-26 11:21:22 +0300 | [diff] [blame] | 6892 | if (bredr_sc_enabled(hdev) && hash256 && rand256) { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6893 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6894 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6895 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 6896 | memcpy(rp.rand192, rand192, sizeof(rp.rand192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6897 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6898 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 6899 | memcpy(rp.rand256, rand256, sizeof(rp.rand256)); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6900 | |
| 6901 | cmd_complete(cmd->sk, hdev->id, |
| 6902 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6903 | &rp, sizeof(rp)); |
| 6904 | } else { |
| 6905 | struct mgmt_rp_read_local_oob_data rp; |
| 6906 | |
| 6907 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 6908 | memcpy(rp.rand, rand192, sizeof(rp.rand)); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6909 | |
| 6910 | cmd_complete(cmd->sk, hdev->id, |
| 6911 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6912 | &rp, sizeof(rp)); |
| 6913 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6914 | } |
| 6915 | |
| 6916 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6917 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6918 | |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 6919 | /* this is reversed hex representation of bluetooth base uuid. We need it for |
| 6920 | * service uuid parsing in eir. |
| 6921 | */ |
| 6922 | static const u8 reverse_base_uuid[] = { |
| 6923 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 6924 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 6925 | }; |
| 6926 | |
| 6927 | static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) |
| 6928 | { |
| 6929 | int i; |
| 6930 | |
| 6931 | for (i = 0; i < uuid_count; i++) { |
| 6932 | if (!memcmp(uuid, uuids[i], 16)) |
| 6933 | return true; |
| 6934 | } |
| 6935 | |
| 6936 | return false; |
| 6937 | } |
| 6938 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 6939 | static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16]) |
| 6940 | { |
Jakub Pawlowski | 799ce93 | 2014-12-05 10:55:58 +0100 | [diff] [blame] | 6941 | u16 parsed = 0; |
| 6942 | |
| 6943 | while (parsed < eir_len) { |
| 6944 | u8 field_len = eir[0]; |
| 6945 | u8 uuid[16]; |
| 6946 | int i; |
| 6947 | |
| 6948 | if (field_len == 0) |
| 6949 | break; |
| 6950 | |
| 6951 | if (eir_len - parsed < field_len + 1) |
| 6952 | break; |
| 6953 | |
| 6954 | switch (eir[1]) { |
| 6955 | case EIR_UUID16_ALL: |
| 6956 | case EIR_UUID16_SOME: |
| 6957 | for (i = 0; i + 3 <= field_len; i += 2) { |
| 6958 | memcpy(uuid, reverse_base_uuid, 16); |
| 6959 | uuid[13] = eir[i + 3]; |
| 6960 | uuid[12] = eir[i + 2]; |
| 6961 | if (has_uuid(uuid, uuid_count, uuids)) |
| 6962 | return true; |
| 6963 | } |
| 6964 | break; |
| 6965 | case EIR_UUID32_ALL: |
| 6966 | case EIR_UUID32_SOME: |
| 6967 | for (i = 0; i + 5 <= field_len; i += 4) { |
| 6968 | memcpy(uuid, reverse_base_uuid, 16); |
| 6969 | uuid[15] = eir[i + 5]; |
| 6970 | uuid[14] = eir[i + 4]; |
| 6971 | uuid[13] = eir[i + 3]; |
| 6972 | uuid[12] = eir[i + 2]; |
| 6973 | if (has_uuid(uuid, uuid_count, uuids)) |
| 6974 | return true; |
| 6975 | } |
| 6976 | break; |
| 6977 | case EIR_UUID128_ALL: |
| 6978 | case EIR_UUID128_SOME: |
| 6979 | for (i = 0; i + 17 <= field_len; i += 16) { |
| 6980 | memcpy(uuid, eir + i + 2, 16); |
| 6981 | if (has_uuid(uuid, uuid_count, uuids)) |
| 6982 | return true; |
| 6983 | } |
| 6984 | break; |
| 6985 | } |
| 6986 | |
| 6987 | parsed += field_len + 1; |
| 6988 | eir += field_len + 1; |
| 6989 | } |
| 6990 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 6991 | return false; |
| 6992 | } |
| 6993 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6994 | 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] | 6995 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6996 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6997 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6998 | char buf[512]; |
| 6999 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7000 | size_t ev_size; |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7001 | bool match; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7002 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7003 | /* Don't send events for a non-kernel initiated discovery. With |
| 7004 | * LE one exception is if we have pend_le_reports > 0 in which |
| 7005 | * case we're doing passive scanning and want these events. |
| 7006 | */ |
| 7007 | if (!hci_discovery_active(hdev)) { |
| 7008 | if (link_type == ACL_LINK) |
| 7009 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 7010 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 7011 | return; |
| 7012 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 7013 | |
Marcel Holtmann | bda157a | 2014-12-05 10:55:56 +0100 | [diff] [blame] | 7014 | /* When using service discovery with a RSSI threshold, then check |
| 7015 | * if such a RSSI threshold is specified. If a RSSI threshold has |
| 7016 | * been specified, then all results with a RSSI smaller than the |
| 7017 | * RSSI threshold will be dropped. |
| 7018 | */ |
| 7019 | if (hdev->discovery.rssi != HCI_RSSI_INVALID && |
| 7020 | rssi < hdev->discovery.rssi) |
| 7021 | return; |
| 7022 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7023 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 7024 | * are for the potential CoD field. |
| 7025 | */ |
| 7026 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7027 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 7028 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7029 | memset(buf, 0, sizeof(buf)); |
| 7030 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 7031 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 7032 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7033 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 7034 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7035 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7036 | if (eir_len > 0) { |
| 7037 | /* When using service discovery and a list of UUID is |
| 7038 | * provided, results with no matching UUID should be |
| 7039 | * dropped. In case there is a match the result is |
| 7040 | * kept and checking possible scan response data |
| 7041 | * will be skipped. |
| 7042 | */ |
| 7043 | if (hdev->discovery.uuid_count > 0) { |
| 7044 | match = eir_has_uuids(eir, eir_len, |
| 7045 | hdev->discovery.uuid_count, |
| 7046 | hdev->discovery.uuids); |
| 7047 | if (!match) |
| 7048 | return; |
| 7049 | } |
| 7050 | |
| 7051 | /* Copy EIR or advertising data into event */ |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 7052 | memcpy(ev->eir, eir, eir_len); |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7053 | } else { |
| 7054 | /* When using service discovery and a list of UUID is |
| 7055 | * provided, results with empty EIR or advertising data |
| 7056 | * should be dropped since they do not match any UUID. |
| 7057 | */ |
| 7058 | if (hdev->discovery.uuid_count > 0) |
| 7059 | return; |
| 7060 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7061 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7062 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 7063 | 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] | 7064 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 7065 | |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7066 | if (scan_rsp_len > 0) { |
| 7067 | /* When using service discovery and a list of UUID is |
| 7068 | * provided, results with no matching UUID should be |
| 7069 | * dropped if there is no previous match from the |
| 7070 | * advertising data. |
| 7071 | */ |
| 7072 | if (hdev->discovery.uuid_count > 0) { |
| 7073 | if (!match && !eir_has_uuids(scan_rsp, scan_rsp_len, |
| 7074 | hdev->discovery.uuid_count, |
| 7075 | hdev->discovery.uuids)) |
| 7076 | return; |
| 7077 | } |
| 7078 | |
| 7079 | /* Append scan response data to event */ |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7080 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
Marcel Holtmann | b487b9c | 2014-12-05 10:55:57 +0100 | [diff] [blame] | 7081 | } else { |
| 7082 | /* When using service discovery and a list of UUID is |
| 7083 | * provided, results with empty scan response and no |
| 7084 | * previous matched advertising data should be dropped. |
| 7085 | */ |
| 7086 | if (hdev->discovery.uuid_count > 0 && !match) |
| 7087 | return; |
| 7088 | } |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 7089 | |
| 7090 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 7091 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 7092 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 7093 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 7094 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7095 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7096 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 7097 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7098 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7099 | struct mgmt_ev_device_found *ev; |
| 7100 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 7101 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7102 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7103 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7104 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7105 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 7106 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7107 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 7108 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7109 | ev->rssi = rssi; |
| 7110 | |
| 7111 | 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] | 7112 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7113 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 7114 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 7115 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 7116 | 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] | 7117 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7118 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7119 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7120 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7121 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 7122 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 7123 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 7124 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 7125 | memset(&ev, 0, sizeof(ev)); |
| 7126 | ev.type = hdev->discovery.type; |
| 7127 | ev.discovering = discovering; |
| 7128 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 7129 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 7130 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 7131 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7132 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 7133 | { |
| 7134 | BT_DBG("%s status %u", hdev->name, status); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7135 | } |
| 7136 | |
| 7137 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 7138 | { |
| 7139 | struct hci_request req; |
| 7140 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7141 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 7142 | return; |
| 7143 | |
| 7144 | hci_req_init(&req, hdev); |
| 7145 | enable_advertising(&req); |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 7146 | hci_req_run(&req, adv_enable_complete); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 7147 | } |