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, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | static const u16 mgmt_events[] = { |
| 99 | MGMT_EV_CONTROLLER_ERROR, |
| 100 | MGMT_EV_INDEX_ADDED, |
| 101 | MGMT_EV_INDEX_REMOVED, |
| 102 | MGMT_EV_NEW_SETTINGS, |
| 103 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 104 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 105 | MGMT_EV_NEW_LINK_KEY, |
| 106 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 107 | MGMT_EV_DEVICE_CONNECTED, |
| 108 | MGMT_EV_DEVICE_DISCONNECTED, |
| 109 | MGMT_EV_CONNECT_FAILED, |
| 110 | MGMT_EV_PIN_CODE_REQUEST, |
| 111 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 112 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 113 | MGMT_EV_AUTH_FAILED, |
| 114 | MGMT_EV_DEVICE_FOUND, |
| 115 | MGMT_EV_DISCOVERING, |
| 116 | MGMT_EV_DEVICE_BLOCKED, |
| 117 | MGMT_EV_DEVICE_UNBLOCKED, |
| 118 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 119 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 120 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 121 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 122 | MGMT_EV_DEVICE_ADDED, |
| 123 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 124 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 125 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 126 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 127 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 130 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 131 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 132 | struct pending_cmd { |
| 133 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 134 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 135 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 136 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 137 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 138 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 139 | }; |
| 140 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 141 | /* HCI to MGMT error code conversion table */ |
| 142 | static u8 mgmt_status_table[] = { |
| 143 | MGMT_STATUS_SUCCESS, |
| 144 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 145 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 146 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 147 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 148 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 149 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 150 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 151 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 152 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 153 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 154 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 155 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 156 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 157 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 158 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 159 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 160 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 161 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 162 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 163 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 164 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 165 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 166 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 167 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 168 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 169 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 170 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 171 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 172 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 173 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 174 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 175 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 176 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 177 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 178 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 179 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 180 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 181 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 182 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 183 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 184 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 185 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 186 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 187 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 188 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 189 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 190 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 191 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 192 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 193 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 194 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 195 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 196 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 197 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 198 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 199 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 200 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 201 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 202 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 203 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 204 | }; |
| 205 | |
| 206 | static u8 mgmt_status(u8 hci_status) |
| 207 | { |
| 208 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 209 | return mgmt_status_table[hci_status]; |
| 210 | |
| 211 | return MGMT_STATUS_FAILED; |
| 212 | } |
| 213 | |
Marcel Holtmann | 04c60f05 | 2014-07-04 19:06:22 +0200 | [diff] [blame] | 214 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 215 | struct sock *skip_sk) |
| 216 | { |
| 217 | struct sk_buff *skb; |
| 218 | struct mgmt_hdr *hdr; |
| 219 | |
| 220 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
| 221 | if (!skb) |
| 222 | return -ENOMEM; |
| 223 | |
| 224 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 225 | hdr->opcode = cpu_to_le16(event); |
| 226 | if (hdev) |
| 227 | hdr->index = cpu_to_le16(hdev->id); |
| 228 | else |
| 229 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 230 | hdr->len = cpu_to_le16(data_len); |
| 231 | |
| 232 | if (data) |
| 233 | memcpy(skb_put(skb, data_len), data, data_len); |
| 234 | |
| 235 | /* Time stamp */ |
| 236 | __net_timestamp(skb); |
| 237 | |
| 238 | hci_send_to_control(skb, skip_sk); |
| 239 | kfree_skb(skb); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 244 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 245 | { |
| 246 | struct sk_buff *skb; |
| 247 | struct mgmt_hdr *hdr; |
| 248 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 249 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 250 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 251 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 252 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 253 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 254 | if (!skb) |
| 255 | return -ENOMEM; |
| 256 | |
| 257 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 258 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 259 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 260 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 261 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 262 | |
| 263 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 264 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 265 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 266 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 267 | err = sock_queue_rcv_skb(sk, skb); |
| 268 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 269 | kfree_skb(skb); |
| 270 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 271 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 274 | 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] | 275 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 276 | { |
| 277 | struct sk_buff *skb; |
| 278 | struct mgmt_hdr *hdr; |
| 279 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 280 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 281 | |
| 282 | BT_DBG("sock %p", sk); |
| 283 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 284 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 285 | if (!skb) |
| 286 | return -ENOMEM; |
| 287 | |
| 288 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 289 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 290 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 291 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 292 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 293 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 294 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 295 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 296 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 297 | |
| 298 | if (rp) |
| 299 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 300 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 301 | err = sock_queue_rcv_skb(sk, skb); |
| 302 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 303 | kfree_skb(skb); |
| 304 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 305 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 308 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 309 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 310 | { |
| 311 | struct mgmt_rp_read_version rp; |
| 312 | |
| 313 | BT_DBG("sock %p", sk); |
| 314 | |
| 315 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 316 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 317 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 318 | 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] | 319 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 322 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 323 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 324 | { |
| 325 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 326 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 327 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 328 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 329 | size_t rp_size; |
| 330 | int i, err; |
| 331 | |
| 332 | BT_DBG("sock %p", sk); |
| 333 | |
| 334 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 335 | |
| 336 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 337 | if (!rp) |
| 338 | return -ENOMEM; |
| 339 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 340 | rp->num_commands = cpu_to_le16(num_commands); |
| 341 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 342 | |
| 343 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 344 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 345 | |
| 346 | for (i = 0; i < num_events; i++, opcode++) |
| 347 | put_unaligned_le16(mgmt_events[i], opcode); |
| 348 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 349 | 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] | 350 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 351 | kfree(rp); |
| 352 | |
| 353 | return err; |
| 354 | } |
| 355 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 356 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 357 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 358 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 359 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 360 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 361 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 362 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 363 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 364 | |
| 365 | BT_DBG("sock %p", sk); |
| 366 | |
| 367 | read_lock(&hci_dev_list_lock); |
| 368 | |
| 369 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 370 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 371 | if (d->dev_type == HCI_BREDR && |
| 372 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 373 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 376 | rp_len = sizeof(*rp) + (2 * count); |
| 377 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 378 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 379 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 380 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 381 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 382 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 383 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 384 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 385 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 386 | test_bit(HCI_CONFIG, &d->dev_flags) || |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 387 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 388 | continue; |
| 389 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 390 | /* Devices marked as raw-only are neither configured |
| 391 | * nor unconfigured controllers. |
| 392 | */ |
| 393 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 394 | continue; |
| 395 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 396 | if (d->dev_type == HCI_BREDR && |
| 397 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 398 | rp->index[count++] = cpu_to_le16(d->id); |
| 399 | BT_DBG("Added hci%u", d->id); |
| 400 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 401 | } |
| 402 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 403 | rp->num_controllers = cpu_to_le16(count); |
| 404 | rp_len = sizeof(*rp) + (2 * count); |
| 405 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 406 | read_unlock(&hci_dev_list_lock); |
| 407 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 408 | 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] | 409 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 410 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 411 | kfree(rp); |
| 412 | |
| 413 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 416 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 417 | void *data, u16 data_len) |
| 418 | { |
| 419 | struct mgmt_rp_read_unconf_index_list *rp; |
| 420 | struct hci_dev *d; |
| 421 | size_t rp_len; |
| 422 | u16 count; |
| 423 | int err; |
| 424 | |
| 425 | BT_DBG("sock %p", sk); |
| 426 | |
| 427 | read_lock(&hci_dev_list_lock); |
| 428 | |
| 429 | count = 0; |
| 430 | list_for_each_entry(d, &hci_dev_list, list) { |
| 431 | if (d->dev_type == HCI_BREDR && |
| 432 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
| 433 | count++; |
| 434 | } |
| 435 | |
| 436 | rp_len = sizeof(*rp) + (2 * count); |
| 437 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 438 | if (!rp) { |
| 439 | read_unlock(&hci_dev_list_lock); |
| 440 | return -ENOMEM; |
| 441 | } |
| 442 | |
| 443 | count = 0; |
| 444 | list_for_each_entry(d, &hci_dev_list, list) { |
| 445 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 446 | test_bit(HCI_CONFIG, &d->dev_flags) || |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 447 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 448 | continue; |
| 449 | |
| 450 | /* Devices marked as raw-only are neither configured |
| 451 | * nor unconfigured controllers. |
| 452 | */ |
| 453 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 454 | continue; |
| 455 | |
| 456 | if (d->dev_type == HCI_BREDR && |
| 457 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
| 458 | rp->index[count++] = cpu_to_le16(d->id); |
| 459 | BT_DBG("Added hci%u", d->id); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | rp->num_controllers = cpu_to_le16(count); |
| 464 | rp_len = sizeof(*rp) + (2 * count); |
| 465 | |
| 466 | read_unlock(&hci_dev_list_lock); |
| 467 | |
| 468 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 469 | 0, rp, rp_len); |
| 470 | |
| 471 | kfree(rp); |
| 472 | |
| 473 | return err; |
| 474 | } |
| 475 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 476 | static bool is_configured(struct hci_dev *hdev) |
| 477 | { |
| 478 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 479 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
| 480 | return false; |
| 481 | |
| 482 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 483 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 484 | return false; |
| 485 | |
| 486 | return true; |
| 487 | } |
| 488 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 489 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 490 | { |
| 491 | u32 options = 0; |
| 492 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 493 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 494 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 495 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 496 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 497 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 498 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 499 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 500 | |
| 501 | return cpu_to_le32(options); |
| 502 | } |
| 503 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 504 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 505 | { |
| 506 | __le32 options = get_missing_options(hdev); |
| 507 | |
| 508 | return mgmt_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 509 | sizeof(options), skip); |
| 510 | } |
| 511 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 512 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 513 | { |
| 514 | __le32 options = get_missing_options(hdev); |
| 515 | |
| 516 | return cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 517 | sizeof(options)); |
| 518 | } |
| 519 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 520 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 521 | void *data, u16 data_len) |
| 522 | { |
| 523 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 524 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 525 | |
| 526 | BT_DBG("sock %p %s", sk, hdev->name); |
| 527 | |
| 528 | hci_dev_lock(hdev); |
| 529 | |
| 530 | memset(&rp, 0, sizeof(rp)); |
| 531 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 532 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 533 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 534 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 535 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 536 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 537 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 538 | |
| 539 | rp.supported_options = cpu_to_le32(options); |
| 540 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 541 | |
| 542 | hci_dev_unlock(hdev); |
| 543 | |
| 544 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp, |
| 545 | sizeof(rp)); |
| 546 | } |
| 547 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 548 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 549 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 550 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 551 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 552 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 553 | settings |= MGMT_SETTING_BONDABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 554 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 555 | settings |= MGMT_SETTING_CONNECTABLE; |
| 556 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 557 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 558 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 559 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 560 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 561 | settings |= MGMT_SETTING_BREDR; |
| 562 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 563 | |
| 564 | if (lmp_ssp_capable(hdev)) { |
| 565 | settings |= MGMT_SETTING_SSP; |
| 566 | settings |= MGMT_SETTING_HS; |
| 567 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 568 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 569 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 570 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 571 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 572 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 573 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 574 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 575 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 576 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 577 | settings |= MGMT_SETTING_SECURE_CONN; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 578 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 579 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 580 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 581 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 582 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 583 | settings |= MGMT_SETTING_CONFIGURATION; |
| 584 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 585 | return settings; |
| 586 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 587 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 588 | static u32 get_current_settings(struct hci_dev *hdev) |
| 589 | { |
| 590 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 591 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 592 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 593 | settings |= MGMT_SETTING_POWERED; |
| 594 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 595 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 596 | settings |= MGMT_SETTING_CONNECTABLE; |
| 597 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 598 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 599 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 600 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 601 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 602 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 603 | |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 604 | if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 605 | settings |= MGMT_SETTING_BONDABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 606 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 607 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 608 | settings |= MGMT_SETTING_BREDR; |
| 609 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 610 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 611 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 612 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 613 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 614 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 615 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 616 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 617 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 618 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 619 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 620 | settings |= MGMT_SETTING_HS; |
| 621 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 622 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 623 | settings |= MGMT_SETTING_ADVERTISING; |
| 624 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 625 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 626 | settings |= MGMT_SETTING_SECURE_CONN; |
| 627 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 628 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 629 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 630 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 631 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 632 | settings |= MGMT_SETTING_PRIVACY; |
| 633 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 634 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 637 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 638 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 639 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 640 | { |
| 641 | u8 *ptr = data, *uuids_start = NULL; |
| 642 | struct bt_uuid *uuid; |
| 643 | |
| 644 | if (len < 4) |
| 645 | return ptr; |
| 646 | |
| 647 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 648 | u16 uuid16; |
| 649 | |
| 650 | if (uuid->size != 16) |
| 651 | continue; |
| 652 | |
| 653 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 654 | if (uuid16 < 0x1100) |
| 655 | continue; |
| 656 | |
| 657 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 658 | continue; |
| 659 | |
| 660 | if (!uuids_start) { |
| 661 | uuids_start = ptr; |
| 662 | uuids_start[0] = 1; |
| 663 | uuids_start[1] = EIR_UUID16_ALL; |
| 664 | ptr += 2; |
| 665 | } |
| 666 | |
| 667 | /* Stop if not enough space to put next UUID */ |
| 668 | if ((ptr - data) + sizeof(u16) > len) { |
| 669 | uuids_start[1] = EIR_UUID16_SOME; |
| 670 | break; |
| 671 | } |
| 672 | |
| 673 | *ptr++ = (uuid16 & 0x00ff); |
| 674 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 675 | uuids_start[0] += sizeof(uuid16); |
| 676 | } |
| 677 | |
| 678 | return ptr; |
| 679 | } |
| 680 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 681 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 682 | { |
| 683 | u8 *ptr = data, *uuids_start = NULL; |
| 684 | struct bt_uuid *uuid; |
| 685 | |
| 686 | if (len < 6) |
| 687 | return ptr; |
| 688 | |
| 689 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 690 | if (uuid->size != 32) |
| 691 | continue; |
| 692 | |
| 693 | if (!uuids_start) { |
| 694 | uuids_start = ptr; |
| 695 | uuids_start[0] = 1; |
| 696 | uuids_start[1] = EIR_UUID32_ALL; |
| 697 | ptr += 2; |
| 698 | } |
| 699 | |
| 700 | /* Stop if not enough space to put next UUID */ |
| 701 | if ((ptr - data) + sizeof(u32) > len) { |
| 702 | uuids_start[1] = EIR_UUID32_SOME; |
| 703 | break; |
| 704 | } |
| 705 | |
| 706 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 707 | ptr += sizeof(u32); |
| 708 | uuids_start[0] += sizeof(u32); |
| 709 | } |
| 710 | |
| 711 | return ptr; |
| 712 | } |
| 713 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 714 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 715 | { |
| 716 | u8 *ptr = data, *uuids_start = NULL; |
| 717 | struct bt_uuid *uuid; |
| 718 | |
| 719 | if (len < 18) |
| 720 | return ptr; |
| 721 | |
| 722 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 723 | if (uuid->size != 128) |
| 724 | continue; |
| 725 | |
| 726 | if (!uuids_start) { |
| 727 | uuids_start = ptr; |
| 728 | uuids_start[0] = 1; |
| 729 | uuids_start[1] = EIR_UUID128_ALL; |
| 730 | ptr += 2; |
| 731 | } |
| 732 | |
| 733 | /* Stop if not enough space to put next UUID */ |
| 734 | if ((ptr - data) + 16 > len) { |
| 735 | uuids_start[1] = EIR_UUID128_SOME; |
| 736 | break; |
| 737 | } |
| 738 | |
| 739 | memcpy(ptr, uuid->uuid, 16); |
| 740 | ptr += 16; |
| 741 | uuids_start[0] += 16; |
| 742 | } |
| 743 | |
| 744 | return ptr; |
| 745 | } |
| 746 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 747 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 748 | { |
| 749 | struct pending_cmd *cmd; |
| 750 | |
| 751 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 752 | if (cmd->opcode == opcode) |
| 753 | return cmd; |
| 754 | } |
| 755 | |
| 756 | return NULL; |
| 757 | } |
| 758 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 759 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 760 | struct hci_dev *hdev, |
| 761 | const void *data) |
| 762 | { |
| 763 | struct pending_cmd *cmd; |
| 764 | |
| 765 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 766 | if (cmd->user_data != data) |
| 767 | continue; |
| 768 | if (cmd->opcode == opcode) |
| 769 | return cmd; |
| 770 | } |
| 771 | |
| 772 | return NULL; |
| 773 | } |
| 774 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 775 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 776 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 777 | u8 ad_len = 0; |
| 778 | size_t name_len; |
| 779 | |
| 780 | name_len = strlen(hdev->dev_name); |
| 781 | if (name_len > 0) { |
| 782 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 783 | |
| 784 | if (name_len > max_len) { |
| 785 | name_len = max_len; |
| 786 | ptr[1] = EIR_NAME_SHORT; |
| 787 | } else |
| 788 | ptr[1] = EIR_NAME_COMPLETE; |
| 789 | |
| 790 | ptr[0] = name_len + 1; |
| 791 | |
| 792 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 793 | |
| 794 | ad_len += (name_len + 2); |
| 795 | ptr += (name_len + 2); |
| 796 | } |
| 797 | |
| 798 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | static void update_scan_rsp_data(struct hci_request *req) |
| 802 | { |
| 803 | struct hci_dev *hdev = req->hdev; |
| 804 | struct hci_cp_le_set_scan_rsp_data cp; |
| 805 | u8 len; |
| 806 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 807 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 808 | return; |
| 809 | |
| 810 | memset(&cp, 0, sizeof(cp)); |
| 811 | |
| 812 | len = create_scan_rsp_data(hdev, cp.data); |
| 813 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 814 | if (hdev->scan_rsp_data_len == len && |
| 815 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 816 | return; |
| 817 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 818 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 819 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 820 | |
| 821 | cp.length = len; |
| 822 | |
| 823 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 824 | } |
| 825 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 826 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 827 | { |
| 828 | struct pending_cmd *cmd; |
| 829 | |
| 830 | /* If there's a pending mgmt command the flags will not yet have |
| 831 | * their final values, so check for this first. |
| 832 | */ |
| 833 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 834 | if (cmd) { |
| 835 | struct mgmt_mode *cp = cmd->param; |
| 836 | if (cp->val == 0x01) |
| 837 | return LE_AD_GENERAL; |
| 838 | else if (cp->val == 0x02) |
| 839 | return LE_AD_LIMITED; |
| 840 | } else { |
| 841 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 842 | return LE_AD_LIMITED; |
| 843 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 844 | return LE_AD_GENERAL; |
| 845 | } |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 850 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 851 | { |
| 852 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 853 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 854 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 855 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 856 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 857 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 858 | |
| 859 | if (flags) { |
| 860 | BT_DBG("adv flags 0x%02x", flags); |
| 861 | |
| 862 | ptr[0] = 2; |
| 863 | ptr[1] = EIR_FLAGS; |
| 864 | ptr[2] = flags; |
| 865 | |
| 866 | ad_len += 3; |
| 867 | ptr += 3; |
| 868 | } |
| 869 | |
| 870 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 871 | ptr[0] = 2; |
| 872 | ptr[1] = EIR_TX_POWER; |
| 873 | ptr[2] = (u8) hdev->adv_tx_power; |
| 874 | |
| 875 | ad_len += 3; |
| 876 | ptr += 3; |
| 877 | } |
| 878 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 879 | return ad_len; |
| 880 | } |
| 881 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 882 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 883 | { |
| 884 | struct hci_dev *hdev = req->hdev; |
| 885 | struct hci_cp_le_set_adv_data cp; |
| 886 | u8 len; |
| 887 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 888 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 889 | return; |
| 890 | |
| 891 | memset(&cp, 0, sizeof(cp)); |
| 892 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 893 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 894 | |
| 895 | if (hdev->adv_data_len == len && |
| 896 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 897 | return; |
| 898 | |
| 899 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 900 | hdev->adv_data_len = len; |
| 901 | |
| 902 | cp.length = len; |
| 903 | |
| 904 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 905 | } |
| 906 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 907 | int mgmt_update_adv_data(struct hci_dev *hdev) |
| 908 | { |
| 909 | struct hci_request req; |
| 910 | |
| 911 | hci_req_init(&req, hdev); |
| 912 | update_adv_data(&req); |
| 913 | |
| 914 | return hci_req_run(&req, NULL); |
| 915 | } |
| 916 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 917 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 918 | { |
| 919 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 920 | size_t name_len; |
| 921 | |
| 922 | name_len = strlen(hdev->dev_name); |
| 923 | |
| 924 | if (name_len > 0) { |
| 925 | /* EIR Data type */ |
| 926 | if (name_len > 48) { |
| 927 | name_len = 48; |
| 928 | ptr[1] = EIR_NAME_SHORT; |
| 929 | } else |
| 930 | ptr[1] = EIR_NAME_COMPLETE; |
| 931 | |
| 932 | /* EIR Data length */ |
| 933 | ptr[0] = name_len + 1; |
| 934 | |
| 935 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 936 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 937 | ptr += (name_len + 2); |
| 938 | } |
| 939 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 940 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 941 | ptr[0] = 2; |
| 942 | ptr[1] = EIR_TX_POWER; |
| 943 | ptr[2] = (u8) hdev->inq_tx_power; |
| 944 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 945 | ptr += 3; |
| 946 | } |
| 947 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 948 | if (hdev->devid_source > 0) { |
| 949 | ptr[0] = 9; |
| 950 | ptr[1] = EIR_DEVICE_ID; |
| 951 | |
| 952 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 953 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 954 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 955 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 956 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 957 | ptr += 10; |
| 958 | } |
| 959 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 960 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 961 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 962 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 963 | } |
| 964 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 965 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 966 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 967 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 968 | struct hci_cp_write_eir cp; |
| 969 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 970 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 971 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 972 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 973 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 974 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 975 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 976 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 977 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 978 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 979 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 980 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 981 | |
| 982 | memset(&cp, 0, sizeof(cp)); |
| 983 | |
| 984 | create_eir(hdev, cp.data); |
| 985 | |
| 986 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 987 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 988 | |
| 989 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 990 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 991 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 992 | } |
| 993 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 994 | static u8 get_service_classes(struct hci_dev *hdev) |
| 995 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 996 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 997 | u8 val = 0; |
| 998 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 999 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1000 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1001 | |
| 1002 | return val; |
| 1003 | } |
| 1004 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1005 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1006 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1007 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1008 | u8 cod[3]; |
| 1009 | |
| 1010 | BT_DBG("%s", hdev->name); |
| 1011 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 1012 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1013 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 1014 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 1015 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1016 | return; |
| 1017 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1018 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1019 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1020 | |
| 1021 | cod[0] = hdev->minor_class; |
| 1022 | cod[1] = hdev->major_class; |
| 1023 | cod[2] = get_service_classes(hdev); |
| 1024 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 1025 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 1026 | cod[1] |= 0x20; |
| 1027 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1028 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1029 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1030 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1031 | 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] | 1032 | } |
| 1033 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1034 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1035 | { |
| 1036 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1037 | |
| 1038 | /* If there's a pending mgmt command the flag will not yet have |
| 1039 | * it's final value, so check for this first. |
| 1040 | */ |
| 1041 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1042 | if (cmd) { |
| 1043 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1044 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1047 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1050 | static void disable_advertising(struct hci_request *req) |
| 1051 | { |
| 1052 | u8 enable = 0x00; |
| 1053 | |
| 1054 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1055 | } |
| 1056 | |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1057 | static void enable_advertising(struct hci_request *req) |
| 1058 | { |
| 1059 | struct hci_dev *hdev = req->hdev; |
| 1060 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1061 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1062 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1063 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1064 | if (hci_conn_num(hdev, LE_LINK) > 0) |
| 1065 | return; |
| 1066 | |
| 1067 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
| 1068 | disable_advertising(req); |
| 1069 | |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1070 | /* Clear the HCI_LE_ADV bit temporarily so that the |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1071 | * hci_update_random_address knows that it's safe to go ahead |
| 1072 | * and write a new random address. The flag will be set back on |
| 1073 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 1074 | */ |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1075 | clear_bit(HCI_LE_ADV, &hdev->dev_flags); |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1076 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1077 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1078 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1079 | /* Set require_privacy to true only when non-connectable |
| 1080 | * advertising is used. In that case it is fine to use a |
| 1081 | * non-resolvable private address. |
| 1082 | */ |
| 1083 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1084 | return; |
| 1085 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 1086 | memset(&cp, 0, sizeof(cp)); |
Georg Lukas | 628531c | 2014-07-26 13:59:57 +0200 | [diff] [blame] | 1087 | cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval); |
| 1088 | cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1089 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1090 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1091 | cp.channel_map = hdev->le_adv_channel_map; |
| 1092 | |
| 1093 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1094 | |
| 1095 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1096 | } |
| 1097 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1098 | static void service_cache_off(struct work_struct *work) |
| 1099 | { |
| 1100 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1101 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1102 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1103 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1104 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1105 | return; |
| 1106 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1107 | hci_req_init(&req, hdev); |
| 1108 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1109 | hci_dev_lock(hdev); |
| 1110 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1111 | update_eir(&req); |
| 1112 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1113 | |
| 1114 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1115 | |
| 1116 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1117 | } |
| 1118 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1119 | static void rpa_expired(struct work_struct *work) |
| 1120 | { |
| 1121 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1122 | rpa_expired.work); |
| 1123 | struct hci_request req; |
| 1124 | |
| 1125 | BT_DBG(""); |
| 1126 | |
| 1127 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 1128 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1129 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1130 | return; |
| 1131 | |
| 1132 | /* The generation of a new RPA and programming it into the |
| 1133 | * controller happens in the enable_advertising() function. |
| 1134 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1135 | hci_req_init(&req, hdev); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1136 | enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1137 | hci_req_run(&req, NULL); |
| 1138 | } |
| 1139 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1140 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1141 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1142 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1143 | return; |
| 1144 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1145 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1146 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1147 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1148 | /* Non-mgmt controlled devices get this bit set |
| 1149 | * implicitly so that pairing works for them, however |
| 1150 | * for mgmt we require user-space to explicitly enable |
| 1151 | * it |
| 1152 | */ |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1153 | clear_bit(HCI_BONDABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1154 | } |
| 1155 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1156 | 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] | 1157 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1158 | { |
| 1159 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1160 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1161 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1162 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1163 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1164 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1165 | memset(&rp, 0, sizeof(rp)); |
| 1166 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1167 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1168 | |
| 1169 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1170 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1171 | |
| 1172 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1173 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1174 | |
| 1175 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1176 | |
| 1177 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1178 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1179 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1180 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1181 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1182 | 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] | 1183 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1187 | { |
| 1188 | sock_put(cmd->sk); |
| 1189 | kfree(cmd->param); |
| 1190 | kfree(cmd); |
| 1191 | } |
| 1192 | |
| 1193 | 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] | 1194 | struct hci_dev *hdev, void *data, |
| 1195 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1196 | { |
| 1197 | struct pending_cmd *cmd; |
| 1198 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1199 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1200 | if (!cmd) |
| 1201 | return NULL; |
| 1202 | |
| 1203 | cmd->opcode = opcode; |
| 1204 | cmd->index = hdev->id; |
| 1205 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1206 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1207 | if (!cmd->param) { |
| 1208 | kfree(cmd); |
| 1209 | return NULL; |
| 1210 | } |
| 1211 | |
| 1212 | if (data) |
| 1213 | memcpy(cmd->param, data, len); |
| 1214 | |
| 1215 | cmd->sk = sk; |
| 1216 | sock_hold(sk); |
| 1217 | |
| 1218 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1219 | |
| 1220 | return cmd; |
| 1221 | } |
| 1222 | |
| 1223 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1224 | void (*cb)(struct pending_cmd *cmd, |
| 1225 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1226 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1227 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1228 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1229 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1230 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1231 | if (opcode > 0 && cmd->opcode != opcode) |
| 1232 | continue; |
| 1233 | |
| 1234 | cb(cmd, data); |
| 1235 | } |
| 1236 | } |
| 1237 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1238 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1239 | { |
| 1240 | list_del(&cmd->list); |
| 1241 | mgmt_pending_free(cmd); |
| 1242 | } |
| 1243 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1244 | 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] | 1245 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1246 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1247 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1248 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1249 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1250 | } |
| 1251 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1252 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1253 | { |
| 1254 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1255 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1256 | if (hci_conn_count(hdev) == 0) { |
| 1257 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1258 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1259 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1260 | } |
| 1261 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1262 | static bool hci_stop_discovery(struct hci_request *req) |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1263 | { |
| 1264 | struct hci_dev *hdev = req->hdev; |
| 1265 | struct hci_cp_remote_name_req_cancel cp; |
| 1266 | struct inquiry_entry *e; |
| 1267 | |
| 1268 | switch (hdev->discovery.state) { |
| 1269 | case DISCOVERY_FINDING: |
| 1270 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1271 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1272 | } else { |
| 1273 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1274 | hci_req_add_le_scan_disable(req); |
| 1275 | } |
| 1276 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1277 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1278 | |
| 1279 | case DISCOVERY_RESOLVING: |
| 1280 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1281 | NAME_PENDING); |
| 1282 | if (!e) |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1283 | break; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1284 | |
| 1285 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1286 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1287 | &cp); |
| 1288 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1289 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1290 | |
| 1291 | default: |
| 1292 | /* Passive scanning */ |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1293 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1294 | hci_req_add_le_scan_disable(req); |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1295 | return true; |
| 1296 | } |
| 1297 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1298 | break; |
| 1299 | } |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1300 | |
| 1301 | return false; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1302 | } |
| 1303 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1304 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1305 | { |
| 1306 | struct hci_request req; |
| 1307 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1308 | bool discov_stopped; |
| 1309 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1310 | |
| 1311 | hci_req_init(&req, hdev); |
| 1312 | |
| 1313 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1314 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1315 | u8 scan = 0x00; |
| 1316 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1317 | } |
| 1318 | |
Johan Hedberg | 73e082f | 2014-07-08 15:07:51 +0300 | [diff] [blame] | 1319 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1320 | disable_advertising(&req); |
| 1321 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1322 | discov_stopped = hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1323 | |
| 1324 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1325 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1326 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1327 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1328 | switch (conn->state) { |
| 1329 | case BT_CONNECTED: |
| 1330 | case BT_CONFIG: |
| 1331 | dc.handle = cpu_to_le16(conn->handle); |
| 1332 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1333 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1334 | break; |
| 1335 | case BT_CONNECT: |
| 1336 | if (conn->type == LE_LINK) |
| 1337 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1338 | 0, NULL); |
| 1339 | else if (conn->type == ACL_LINK) |
| 1340 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1341 | 6, &conn->dst); |
| 1342 | break; |
| 1343 | case BT_CONNECT2: |
| 1344 | bacpy(&rej.bdaddr, &conn->dst); |
| 1345 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1346 | if (conn->type == ACL_LINK) |
| 1347 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1348 | sizeof(rej), &rej); |
| 1349 | else if (conn->type == SCO_LINK) |
| 1350 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1351 | sizeof(rej), &rej); |
| 1352 | break; |
| 1353 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1354 | } |
| 1355 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1356 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1357 | if (!err && discov_stopped) |
| 1358 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1359 | |
| 1360 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1361 | } |
| 1362 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1363 | 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] | 1364 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1365 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1366 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1367 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1368 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1369 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1370 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1371 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1372 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1373 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1374 | MGMT_STATUS_INVALID_PARAMS); |
| 1375 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1376 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1377 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1378 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1379 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1380 | MGMT_STATUS_BUSY); |
| 1381 | goto failed; |
| 1382 | } |
| 1383 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1384 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1385 | cancel_delayed_work(&hdev->power_off); |
| 1386 | |
| 1387 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1388 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1389 | data, len); |
| 1390 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1391 | goto failed; |
| 1392 | } |
| 1393 | } |
| 1394 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1395 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1396 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1397 | goto failed; |
| 1398 | } |
| 1399 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1400 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1401 | if (!cmd) { |
| 1402 | err = -ENOMEM; |
| 1403 | goto failed; |
| 1404 | } |
| 1405 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1406 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1407 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1408 | err = 0; |
| 1409 | } else { |
| 1410 | /* Disconnect connections, stop scans, etc */ |
| 1411 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1412 | if (!err) |
| 1413 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1414 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1415 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1416 | /* ENODATA means there were no HCI commands queued */ |
| 1417 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1418 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1419 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1420 | err = 0; |
| 1421 | } |
| 1422 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1423 | |
| 1424 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1425 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1426 | return err; |
| 1427 | } |
| 1428 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1429 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1430 | { |
| 1431 | __le32 ev; |
| 1432 | |
| 1433 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1434 | |
| 1435 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1436 | } |
| 1437 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1438 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1439 | { |
| 1440 | return new_settings(hdev, NULL); |
| 1441 | } |
| 1442 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1443 | struct cmd_lookup { |
| 1444 | struct sock *sk; |
| 1445 | struct hci_dev *hdev; |
| 1446 | u8 mgmt_status; |
| 1447 | }; |
| 1448 | |
| 1449 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1450 | { |
| 1451 | struct cmd_lookup *match = data; |
| 1452 | |
| 1453 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1454 | |
| 1455 | list_del(&cmd->list); |
| 1456 | |
| 1457 | if (match->sk == NULL) { |
| 1458 | match->sk = cmd->sk; |
| 1459 | sock_hold(match->sk); |
| 1460 | } |
| 1461 | |
| 1462 | mgmt_pending_free(cmd); |
| 1463 | } |
| 1464 | |
| 1465 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1466 | { |
| 1467 | u8 *status = data; |
| 1468 | |
| 1469 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1470 | mgmt_pending_remove(cmd); |
| 1471 | } |
| 1472 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1473 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1474 | { |
| 1475 | if (!lmp_bredr_capable(hdev)) |
| 1476 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1477 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1478 | return MGMT_STATUS_REJECTED; |
| 1479 | else |
| 1480 | return MGMT_STATUS_SUCCESS; |
| 1481 | } |
| 1482 | |
| 1483 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1484 | { |
| 1485 | if (!lmp_le_capable(hdev)) |
| 1486 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1487 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1488 | return MGMT_STATUS_REJECTED; |
| 1489 | else |
| 1490 | return MGMT_STATUS_SUCCESS; |
| 1491 | } |
| 1492 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1493 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1494 | { |
| 1495 | struct pending_cmd *cmd; |
| 1496 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1497 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1498 | bool changed; |
| 1499 | |
| 1500 | BT_DBG("status 0x%02x", status); |
| 1501 | |
| 1502 | hci_dev_lock(hdev); |
| 1503 | |
| 1504 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1505 | if (!cmd) |
| 1506 | goto unlock; |
| 1507 | |
| 1508 | if (status) { |
| 1509 | u8 mgmt_err = mgmt_status(status); |
| 1510 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1511 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1512 | goto remove_cmd; |
| 1513 | } |
| 1514 | |
| 1515 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1516 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1517 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1518 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1519 | |
| 1520 | if (hdev->discov_timeout > 0) { |
| 1521 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1522 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1523 | to); |
| 1524 | } |
| 1525 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1526 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1527 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1528 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1529 | |
| 1530 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1531 | |
| 1532 | if (changed) |
| 1533 | new_settings(hdev, cmd->sk); |
| 1534 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1535 | /* When the discoverable mode gets changed, make sure |
| 1536 | * that class of device has the limited discoverable |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1537 | * bit correctly set. Also update page scan based on whitelist |
| 1538 | * entries. |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1539 | */ |
| 1540 | hci_req_init(&req, hdev); |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1541 | hci_update_page_scan(hdev, &req); |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1542 | update_class(&req); |
| 1543 | hci_req_run(&req, NULL); |
| 1544 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1545 | remove_cmd: |
| 1546 | mgmt_pending_remove(cmd); |
| 1547 | |
| 1548 | unlock: |
| 1549 | hci_dev_unlock(hdev); |
| 1550 | } |
| 1551 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1552 | 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] | 1553 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1554 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1555 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1556 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1557 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1558 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1559 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1560 | int err; |
| 1561 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1562 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1563 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1564 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1565 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1566 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1567 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1568 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1569 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1570 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1571 | MGMT_STATUS_INVALID_PARAMS); |
| 1572 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1573 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1574 | |
| 1575 | /* Disabling discoverable requires that no timeout is set, |
| 1576 | * and enabling limited discoverable requires a timeout. |
| 1577 | */ |
| 1578 | if ((cp->val == 0x00 && timeout > 0) || |
| 1579 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1580 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1581 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1582 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1583 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1584 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1585 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1586 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1587 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1588 | goto failed; |
| 1589 | } |
| 1590 | |
| 1591 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1592 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1593 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1594 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1595 | goto failed; |
| 1596 | } |
| 1597 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1598 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1599 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1600 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1601 | goto failed; |
| 1602 | } |
| 1603 | |
| 1604 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1605 | bool changed = false; |
| 1606 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1607 | /* Setting limited discoverable when powered off is |
| 1608 | * not a valid operation since it requires a timeout |
| 1609 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1610 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1611 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1612 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1613 | changed = true; |
| 1614 | } |
| 1615 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1616 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1617 | if (err < 0) |
| 1618 | goto failed; |
| 1619 | |
| 1620 | if (changed) |
| 1621 | err = new_settings(hdev, sk); |
| 1622 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1623 | goto failed; |
| 1624 | } |
| 1625 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1626 | /* If the current mode is the same, then just update the timeout |
| 1627 | * value with the new value. And if only the timeout gets updated, |
| 1628 | * then no need for any HCI transactions. |
| 1629 | */ |
| 1630 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1631 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1632 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1633 | cancel_delayed_work(&hdev->discov_off); |
| 1634 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1635 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1636 | if (cp->val && hdev->discov_timeout > 0) { |
| 1637 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1638 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1639 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1642 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1643 | goto failed; |
| 1644 | } |
| 1645 | |
| 1646 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1647 | if (!cmd) { |
| 1648 | err = -ENOMEM; |
| 1649 | goto failed; |
| 1650 | } |
| 1651 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1652 | /* Cancel any potential discoverable timeout that might be |
| 1653 | * still active and store new timeout value. The arming of |
| 1654 | * the timeout happens in the complete handler. |
| 1655 | */ |
| 1656 | cancel_delayed_work(&hdev->discov_off); |
| 1657 | hdev->discov_timeout = timeout; |
| 1658 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1659 | /* Limited discoverable mode */ |
| 1660 | if (cp->val == 0x02) |
| 1661 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1662 | else |
| 1663 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1664 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1665 | hci_req_init(&req, hdev); |
| 1666 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1667 | /* The procedure for LE-only controllers is much simpler - just |
| 1668 | * update the advertising data. |
| 1669 | */ |
| 1670 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1671 | goto update_ad; |
| 1672 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1673 | scan = SCAN_PAGE; |
| 1674 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1675 | if (cp->val) { |
| 1676 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1677 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1678 | if (cp->val == 0x02) { |
| 1679 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1680 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1681 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1682 | hci_cp.iac_lap[1] = 0x8b; |
| 1683 | hci_cp.iac_lap[2] = 0x9e; |
| 1684 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1685 | hci_cp.iac_lap[4] = 0x8b; |
| 1686 | hci_cp.iac_lap[5] = 0x9e; |
| 1687 | } else { |
| 1688 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1689 | hci_cp.num_iac = 1; |
| 1690 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1691 | hci_cp.iac_lap[1] = 0x8b; |
| 1692 | hci_cp.iac_lap[2] = 0x9e; |
| 1693 | } |
| 1694 | |
| 1695 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1696 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1697 | |
| 1698 | scan |= SCAN_INQUIRY; |
| 1699 | } else { |
| 1700 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1701 | } |
| 1702 | |
| 1703 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1704 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1705 | update_ad: |
| 1706 | update_adv_data(&req); |
| 1707 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1708 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1709 | if (err < 0) |
| 1710 | mgmt_pending_remove(cmd); |
| 1711 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1712 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1713 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1714 | return err; |
| 1715 | } |
| 1716 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1717 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1718 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1719 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1720 | struct hci_cp_write_page_scan_activity acp; |
| 1721 | u8 type; |
| 1722 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1723 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1724 | return; |
| 1725 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1726 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1727 | return; |
| 1728 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1729 | if (enable) { |
| 1730 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1731 | |
| 1732 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1733 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1734 | } else { |
| 1735 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1736 | |
| 1737 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1738 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1739 | } |
| 1740 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1741 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1742 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1743 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1744 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1745 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1746 | sizeof(acp), &acp); |
| 1747 | |
| 1748 | if (hdev->page_scan_type != type) |
| 1749 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1750 | } |
| 1751 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1752 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1753 | { |
| 1754 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1755 | struct mgmt_mode *cp; |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1756 | bool conn_changed, discov_changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1757 | |
| 1758 | BT_DBG("status 0x%02x", status); |
| 1759 | |
| 1760 | hci_dev_lock(hdev); |
| 1761 | |
| 1762 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1763 | if (!cmd) |
| 1764 | goto unlock; |
| 1765 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1766 | if (status) { |
| 1767 | u8 mgmt_err = mgmt_status(status); |
| 1768 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1769 | goto remove_cmd; |
| 1770 | } |
| 1771 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1772 | cp = cmd->param; |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1773 | if (cp->val) { |
| 1774 | conn_changed = !test_and_set_bit(HCI_CONNECTABLE, |
| 1775 | &hdev->dev_flags); |
| 1776 | discov_changed = false; |
| 1777 | } else { |
| 1778 | conn_changed = test_and_clear_bit(HCI_CONNECTABLE, |
| 1779 | &hdev->dev_flags); |
| 1780 | discov_changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1781 | &hdev->dev_flags); |
| 1782 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1783 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1784 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1785 | |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1786 | if (conn_changed || discov_changed) { |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1787 | new_settings(hdev, cmd->sk); |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1788 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | bc6d2d0 | 2014-07-10 12:09:08 +0300 | [diff] [blame] | 1789 | if (discov_changed) |
| 1790 | mgmt_update_adv_data(hdev); |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1791 | hci_update_background_scan(hdev); |
| 1792 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1793 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1794 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1795 | mgmt_pending_remove(cmd); |
| 1796 | |
| 1797 | unlock: |
| 1798 | hci_dev_unlock(hdev); |
| 1799 | } |
| 1800 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1801 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1802 | struct sock *sk, u8 val) |
| 1803 | { |
| 1804 | bool changed = false; |
| 1805 | int err; |
| 1806 | |
| 1807 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1808 | changed = true; |
| 1809 | |
| 1810 | if (val) { |
| 1811 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1812 | } else { |
| 1813 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1814 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1815 | } |
| 1816 | |
| 1817 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1818 | if (err < 0) |
| 1819 | return err; |
| 1820 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1821 | if (changed) { |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 1822 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1823 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1824 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1825 | } |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1826 | |
| 1827 | return 0; |
| 1828 | } |
| 1829 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1830 | 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] | 1831 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1832 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1833 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1834 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1835 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1836 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1837 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1838 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1839 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1840 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1841 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1842 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1843 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1844 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1845 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1846 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1847 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1848 | MGMT_STATUS_INVALID_PARAMS); |
| 1849 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1850 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1851 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1852 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1853 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1854 | goto failed; |
| 1855 | } |
| 1856 | |
| 1857 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1858 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1859 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1860 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1861 | goto failed; |
| 1862 | } |
| 1863 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1864 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1865 | if (!cmd) { |
| 1866 | err = -ENOMEM; |
| 1867 | goto failed; |
| 1868 | } |
| 1869 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1870 | hci_req_init(&req, hdev); |
| 1871 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1872 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1873 | * by-product of disabling connectable, we need to update the |
| 1874 | * advertising flags. |
| 1875 | */ |
| 1876 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1877 | if (!cp->val) { |
| 1878 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1879 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1880 | } |
| 1881 | update_adv_data(&req); |
| 1882 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1883 | if (cp->val) { |
| 1884 | scan = SCAN_PAGE; |
| 1885 | } else { |
Johan Hedberg | 3bd2724 | 2014-07-28 20:53:58 +0300 | [diff] [blame] | 1886 | /* If we don't have any whitelist entries just |
| 1887 | * disable all scanning. If there are entries |
| 1888 | * and we had both page and inquiry scanning |
| 1889 | * enabled then fall back to only page scanning. |
| 1890 | * Otherwise no changes are needed. |
| 1891 | */ |
| 1892 | if (list_empty(&hdev->whitelist)) |
| 1893 | scan = SCAN_DISABLED; |
| 1894 | else if (test_bit(HCI_ISCAN, &hdev->flags)) |
| 1895 | scan = SCAN_PAGE; |
| 1896 | else |
| 1897 | goto no_scan_update; |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1898 | |
| 1899 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1900 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1901 | cancel_delayed_work(&hdev->discov_off); |
| 1902 | } |
| 1903 | |
| 1904 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1905 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1906 | |
Johan Hedberg | 3bd2724 | 2014-07-28 20:53:58 +0300 | [diff] [blame] | 1907 | no_scan_update: |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1908 | /* If we're going from non-connectable to connectable or |
| 1909 | * vice-versa when fast connectable is enabled ensure that fast |
| 1910 | * connectable gets disabled. write_fast_connectable won't do |
| 1911 | * anything if the page scan parameters are already what they |
| 1912 | * should be. |
| 1913 | */ |
| 1914 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1915 | write_fast_connectable(&req, false); |
| 1916 | |
Johan Hedberg | e8b1202 | 2014-07-10 10:51:27 +0300 | [diff] [blame] | 1917 | /* Update the advertising parameters if necessary */ |
| 1918 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1919 | enable_advertising(&req); |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1920 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1921 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1922 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1923 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1924 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1925 | err = set_connectable_update_settings(hdev, sk, |
| 1926 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1927 | goto failed; |
| 1928 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1929 | |
| 1930 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1931 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1932 | return err; |
| 1933 | } |
| 1934 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1935 | 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] | 1936 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1937 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1938 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1939 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1940 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1941 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1942 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1943 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1944 | if (cp->val != 0x00 && cp->val != 0x01) |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1945 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1946 | MGMT_STATUS_INVALID_PARAMS); |
| 1947 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1948 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1949 | |
| 1950 | if (cp->val) |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1951 | changed = !test_and_set_bit(HCI_BONDABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1952 | else |
Johan Hedberg | b6ae845 | 2014-07-30 09:22:22 +0300 | [diff] [blame] | 1953 | changed = test_and_clear_bit(HCI_BONDABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1954 | |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 1955 | err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1956 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1957 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1958 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1959 | if (changed) |
| 1960 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1961 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1962 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1963 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1964 | return err; |
| 1965 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1966 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1967 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1968 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1969 | { |
| 1970 | struct mgmt_mode *cp = data; |
| 1971 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1972 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1973 | int err; |
| 1974 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1975 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1976 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1977 | status = mgmt_bredr_support(hdev); |
| 1978 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1979 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1980 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1981 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1982 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1983 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1984 | MGMT_STATUS_INVALID_PARAMS); |
| 1985 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1986 | hci_dev_lock(hdev); |
| 1987 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1988 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1989 | bool changed = false; |
| 1990 | |
| 1991 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1992 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1993 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1994 | changed = true; |
| 1995 | } |
| 1996 | |
| 1997 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1998 | if (err < 0) |
| 1999 | goto failed; |
| 2000 | |
| 2001 | if (changed) |
| 2002 | err = new_settings(hdev, sk); |
| 2003 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2004 | goto failed; |
| 2005 | } |
| 2006 | |
| 2007 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2008 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2009 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2010 | goto failed; |
| 2011 | } |
| 2012 | |
| 2013 | val = !!cp->val; |
| 2014 | |
| 2015 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 2016 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 2017 | goto failed; |
| 2018 | } |
| 2019 | |
| 2020 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 2021 | if (!cmd) { |
| 2022 | err = -ENOMEM; |
| 2023 | goto failed; |
| 2024 | } |
| 2025 | |
| 2026 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 2027 | if (err < 0) { |
| 2028 | mgmt_pending_remove(cmd); |
| 2029 | goto failed; |
| 2030 | } |
| 2031 | |
| 2032 | failed: |
| 2033 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2034 | return err; |
| 2035 | } |
| 2036 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2037 | 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] | 2038 | { |
| 2039 | struct mgmt_mode *cp = data; |
| 2040 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2041 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2042 | int err; |
| 2043 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2044 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2045 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 2046 | status = mgmt_bredr_support(hdev); |
| 2047 | if (status) |
| 2048 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 2049 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2050 | if (!lmp_ssp_capable(hdev)) |
| 2051 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2052 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2053 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2054 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2055 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2056 | MGMT_STATUS_INVALID_PARAMS); |
| 2057 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2058 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 2059 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2060 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2061 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2062 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2063 | if (cp->val) { |
| 2064 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 2065 | &hdev->dev_flags); |
| 2066 | } else { |
| 2067 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 2068 | &hdev->dev_flags); |
| 2069 | if (!changed) |
| 2070 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 2071 | &hdev->dev_flags); |
| 2072 | else |
| 2073 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2077 | if (err < 0) |
| 2078 | goto failed; |
| 2079 | |
| 2080 | if (changed) |
| 2081 | err = new_settings(hdev, sk); |
| 2082 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2083 | goto failed; |
| 2084 | } |
| 2085 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2086 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 2087 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 2088 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2089 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2090 | goto failed; |
| 2091 | } |
| 2092 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2093 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2094 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2095 | goto failed; |
| 2096 | } |
| 2097 | |
| 2098 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 2099 | if (!cmd) { |
| 2100 | err = -ENOMEM; |
| 2101 | goto failed; |
| 2102 | } |
| 2103 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 2104 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 2105 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2106 | sizeof(cp->val), &cp->val); |
| 2107 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2108 | 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] | 2109 | if (err < 0) { |
| 2110 | mgmt_pending_remove(cmd); |
| 2111 | goto failed; |
| 2112 | } |
| 2113 | |
| 2114 | failed: |
| 2115 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2116 | return err; |
| 2117 | } |
| 2118 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2119 | 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] | 2120 | { |
| 2121 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2122 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2123 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2124 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2125 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2126 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2127 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2128 | status = mgmt_bredr_support(hdev); |
| 2129 | if (status) |
| 2130 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2131 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2132 | if (!lmp_ssp_capable(hdev)) |
| 2133 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2134 | MGMT_STATUS_NOT_SUPPORTED); |
| 2135 | |
| 2136 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 2137 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2138 | MGMT_STATUS_REJECTED); |
| 2139 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2140 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2141 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2142 | MGMT_STATUS_INVALID_PARAMS); |
| 2143 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2144 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2145 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2146 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2147 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2148 | } else { |
| 2149 | if (hdev_is_powered(hdev)) { |
| 2150 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2151 | MGMT_STATUS_REJECTED); |
| 2152 | goto unlock; |
| 2153 | } |
| 2154 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2155 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2156 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2157 | |
| 2158 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2159 | if (err < 0) |
| 2160 | goto unlock; |
| 2161 | |
| 2162 | if (changed) |
| 2163 | err = new_settings(hdev, sk); |
| 2164 | |
| 2165 | unlock: |
| 2166 | hci_dev_unlock(hdev); |
| 2167 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2168 | } |
| 2169 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2170 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 2171 | { |
| 2172 | struct cmd_lookup match = { NULL, hdev }; |
| 2173 | |
| 2174 | if (status) { |
| 2175 | u8 mgmt_err = mgmt_status(status); |
| 2176 | |
| 2177 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2178 | &mgmt_err); |
| 2179 | return; |
| 2180 | } |
| 2181 | |
| 2182 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2183 | |
| 2184 | new_settings(hdev, match.sk); |
| 2185 | |
| 2186 | if (match.sk) |
| 2187 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2188 | |
| 2189 | /* Make sure the controller has a good default for |
| 2190 | * advertising data. Restrict the update to when LE |
| 2191 | * has actually been enabled. During power on, the |
| 2192 | * update in powered_update_hci will take care of it. |
| 2193 | */ |
| 2194 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2195 | struct hci_request req; |
| 2196 | |
| 2197 | hci_dev_lock(hdev); |
| 2198 | |
| 2199 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2200 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2201 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2202 | hci_req_run(&req, NULL); |
| 2203 | |
Johan Hedberg | a70f4b5 | 2014-07-07 15:19:50 +0300 | [diff] [blame] | 2204 | hci_update_background_scan(hdev); |
| 2205 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2206 | hci_dev_unlock(hdev); |
| 2207 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2208 | } |
| 2209 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2210 | 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] | 2211 | { |
| 2212 | struct mgmt_mode *cp = data; |
| 2213 | struct hci_cp_write_le_host_supported hci_cp; |
| 2214 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2215 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2216 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2217 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2218 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2219 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2220 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2221 | if (!lmp_le_capable(hdev)) |
| 2222 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2223 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2224 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2225 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2226 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2227 | MGMT_STATUS_INVALID_PARAMS); |
| 2228 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2229 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2230 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2231 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2232 | MGMT_STATUS_REJECTED); |
| 2233 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2234 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2235 | |
| 2236 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2237 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2238 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2239 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2240 | bool changed = false; |
| 2241 | |
| 2242 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2243 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2244 | changed = true; |
| 2245 | } |
| 2246 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2247 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2248 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2249 | changed = true; |
| 2250 | } |
| 2251 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2252 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2253 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2254 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2255 | |
| 2256 | if (changed) |
| 2257 | err = new_settings(hdev, sk); |
| 2258 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2259 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2260 | } |
| 2261 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2262 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2263 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2264 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2265 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2266 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2270 | if (!cmd) { |
| 2271 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2272 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2273 | } |
| 2274 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2275 | hci_req_init(&req, hdev); |
| 2276 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2277 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2278 | |
| 2279 | if (val) { |
| 2280 | hci_cp.le = val; |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 2281 | hci_cp.simul = 0x00; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2282 | } else { |
Johan Hedberg | 73e082f | 2014-07-08 15:07:51 +0300 | [diff] [blame] | 2283 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2284 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2285 | } |
| 2286 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2287 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2288 | &hci_cp); |
| 2289 | |
| 2290 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2291 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2292 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2293 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2294 | unlock: |
| 2295 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2296 | return err; |
| 2297 | } |
| 2298 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2299 | /* This is a helper function to test for pending mgmt commands that can |
| 2300 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2301 | * mgmt command at a time since otherwise we cannot easily track what |
| 2302 | * the current values are, will be, and based on that calculate if a new |
| 2303 | * HCI command needs to be sent and if yes with what value. |
| 2304 | */ |
| 2305 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2306 | { |
| 2307 | struct pending_cmd *cmd; |
| 2308 | |
| 2309 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2310 | switch (cmd->opcode) { |
| 2311 | case MGMT_OP_ADD_UUID: |
| 2312 | case MGMT_OP_REMOVE_UUID: |
| 2313 | case MGMT_OP_SET_DEV_CLASS: |
| 2314 | case MGMT_OP_SET_POWERED: |
| 2315 | return true; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | return false; |
| 2320 | } |
| 2321 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2322 | static const u8 bluetooth_base_uuid[] = { |
| 2323 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2324 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2325 | }; |
| 2326 | |
| 2327 | static u8 get_uuid_size(const u8 *uuid) |
| 2328 | { |
| 2329 | u32 val; |
| 2330 | |
| 2331 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2332 | return 128; |
| 2333 | |
| 2334 | val = get_unaligned_le32(&uuid[12]); |
| 2335 | if (val > 0xffff) |
| 2336 | return 32; |
| 2337 | |
| 2338 | return 16; |
| 2339 | } |
| 2340 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2341 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2342 | { |
| 2343 | struct pending_cmd *cmd; |
| 2344 | |
| 2345 | hci_dev_lock(hdev); |
| 2346 | |
| 2347 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2348 | if (!cmd) |
| 2349 | goto unlock; |
| 2350 | |
| 2351 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2352 | hdev->dev_class, 3); |
| 2353 | |
| 2354 | mgmt_pending_remove(cmd); |
| 2355 | |
| 2356 | unlock: |
| 2357 | hci_dev_unlock(hdev); |
| 2358 | } |
| 2359 | |
| 2360 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2361 | { |
| 2362 | BT_DBG("status 0x%02x", status); |
| 2363 | |
| 2364 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2365 | } |
| 2366 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2367 | 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] | 2368 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2369 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2370 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2371 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2372 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2373 | int err; |
| 2374 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2375 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2376 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2377 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2378 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2379 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2380 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2381 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2382 | goto failed; |
| 2383 | } |
| 2384 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2385 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2386 | if (!uuid) { |
| 2387 | err = -ENOMEM; |
| 2388 | goto failed; |
| 2389 | } |
| 2390 | |
| 2391 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2392 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2393 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2394 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2395 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2396 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2397 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2398 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2399 | update_class(&req); |
| 2400 | update_eir(&req); |
| 2401 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2402 | err = hci_req_run(&req, add_uuid_complete); |
| 2403 | if (err < 0) { |
| 2404 | if (err != -ENODATA) |
| 2405 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2406 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2407 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2408 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2409 | goto failed; |
| 2410 | } |
| 2411 | |
| 2412 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2413 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2414 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2415 | goto failed; |
| 2416 | } |
| 2417 | |
| 2418 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2419 | |
| 2420 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2421 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2422 | return err; |
| 2423 | } |
| 2424 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2425 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2426 | { |
| 2427 | if (!hdev_is_powered(hdev)) |
| 2428 | return false; |
| 2429 | |
| 2430 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2431 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2432 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2433 | return true; |
| 2434 | } |
| 2435 | |
| 2436 | return false; |
| 2437 | } |
| 2438 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2439 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2440 | { |
| 2441 | BT_DBG("status 0x%02x", status); |
| 2442 | |
| 2443 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2444 | } |
| 2445 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2446 | 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] | 2447 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2448 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2449 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2450 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2451 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2452 | 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] | 2453 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2454 | int err, found; |
| 2455 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2456 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2457 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2458 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2459 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2460 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2461 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2462 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2463 | goto unlock; |
| 2464 | } |
| 2465 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2466 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2467 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2468 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2469 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2470 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2471 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2472 | goto unlock; |
| 2473 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2474 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2475 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2476 | } |
| 2477 | |
| 2478 | found = 0; |
| 2479 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2480 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2481 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2482 | continue; |
| 2483 | |
| 2484 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2485 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2486 | found++; |
| 2487 | } |
| 2488 | |
| 2489 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2490 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2491 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2492 | goto unlock; |
| 2493 | } |
| 2494 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2495 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2496 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2497 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2498 | update_class(&req); |
| 2499 | update_eir(&req); |
| 2500 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2501 | err = hci_req_run(&req, remove_uuid_complete); |
| 2502 | if (err < 0) { |
| 2503 | if (err != -ENODATA) |
| 2504 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2505 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2506 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2507 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2508 | goto unlock; |
| 2509 | } |
| 2510 | |
| 2511 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2512 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2513 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2514 | goto unlock; |
| 2515 | } |
| 2516 | |
| 2517 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2518 | |
| 2519 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2520 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2521 | return err; |
| 2522 | } |
| 2523 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2524 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2525 | { |
| 2526 | BT_DBG("status 0x%02x", status); |
| 2527 | |
| 2528 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2529 | } |
| 2530 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2531 | 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] | 2532 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2533 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2534 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2535 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2536 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2537 | int err; |
| 2538 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2539 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2540 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2541 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2542 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2543 | MGMT_STATUS_NOT_SUPPORTED); |
| 2544 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2545 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2546 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2547 | if (pending_eir_or_class(hdev)) { |
| 2548 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2549 | MGMT_STATUS_BUSY); |
| 2550 | goto unlock; |
| 2551 | } |
| 2552 | |
| 2553 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2554 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2555 | MGMT_STATUS_INVALID_PARAMS); |
| 2556 | goto unlock; |
| 2557 | } |
| 2558 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2559 | hdev->major_class = cp->major; |
| 2560 | hdev->minor_class = cp->minor; |
| 2561 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2562 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2563 | 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] | 2564 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2565 | goto unlock; |
| 2566 | } |
| 2567 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2568 | hci_req_init(&req, hdev); |
| 2569 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2570 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2571 | hci_dev_unlock(hdev); |
| 2572 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2573 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2574 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2575 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2576 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2577 | update_class(&req); |
| 2578 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2579 | err = hci_req_run(&req, set_class_complete); |
| 2580 | if (err < 0) { |
| 2581 | if (err != -ENODATA) |
| 2582 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2583 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2584 | 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] | 2585 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2586 | goto unlock; |
| 2587 | } |
| 2588 | |
| 2589 | 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] | 2590 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2591 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2592 | goto unlock; |
| 2593 | } |
| 2594 | |
| 2595 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2596 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2597 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2598 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2599 | return err; |
| 2600 | } |
| 2601 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2602 | 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] | 2603 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2604 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2605 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2606 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2607 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2608 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2609 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2610 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2611 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2612 | BT_DBG("request for %s", hdev->name); |
| 2613 | |
| 2614 | if (!lmp_bredr_capable(hdev)) |
| 2615 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2616 | MGMT_STATUS_NOT_SUPPORTED); |
| 2617 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2618 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2619 | if (key_count > max_key_count) { |
| 2620 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2621 | key_count); |
| 2622 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2623 | MGMT_STATUS_INVALID_PARAMS); |
| 2624 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2625 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2626 | expected_len = sizeof(*cp) + key_count * |
| 2627 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2628 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2629 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2630 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2631 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2632 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2633 | } |
| 2634 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2635 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2636 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2637 | MGMT_STATUS_INVALID_PARAMS); |
| 2638 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2639 | 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] | 2640 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2641 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2642 | for (i = 0; i < key_count; i++) { |
| 2643 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2644 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2645 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2646 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2647 | MGMT_STATUS_INVALID_PARAMS); |
| 2648 | } |
| 2649 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2650 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2651 | |
| 2652 | hci_link_keys_clear(hdev); |
| 2653 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2654 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2655 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2656 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2657 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2658 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2659 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2660 | |
| 2661 | if (changed) |
| 2662 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2663 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2664 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2665 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2666 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2667 | /* Always ignore debug keys and require a new pairing if |
| 2668 | * the user wants to use them. |
| 2669 | */ |
| 2670 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2671 | continue; |
| 2672 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2673 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2674 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2675 | } |
| 2676 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2677 | 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] | 2678 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2679 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2680 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2681 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2682 | } |
| 2683 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2684 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2685 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2686 | { |
| 2687 | struct mgmt_ev_device_unpaired ev; |
| 2688 | |
| 2689 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2690 | ev.addr.type = addr_type; |
| 2691 | |
| 2692 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2693 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2694 | } |
| 2695 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2696 | 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] | 2697 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2698 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2699 | struct mgmt_cp_unpair_device *cp = data; |
| 2700 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2701 | struct hci_cp_disconnect dc; |
| 2702 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2703 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2704 | int err; |
| 2705 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2706 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2707 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2708 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2709 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2710 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2711 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2712 | MGMT_STATUS_INVALID_PARAMS, |
| 2713 | &rp, sizeof(rp)); |
| 2714 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2715 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2716 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2717 | MGMT_STATUS_INVALID_PARAMS, |
| 2718 | &rp, sizeof(rp)); |
| 2719 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2720 | hci_dev_lock(hdev); |
| 2721 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2722 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2723 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2724 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2725 | goto unlock; |
| 2726 | } |
| 2727 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2728 | if (cp->addr.type == BDADDR_BREDR) { |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2729 | /* If disconnection is requested, then look up the |
| 2730 | * connection. If the remote device is connected, it |
| 2731 | * will be later used to terminate the link. |
| 2732 | * |
| 2733 | * Setting it to NULL explicitly will cause no |
| 2734 | * termination of the link. |
| 2735 | */ |
| 2736 | if (cp->disconnect) |
| 2737 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2738 | &cp->addr.bdaddr); |
| 2739 | else |
| 2740 | conn = NULL; |
| 2741 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2742 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2743 | } else { |
| 2744 | u8 addr_type; |
| 2745 | |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2746 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
| 2747 | &cp->addr.bdaddr); |
| 2748 | if (conn) { |
| 2749 | /* Defer clearing up the connection parameters |
| 2750 | * until closing to give a chance of keeping |
| 2751 | * them if a repairing happens. |
| 2752 | */ |
| 2753 | set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
| 2754 | |
| 2755 | /* If disconnection is not requested, then |
| 2756 | * clear the connection variable so that the |
| 2757 | * link is not terminated. |
| 2758 | */ |
| 2759 | if (!cp->disconnect) |
| 2760 | conn = NULL; |
| 2761 | } |
| 2762 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2763 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2764 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2765 | else |
| 2766 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2767 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2768 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2769 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2770 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2771 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2772 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2773 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2774 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2775 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2776 | goto unlock; |
| 2777 | } |
| 2778 | |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 2779 | /* If the connection variable is set, then termination of the |
| 2780 | * link is requested. |
| 2781 | */ |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2782 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2783 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2784 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2785 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2786 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2787 | } |
| 2788 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2789 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2790 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2791 | if (!cmd) { |
| 2792 | err = -ENOMEM; |
| 2793 | goto unlock; |
| 2794 | } |
| 2795 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2796 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2797 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2798 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2799 | if (err < 0) |
| 2800 | mgmt_pending_remove(cmd); |
| 2801 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2802 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2803 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2804 | return err; |
| 2805 | } |
| 2806 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2807 | 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] | 2808 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2809 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2810 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2811 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2812 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2813 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2814 | int err; |
| 2815 | |
| 2816 | BT_DBG(""); |
| 2817 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2818 | memset(&rp, 0, sizeof(rp)); |
| 2819 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2820 | rp.addr.type = cp->addr.type; |
| 2821 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2822 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2823 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2824 | MGMT_STATUS_INVALID_PARAMS, |
| 2825 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2826 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2827 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2828 | |
| 2829 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2830 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2831 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2832 | goto failed; |
| 2833 | } |
| 2834 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2835 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2836 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2837 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2838 | goto failed; |
| 2839 | } |
| 2840 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2841 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2842 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2843 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2844 | else |
| 2845 | 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] | 2846 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2847 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2848 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2849 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2850 | goto failed; |
| 2851 | } |
| 2852 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2853 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2854 | if (!cmd) { |
| 2855 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2856 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2857 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2858 | |
Johan Hedberg | e3f2f92 | 2014-08-18 20:33:33 +0300 | [diff] [blame] | 2859 | err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2860 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2861 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2862 | |
| 2863 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2864 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2865 | return err; |
| 2866 | } |
| 2867 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2868 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2869 | { |
| 2870 | switch (link_type) { |
| 2871 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2872 | switch (addr_type) { |
| 2873 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2874 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2875 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2876 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2877 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2878 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2879 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2880 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2881 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2882 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2883 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2884 | } |
| 2885 | } |
| 2886 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2887 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2888 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2889 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2890 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2891 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2892 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2893 | int err; |
| 2894 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2895 | |
| 2896 | BT_DBG(""); |
| 2897 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2898 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2899 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2900 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2901 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2902 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2903 | goto unlock; |
| 2904 | } |
| 2905 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2906 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2907 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2908 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2909 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2910 | } |
| 2911 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2912 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2913 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2914 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2915 | err = -ENOMEM; |
| 2916 | goto unlock; |
| 2917 | } |
| 2918 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2919 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2920 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2921 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2922 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2923 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2924 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2925 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2926 | continue; |
| 2927 | i++; |
| 2928 | } |
| 2929 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2930 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2931 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2932 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2933 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2934 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2935 | 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] | 2936 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2937 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2938 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2939 | |
| 2940 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2941 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2942 | return err; |
| 2943 | } |
| 2944 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2945 | 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] | 2946 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2947 | { |
| 2948 | struct pending_cmd *cmd; |
| 2949 | int err; |
| 2950 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2951 | 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] | 2952 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2953 | if (!cmd) |
| 2954 | return -ENOMEM; |
| 2955 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2956 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2957 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2958 | if (err < 0) |
| 2959 | mgmt_pending_remove(cmd); |
| 2960 | |
| 2961 | return err; |
| 2962 | } |
| 2963 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2964 | 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] | 2965 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2966 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2967 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2968 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2969 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2970 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2971 | int err; |
| 2972 | |
| 2973 | BT_DBG(""); |
| 2974 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2975 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2976 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2977 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2978 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2979 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2980 | goto failed; |
| 2981 | } |
| 2982 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2983 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2984 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2985 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2986 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2987 | goto failed; |
| 2988 | } |
| 2989 | |
| 2990 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2991 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2992 | |
| 2993 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2994 | |
| 2995 | BT_ERR("PIN code is not 16 bytes long"); |
| 2996 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2997 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2998 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2999 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3000 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 3001 | |
| 3002 | goto failed; |
| 3003 | } |
| 3004 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 3005 | 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] | 3006 | if (!cmd) { |
| 3007 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3008 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 3009 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3010 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 3011 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3012 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 3013 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3014 | |
| 3015 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 3016 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3017 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3018 | |
| 3019 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3020 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 3021 | return err; |
| 3022 | } |
| 3023 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3024 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3025 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3026 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3027 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3028 | |
| 3029 | BT_DBG(""); |
| 3030 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3031 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 3032 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 3033 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 3034 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3035 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3036 | |
| 3037 | hdev->io_capability = cp->io_capability; |
| 3038 | |
| 3039 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3040 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3041 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3042 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3043 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3044 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 3045 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 3046 | } |
| 3047 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3048 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3049 | { |
| 3050 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3051 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3052 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3053 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3054 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 3055 | continue; |
| 3056 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3057 | if (cmd->user_data != conn) |
| 3058 | continue; |
| 3059 | |
| 3060 | return cmd; |
| 3061 | } |
| 3062 | |
| 3063 | return NULL; |
| 3064 | } |
| 3065 | |
| 3066 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 3067 | { |
| 3068 | struct mgmt_rp_pair_device rp; |
| 3069 | struct hci_conn *conn = cmd->user_data; |
| 3070 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 3071 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 3072 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3073 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3074 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3075 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3076 | |
| 3077 | /* So we don't get further callbacks for this connection */ |
| 3078 | conn->connect_cfm_cb = NULL; |
| 3079 | conn->security_cfm_cb = NULL; |
| 3080 | conn->disconn_cfm_cb = NULL; |
| 3081 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3082 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3083 | hci_conn_put(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3084 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3085 | mgmt_pending_remove(cmd); |
Alfonso Acosta | 89cbb06 | 2014-10-11 21:44:47 +0000 | [diff] [blame] | 3086 | |
| 3087 | /* The device is paired so there is no need to remove |
| 3088 | * its connection parameters anymore. |
| 3089 | */ |
| 3090 | clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3091 | } |
| 3092 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3093 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 3094 | { |
| 3095 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 3096 | struct pending_cmd *cmd; |
| 3097 | |
| 3098 | cmd = find_pairing(conn); |
| 3099 | if (cmd) |
| 3100 | pairing_complete(cmd, status); |
| 3101 | } |
| 3102 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3103 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 3104 | { |
| 3105 | struct pending_cmd *cmd; |
| 3106 | |
| 3107 | BT_DBG("status %u", status); |
| 3108 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3109 | cmd = find_pairing(conn); |
| 3110 | if (!cmd) |
| 3111 | BT_DBG("Unable to find a pending command"); |
| 3112 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 3113 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3114 | } |
| 3115 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3116 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3117 | { |
| 3118 | struct pending_cmd *cmd; |
| 3119 | |
| 3120 | BT_DBG("status %u", status); |
| 3121 | |
| 3122 | if (!status) |
| 3123 | return; |
| 3124 | |
| 3125 | cmd = find_pairing(conn); |
| 3126 | if (!cmd) |
| 3127 | BT_DBG("Unable to find a pending command"); |
| 3128 | else |
| 3129 | pairing_complete(cmd, mgmt_status(status)); |
| 3130 | } |
| 3131 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3132 | 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] | 3133 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3134 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3135 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3136 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3137 | struct pending_cmd *cmd; |
| 3138 | u8 sec_level, auth_type; |
| 3139 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3140 | int err; |
| 3141 | |
| 3142 | BT_DBG(""); |
| 3143 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3144 | memset(&rp, 0, sizeof(rp)); |
| 3145 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3146 | rp.addr.type = cp->addr.type; |
| 3147 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3148 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 3149 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3150 | MGMT_STATUS_INVALID_PARAMS, |
| 3151 | &rp, sizeof(rp)); |
| 3152 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3153 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 3154 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3155 | MGMT_STATUS_INVALID_PARAMS, |
| 3156 | &rp, sizeof(rp)); |
| 3157 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3158 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3159 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3160 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3161 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3162 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3163 | goto unlock; |
| 3164 | } |
| 3165 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3166 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3167 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3168 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3169 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3170 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3171 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3172 | } else { |
| 3173 | u8 addr_type; |
| 3174 | |
| 3175 | /* Convert from L2CAP channel address type to HCI address type |
| 3176 | */ |
| 3177 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3178 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3179 | else |
| 3180 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3181 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3182 | /* When pairing a new device, it is expected to remember |
| 3183 | * this device for future connections. Adding the connection |
| 3184 | * parameter information ahead of time allows tracking |
| 3185 | * of the slave preferred values and will speed up any |
| 3186 | * further connection establishment. |
| 3187 | * |
| 3188 | * If connection parameters already exist, then they |
| 3189 | * will be kept and this function does nothing. |
| 3190 | */ |
| 3191 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3192 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3193 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Johan Hedberg | e804d25 | 2014-07-16 11:42:28 +0300 | [diff] [blame] | 3194 | sec_level, HCI_LE_CONN_TIMEOUT, |
| 3195 | HCI_ROLE_MASTER); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3196 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3197 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3198 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3199 | int status; |
| 3200 | |
| 3201 | if (PTR_ERR(conn) == -EBUSY) |
| 3202 | status = MGMT_STATUS_BUSY; |
| 3203 | else |
| 3204 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3205 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3206 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3207 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3208 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3209 | goto unlock; |
| 3210 | } |
| 3211 | |
| 3212 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3213 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3214 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3215 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3216 | goto unlock; |
| 3217 | } |
| 3218 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3219 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3220 | if (!cmd) { |
| 3221 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3222 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3223 | goto unlock; |
| 3224 | } |
| 3225 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3226 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3227 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3228 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3229 | conn->security_cfm_cb = pairing_complete_cb; |
| 3230 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3231 | } else { |
| 3232 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3233 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3234 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3235 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3236 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3237 | conn->io_capability = cp->io_cap; |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 3238 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3239 | |
Johan Hedberg | 6f78fd4 | 2014-07-30 08:35:48 +0300 | [diff] [blame] | 3240 | if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) && |
Johan Hedberg | e7cafc4 | 2014-07-17 15:35:38 +0300 | [diff] [blame] | 3241 | hci_conn_security(conn, sec_level, auth_type, true)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3242 | pairing_complete(cmd, 0); |
| 3243 | |
| 3244 | err = 0; |
| 3245 | |
| 3246 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3247 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3248 | return err; |
| 3249 | } |
| 3250 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3251 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3252 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3253 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3254 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3255 | struct pending_cmd *cmd; |
| 3256 | struct hci_conn *conn; |
| 3257 | int err; |
| 3258 | |
| 3259 | BT_DBG(""); |
| 3260 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3261 | hci_dev_lock(hdev); |
| 3262 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3263 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3264 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3265 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3266 | goto unlock; |
| 3267 | } |
| 3268 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3269 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3270 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3271 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3272 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3273 | goto unlock; |
| 3274 | } |
| 3275 | |
| 3276 | conn = cmd->user_data; |
| 3277 | |
| 3278 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3279 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3280 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3281 | goto unlock; |
| 3282 | } |
| 3283 | |
| 3284 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3285 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3286 | 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] | 3287 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3288 | unlock: |
| 3289 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3290 | return err; |
| 3291 | } |
| 3292 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3293 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3294 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3295 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3296 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3297 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3298 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3299 | int err; |
| 3300 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3301 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3302 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3303 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3304 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3305 | MGMT_STATUS_NOT_POWERED, addr, |
| 3306 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3307 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3308 | } |
| 3309 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3310 | if (addr->type == BDADDR_BREDR) |
| 3311 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3312 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3313 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3314 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3315 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3316 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3317 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3318 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3319 | goto done; |
| 3320 | } |
| 3321 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3322 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3323 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3324 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3325 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3326 | MGMT_STATUS_SUCCESS, addr, |
| 3327 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3328 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3329 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3330 | MGMT_STATUS_FAILED, addr, |
| 3331 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3332 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3333 | goto done; |
| 3334 | } |
| 3335 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3336 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3337 | if (!cmd) { |
| 3338 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3339 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3340 | } |
| 3341 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3342 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3343 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3344 | struct hci_cp_user_passkey_reply cp; |
| 3345 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3346 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3347 | cp.passkey = passkey; |
| 3348 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3349 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3350 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3351 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3352 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3353 | if (err < 0) |
| 3354 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3355 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3356 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3357 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3358 | return err; |
| 3359 | } |
| 3360 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3361 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3362 | void *data, u16 len) |
| 3363 | { |
| 3364 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3365 | |
| 3366 | BT_DBG(""); |
| 3367 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3368 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3369 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3370 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3371 | } |
| 3372 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3373 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3374 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3375 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3376 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3377 | |
| 3378 | BT_DBG(""); |
| 3379 | |
| 3380 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3381 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3382 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3383 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3384 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3385 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3386 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3387 | } |
| 3388 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3389 | 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] | 3390 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3391 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3392 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3393 | |
| 3394 | BT_DBG(""); |
| 3395 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3396 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3397 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3398 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3399 | } |
| 3400 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3401 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3402 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3403 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3404 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3405 | |
| 3406 | BT_DBG(""); |
| 3407 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3408 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3409 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3410 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3411 | } |
| 3412 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3413 | 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] | 3414 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3415 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3416 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3417 | |
| 3418 | BT_DBG(""); |
| 3419 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3420 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3421 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3422 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3423 | } |
| 3424 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3425 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3426 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3427 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3428 | struct hci_cp_write_local_name cp; |
| 3429 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3430 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3431 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3432 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3433 | } |
| 3434 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3435 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3436 | { |
| 3437 | struct mgmt_cp_set_local_name *cp; |
| 3438 | struct pending_cmd *cmd; |
| 3439 | |
| 3440 | BT_DBG("status 0x%02x", status); |
| 3441 | |
| 3442 | hci_dev_lock(hdev); |
| 3443 | |
| 3444 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3445 | if (!cmd) |
| 3446 | goto unlock; |
| 3447 | |
| 3448 | cp = cmd->param; |
| 3449 | |
| 3450 | if (status) |
| 3451 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3452 | mgmt_status(status)); |
| 3453 | else |
| 3454 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3455 | cp, sizeof(*cp)); |
| 3456 | |
| 3457 | mgmt_pending_remove(cmd); |
| 3458 | |
| 3459 | unlock: |
| 3460 | hci_dev_unlock(hdev); |
| 3461 | } |
| 3462 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3463 | 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] | 3464 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3465 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3466 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3467 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3468 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3469 | int err; |
| 3470 | |
| 3471 | BT_DBG(""); |
| 3472 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3473 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3474 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3475 | /* If the old values are the same as the new ones just return a |
| 3476 | * direct command complete event. |
| 3477 | */ |
| 3478 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3479 | !memcmp(hdev->short_name, cp->short_name, |
| 3480 | sizeof(hdev->short_name))) { |
| 3481 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3482 | data, len); |
| 3483 | goto failed; |
| 3484 | } |
| 3485 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3486 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3487 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3488 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3489 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3490 | |
| 3491 | 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] | 3492 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3493 | if (err < 0) |
| 3494 | goto failed; |
| 3495 | |
| 3496 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3497 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3498 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3499 | goto failed; |
| 3500 | } |
| 3501 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3502 | 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] | 3503 | if (!cmd) { |
| 3504 | err = -ENOMEM; |
| 3505 | goto failed; |
| 3506 | } |
| 3507 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3508 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3509 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3510 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3511 | |
| 3512 | if (lmp_bredr_capable(hdev)) { |
| 3513 | update_name(&req); |
| 3514 | update_eir(&req); |
| 3515 | } |
| 3516 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3517 | /* The name is stored in the scan response data and so |
| 3518 | * no need to udpate the advertising data here. |
| 3519 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3520 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3521 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3522 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3523 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3524 | if (err < 0) |
| 3525 | mgmt_pending_remove(cmd); |
| 3526 | |
| 3527 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3528 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3529 | return err; |
| 3530 | } |
| 3531 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3532 | 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] | 3533 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3534 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3535 | struct pending_cmd *cmd; |
| 3536 | int err; |
| 3537 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3538 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3539 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3540 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3541 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3542 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3543 | 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] | 3544 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3545 | goto unlock; |
| 3546 | } |
| 3547 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3548 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3549 | 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] | 3550 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3551 | goto unlock; |
| 3552 | } |
| 3553 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3554 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3555 | 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] | 3556 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3557 | goto unlock; |
| 3558 | } |
| 3559 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3560 | 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] | 3561 | if (!cmd) { |
| 3562 | err = -ENOMEM; |
| 3563 | goto unlock; |
| 3564 | } |
| 3565 | |
Johan Hedberg | 710f11c | 2014-05-26 11:21:22 +0300 | [diff] [blame] | 3566 | if (bredr_sc_enabled(hdev)) |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3567 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3568 | 0, NULL); |
| 3569 | else |
| 3570 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3571 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3572 | if (err < 0) |
| 3573 | mgmt_pending_remove(cmd); |
| 3574 | |
| 3575 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3576 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3577 | return err; |
| 3578 | } |
| 3579 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3580 | 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] | 3581 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3582 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3583 | int err; |
| 3584 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3585 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3586 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3587 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3588 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3589 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3590 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3591 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3592 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3593 | if (cp->addr.type != BDADDR_BREDR) { |
| 3594 | err = cmd_complete(sk, hdev->id, |
| 3595 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3596 | MGMT_STATUS_INVALID_PARAMS, |
| 3597 | &cp->addr, sizeof(cp->addr)); |
| 3598 | goto unlock; |
| 3599 | } |
| 3600 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3601 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3602 | cp->addr.type, cp->hash, |
| 3603 | cp->rand, NULL, NULL); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3604 | if (err < 0) |
| 3605 | status = MGMT_STATUS_FAILED; |
| 3606 | else |
| 3607 | status = MGMT_STATUS_SUCCESS; |
| 3608 | |
| 3609 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3610 | status, &cp->addr, sizeof(cp->addr)); |
| 3611 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3612 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3613 | u8 *rand192, *hash192; |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3614 | u8 status; |
| 3615 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3616 | if (cp->addr.type != BDADDR_BREDR) { |
| 3617 | err = cmd_complete(sk, hdev->id, |
| 3618 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3619 | MGMT_STATUS_INVALID_PARAMS, |
| 3620 | &cp->addr, sizeof(cp->addr)); |
| 3621 | goto unlock; |
| 3622 | } |
| 3623 | |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3624 | if (bdaddr_type_is_le(cp->addr.type)) { |
| 3625 | rand192 = NULL; |
| 3626 | hash192 = NULL; |
| 3627 | } else { |
| 3628 | rand192 = cp->rand192; |
| 3629 | hash192 = cp->hash192; |
| 3630 | } |
| 3631 | |
Johan Hedberg | 81328d5c | 2014-10-26 20:33:47 +0100 | [diff] [blame] | 3632 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
Johan Hedberg | 86df920 | 2014-10-26 20:52:27 +0100 | [diff] [blame] | 3633 | cp->addr.type, hash192, rand192, |
| 3634 | cp->hash256, cp->rand256); |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3635 | if (err < 0) |
| 3636 | status = MGMT_STATUS_FAILED; |
| 3637 | else |
| 3638 | status = MGMT_STATUS_SUCCESS; |
| 3639 | |
| 3640 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3641 | status, &cp->addr, sizeof(cp->addr)); |
| 3642 | } else { |
| 3643 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3644 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3645 | MGMT_STATUS_INVALID_PARAMS); |
| 3646 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3647 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3648 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3649 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3650 | return err; |
| 3651 | } |
| 3652 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3653 | 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] | 3654 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3655 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3656 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3657 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3658 | int err; |
| 3659 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3660 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3661 | |
Johan Hedberg | c19a495 | 2014-11-17 20:52:19 +0200 | [diff] [blame] | 3662 | if (cp->addr.type != BDADDR_BREDR) |
| 3663 | return cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 3664 | MGMT_STATUS_INVALID_PARAMS, |
| 3665 | &cp->addr, sizeof(cp->addr)); |
| 3666 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3667 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3668 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3669 | if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 3670 | hci_remote_oob_data_clear(hdev); |
| 3671 | status = MGMT_STATUS_SUCCESS; |
| 3672 | goto done; |
| 3673 | } |
| 3674 | |
Johan Hedberg | 6928a92 | 2014-10-26 20:46:09 +0100 | [diff] [blame] | 3675 | 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] | 3676 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3677 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3678 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3679 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3680 | |
Johan Hedberg | eedbd58 | 2014-11-15 09:34:23 +0200 | [diff] [blame] | 3681 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3682 | 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] | 3683 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3684 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3685 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3686 | return err; |
| 3687 | } |
| 3688 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame^] | 3689 | static bool trigger_discovery(struct hci_request *req, u8 *status) |
| 3690 | { |
| 3691 | struct hci_dev *hdev = req->hdev; |
| 3692 | struct hci_cp_le_set_scan_param param_cp; |
| 3693 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3694 | struct hci_cp_inquiry inq_cp; |
| 3695 | /* General inquiry access code (GIAC) */ |
| 3696 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
| 3697 | u8 own_addr_type; |
| 3698 | int err; |
| 3699 | |
| 3700 | switch (hdev->discovery.type) { |
| 3701 | case DISCOV_TYPE_BREDR: |
| 3702 | *status = mgmt_bredr_support(hdev); |
| 3703 | if (*status) |
| 3704 | return false; |
| 3705 | |
| 3706 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3707 | *status = MGMT_STATUS_BUSY; |
| 3708 | return false; |
| 3709 | } |
| 3710 | |
| 3711 | hci_inquiry_cache_flush(hdev); |
| 3712 | |
| 3713 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3714 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
| 3715 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
| 3716 | hci_req_add(req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
| 3717 | break; |
| 3718 | |
| 3719 | case DISCOV_TYPE_LE: |
| 3720 | case DISCOV_TYPE_INTERLEAVED: |
| 3721 | *status = mgmt_le_support(hdev); |
| 3722 | if (*status) |
| 3723 | return false; |
| 3724 | |
| 3725 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
| 3726 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 3727 | *status = MGMT_STATUS_NOT_SUPPORTED; |
| 3728 | return false; |
| 3729 | } |
| 3730 | |
| 3731 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) { |
| 3732 | /* Don't let discovery abort an outgoing |
| 3733 | * connection attempt that's using directed |
| 3734 | * advertising. |
| 3735 | */ |
| 3736 | if (hci_conn_hash_lookup_state(hdev, LE_LINK, |
| 3737 | BT_CONNECT)) { |
| 3738 | *status = MGMT_STATUS_REJECTED; |
| 3739 | return false; |
| 3740 | } |
| 3741 | |
| 3742 | disable_advertising(req); |
| 3743 | } |
| 3744 | |
| 3745 | /* If controller is scanning, it means the background scanning |
| 3746 | * is running. Thus, we should temporarily stop it in order to |
| 3747 | * set the discovery scanning parameters. |
| 3748 | */ |
| 3749 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3750 | hci_req_add_le_scan_disable(req); |
| 3751 | |
| 3752 | memset(¶m_cp, 0, sizeof(param_cp)); |
| 3753 | |
| 3754 | /* All active scans will be done with either a resolvable |
| 3755 | * private address (when privacy feature has been enabled) |
| 3756 | * or unresolvable private address. |
| 3757 | */ |
| 3758 | err = hci_update_random_address(req, true, &own_addr_type); |
| 3759 | if (err < 0) { |
| 3760 | *status = MGMT_STATUS_FAILED; |
| 3761 | return false; |
| 3762 | } |
| 3763 | |
| 3764 | param_cp.type = LE_SCAN_ACTIVE; |
| 3765 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3766 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
| 3767 | param_cp.own_address_type = own_addr_type; |
| 3768 | hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3769 | ¶m_cp); |
| 3770 | |
| 3771 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3772 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3773 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3774 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3775 | &enable_cp); |
| 3776 | break; |
| 3777 | |
| 3778 | default: |
| 3779 | *status = MGMT_STATUS_INVALID_PARAMS; |
| 3780 | return false; |
| 3781 | } |
| 3782 | |
| 3783 | return true; |
| 3784 | } |
| 3785 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3786 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3787 | { |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3788 | struct pending_cmd *cmd; |
| 3789 | unsigned long timeout; |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3790 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3791 | BT_DBG("status %d", status); |
| 3792 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3793 | hci_dev_lock(hdev); |
| 3794 | |
| 3795 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3796 | if (cmd) { |
| 3797 | u8 type = hdev->discovery.type; |
| 3798 | |
| 3799 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, |
| 3800 | mgmt_status(status), &type, sizeof(type)); |
| 3801 | mgmt_pending_remove(cmd); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3802 | } |
| 3803 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3804 | if (status) { |
| 3805 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3806 | goto unlock; |
| 3807 | } |
| 3808 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3809 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3810 | |
| 3811 | switch (hdev->discovery.type) { |
| 3812 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3813 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3814 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3815 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3816 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3817 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3818 | case DISCOV_TYPE_BREDR: |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3819 | timeout = 0; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3820 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3821 | default: |
| 3822 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3823 | timeout = 0; |
| 3824 | break; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3825 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3826 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3827 | if (timeout) |
| 3828 | queue_delayed_work(hdev->workqueue, |
| 3829 | &hdev->le_scan_disable, timeout); |
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 | unlock: |
| 3832 | hci_dev_unlock(hdev); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3833 | } |
| 3834 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3835 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3836 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3837 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3838 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3839 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3840 | struct hci_request req; |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame^] | 3841 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3842 | int err; |
| 3843 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3844 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3845 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3846 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3847 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3848 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | a736abc | 2014-11-03 14:20:56 +0100 | [diff] [blame] | 3849 | err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3850 | MGMT_STATUS_NOT_POWERED, |
| 3851 | &cp->type, sizeof(cp->type)); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3852 | goto failed; |
| 3853 | } |
| 3854 | |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3855 | if (hdev->discovery.state != DISCOVERY_STOPPED || |
| 3856 | test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
Szymon Janc | a736abc | 2014-11-03 14:20:56 +0100 | [diff] [blame] | 3857 | err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3858 | MGMT_STATUS_BUSY, &cp->type, |
| 3859 | sizeof(cp->type)); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3860 | goto failed; |
| 3861 | } |
| 3862 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3863 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3864 | if (!cmd) { |
| 3865 | err = -ENOMEM; |
| 3866 | goto failed; |
| 3867 | } |
| 3868 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3869 | hdev->discovery.type = cp->type; |
| 3870 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3871 | hci_req_init(&req, hdev); |
| 3872 | |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame^] | 3873 | if (!trigger_discovery(&req, &status)) { |
Szymon Janc | a736abc | 2014-11-03 14:20:56 +0100 | [diff] [blame] | 3874 | err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Marcel Holtmann | 8019044 | 2014-12-04 11:36:36 +0100 | [diff] [blame^] | 3875 | status, &cp->type, sizeof(cp->type)); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3876 | mgmt_pending_remove(cmd); |
| 3877 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3878 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3879 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3880 | err = hci_req_run(&req, start_discovery_complete); |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3881 | if (err < 0) { |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3882 | mgmt_pending_remove(cmd); |
Marcel Holtmann | f5a969f | 2014-12-04 11:36:34 +0100 | [diff] [blame] | 3883 | goto failed; |
| 3884 | } |
| 3885 | |
| 3886 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3887 | |
| 3888 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3889 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3890 | return err; |
| 3891 | } |
| 3892 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3893 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3894 | { |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3895 | struct pending_cmd *cmd; |
| 3896 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3897 | BT_DBG("status %d", status); |
| 3898 | |
| 3899 | hci_dev_lock(hdev); |
| 3900 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3901 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3902 | if (cmd) { |
| 3903 | u8 type = hdev->discovery.type; |
| 3904 | |
| 3905 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, |
| 3906 | mgmt_status(status), &type, sizeof(type)); |
| 3907 | mgmt_pending_remove(cmd); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3908 | } |
| 3909 | |
Marcel Holtmann | 11e6e25 | 2014-12-04 11:36:35 +0100 | [diff] [blame] | 3910 | if (!status) |
| 3911 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3912 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3913 | hci_dev_unlock(hdev); |
| 3914 | } |
| 3915 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3916 | 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] | 3917 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3918 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3919 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3920 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3921 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3922 | int err; |
| 3923 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3924 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3925 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3926 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3927 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3928 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3929 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3930 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3931 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3932 | goto unlock; |
| 3933 | } |
| 3934 | |
| 3935 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3936 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3937 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3938 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3939 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3940 | } |
| 3941 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3942 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3943 | if (!cmd) { |
| 3944 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3945 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3946 | } |
| 3947 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3948 | hci_req_init(&req, hdev); |
| 3949 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3950 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3951 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3952 | err = hci_req_run(&req, stop_discovery_complete); |
| 3953 | if (!err) { |
| 3954 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3955 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3956 | } |
| 3957 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3958 | mgmt_pending_remove(cmd); |
| 3959 | |
| 3960 | /* If no HCI commands were sent we're done */ |
| 3961 | if (err == -ENODATA) { |
| 3962 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3963 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3964 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3965 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3966 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3967 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3968 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3969 | return err; |
| 3970 | } |
| 3971 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3972 | 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] | 3973 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3974 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3975 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3976 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3977 | int err; |
| 3978 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3979 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3980 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3981 | hci_dev_lock(hdev); |
| 3982 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3983 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3984 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3985 | MGMT_STATUS_FAILED, &cp->addr, |
| 3986 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3987 | goto failed; |
| 3988 | } |
| 3989 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3990 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3991 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3992 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3993 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3994 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3995 | goto failed; |
| 3996 | } |
| 3997 | |
| 3998 | if (cp->name_known) { |
| 3999 | e->name_state = NAME_KNOWN; |
| 4000 | list_del(&e->list); |
| 4001 | } else { |
| 4002 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 4003 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4004 | } |
| 4005 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 4006 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 4007 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4008 | |
| 4009 | failed: |
| 4010 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 4011 | return err; |
| 4012 | } |
| 4013 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4014 | 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] | 4015 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4016 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4017 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4018 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4019 | int err; |
| 4020 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4021 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4022 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4023 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 4024 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 4025 | MGMT_STATUS_INVALID_PARAMS, |
| 4026 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4027 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4028 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4029 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4030 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 4031 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4032 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4033 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4034 | goto done; |
| 4035 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4036 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4037 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4038 | sk); |
| 4039 | status = MGMT_STATUS_SUCCESS; |
| 4040 | |
| 4041 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4042 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4043 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4044 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4045 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4046 | |
| 4047 | return err; |
| 4048 | } |
| 4049 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4050 | 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] | 4051 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4052 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4053 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4054 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4055 | int err; |
| 4056 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4057 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4058 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4059 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 4060 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 4061 | MGMT_STATUS_INVALID_PARAMS, |
| 4062 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 4063 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4064 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4065 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4066 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 4067 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4068 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4069 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4070 | goto done; |
| 4071 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4072 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4073 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4074 | sk); |
| 4075 | status = MGMT_STATUS_SUCCESS; |
| 4076 | |
| 4077 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4078 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4079 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4080 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4081 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4082 | |
| 4083 | return err; |
| 4084 | } |
| 4085 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4086 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4087 | u16 len) |
| 4088 | { |
| 4089 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4090 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4091 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4092 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4093 | |
| 4094 | BT_DBG("%s", hdev->name); |
| 4095 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4096 | source = __le16_to_cpu(cp->source); |
| 4097 | |
| 4098 | if (source > 0x0002) |
| 4099 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4100 | MGMT_STATUS_INVALID_PARAMS); |
| 4101 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4102 | hci_dev_lock(hdev); |
| 4103 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4104 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4105 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4106 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4107 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4108 | |
| 4109 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 4110 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4111 | hci_req_init(&req, hdev); |
| 4112 | update_eir(&req); |
| 4113 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4114 | |
| 4115 | hci_dev_unlock(hdev); |
| 4116 | |
| 4117 | return err; |
| 4118 | } |
| 4119 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4120 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 4121 | { |
| 4122 | struct cmd_lookup match = { NULL, hdev }; |
| 4123 | |
| 4124 | if (status) { |
| 4125 | u8 mgmt_err = mgmt_status(status); |
| 4126 | |
| 4127 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4128 | cmd_status_rsp, &mgmt_err); |
| 4129 | return; |
| 4130 | } |
| 4131 | |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4132 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
| 4133 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4134 | else |
| 4135 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4136 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4137 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4138 | &match); |
| 4139 | |
| 4140 | new_settings(hdev, match.sk); |
| 4141 | |
| 4142 | if (match.sk) |
| 4143 | sock_put(match.sk); |
| 4144 | } |
| 4145 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4146 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4147 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4148 | { |
| 4149 | struct mgmt_mode *cp = data; |
| 4150 | struct pending_cmd *cmd; |
| 4151 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4152 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4153 | int err; |
| 4154 | |
| 4155 | BT_DBG("request for %s", hdev->name); |
| 4156 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4157 | status = mgmt_le_support(hdev); |
| 4158 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4159 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4160 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4161 | |
| 4162 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4163 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4164 | MGMT_STATUS_INVALID_PARAMS); |
| 4165 | |
| 4166 | hci_dev_lock(hdev); |
| 4167 | |
| 4168 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4169 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4170 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4171 | /* The following conditions are ones which mean that we should |
| 4172 | * not do any HCI communication but directly send a mgmt |
| 4173 | * response to user space (after toggling the flag if |
| 4174 | * necessary). |
| 4175 | */ |
| 4176 | if (!hdev_is_powered(hdev) || val == enabled || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4177 | hci_conn_num(hdev, LE_LINK) > 0 || |
| 4178 | (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4179 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4180 | bool changed = false; |
| 4181 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4182 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 4183 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4184 | changed = true; |
| 4185 | } |
| 4186 | |
| 4187 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4188 | if (err < 0) |
| 4189 | goto unlock; |
| 4190 | |
| 4191 | if (changed) |
| 4192 | err = new_settings(hdev, sk); |
| 4193 | |
| 4194 | goto unlock; |
| 4195 | } |
| 4196 | |
| 4197 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4198 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 4199 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4200 | MGMT_STATUS_BUSY); |
| 4201 | goto unlock; |
| 4202 | } |
| 4203 | |
| 4204 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4205 | if (!cmd) { |
| 4206 | err = -ENOMEM; |
| 4207 | goto unlock; |
| 4208 | } |
| 4209 | |
| 4210 | hci_req_init(&req, hdev); |
| 4211 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4212 | if (val) |
| 4213 | enable_advertising(&req); |
| 4214 | else |
| 4215 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4216 | |
| 4217 | err = hci_req_run(&req, set_advertising_complete); |
| 4218 | if (err < 0) |
| 4219 | mgmt_pending_remove(cmd); |
| 4220 | |
| 4221 | unlock: |
| 4222 | hci_dev_unlock(hdev); |
| 4223 | return err; |
| 4224 | } |
| 4225 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4226 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4227 | void *data, u16 len) |
| 4228 | { |
| 4229 | struct mgmt_cp_set_static_address *cp = data; |
| 4230 | int err; |
| 4231 | |
| 4232 | BT_DBG("%s", hdev->name); |
| 4233 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4234 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4235 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4236 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4237 | |
| 4238 | if (hdev_is_powered(hdev)) |
| 4239 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4240 | MGMT_STATUS_REJECTED); |
| 4241 | |
| 4242 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4243 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 4244 | return cmd_status(sk, hdev->id, |
| 4245 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4246 | MGMT_STATUS_INVALID_PARAMS); |
| 4247 | |
| 4248 | /* Two most significant bits shall be set */ |
| 4249 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4250 | return cmd_status(sk, hdev->id, |
| 4251 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4252 | MGMT_STATUS_INVALID_PARAMS); |
| 4253 | } |
| 4254 | |
| 4255 | hci_dev_lock(hdev); |
| 4256 | |
| 4257 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4258 | |
| 4259 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4260 | |
| 4261 | hci_dev_unlock(hdev); |
| 4262 | |
| 4263 | return err; |
| 4264 | } |
| 4265 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4266 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4267 | void *data, u16 len) |
| 4268 | { |
| 4269 | struct mgmt_cp_set_scan_params *cp = data; |
| 4270 | __u16 interval, window; |
| 4271 | int err; |
| 4272 | |
| 4273 | BT_DBG("%s", hdev->name); |
| 4274 | |
| 4275 | if (!lmp_le_capable(hdev)) |
| 4276 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4277 | MGMT_STATUS_NOT_SUPPORTED); |
| 4278 | |
| 4279 | interval = __le16_to_cpu(cp->interval); |
| 4280 | |
| 4281 | if (interval < 0x0004 || interval > 0x4000) |
| 4282 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4283 | MGMT_STATUS_INVALID_PARAMS); |
| 4284 | |
| 4285 | window = __le16_to_cpu(cp->window); |
| 4286 | |
| 4287 | if (window < 0x0004 || window > 0x4000) |
| 4288 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4289 | MGMT_STATUS_INVALID_PARAMS); |
| 4290 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4291 | if (window > interval) |
| 4292 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4293 | MGMT_STATUS_INVALID_PARAMS); |
| 4294 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4295 | hci_dev_lock(hdev); |
| 4296 | |
| 4297 | hdev->le_scan_interval = interval; |
| 4298 | hdev->le_scan_window = window; |
| 4299 | |
| 4300 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4301 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4302 | /* If background scan is running, restart it so new parameters are |
| 4303 | * loaded. |
| 4304 | */ |
| 4305 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4306 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4307 | struct hci_request req; |
| 4308 | |
| 4309 | hci_req_init(&req, hdev); |
| 4310 | |
| 4311 | hci_req_add_le_scan_disable(&req); |
| 4312 | hci_req_add_le_passive_scan(&req); |
| 4313 | |
| 4314 | hci_req_run(&req, NULL); |
| 4315 | } |
| 4316 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4317 | hci_dev_unlock(hdev); |
| 4318 | |
| 4319 | return err; |
| 4320 | } |
| 4321 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4322 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4323 | { |
| 4324 | struct pending_cmd *cmd; |
| 4325 | |
| 4326 | BT_DBG("status 0x%02x", status); |
| 4327 | |
| 4328 | hci_dev_lock(hdev); |
| 4329 | |
| 4330 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4331 | if (!cmd) |
| 4332 | goto unlock; |
| 4333 | |
| 4334 | if (status) { |
| 4335 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4336 | mgmt_status(status)); |
| 4337 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4338 | struct mgmt_mode *cp = cmd->param; |
| 4339 | |
| 4340 | if (cp->val) |
| 4341 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4342 | else |
| 4343 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4344 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4345 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4346 | new_settings(hdev, cmd->sk); |
| 4347 | } |
| 4348 | |
| 4349 | mgmt_pending_remove(cmd); |
| 4350 | |
| 4351 | unlock: |
| 4352 | hci_dev_unlock(hdev); |
| 4353 | } |
| 4354 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4355 | 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] | 4356 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4357 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4358 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4359 | struct pending_cmd *cmd; |
| 4360 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4361 | int err; |
| 4362 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4363 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4364 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4365 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4366 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4367 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4368 | MGMT_STATUS_NOT_SUPPORTED); |
| 4369 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4370 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4371 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4372 | MGMT_STATUS_INVALID_PARAMS); |
| 4373 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4374 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4375 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4376 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4377 | |
| 4378 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4379 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4380 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4381 | |
| 4382 | hci_dev_lock(hdev); |
| 4383 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4384 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4385 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4386 | MGMT_STATUS_BUSY); |
| 4387 | goto unlock; |
| 4388 | } |
| 4389 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4390 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4391 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4392 | hdev); |
| 4393 | goto unlock; |
| 4394 | } |
| 4395 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4396 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4397 | data, len); |
| 4398 | if (!cmd) { |
| 4399 | err = -ENOMEM; |
| 4400 | goto unlock; |
| 4401 | } |
| 4402 | |
| 4403 | hci_req_init(&req, hdev); |
| 4404 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4405 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4406 | |
| 4407 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4408 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4409 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4410 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4411 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4412 | } |
| 4413 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4414 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4415 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4416 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4417 | return err; |
| 4418 | } |
| 4419 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4420 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4421 | { |
| 4422 | struct pending_cmd *cmd; |
| 4423 | |
| 4424 | BT_DBG("status 0x%02x", status); |
| 4425 | |
| 4426 | hci_dev_lock(hdev); |
| 4427 | |
| 4428 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4429 | if (!cmd) |
| 4430 | goto unlock; |
| 4431 | |
| 4432 | if (status) { |
| 4433 | u8 mgmt_err = mgmt_status(status); |
| 4434 | |
| 4435 | /* We need to restore the flag if related HCI commands |
| 4436 | * failed. |
| 4437 | */ |
| 4438 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4439 | |
| 4440 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4441 | } else { |
| 4442 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4443 | new_settings(hdev, cmd->sk); |
| 4444 | } |
| 4445 | |
| 4446 | mgmt_pending_remove(cmd); |
| 4447 | |
| 4448 | unlock: |
| 4449 | hci_dev_unlock(hdev); |
| 4450 | } |
| 4451 | |
| 4452 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4453 | { |
| 4454 | struct mgmt_mode *cp = data; |
| 4455 | struct pending_cmd *cmd; |
| 4456 | struct hci_request req; |
| 4457 | int err; |
| 4458 | |
| 4459 | BT_DBG("request for %s", hdev->name); |
| 4460 | |
| 4461 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4462 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4463 | MGMT_STATUS_NOT_SUPPORTED); |
| 4464 | |
| 4465 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4466 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4467 | MGMT_STATUS_REJECTED); |
| 4468 | |
| 4469 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4470 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4471 | MGMT_STATUS_INVALID_PARAMS); |
| 4472 | |
| 4473 | hci_dev_lock(hdev); |
| 4474 | |
| 4475 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4476 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4477 | goto unlock; |
| 4478 | } |
| 4479 | |
| 4480 | if (!hdev_is_powered(hdev)) { |
| 4481 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4482 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4483 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4484 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4485 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4486 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4487 | } |
| 4488 | |
| 4489 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4490 | |
| 4491 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4492 | if (err < 0) |
| 4493 | goto unlock; |
| 4494 | |
| 4495 | err = new_settings(hdev, sk); |
| 4496 | goto unlock; |
| 4497 | } |
| 4498 | |
| 4499 | /* Reject disabling when powered on */ |
| 4500 | if (!cp->val) { |
| 4501 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4502 | MGMT_STATUS_REJECTED); |
| 4503 | goto unlock; |
| 4504 | } |
| 4505 | |
| 4506 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4507 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4508 | MGMT_STATUS_BUSY); |
| 4509 | goto unlock; |
| 4510 | } |
| 4511 | |
| 4512 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4513 | if (!cmd) { |
| 4514 | err = -ENOMEM; |
| 4515 | goto unlock; |
| 4516 | } |
| 4517 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4518 | /* 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] | 4519 | * generates the correct flags. |
| 4520 | */ |
| 4521 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4522 | |
| 4523 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4524 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 4525 | write_fast_connectable(&req, false); |
| 4526 | hci_update_page_scan(hdev, &req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4527 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4528 | /* Since only the advertising data flags will change, there |
| 4529 | * is no need to update the scan response data. |
| 4530 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4531 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4532 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4533 | err = hci_req_run(&req, set_bredr_complete); |
| 4534 | if (err < 0) |
| 4535 | mgmt_pending_remove(cmd); |
| 4536 | |
| 4537 | unlock: |
| 4538 | hci_dev_unlock(hdev); |
| 4539 | return err; |
| 4540 | } |
| 4541 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4542 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4543 | void *data, u16 len) |
| 4544 | { |
| 4545 | struct mgmt_mode *cp = data; |
| 4546 | struct pending_cmd *cmd; |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4547 | u8 val; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4548 | int err; |
| 4549 | |
| 4550 | BT_DBG("request for %s", hdev->name); |
| 4551 | |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4552 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 4553 | !lmp_sc_capable(hdev) && !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4554 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4555 | MGMT_STATUS_NOT_SUPPORTED); |
| 4556 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4557 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4558 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4559 | MGMT_STATUS_INVALID_PARAMS); |
| 4560 | |
| 4561 | hci_dev_lock(hdev); |
| 4562 | |
Johan Hedberg | a320969 | 2014-05-26 11:23:35 +0300 | [diff] [blame] | 4563 | if (!hdev_is_powered(hdev) || |
| 4564 | (!lmp_sc_capable(hdev) && |
| 4565 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) || |
| 4566 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4567 | bool changed; |
| 4568 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4569 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4570 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4571 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4572 | if (cp->val == 0x02) |
| 4573 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4574 | else |
| 4575 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4576 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4577 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4578 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4579 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4580 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4581 | |
| 4582 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4583 | if (err < 0) |
| 4584 | goto failed; |
| 4585 | |
| 4586 | if (changed) |
| 4587 | err = new_settings(hdev, sk); |
| 4588 | |
| 4589 | goto failed; |
| 4590 | } |
| 4591 | |
| 4592 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4593 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4594 | MGMT_STATUS_BUSY); |
| 4595 | goto failed; |
| 4596 | } |
| 4597 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4598 | val = !!cp->val; |
| 4599 | |
| 4600 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4601 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4602 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4603 | goto failed; |
| 4604 | } |
| 4605 | |
| 4606 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4607 | if (!cmd) { |
| 4608 | err = -ENOMEM; |
| 4609 | goto failed; |
| 4610 | } |
| 4611 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4612 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4613 | if (err < 0) { |
| 4614 | mgmt_pending_remove(cmd); |
| 4615 | goto failed; |
| 4616 | } |
| 4617 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4618 | if (cp->val == 0x02) |
| 4619 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4620 | else |
| 4621 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4622 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4623 | failed: |
| 4624 | hci_dev_unlock(hdev); |
| 4625 | return err; |
| 4626 | } |
| 4627 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4628 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4629 | void *data, u16 len) |
| 4630 | { |
| 4631 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4632 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4633 | int err; |
| 4634 | |
| 4635 | BT_DBG("request for %s", hdev->name); |
| 4636 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4637 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4638 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4639 | MGMT_STATUS_INVALID_PARAMS); |
| 4640 | |
| 4641 | hci_dev_lock(hdev); |
| 4642 | |
| 4643 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4644 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4645 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4646 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4647 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4648 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4649 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4650 | if (cp->val == 0x02) |
| 4651 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4652 | &hdev->dev_flags); |
| 4653 | else |
| 4654 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4655 | &hdev->dev_flags); |
| 4656 | |
| 4657 | if (hdev_is_powered(hdev) && use_changed && |
| 4658 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4659 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4660 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4661 | sizeof(mode), &mode); |
| 4662 | } |
| 4663 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4664 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4665 | if (err < 0) |
| 4666 | goto unlock; |
| 4667 | |
| 4668 | if (changed) |
| 4669 | err = new_settings(hdev, sk); |
| 4670 | |
| 4671 | unlock: |
| 4672 | hci_dev_unlock(hdev); |
| 4673 | return err; |
| 4674 | } |
| 4675 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4676 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4677 | u16 len) |
| 4678 | { |
| 4679 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4680 | bool changed; |
| 4681 | int err; |
| 4682 | |
| 4683 | BT_DBG("request for %s", hdev->name); |
| 4684 | |
| 4685 | if (!lmp_le_capable(hdev)) |
| 4686 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4687 | MGMT_STATUS_NOT_SUPPORTED); |
| 4688 | |
| 4689 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4690 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4691 | MGMT_STATUS_INVALID_PARAMS); |
| 4692 | |
| 4693 | if (hdev_is_powered(hdev)) |
| 4694 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4695 | MGMT_STATUS_REJECTED); |
| 4696 | |
| 4697 | hci_dev_lock(hdev); |
| 4698 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4699 | /* If user space supports this command it is also expected to |
| 4700 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4701 | */ |
| 4702 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4703 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4704 | if (cp->privacy) { |
| 4705 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4706 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4707 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4708 | } else { |
| 4709 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4710 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4711 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4712 | } |
| 4713 | |
| 4714 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4715 | if (err < 0) |
| 4716 | goto unlock; |
| 4717 | |
| 4718 | if (changed) |
| 4719 | err = new_settings(hdev, sk); |
| 4720 | |
| 4721 | unlock: |
| 4722 | hci_dev_unlock(hdev); |
| 4723 | return err; |
| 4724 | } |
| 4725 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4726 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4727 | { |
| 4728 | switch (irk->addr.type) { |
| 4729 | case BDADDR_LE_PUBLIC: |
| 4730 | return true; |
| 4731 | |
| 4732 | case BDADDR_LE_RANDOM: |
| 4733 | /* Two most significant bits shall be set */ |
| 4734 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4735 | return false; |
| 4736 | return true; |
| 4737 | } |
| 4738 | |
| 4739 | return false; |
| 4740 | } |
| 4741 | |
| 4742 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4743 | u16 len) |
| 4744 | { |
| 4745 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4746 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4747 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4748 | u16 irk_count, expected_len; |
| 4749 | int i, err; |
| 4750 | |
| 4751 | BT_DBG("request for %s", hdev->name); |
| 4752 | |
| 4753 | if (!lmp_le_capable(hdev)) |
| 4754 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4755 | MGMT_STATUS_NOT_SUPPORTED); |
| 4756 | |
| 4757 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4758 | if (irk_count > max_irk_count) { |
| 4759 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
| 4760 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4761 | MGMT_STATUS_INVALID_PARAMS); |
| 4762 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4763 | |
| 4764 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4765 | if (expected_len != len) { |
| 4766 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4767 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4768 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4769 | MGMT_STATUS_INVALID_PARAMS); |
| 4770 | } |
| 4771 | |
| 4772 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4773 | |
| 4774 | for (i = 0; i < irk_count; i++) { |
| 4775 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4776 | |
| 4777 | if (!irk_is_valid(key)) |
| 4778 | return cmd_status(sk, hdev->id, |
| 4779 | MGMT_OP_LOAD_IRKS, |
| 4780 | MGMT_STATUS_INVALID_PARAMS); |
| 4781 | } |
| 4782 | |
| 4783 | hci_dev_lock(hdev); |
| 4784 | |
| 4785 | hci_smp_irks_clear(hdev); |
| 4786 | |
| 4787 | for (i = 0; i < irk_count; i++) { |
| 4788 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4789 | u8 addr_type; |
| 4790 | |
| 4791 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4792 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4793 | else |
| 4794 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4795 | |
| 4796 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4797 | BDADDR_ANY); |
| 4798 | } |
| 4799 | |
| 4800 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4801 | |
| 4802 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4803 | |
| 4804 | hci_dev_unlock(hdev); |
| 4805 | |
| 4806 | return err; |
| 4807 | } |
| 4808 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4809 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4810 | { |
| 4811 | if (key->master != 0x00 && key->master != 0x01) |
| 4812 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4813 | |
| 4814 | switch (key->addr.type) { |
| 4815 | case BDADDR_LE_PUBLIC: |
| 4816 | return true; |
| 4817 | |
| 4818 | case BDADDR_LE_RANDOM: |
| 4819 | /* Two most significant bits shall be set */ |
| 4820 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4821 | return false; |
| 4822 | return true; |
| 4823 | } |
| 4824 | |
| 4825 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4826 | } |
| 4827 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4828 | 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] | 4829 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4830 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4831 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4832 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4833 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4834 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4835 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4836 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4837 | BT_DBG("request for %s", hdev->name); |
| 4838 | |
| 4839 | if (!lmp_le_capable(hdev)) |
| 4840 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4841 | MGMT_STATUS_NOT_SUPPORTED); |
| 4842 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4843 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4844 | if (key_count > max_key_count) { |
| 4845 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
| 4846 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4847 | MGMT_STATUS_INVALID_PARAMS); |
| 4848 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4849 | |
| 4850 | expected_len = sizeof(*cp) + key_count * |
| 4851 | sizeof(struct mgmt_ltk_info); |
| 4852 | if (expected_len != len) { |
| 4853 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4854 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4855 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4856 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4857 | } |
| 4858 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4859 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4860 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4861 | for (i = 0; i < key_count; i++) { |
| 4862 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4863 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4864 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4865 | return cmd_status(sk, hdev->id, |
| 4866 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4867 | MGMT_STATUS_INVALID_PARAMS); |
| 4868 | } |
| 4869 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4870 | hci_dev_lock(hdev); |
| 4871 | |
| 4872 | hci_smp_ltks_clear(hdev); |
| 4873 | |
| 4874 | for (i = 0; i < key_count; i++) { |
| 4875 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4876 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4877 | |
| 4878 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4879 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4880 | else |
| 4881 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4882 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4883 | switch (key->type) { |
| 4884 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4885 | authenticated = 0x00; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 4886 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4887 | break; |
| 4888 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4889 | authenticated = 0x01; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 4890 | type = key->master ? SMP_LTK : SMP_LTK_SLAVE; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4891 | break; |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 4892 | case MGMT_LTK_P256_UNAUTH: |
| 4893 | authenticated = 0x00; |
| 4894 | type = SMP_LTK_P256; |
| 4895 | break; |
| 4896 | case MGMT_LTK_P256_AUTH: |
| 4897 | authenticated = 0x01; |
| 4898 | type = SMP_LTK_P256; |
| 4899 | break; |
| 4900 | case MGMT_LTK_P256_DEBUG: |
| 4901 | authenticated = 0x00; |
| 4902 | type = SMP_LTK_P256_DEBUG; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4903 | default: |
| 4904 | continue; |
| 4905 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4906 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4907 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4908 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4909 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4910 | } |
| 4911 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4912 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4913 | NULL, 0); |
| 4914 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4915 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4916 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4917 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4918 | } |
| 4919 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4920 | struct cmd_conn_lookup { |
| 4921 | struct hci_conn *conn; |
| 4922 | bool valid_tx_power; |
| 4923 | u8 mgmt_status; |
| 4924 | }; |
| 4925 | |
| 4926 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4927 | { |
| 4928 | struct cmd_conn_lookup *match = data; |
| 4929 | struct mgmt_cp_get_conn_info *cp; |
| 4930 | struct mgmt_rp_get_conn_info rp; |
| 4931 | struct hci_conn *conn = cmd->user_data; |
| 4932 | |
| 4933 | if (conn != match->conn) |
| 4934 | return; |
| 4935 | |
| 4936 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4937 | |
| 4938 | memset(&rp, 0, sizeof(rp)); |
| 4939 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4940 | rp.addr.type = cp->addr.type; |
| 4941 | |
| 4942 | if (!match->mgmt_status) { |
| 4943 | rp.rssi = conn->rssi; |
| 4944 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4945 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4946 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4947 | rp.max_tx_power = conn->max_tx_power; |
| 4948 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4949 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4950 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4951 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4955 | match->mgmt_status, &rp, sizeof(rp)); |
| 4956 | |
| 4957 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 4958 | hci_conn_put(conn); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4959 | |
| 4960 | mgmt_pending_remove(cmd); |
| 4961 | } |
| 4962 | |
| 4963 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4964 | { |
| 4965 | struct hci_cp_read_rssi *cp; |
| 4966 | struct hci_conn *conn; |
| 4967 | struct cmd_conn_lookup match; |
| 4968 | u16 handle; |
| 4969 | |
| 4970 | BT_DBG("status 0x%02x", status); |
| 4971 | |
| 4972 | hci_dev_lock(hdev); |
| 4973 | |
| 4974 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4975 | * otherwise we assume it's not valid. At the moment we assume that |
| 4976 | * either both or none of current and max values are valid to keep code |
| 4977 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4978 | */ |
| 4979 | match.valid_tx_power = !status; |
| 4980 | |
| 4981 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4982 | * Level so we check which one was last sent to retrieve connection |
| 4983 | * handle. Both commands have handle as first parameter so it's safe to |
| 4984 | * cast data on the same command struct. |
| 4985 | * |
| 4986 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4987 | * In other case we simply override error to indicate success as we |
| 4988 | * already remembered if TX power value is actually valid. |
| 4989 | */ |
| 4990 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4991 | if (!cp) { |
| 4992 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4993 | status = 0; |
| 4994 | } |
| 4995 | |
| 4996 | if (!cp) { |
| 4997 | BT_ERR("invalid sent_cmd in response"); |
| 4998 | goto unlock; |
| 4999 | } |
| 5000 | |
| 5001 | handle = __le16_to_cpu(cp->handle); |
| 5002 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5003 | if (!conn) { |
| 5004 | BT_ERR("unknown handle (%d) in response", handle); |
| 5005 | goto unlock; |
| 5006 | } |
| 5007 | |
| 5008 | match.conn = conn; |
| 5009 | match.mgmt_status = mgmt_status(status); |
| 5010 | |
| 5011 | /* Cache refresh is complete, now reply for mgmt request for given |
| 5012 | * connection only. |
| 5013 | */ |
| 5014 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 5015 | get_conn_info_complete, &match); |
| 5016 | |
| 5017 | unlock: |
| 5018 | hci_dev_unlock(hdev); |
| 5019 | } |
| 5020 | |
| 5021 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5022 | u16 len) |
| 5023 | { |
| 5024 | struct mgmt_cp_get_conn_info *cp = data; |
| 5025 | struct mgmt_rp_get_conn_info rp; |
| 5026 | struct hci_conn *conn; |
| 5027 | unsigned long conn_info_age; |
| 5028 | int err = 0; |
| 5029 | |
| 5030 | BT_DBG("%s", hdev->name); |
| 5031 | |
| 5032 | memset(&rp, 0, sizeof(rp)); |
| 5033 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5034 | rp.addr.type = cp->addr.type; |
| 5035 | |
| 5036 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 5037 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5038 | MGMT_STATUS_INVALID_PARAMS, |
| 5039 | &rp, sizeof(rp)); |
| 5040 | |
| 5041 | hci_dev_lock(hdev); |
| 5042 | |
| 5043 | if (!hdev_is_powered(hdev)) { |
| 5044 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5045 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5046 | goto unlock; |
| 5047 | } |
| 5048 | |
| 5049 | if (cp->addr.type == BDADDR_BREDR) |
| 5050 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5051 | &cp->addr.bdaddr); |
| 5052 | else |
| 5053 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 5054 | |
| 5055 | if (!conn || conn->state != BT_CONNECTED) { |
| 5056 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5057 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 5058 | goto unlock; |
| 5059 | } |
| 5060 | |
| 5061 | /* To avoid client trying to guess when to poll again for information we |
| 5062 | * calculate conn info age as random value between min/max set in hdev. |
| 5063 | */ |
| 5064 | conn_info_age = hdev->conn_info_min_age + |
| 5065 | prandom_u32_max(hdev->conn_info_max_age - |
| 5066 | hdev->conn_info_min_age); |
| 5067 | |
| 5068 | /* Query controller to refresh cached values if they are too old or were |
| 5069 | * never read. |
| 5070 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5071 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5072 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5073 | !conn->conn_info_timestamp) { |
| 5074 | struct hci_request req; |
| 5075 | struct hci_cp_read_tx_power req_txp_cp; |
| 5076 | struct hci_cp_read_rssi req_rssi_cp; |
| 5077 | struct pending_cmd *cmd; |
| 5078 | |
| 5079 | hci_req_init(&req, hdev); |
| 5080 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5081 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5082 | &req_rssi_cp); |
| 5083 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5084 | /* For LE links TX power does not change thus we don't need to |
| 5085 | * query for it once value is known. |
| 5086 | */ |
| 5087 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5088 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5089 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5090 | req_txp_cp.type = 0x00; |
| 5091 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5092 | sizeof(req_txp_cp), &req_txp_cp); |
| 5093 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5094 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5095 | /* Max TX power needs to be read only once per connection */ |
| 5096 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5097 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5098 | req_txp_cp.type = 0x01; |
| 5099 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5100 | sizeof(req_txp_cp), &req_txp_cp); |
| 5101 | } |
| 5102 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5103 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5104 | if (err < 0) |
| 5105 | goto unlock; |
| 5106 | |
| 5107 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5108 | data, len); |
| 5109 | if (!cmd) { |
| 5110 | err = -ENOMEM; |
| 5111 | goto unlock; |
| 5112 | } |
| 5113 | |
| 5114 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5115 | cmd->user_data = hci_conn_get(conn); |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5116 | |
| 5117 | conn->conn_info_timestamp = jiffies; |
| 5118 | } else { |
| 5119 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5120 | rp.rssi = conn->rssi; |
| 5121 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5122 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5123 | |
| 5124 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5125 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 5126 | } |
| 5127 | |
| 5128 | unlock: |
| 5129 | hci_dev_unlock(hdev); |
| 5130 | return err; |
| 5131 | } |
| 5132 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5133 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 5134 | { |
| 5135 | struct mgmt_cp_get_clock_info *cp; |
| 5136 | struct mgmt_rp_get_clock_info rp; |
| 5137 | struct hci_cp_read_clock *hci_cp; |
| 5138 | struct pending_cmd *cmd; |
| 5139 | struct hci_conn *conn; |
| 5140 | |
| 5141 | BT_DBG("%s status %u", hdev->name, status); |
| 5142 | |
| 5143 | hci_dev_lock(hdev); |
| 5144 | |
| 5145 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5146 | if (!hci_cp) |
| 5147 | goto unlock; |
| 5148 | |
| 5149 | if (hci_cp->which) { |
| 5150 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5151 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5152 | } else { |
| 5153 | conn = NULL; |
| 5154 | } |
| 5155 | |
| 5156 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5157 | if (!cmd) |
| 5158 | goto unlock; |
| 5159 | |
| 5160 | cp = cmd->param; |
| 5161 | |
| 5162 | memset(&rp, 0, sizeof(rp)); |
| 5163 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 5164 | |
| 5165 | if (status) |
| 5166 | goto send_rsp; |
| 5167 | |
| 5168 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5169 | |
| 5170 | if (conn) { |
| 5171 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5172 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5173 | } |
| 5174 | |
| 5175 | send_rsp: |
| 5176 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 5177 | &rp, sizeof(rp)); |
| 5178 | mgmt_pending_remove(cmd); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5179 | if (conn) { |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5180 | hci_conn_drop(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5181 | hci_conn_put(conn); |
| 5182 | } |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5183 | |
| 5184 | unlock: |
| 5185 | hci_dev_unlock(hdev); |
| 5186 | } |
| 5187 | |
| 5188 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5189 | u16 len) |
| 5190 | { |
| 5191 | struct mgmt_cp_get_clock_info *cp = data; |
| 5192 | struct mgmt_rp_get_clock_info rp; |
| 5193 | struct hci_cp_read_clock hci_cp; |
| 5194 | struct pending_cmd *cmd; |
| 5195 | struct hci_request req; |
| 5196 | struct hci_conn *conn; |
| 5197 | int err; |
| 5198 | |
| 5199 | BT_DBG("%s", hdev->name); |
| 5200 | |
| 5201 | memset(&rp, 0, sizeof(rp)); |
| 5202 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5203 | rp.addr.type = cp->addr.type; |
| 5204 | |
| 5205 | if (cp->addr.type != BDADDR_BREDR) |
| 5206 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5207 | MGMT_STATUS_INVALID_PARAMS, |
| 5208 | &rp, sizeof(rp)); |
| 5209 | |
| 5210 | hci_dev_lock(hdev); |
| 5211 | |
| 5212 | if (!hdev_is_powered(hdev)) { |
| 5213 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5214 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5215 | goto unlock; |
| 5216 | } |
| 5217 | |
| 5218 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5219 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5220 | &cp->addr.bdaddr); |
| 5221 | if (!conn || conn->state != BT_CONNECTED) { |
| 5222 | err = cmd_complete(sk, hdev->id, |
| 5223 | MGMT_OP_GET_CLOCK_INFO, |
| 5224 | MGMT_STATUS_NOT_CONNECTED, |
| 5225 | &rp, sizeof(rp)); |
| 5226 | goto unlock; |
| 5227 | } |
| 5228 | } else { |
| 5229 | conn = NULL; |
| 5230 | } |
| 5231 | |
| 5232 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5233 | if (!cmd) { |
| 5234 | err = -ENOMEM; |
| 5235 | goto unlock; |
| 5236 | } |
| 5237 | |
| 5238 | hci_req_init(&req, hdev); |
| 5239 | |
| 5240 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5241 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5242 | |
| 5243 | if (conn) { |
| 5244 | hci_conn_hold(conn); |
Johan Hedberg | f8aaf9b | 2014-08-17 23:28:57 +0300 | [diff] [blame] | 5245 | cmd->user_data = hci_conn_get(conn); |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5246 | |
| 5247 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5248 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5249 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5250 | } |
| 5251 | |
| 5252 | err = hci_req_run(&req, get_clock_info_complete); |
| 5253 | if (err < 0) |
| 5254 | mgmt_pending_remove(cmd); |
| 5255 | |
| 5256 | unlock: |
| 5257 | hci_dev_unlock(hdev); |
| 5258 | return err; |
| 5259 | } |
| 5260 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5261 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5262 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5263 | { |
| 5264 | struct mgmt_ev_device_added ev; |
| 5265 | |
| 5266 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5267 | ev.addr.type = type; |
| 5268 | ev.action = action; |
| 5269 | |
| 5270 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5271 | } |
| 5272 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5273 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5274 | void *data, u16 len) |
| 5275 | { |
| 5276 | struct mgmt_cp_add_device *cp = data; |
| 5277 | u8 auto_conn, addr_type; |
| 5278 | int err; |
| 5279 | |
| 5280 | BT_DBG("%s", hdev->name); |
| 5281 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5282 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5283 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5284 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5285 | MGMT_STATUS_INVALID_PARAMS, |
| 5286 | &cp->addr, sizeof(cp->addr)); |
| 5287 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5288 | if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5289 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5290 | MGMT_STATUS_INVALID_PARAMS, |
| 5291 | &cp->addr, sizeof(cp->addr)); |
| 5292 | |
| 5293 | hci_dev_lock(hdev); |
| 5294 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5295 | if (cp->addr.type == BDADDR_BREDR) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5296 | /* Only incoming connections action is supported for now */ |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5297 | if (cp->action != 0x01) { |
| 5298 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5299 | MGMT_STATUS_INVALID_PARAMS, |
| 5300 | &cp->addr, sizeof(cp->addr)); |
| 5301 | goto unlock; |
| 5302 | } |
| 5303 | |
| 5304 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5305 | cp->addr.type); |
| 5306 | if (err) |
| 5307 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5308 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 5309 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5310 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5311 | goto added; |
| 5312 | } |
| 5313 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5314 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5315 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5316 | else |
| 5317 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5318 | |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5319 | if (cp->action == 0x02) |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5320 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5321 | else if (cp->action == 0x01) |
| 5322 | auto_conn = HCI_AUTO_CONN_DIRECT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5323 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5324 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5325 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5326 | /* If the connection parameters don't exist for this device, |
| 5327 | * they will be created and configured with defaults. |
| 5328 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5329 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5330 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5331 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5332 | MGMT_STATUS_FAILED, |
| 5333 | &cp->addr, sizeof(cp->addr)); |
| 5334 | goto unlock; |
| 5335 | } |
| 5336 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5337 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5338 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5339 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5340 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5341 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5342 | |
| 5343 | unlock: |
| 5344 | hci_dev_unlock(hdev); |
| 5345 | return err; |
| 5346 | } |
| 5347 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5348 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5349 | bdaddr_t *bdaddr, u8 type) |
| 5350 | { |
| 5351 | struct mgmt_ev_device_removed ev; |
| 5352 | |
| 5353 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5354 | ev.addr.type = type; |
| 5355 | |
| 5356 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5357 | } |
| 5358 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5359 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5360 | void *data, u16 len) |
| 5361 | { |
| 5362 | struct mgmt_cp_remove_device *cp = data; |
| 5363 | int err; |
| 5364 | |
| 5365 | BT_DBG("%s", hdev->name); |
| 5366 | |
| 5367 | hci_dev_lock(hdev); |
| 5368 | |
| 5369 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5370 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5371 | u8 addr_type; |
| 5372 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5373 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5374 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5375 | MGMT_STATUS_INVALID_PARAMS, |
| 5376 | &cp->addr, sizeof(cp->addr)); |
| 5377 | goto unlock; |
| 5378 | } |
| 5379 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5380 | if (cp->addr.type == BDADDR_BREDR) { |
| 5381 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5382 | &cp->addr.bdaddr, |
| 5383 | cp->addr.type); |
| 5384 | if (err) { |
| 5385 | err = cmd_complete(sk, hdev->id, |
| 5386 | MGMT_OP_REMOVE_DEVICE, |
| 5387 | MGMT_STATUS_INVALID_PARAMS, |
| 5388 | &cp->addr, sizeof(cp->addr)); |
| 5389 | goto unlock; |
| 5390 | } |
| 5391 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 5392 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5393 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5394 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5395 | cp->addr.type); |
| 5396 | goto complete; |
| 5397 | } |
| 5398 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5399 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5400 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5401 | else |
| 5402 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5403 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5404 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5405 | addr_type); |
| 5406 | if (!params) { |
| 5407 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5408 | MGMT_STATUS_INVALID_PARAMS, |
| 5409 | &cp->addr, sizeof(cp->addr)); |
| 5410 | goto unlock; |
| 5411 | } |
| 5412 | |
| 5413 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5414 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5415 | MGMT_STATUS_INVALID_PARAMS, |
| 5416 | &cp->addr, sizeof(cp->addr)); |
| 5417 | goto unlock; |
| 5418 | } |
| 5419 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5420 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5421 | list_del(¶ms->list); |
| 5422 | kfree(params); |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame] | 5423 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5424 | |
| 5425 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5426 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5427 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5428 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5429 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5430 | if (cp->addr.type) { |
| 5431 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5432 | MGMT_STATUS_INVALID_PARAMS, |
| 5433 | &cp->addr, sizeof(cp->addr)); |
| 5434 | goto unlock; |
| 5435 | } |
| 5436 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5437 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 5438 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 5439 | list_del(&b->list); |
| 5440 | kfree(b); |
| 5441 | } |
| 5442 | |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 5443 | hci_update_page_scan(hdev, NULL); |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5444 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5445 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5446 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5447 | continue; |
| 5448 | device_removed(sk, hdev, &p->addr, p->addr_type); |
| 5449 | list_del(&p->action); |
| 5450 | list_del(&p->list); |
| 5451 | kfree(p); |
| 5452 | } |
| 5453 | |
| 5454 | BT_DBG("All LE connection parameters were removed"); |
| 5455 | |
| 5456 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5457 | } |
| 5458 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5459 | complete: |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5460 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5461 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5462 | |
| 5463 | unlock: |
| 5464 | hci_dev_unlock(hdev); |
| 5465 | return err; |
| 5466 | } |
| 5467 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5468 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5469 | u16 len) |
| 5470 | { |
| 5471 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5472 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5473 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5474 | u16 param_count, expected_len; |
| 5475 | int i; |
| 5476 | |
| 5477 | if (!lmp_le_capable(hdev)) |
| 5478 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5479 | MGMT_STATUS_NOT_SUPPORTED); |
| 5480 | |
| 5481 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5482 | if (param_count > max_param_count) { |
| 5483 | BT_ERR("load_conn_param: too big param_count value %u", |
| 5484 | param_count); |
| 5485 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5486 | MGMT_STATUS_INVALID_PARAMS); |
| 5487 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5488 | |
| 5489 | expected_len = sizeof(*cp) + param_count * |
| 5490 | sizeof(struct mgmt_conn_param); |
| 5491 | if (expected_len != len) { |
| 5492 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5493 | expected_len, len); |
| 5494 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5495 | MGMT_STATUS_INVALID_PARAMS); |
| 5496 | } |
| 5497 | |
| 5498 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5499 | |
| 5500 | hci_dev_lock(hdev); |
| 5501 | |
| 5502 | hci_conn_params_clear_disabled(hdev); |
| 5503 | |
| 5504 | for (i = 0; i < param_count; i++) { |
| 5505 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5506 | struct hci_conn_params *hci_param; |
| 5507 | u16 min, max, latency, timeout; |
| 5508 | u8 addr_type; |
| 5509 | |
| 5510 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5511 | param->addr.type); |
| 5512 | |
| 5513 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5514 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5515 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5516 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5517 | } else { |
| 5518 | BT_ERR("Ignoring invalid connection parameters"); |
| 5519 | continue; |
| 5520 | } |
| 5521 | |
| 5522 | min = le16_to_cpu(param->min_interval); |
| 5523 | max = le16_to_cpu(param->max_interval); |
| 5524 | latency = le16_to_cpu(param->latency); |
| 5525 | timeout = le16_to_cpu(param->timeout); |
| 5526 | |
| 5527 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5528 | min, max, latency, timeout); |
| 5529 | |
| 5530 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5531 | BT_ERR("Ignoring invalid connection parameters"); |
| 5532 | continue; |
| 5533 | } |
| 5534 | |
| 5535 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5536 | addr_type); |
| 5537 | if (!hci_param) { |
| 5538 | BT_ERR("Failed to add connection parameters"); |
| 5539 | continue; |
| 5540 | } |
| 5541 | |
| 5542 | hci_param->conn_min_interval = min; |
| 5543 | hci_param->conn_max_interval = max; |
| 5544 | hci_param->conn_latency = latency; |
| 5545 | hci_param->supervision_timeout = timeout; |
| 5546 | } |
| 5547 | |
| 5548 | hci_dev_unlock(hdev); |
| 5549 | |
| 5550 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5551 | } |
| 5552 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5553 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5554 | void *data, u16 len) |
| 5555 | { |
| 5556 | struct mgmt_cp_set_external_config *cp = data; |
| 5557 | bool changed; |
| 5558 | int err; |
| 5559 | |
| 5560 | BT_DBG("%s", hdev->name); |
| 5561 | |
| 5562 | if (hdev_is_powered(hdev)) |
| 5563 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5564 | MGMT_STATUS_REJECTED); |
| 5565 | |
| 5566 | if (cp->config != 0x00 && cp->config != 0x01) |
| 5567 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5568 | MGMT_STATUS_INVALID_PARAMS); |
| 5569 | |
| 5570 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 5571 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5572 | MGMT_STATUS_NOT_SUPPORTED); |
| 5573 | |
| 5574 | hci_dev_lock(hdev); |
| 5575 | |
| 5576 | if (cp->config) |
| 5577 | changed = !test_and_set_bit(HCI_EXT_CONFIGURED, |
| 5578 | &hdev->dev_flags); |
| 5579 | else |
| 5580 | changed = test_and_clear_bit(HCI_EXT_CONFIGURED, |
| 5581 | &hdev->dev_flags); |
| 5582 | |
| 5583 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5584 | if (err < 0) |
| 5585 | goto unlock; |
| 5586 | |
| 5587 | if (!changed) |
| 5588 | goto unlock; |
| 5589 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 5590 | err = new_options(hdev, sk); |
| 5591 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5592 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) { |
| 5593 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5594 | |
| 5595 | if (test_and_change_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { |
| 5596 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5597 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5598 | |
| 5599 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5600 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 5601 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5602 | mgmt_index_added(hdev); |
| 5603 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | unlock: |
| 5607 | hci_dev_unlock(hdev); |
| 5608 | return err; |
| 5609 | } |
| 5610 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5611 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 5612 | void *data, u16 len) |
| 5613 | { |
| 5614 | struct mgmt_cp_set_public_address *cp = data; |
| 5615 | bool changed; |
| 5616 | int err; |
| 5617 | |
| 5618 | BT_DBG("%s", hdev->name); |
| 5619 | |
| 5620 | if (hdev_is_powered(hdev)) |
| 5621 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5622 | MGMT_STATUS_REJECTED); |
| 5623 | |
| 5624 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
| 5625 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5626 | MGMT_STATUS_INVALID_PARAMS); |
| 5627 | |
| 5628 | if (!hdev->set_bdaddr) |
| 5629 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5630 | MGMT_STATUS_NOT_SUPPORTED); |
| 5631 | |
| 5632 | hci_dev_lock(hdev); |
| 5633 | |
| 5634 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 5635 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 5636 | |
| 5637 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 5638 | if (err < 0) |
| 5639 | goto unlock; |
| 5640 | |
| 5641 | if (!changed) |
| 5642 | goto unlock; |
| 5643 | |
| 5644 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5645 | err = new_options(hdev, sk); |
| 5646 | |
| 5647 | if (is_configured(hdev)) { |
| 5648 | mgmt_index_removed(hdev); |
| 5649 | |
| 5650 | clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags); |
| 5651 | |
| 5652 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5653 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5654 | |
| 5655 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5656 | } |
| 5657 | |
| 5658 | unlock: |
| 5659 | hci_dev_unlock(hdev); |
| 5660 | return err; |
| 5661 | } |
| 5662 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5663 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5664 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5665 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5666 | bool var_len; |
| 5667 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5668 | } mgmt_handlers[] = { |
| 5669 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5670 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5671 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5672 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5673 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5674 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5675 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5676 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5677 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | b293947 | 2014-07-30 09:22:23 +0300 | [diff] [blame] | 5678 | { set_bondable, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5679 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5680 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5681 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5682 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5683 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5684 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5685 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5686 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5687 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5688 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5689 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5690 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5691 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5692 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5693 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5694 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5695 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5696 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5697 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5698 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5699 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5700 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5701 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5702 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5703 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5704 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5705 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5706 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5707 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5708 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5709 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5710 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5711 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5712 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5713 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5714 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5715 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5716 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5717 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5718 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5719 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5720 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5721 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5722 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
| 5723 | { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE }, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 5724 | { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE }, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5725 | { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE }, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5726 | { set_public_address, false, MGMT_SET_PUBLIC_ADDRESS_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5727 | }; |
| 5728 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5729 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5730 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5731 | void *buf; |
| 5732 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5733 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5734 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5735 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5736 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5737 | int err; |
| 5738 | |
| 5739 | BT_DBG("got %zu bytes", msglen); |
| 5740 | |
| 5741 | if (msglen < sizeof(*hdr)) |
| 5742 | return -EINVAL; |
| 5743 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5744 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5745 | if (!buf) |
| 5746 | return -ENOMEM; |
| 5747 | |
| 5748 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5749 | err = -EFAULT; |
| 5750 | goto done; |
| 5751 | } |
| 5752 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5753 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5754 | opcode = __le16_to_cpu(hdr->opcode); |
| 5755 | index = __le16_to_cpu(hdr->index); |
| 5756 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5757 | |
| 5758 | if (len != msglen - sizeof(*hdr)) { |
| 5759 | err = -EINVAL; |
| 5760 | goto done; |
| 5761 | } |
| 5762 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5763 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5764 | hdev = hci_dev_get(index); |
| 5765 | if (!hdev) { |
| 5766 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5767 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5768 | goto done; |
| 5769 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5770 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5771 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5772 | test_bit(HCI_CONFIG, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5773 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5774 | err = cmd_status(sk, index, opcode, |
| 5775 | MGMT_STATUS_INVALID_INDEX); |
| 5776 | goto done; |
| 5777 | } |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5778 | |
| 5779 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5780 | opcode != MGMT_OP_READ_CONFIG_INFO && |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5781 | opcode != MGMT_OP_SET_EXTERNAL_CONFIG && |
| 5782 | opcode != MGMT_OP_SET_PUBLIC_ADDRESS) { |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5783 | err = cmd_status(sk, index, opcode, |
| 5784 | MGMT_STATUS_INVALID_INDEX); |
| 5785 | goto done; |
| 5786 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5787 | } |
| 5788 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5789 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5790 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5791 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5792 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5793 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5794 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5795 | } |
| 5796 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5797 | if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || |
| 5798 | opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
| 5799 | err = cmd_status(sk, index, opcode, |
| 5800 | MGMT_STATUS_INVALID_INDEX); |
| 5801 | goto done; |
| 5802 | } |
| 5803 | |
| 5804 | if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && |
| 5805 | opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5806 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5807 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5808 | goto done; |
| 5809 | } |
| 5810 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5811 | handler = &mgmt_handlers[opcode]; |
| 5812 | |
| 5813 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5814 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5815 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5816 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5817 | goto done; |
| 5818 | } |
| 5819 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5820 | if (hdev) |
| 5821 | mgmt_init_hdev(sk, hdev); |
| 5822 | |
| 5823 | cp = buf + sizeof(*hdr); |
| 5824 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5825 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5826 | if (err < 0) |
| 5827 | goto done; |
| 5828 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5829 | err = msglen; |
| 5830 | |
| 5831 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5832 | if (hdev) |
| 5833 | hci_dev_put(hdev); |
| 5834 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5835 | kfree(buf); |
| 5836 | return err; |
| 5837 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5838 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5839 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5840 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5841 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5842 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5843 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5844 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5845 | return; |
| 5846 | |
| 5847 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5848 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5849 | else |
| 5850 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5851 | } |
| 5852 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5853 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5854 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5855 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5856 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5857 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5858 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5859 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5860 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5861 | return; |
| 5862 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5863 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5864 | |
Marcel Holtmann | edd3896b | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 5865 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5866 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5867 | else |
| 5868 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5869 | } |
| 5870 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5871 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5872 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5873 | { |
| 5874 | struct hci_conn_params *p; |
| 5875 | |
| 5876 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5877 | /* Needed for AUTO_OFF case where might not "really" |
| 5878 | * have been powered off. |
| 5879 | */ |
| 5880 | list_del_init(&p->action); |
| 5881 | |
| 5882 | switch (p->auto_connect) { |
Marcel Holtmann | 4b9e7e7 | 2014-07-23 21:55:23 +0200 | [diff] [blame] | 5883 | case HCI_AUTO_CONN_DIRECT: |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5884 | case HCI_AUTO_CONN_ALWAYS: |
| 5885 | list_add(&p->action, &hdev->pend_le_conns); |
| 5886 | break; |
| 5887 | case HCI_AUTO_CONN_REPORT: |
| 5888 | list_add(&p->action, &hdev->pend_le_reports); |
| 5889 | break; |
| 5890 | default: |
| 5891 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5892 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5893 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5894 | |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5895 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5896 | } |
| 5897 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5898 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5899 | { |
| 5900 | struct cmd_lookup match = { NULL, hdev }; |
| 5901 | |
| 5902 | BT_DBG("status 0x%02x", status); |
| 5903 | |
| 5904 | hci_dev_lock(hdev); |
| 5905 | |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5906 | restart_le_actions(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5907 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5908 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5909 | |
| 5910 | new_settings(hdev, match.sk); |
| 5911 | |
| 5912 | hci_dev_unlock(hdev); |
| 5913 | |
| 5914 | if (match.sk) |
| 5915 | sock_put(match.sk); |
| 5916 | } |
| 5917 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5918 | static int powered_update_hci(struct hci_dev *hdev) |
| 5919 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5920 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5921 | u8 link_sec; |
| 5922 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5923 | hci_req_init(&req, hdev); |
| 5924 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5925 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5926 | !lmp_host_ssp_capable(hdev)) { |
| 5927 | u8 ssp = 1; |
| 5928 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5929 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5930 | } |
| 5931 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5932 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5933 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5934 | struct hci_cp_write_le_host_supported cp; |
| 5935 | |
Marcel Holtmann | 32226e4 | 2014-07-24 20:04:16 +0200 | [diff] [blame] | 5936 | cp.le = 0x01; |
| 5937 | cp.simul = 0x00; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5938 | |
| 5939 | /* Check first if we already have the right |
| 5940 | * host state (host features set) |
| 5941 | */ |
| 5942 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5943 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5944 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5945 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5946 | } |
| 5947 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5948 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5949 | /* Make sure the controller has a good default for |
| 5950 | * advertising data. This also applies to the case |
| 5951 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5952 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5953 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5954 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5955 | update_scan_rsp_data(&req); |
| 5956 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5957 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5958 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5959 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5960 | } |
| 5961 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5962 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5963 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5964 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5965 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5966 | |
| 5967 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 432df05 | 2014-08-01 11:13:31 +0300 | [diff] [blame] | 5968 | write_fast_connectable(&req, false); |
| 5969 | hci_update_page_scan(hdev, &req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5970 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5971 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5972 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5973 | } |
| 5974 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5975 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5976 | } |
| 5977 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5978 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5979 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5980 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5981 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5982 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5983 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5984 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5985 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5986 | return 0; |
| 5987 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5988 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5989 | if (powered_update_hci(hdev) == 0) |
| 5990 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5991 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5992 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5993 | &match); |
| 5994 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5995 | } |
| 5996 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5997 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5998 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5999 | |
| 6000 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 6001 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 6002 | zero_cod, sizeof(zero_cod), NULL); |
| 6003 | |
| 6004 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 6005 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 6006 | |
| 6007 | if (match.sk) |
| 6008 | sock_put(match.sk); |
| 6009 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 6010 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 6011 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6012 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6013 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6014 | { |
| 6015 | struct pending_cmd *cmd; |
| 6016 | u8 status; |
| 6017 | |
| 6018 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6019 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6020 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6021 | |
| 6022 | if (err == -ERFKILL) |
| 6023 | status = MGMT_STATUS_RFKILLED; |
| 6024 | else |
| 6025 | status = MGMT_STATUS_FAILED; |
| 6026 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 6027 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6028 | |
| 6029 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6030 | } |
| 6031 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6032 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 6033 | { |
| 6034 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6035 | |
| 6036 | hci_dev_lock(hdev); |
| 6037 | |
| 6038 | /* When discoverable timeout triggers, then just make sure |
| 6039 | * the limited discoverable flag is cleared. Even in the case |
| 6040 | * of a timeout triggered from general discoverable, it is |
| 6041 | * safe to unconditionally clear the flag. |
| 6042 | */ |
| 6043 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6044 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6045 | |
| 6046 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 6047 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 6048 | u8 scan = SCAN_PAGE; |
| 6049 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 6050 | sizeof(scan), &scan); |
| 6051 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6052 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6053 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6054 | hci_req_run(&req, NULL); |
| 6055 | |
| 6056 | hdev->discov_timeout = 0; |
| 6057 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6058 | new_settings(hdev, NULL); |
| 6059 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6060 | hci_dev_unlock(hdev); |
| 6061 | } |
| 6062 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6063 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 6064 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6065 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 6066 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6067 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6068 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6069 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6070 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 6071 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6072 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6073 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 6074 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6075 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6076 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6077 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6078 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6079 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6080 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 6081 | { |
Johan Hedberg | 23fb8de | 2014-05-23 13:15:37 +0300 | [diff] [blame] | 6082 | switch (ltk->type) { |
| 6083 | case SMP_LTK: |
| 6084 | case SMP_LTK_SLAVE: |
| 6085 | if (ltk->authenticated) |
| 6086 | return MGMT_LTK_AUTHENTICATED; |
| 6087 | return MGMT_LTK_UNAUTHENTICATED; |
| 6088 | case SMP_LTK_P256: |
| 6089 | if (ltk->authenticated) |
| 6090 | return MGMT_LTK_P256_AUTH; |
| 6091 | return MGMT_LTK_P256_UNAUTH; |
| 6092 | case SMP_LTK_P256_DEBUG: |
| 6093 | return MGMT_LTK_P256_DEBUG; |
| 6094 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6095 | |
| 6096 | return MGMT_LTK_UNAUTHENTICATED; |
| 6097 | } |
| 6098 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6099 | 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] | 6100 | { |
| 6101 | struct mgmt_ev_new_long_term_key ev; |
| 6102 | |
| 6103 | memset(&ev, 0, sizeof(ev)); |
| 6104 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6105 | /* Devices using resolvable or non-resolvable random addresses |
| 6106 | * without providing an indentity resolving key don't require |
| 6107 | * to store long term keys. Their addresses will change the |
| 6108 | * next time around. |
| 6109 | * |
| 6110 | * Only when a remote device provides an identity address |
| 6111 | * make sure the long term key is stored. If the remote |
| 6112 | * identity is known, the long term keys are internally |
| 6113 | * mapped to the identity address. So allow static random |
| 6114 | * and public addresses here. |
| 6115 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6116 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6117 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6118 | ev.store_hint = 0x00; |
| 6119 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6120 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6121 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6122 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6123 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6124 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6125 | ev.key.enc_size = key->enc_size; |
| 6126 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 6127 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6128 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 6129 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6130 | ev.key.master = 1; |
| 6131 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6132 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 6133 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 6134 | 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] | 6135 | } |
| 6136 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6137 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 6138 | { |
| 6139 | struct mgmt_ev_new_irk ev; |
| 6140 | |
| 6141 | memset(&ev, 0, sizeof(ev)); |
| 6142 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6143 | /* For identity resolving keys from devices that are already |
| 6144 | * using a public address or static random address, do not |
| 6145 | * ask for storing this key. The identity resolving key really |
| 6146 | * is only mandatory for devices using resovlable random |
| 6147 | * addresses. |
| 6148 | * |
| 6149 | * Storing all identity resolving keys has the downside that |
| 6150 | * they will be also loaded on next boot of they system. More |
| 6151 | * identity resolving keys, means more time during scanning is |
| 6152 | * needed to actually resolve these addresses. |
| 6153 | */ |
| 6154 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 6155 | ev.store_hint = 0x01; |
| 6156 | else |
| 6157 | ev.store_hint = 0x00; |
| 6158 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6159 | bacpy(&ev.rpa, &irk->rpa); |
| 6160 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6161 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6162 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6163 | |
| 6164 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6165 | } |
| 6166 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6167 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6168 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6169 | { |
| 6170 | struct mgmt_ev_new_csrk ev; |
| 6171 | |
| 6172 | memset(&ev, 0, sizeof(ev)); |
| 6173 | |
| 6174 | /* Devices using resolvable or non-resolvable random addresses |
| 6175 | * without providing an indentity resolving key don't require |
| 6176 | * to store signature resolving keys. Their addresses will change |
| 6177 | * the next time around. |
| 6178 | * |
| 6179 | * Only when a remote device provides an identity address |
| 6180 | * make sure the signature resolving key is stored. So allow |
| 6181 | * static random and public addresses here. |
| 6182 | */ |
| 6183 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6184 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6185 | ev.store_hint = 0x00; |
| 6186 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6187 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6188 | |
| 6189 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6190 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 6191 | ev.key.master = csrk->master; |
| 6192 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6193 | |
| 6194 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6195 | } |
| 6196 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6197 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6198 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6199 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6200 | { |
| 6201 | struct mgmt_ev_new_conn_param ev; |
| 6202 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6203 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6204 | return; |
| 6205 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6206 | memset(&ev, 0, sizeof(ev)); |
| 6207 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6208 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6209 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6210 | ev.min_interval = cpu_to_le16(min_interval); |
| 6211 | ev.max_interval = cpu_to_le16(max_interval); |
| 6212 | ev.latency = cpu_to_le16(latency); |
| 6213 | ev.timeout = cpu_to_le16(timeout); |
| 6214 | |
| 6215 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6216 | } |
| 6217 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 6218 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 6219 | u8 data_len) |
| 6220 | { |
| 6221 | eir[eir_len++] = sizeof(type) + data_len; |
| 6222 | eir[eir_len++] = type; |
| 6223 | memcpy(&eir[eir_len], data, data_len); |
| 6224 | eir_len += data_len; |
| 6225 | |
| 6226 | return eir_len; |
| 6227 | } |
| 6228 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6229 | void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, |
| 6230 | u32 flags, u8 *name, u8 name_len) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6231 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6232 | char buf[512]; |
| 6233 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6234 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6235 | |
Alfonso Acosta | 48ec92f | 2014-10-07 08:44:10 +0000 | [diff] [blame] | 6236 | bacpy(&ev->addr.bdaddr, &conn->dst); |
| 6237 | ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6238 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6239 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6240 | |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6241 | /* We must ensure that the EIR Data fields are ordered and |
| 6242 | * unique. Keep it simple for now and avoid the problem by not |
| 6243 | * adding any BR/EDR data to the LE adv. |
| 6244 | */ |
| 6245 | if (conn->le_adv_data_len > 0) { |
| 6246 | memcpy(&ev->eir[eir_len], |
| 6247 | conn->le_adv_data, conn->le_adv_data_len); |
| 6248 | eir_len = conn->le_adv_data_len; |
| 6249 | } else { |
| 6250 | if (name_len > 0) |
| 6251 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
| 6252 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6253 | |
Alfonso Acosta | ddbea5c | 2014-10-07 08:44:12 +0000 | [diff] [blame] | 6254 | if (memcmp(conn->dev_class, "\0\0\0", 3) != 0) |
Alfonso Acosta | fd45ada | 2014-10-07 08:44:11 +0000 | [diff] [blame] | 6255 | eir_len = eir_append_data(ev->eir, eir_len, |
| 6256 | EIR_CLASS_OF_DEV, |
| 6257 | conn->dev_class, 3); |
| 6258 | } |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6259 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6260 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6261 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6262 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6263 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6264 | } |
| 6265 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6266 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 6267 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 6268 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6269 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 6270 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6271 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6272 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6273 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6274 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6275 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6276 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6277 | |
| 6278 | *sk = cmd->sk; |
| 6279 | sock_hold(*sk); |
| 6280 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6281 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6282 | } |
| 6283 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6284 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6285 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6286 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6287 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 6288 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6289 | |
| 6290 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6291 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6292 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6293 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6294 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6295 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6296 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6297 | |
| 6298 | mgmt_pending_remove(cmd); |
| 6299 | } |
| 6300 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6301 | bool mgmt_powering_down(struct hci_dev *hdev) |
| 6302 | { |
| 6303 | struct pending_cmd *cmd; |
| 6304 | struct mgmt_mode *cp; |
| 6305 | |
| 6306 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6307 | if (!cmd) |
| 6308 | return false; |
| 6309 | |
| 6310 | cp = cmd->param; |
| 6311 | if (!cp->val) |
| 6312 | return true; |
| 6313 | |
| 6314 | return false; |
| 6315 | } |
| 6316 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6317 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6318 | u8 link_type, u8 addr_type, u8 reason, |
| 6319 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6320 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6321 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6322 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6323 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6324 | /* The connection is still in hci_conn_hash so test for 1 |
| 6325 | * instead of 0 to know if this is the last one. |
| 6326 | */ |
| 6327 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 6328 | cancel_delayed_work(&hdev->power_off); |
| 6329 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6330 | } |
| 6331 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6332 | if (!mgmt_connected) |
| 6333 | return; |
| 6334 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6335 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6336 | return; |
| 6337 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6338 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6339 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6340 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6341 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6342 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6343 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6344 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6345 | |
| 6346 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6347 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6348 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6349 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6350 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6351 | } |
| 6352 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6353 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6354 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6355 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6356 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6357 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6358 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6359 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6360 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6361 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6362 | hdev); |
| 6363 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6364 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6365 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6366 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6367 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6368 | cp = cmd->param; |
| 6369 | |
| 6370 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6371 | return; |
| 6372 | |
| 6373 | if (cp->addr.type != bdaddr_type) |
| 6374 | return; |
| 6375 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6376 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6377 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 6378 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6379 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 6380 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6381 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6382 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6383 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6384 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6385 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6386 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6387 | { |
| 6388 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6389 | |
Johan Hedberg | 84c61d9 | 2014-08-01 11:13:30 +0300 | [diff] [blame] | 6390 | /* The connection is still in hci_conn_hash so test for 1 |
| 6391 | * instead of 0 to know if this is the last one. |
| 6392 | */ |
| 6393 | if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) { |
| 6394 | cancel_delayed_work(&hdev->power_off); |
| 6395 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6396 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6397 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6398 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6399 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6400 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6401 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6402 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6403 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6404 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6405 | 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] | 6406 | { |
| 6407 | struct mgmt_ev_pin_code_request ev; |
| 6408 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6409 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6410 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6411 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6412 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6413 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6414 | } |
| 6415 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6416 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6417 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6418 | { |
| 6419 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6420 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6421 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6422 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6423 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6424 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6425 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6426 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6427 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6428 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6429 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6430 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6431 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6432 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6433 | } |
| 6434 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6435 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6436 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6437 | { |
| 6438 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6439 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6440 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6441 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6442 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6443 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6444 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6445 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6446 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6447 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6448 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6449 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6450 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6451 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6452 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6453 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6454 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6455 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6456 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6457 | { |
| 6458 | struct mgmt_ev_user_confirm_request ev; |
| 6459 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6460 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6461 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6462 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6463 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6464 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6465 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6466 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6467 | 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] | 6468 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6469 | } |
| 6470 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6471 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6472 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6473 | { |
| 6474 | struct mgmt_ev_user_passkey_request ev; |
| 6475 | |
| 6476 | BT_DBG("%s", hdev->name); |
| 6477 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6478 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6479 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6480 | |
| 6481 | 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] | 6482 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6483 | } |
| 6484 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6485 | 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] | 6486 | u8 link_type, u8 addr_type, u8 status, |
| 6487 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6488 | { |
| 6489 | struct pending_cmd *cmd; |
| 6490 | struct mgmt_rp_user_confirm_reply rp; |
| 6491 | int err; |
| 6492 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6493 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6494 | if (!cmd) |
| 6495 | return -ENOENT; |
| 6496 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6497 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6498 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6499 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6500 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6501 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6502 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6503 | |
| 6504 | return err; |
| 6505 | } |
| 6506 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6507 | 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] | 6508 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6509 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6510 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6511 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6512 | } |
| 6513 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6514 | 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] | 6515 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6516 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6517 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6518 | status, |
| 6519 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6520 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6521 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6522 | 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] | 6523 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6524 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6525 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6526 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6527 | } |
| 6528 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6529 | 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] | 6530 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6531 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6532 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6533 | status, |
| 6534 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6535 | } |
| 6536 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6537 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6538 | u8 link_type, u8 addr_type, u32 passkey, |
| 6539 | u8 entered) |
| 6540 | { |
| 6541 | struct mgmt_ev_passkey_notify ev; |
| 6542 | |
| 6543 | BT_DBG("%s", hdev->name); |
| 6544 | |
| 6545 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6546 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6547 | ev.passkey = __cpu_to_le32(passkey); |
| 6548 | ev.entered = entered; |
| 6549 | |
| 6550 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6551 | } |
| 6552 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 6553 | void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6554 | { |
| 6555 | struct mgmt_ev_auth_failed ev; |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 6556 | struct pending_cmd *cmd; |
| 6557 | u8 status = mgmt_status(hci_status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6558 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 6559 | bacpy(&ev.addr.bdaddr, &conn->dst); |
| 6560 | ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
| 6561 | ev.status = status; |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6562 | |
Johan Hedberg | e1e930f | 2014-09-08 17:09:49 -0700 | [diff] [blame] | 6563 | cmd = find_pairing(conn); |
| 6564 | |
| 6565 | mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev), |
| 6566 | cmd ? cmd->sk : NULL); |
| 6567 | |
| 6568 | if (cmd) |
| 6569 | pairing_complete(cmd, status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6570 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6571 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6572 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6573 | { |
| 6574 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6575 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6576 | |
| 6577 | if (status) { |
| 6578 | u8 mgmt_err = mgmt_status(status); |
| 6579 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6580 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6581 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6582 | } |
| 6583 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6584 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6585 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6586 | &hdev->dev_flags); |
| 6587 | else |
| 6588 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6589 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6590 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6591 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6592 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6593 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6594 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6595 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6596 | |
| 6597 | if (match.sk) |
| 6598 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6599 | } |
| 6600 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6601 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6602 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6603 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6604 | struct hci_cp_write_eir cp; |
| 6605 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6606 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6607 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6608 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6609 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6610 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6611 | memset(&cp, 0, sizeof(cp)); |
| 6612 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6613 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6614 | } |
| 6615 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6616 | 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] | 6617 | { |
| 6618 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6619 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6620 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6621 | |
| 6622 | if (status) { |
| 6623 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6624 | |
| 6625 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6626 | &hdev->dev_flags)) { |
| 6627 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6628 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6629 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6630 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6631 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6632 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6633 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6634 | } |
| 6635 | |
| 6636 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6637 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6638 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6639 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6640 | if (!changed) |
| 6641 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6642 | &hdev->dev_flags); |
| 6643 | else |
| 6644 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6645 | } |
| 6646 | |
| 6647 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6648 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6649 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6650 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6651 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6652 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6653 | sock_put(match.sk); |
| 6654 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6655 | hci_req_init(&req, hdev); |
| 6656 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6657 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6658 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6659 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6660 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6661 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6662 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6663 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6664 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6665 | |
| 6666 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6667 | } |
| 6668 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6669 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6670 | { |
| 6671 | struct cmd_lookup match = { NULL, hdev }; |
| 6672 | bool changed = false; |
| 6673 | |
| 6674 | if (status) { |
| 6675 | u8 mgmt_err = mgmt_status(status); |
| 6676 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6677 | if (enable) { |
| 6678 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6679 | &hdev->dev_flags)) |
| 6680 | new_settings(hdev, NULL); |
| 6681 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6682 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6683 | |
| 6684 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6685 | cmd_status_rsp, &mgmt_err); |
| 6686 | return; |
| 6687 | } |
| 6688 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6689 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6690 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6691 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6692 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6693 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6694 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6695 | |
| 6696 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6697 | settings_rsp, &match); |
| 6698 | |
| 6699 | if (changed) |
| 6700 | new_settings(hdev, match.sk); |
| 6701 | |
| 6702 | if (match.sk) |
| 6703 | sock_put(match.sk); |
| 6704 | } |
| 6705 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6706 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6707 | { |
| 6708 | struct cmd_lookup *match = data; |
| 6709 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6710 | if (match->sk == NULL) { |
| 6711 | match->sk = cmd->sk; |
| 6712 | sock_hold(match->sk); |
| 6713 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6714 | } |
| 6715 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6716 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6717 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6718 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6719 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6720 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6721 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6722 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6723 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6724 | |
| 6725 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6726 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6727 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6728 | |
| 6729 | if (match.sk) |
| 6730 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6731 | } |
| 6732 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6733 | 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] | 6734 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6735 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6736 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6737 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6738 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6739 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6740 | |
| 6741 | memset(&ev, 0, sizeof(ev)); |
| 6742 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6743 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6744 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6745 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6746 | if (!cmd) { |
| 6747 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6748 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6749 | /* If this is a HCI command related to powering on the |
| 6750 | * HCI dev don't send any mgmt signals. |
| 6751 | */ |
| 6752 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6753 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6754 | } |
| 6755 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6756 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6757 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6758 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6759 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6760 | 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] | 6761 | u8 *rand192, u8 *hash256, u8 *rand256, |
| 6762 | u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6763 | { |
| 6764 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6765 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6766 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6767 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6768 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6769 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6770 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6771 | |
| 6772 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6773 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6774 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6775 | } else { |
Johan Hedberg | 710f11c | 2014-05-26 11:21:22 +0300 | [diff] [blame] | 6776 | if (bredr_sc_enabled(hdev) && hash256 && rand256) { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6777 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6778 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6779 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 6780 | memcpy(rp.rand192, rand192, sizeof(rp.rand192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6781 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6782 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 6783 | memcpy(rp.rand256, rand256, sizeof(rp.rand256)); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6784 | |
| 6785 | cmd_complete(cmd->sk, hdev->id, |
| 6786 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6787 | &rp, sizeof(rp)); |
| 6788 | } else { |
| 6789 | struct mgmt_rp_read_local_oob_data rp; |
| 6790 | |
| 6791 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
Johan Hedberg | 38da170 | 2014-11-17 20:52:20 +0200 | [diff] [blame] | 6792 | memcpy(rp.rand, rand192, sizeof(rp.rand)); |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6793 | |
| 6794 | cmd_complete(cmd->sk, hdev->id, |
| 6795 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6796 | &rp, sizeof(rp)); |
| 6797 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6798 | } |
| 6799 | |
| 6800 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6801 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6802 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6803 | 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] | 6804 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6805 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6806 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6807 | char buf[512]; |
| 6808 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6809 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6810 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6811 | /* Don't send events for a non-kernel initiated discovery. With |
| 6812 | * LE one exception is if we have pend_le_reports > 0 in which |
| 6813 | * case we're doing passive scanning and want these events. |
| 6814 | */ |
| 6815 | if (!hci_discovery_active(hdev)) { |
| 6816 | if (link_type == ACL_LINK) |
| 6817 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 6818 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6819 | return; |
| 6820 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6821 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6822 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6823 | * are for the potential CoD field. |
| 6824 | */ |
| 6825 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6826 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6827 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6828 | memset(buf, 0, sizeof(buf)); |
| 6829 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 6830 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6831 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6832 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6833 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6834 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6835 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6836 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6837 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6838 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6839 | 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] | 6840 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6841 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6842 | if (scan_rsp_len > 0) |
| 6843 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6844 | |
| 6845 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6846 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6847 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6848 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6849 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6850 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6851 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6852 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6853 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6854 | struct mgmt_ev_device_found *ev; |
| 6855 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6856 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6857 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6858 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6859 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6860 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6861 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6862 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6863 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6864 | ev->rssi = rssi; |
| 6865 | |
| 6866 | 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] | 6867 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6868 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6869 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6870 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6871 | 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] | 6872 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6873 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6874 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6875 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6876 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6877 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6878 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6879 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6880 | memset(&ev, 0, sizeof(ev)); |
| 6881 | ev.type = hdev->discovery.type; |
| 6882 | ev.discovering = discovering; |
| 6883 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6884 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6885 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6886 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6887 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6888 | { |
| 6889 | BT_DBG("%s status %u", hdev->name, status); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6890 | } |
| 6891 | |
| 6892 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6893 | { |
| 6894 | struct hci_request req; |
| 6895 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6896 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6897 | return; |
| 6898 | |
| 6899 | hci_req_init(&req, hdev); |
| 6900 | enable_advertising(&req); |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 6901 | hci_req_run(&req, adv_enable_complete); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6902 | } |