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 | 730f091 | 2014-06-28 12:36:10 +0200 | [diff] [blame] | 38 | #define MGMT_REVISION 7 |
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, |
| 47 | MGMT_OP_SET_PAIRABLE, |
| 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 | edd3896 | 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 | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 132 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 133 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 134 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 135 | struct pending_cmd { |
| 136 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 137 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 138 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 139 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 140 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 141 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 142 | }; |
| 143 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 144 | /* HCI to MGMT error code conversion table */ |
| 145 | static u8 mgmt_status_table[] = { |
| 146 | MGMT_STATUS_SUCCESS, |
| 147 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 148 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 149 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 150 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 151 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 152 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 153 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 154 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 155 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 156 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 157 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 158 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 159 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 160 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 161 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 162 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 163 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 164 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 165 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 166 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 167 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 168 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 169 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 170 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 171 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 172 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 173 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 174 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 175 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 176 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 177 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 178 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 179 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 180 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 181 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 182 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 183 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 184 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 185 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 186 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 187 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 188 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 189 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 190 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 191 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 192 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 193 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 194 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 195 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 196 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 197 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 198 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 199 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 200 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 201 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 202 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 203 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 204 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 205 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 206 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 207 | }; |
| 208 | |
| 209 | static u8 mgmt_status(u8 hci_status) |
| 210 | { |
| 211 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 212 | return mgmt_status_table[hci_status]; |
| 213 | |
| 214 | return MGMT_STATUS_FAILED; |
| 215 | } |
| 216 | |
Marcel Holtmann | 04c60f05 | 2014-07-04 19:06:22 +0200 | [diff] [blame] | 217 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 218 | struct sock *skip_sk) |
| 219 | { |
| 220 | struct sk_buff *skb; |
| 221 | struct mgmt_hdr *hdr; |
| 222 | |
| 223 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
| 224 | if (!skb) |
| 225 | return -ENOMEM; |
| 226 | |
| 227 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 228 | hdr->opcode = cpu_to_le16(event); |
| 229 | if (hdev) |
| 230 | hdr->index = cpu_to_le16(hdev->id); |
| 231 | else |
| 232 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 233 | hdr->len = cpu_to_le16(data_len); |
| 234 | |
| 235 | if (data) |
| 236 | memcpy(skb_put(skb, data_len), data, data_len); |
| 237 | |
| 238 | /* Time stamp */ |
| 239 | __net_timestamp(skb); |
| 240 | |
| 241 | hci_send_to_control(skb, skip_sk); |
| 242 | kfree_skb(skb); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 247 | 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] | 248 | { |
| 249 | struct sk_buff *skb; |
| 250 | struct mgmt_hdr *hdr; |
| 251 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 252 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 253 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 254 | 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] | 255 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 256 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 257 | if (!skb) |
| 258 | return -ENOMEM; |
| 259 | |
| 260 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 261 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 262 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 263 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 264 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 265 | |
| 266 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 267 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 268 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 269 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 270 | err = sock_queue_rcv_skb(sk, skb); |
| 271 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 272 | kfree_skb(skb); |
| 273 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 274 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 277 | 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] | 278 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 279 | { |
| 280 | struct sk_buff *skb; |
| 281 | struct mgmt_hdr *hdr; |
| 282 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 283 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 284 | |
| 285 | BT_DBG("sock %p", sk); |
| 286 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 287 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 288 | if (!skb) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 292 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 293 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 294 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 295 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 296 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 297 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 298 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 299 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 300 | |
| 301 | if (rp) |
| 302 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 303 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 304 | err = sock_queue_rcv_skb(sk, skb); |
| 305 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 306 | kfree_skb(skb); |
| 307 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 308 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 311 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 312 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 313 | { |
| 314 | struct mgmt_rp_read_version rp; |
| 315 | |
| 316 | BT_DBG("sock %p", sk); |
| 317 | |
| 318 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 319 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 320 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 321 | 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] | 322 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 325 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 326 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 327 | { |
| 328 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 329 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 330 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 331 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 332 | size_t rp_size; |
| 333 | int i, err; |
| 334 | |
| 335 | BT_DBG("sock %p", sk); |
| 336 | |
| 337 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 338 | |
| 339 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 340 | if (!rp) |
| 341 | return -ENOMEM; |
| 342 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 343 | rp->num_commands = cpu_to_le16(num_commands); |
| 344 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 345 | |
| 346 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 347 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 348 | |
| 349 | for (i = 0; i < num_events; i++, opcode++) |
| 350 | put_unaligned_le16(mgmt_events[i], opcode); |
| 351 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 352 | 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] | 353 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 354 | kfree(rp); |
| 355 | |
| 356 | return err; |
| 357 | } |
| 358 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 359 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 360 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 361 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 362 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 363 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 364 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 365 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 366 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 367 | |
| 368 | BT_DBG("sock %p", sk); |
| 369 | |
| 370 | read_lock(&hci_dev_list_lock); |
| 371 | |
| 372 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 373 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 374 | if (d->dev_type == HCI_BREDR && |
| 375 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 376 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 379 | rp_len = sizeof(*rp) + (2 * count); |
| 380 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 381 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 382 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 383 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 384 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 385 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 386 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 387 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 388 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 389 | test_bit(HCI_CONFIG, &d->dev_flags) || |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 390 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 391 | continue; |
| 392 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 393 | /* Devices marked as raw-only are neither configured |
| 394 | * nor unconfigured controllers. |
| 395 | */ |
| 396 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 397 | continue; |
| 398 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 399 | if (d->dev_type == HCI_BREDR && |
| 400 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 401 | rp->index[count++] = cpu_to_le16(d->id); |
| 402 | BT_DBG("Added hci%u", d->id); |
| 403 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 406 | rp->num_controllers = cpu_to_le16(count); |
| 407 | rp_len = sizeof(*rp) + (2 * count); |
| 408 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 409 | read_unlock(&hci_dev_list_lock); |
| 410 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 411 | 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] | 412 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 413 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 414 | kfree(rp); |
| 415 | |
| 416 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 419 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 420 | void *data, u16 data_len) |
| 421 | { |
| 422 | struct mgmt_rp_read_unconf_index_list *rp; |
| 423 | struct hci_dev *d; |
| 424 | size_t rp_len; |
| 425 | u16 count; |
| 426 | int err; |
| 427 | |
| 428 | BT_DBG("sock %p", sk); |
| 429 | |
| 430 | read_lock(&hci_dev_list_lock); |
| 431 | |
| 432 | count = 0; |
| 433 | list_for_each_entry(d, &hci_dev_list, list) { |
| 434 | if (d->dev_type == HCI_BREDR && |
| 435 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
| 436 | count++; |
| 437 | } |
| 438 | |
| 439 | rp_len = sizeof(*rp) + (2 * count); |
| 440 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 441 | if (!rp) { |
| 442 | read_unlock(&hci_dev_list_lock); |
| 443 | return -ENOMEM; |
| 444 | } |
| 445 | |
| 446 | count = 0; |
| 447 | list_for_each_entry(d, &hci_dev_list, list) { |
| 448 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 449 | test_bit(HCI_CONFIG, &d->dev_flags) || |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 450 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 451 | continue; |
| 452 | |
| 453 | /* Devices marked as raw-only are neither configured |
| 454 | * nor unconfigured controllers. |
| 455 | */ |
| 456 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 457 | continue; |
| 458 | |
| 459 | if (d->dev_type == HCI_BREDR && |
| 460 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
| 461 | rp->index[count++] = cpu_to_le16(d->id); |
| 462 | BT_DBG("Added hci%u", d->id); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | rp->num_controllers = cpu_to_le16(count); |
| 467 | rp_len = sizeof(*rp) + (2 * count); |
| 468 | |
| 469 | read_unlock(&hci_dev_list_lock); |
| 470 | |
| 471 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 472 | 0, rp, rp_len); |
| 473 | |
| 474 | kfree(rp); |
| 475 | |
| 476 | return err; |
| 477 | } |
| 478 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 479 | static bool is_configured(struct hci_dev *hdev) |
| 480 | { |
| 481 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 482 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
| 483 | return false; |
| 484 | |
| 485 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 486 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 487 | return false; |
| 488 | |
| 489 | return true; |
| 490 | } |
| 491 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 492 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 493 | { |
| 494 | u32 options = 0; |
| 495 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 496 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 497 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 498 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 499 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 500 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 501 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 502 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 503 | |
| 504 | return cpu_to_le32(options); |
| 505 | } |
| 506 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 507 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 508 | { |
| 509 | __le32 options = get_missing_options(hdev); |
| 510 | |
| 511 | return mgmt_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 512 | sizeof(options), skip); |
| 513 | } |
| 514 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 515 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 516 | { |
| 517 | __le32 options = get_missing_options(hdev); |
| 518 | |
| 519 | return cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 520 | sizeof(options)); |
| 521 | } |
| 522 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 523 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 524 | void *data, u16 data_len) |
| 525 | { |
| 526 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 527 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 528 | |
| 529 | BT_DBG("sock %p %s", sk, hdev->name); |
| 530 | |
| 531 | hci_dev_lock(hdev); |
| 532 | |
| 533 | memset(&rp, 0, sizeof(rp)); |
| 534 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 535 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 536 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 537 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 538 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 539 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 540 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 541 | |
| 542 | rp.supported_options = cpu_to_le32(options); |
| 543 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 544 | |
| 545 | hci_dev_unlock(hdev); |
| 546 | |
| 547 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp, |
| 548 | sizeof(rp)); |
| 549 | } |
| 550 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 551 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 552 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 553 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 554 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 555 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 556 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 557 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 3742abf | 2014-07-08 16:07:34 +0300 | [diff] [blame] | 558 | settings |= MGMT_SETTING_CONNECTABLE; |
| 559 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 560 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 561 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 562 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 563 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 564 | settings |= MGMT_SETTING_BREDR; |
| 565 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 566 | |
| 567 | if (lmp_ssp_capable(hdev)) { |
| 568 | settings |= MGMT_SETTING_SSP; |
| 569 | settings |= MGMT_SETTING_HS; |
| 570 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 571 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 572 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 573 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 574 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 575 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 576 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 577 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 578 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 579 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 580 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 581 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 582 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 583 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 584 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 585 | settings |= MGMT_SETTING_CONFIGURATION; |
| 586 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 587 | return settings; |
| 588 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 589 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 590 | static u32 get_current_settings(struct hci_dev *hdev) |
| 591 | { |
| 592 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 593 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 594 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 595 | settings |= MGMT_SETTING_POWERED; |
| 596 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 597 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 598 | settings |= MGMT_SETTING_CONNECTABLE; |
| 599 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 600 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 601 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 602 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 603 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 604 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 605 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 606 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 607 | settings |= MGMT_SETTING_PAIRABLE; |
| 608 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 609 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 610 | settings |= MGMT_SETTING_BREDR; |
| 611 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 612 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 613 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 614 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 615 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 616 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 617 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 618 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 619 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 620 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 621 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 622 | settings |= MGMT_SETTING_HS; |
| 623 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 624 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 625 | settings |= MGMT_SETTING_ADVERTISING; |
| 626 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 627 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 628 | settings |= MGMT_SETTING_SECURE_CONN; |
| 629 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 630 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 631 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 632 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 633 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 634 | settings |= MGMT_SETTING_PRIVACY; |
| 635 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 636 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 639 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 640 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 641 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 642 | { |
| 643 | u8 *ptr = data, *uuids_start = NULL; |
| 644 | struct bt_uuid *uuid; |
| 645 | |
| 646 | if (len < 4) |
| 647 | return ptr; |
| 648 | |
| 649 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 650 | u16 uuid16; |
| 651 | |
| 652 | if (uuid->size != 16) |
| 653 | continue; |
| 654 | |
| 655 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 656 | if (uuid16 < 0x1100) |
| 657 | continue; |
| 658 | |
| 659 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 660 | continue; |
| 661 | |
| 662 | if (!uuids_start) { |
| 663 | uuids_start = ptr; |
| 664 | uuids_start[0] = 1; |
| 665 | uuids_start[1] = EIR_UUID16_ALL; |
| 666 | ptr += 2; |
| 667 | } |
| 668 | |
| 669 | /* Stop if not enough space to put next UUID */ |
| 670 | if ((ptr - data) + sizeof(u16) > len) { |
| 671 | uuids_start[1] = EIR_UUID16_SOME; |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | *ptr++ = (uuid16 & 0x00ff); |
| 676 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 677 | uuids_start[0] += sizeof(uuid16); |
| 678 | } |
| 679 | |
| 680 | return ptr; |
| 681 | } |
| 682 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 683 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 684 | { |
| 685 | u8 *ptr = data, *uuids_start = NULL; |
| 686 | struct bt_uuid *uuid; |
| 687 | |
| 688 | if (len < 6) |
| 689 | return ptr; |
| 690 | |
| 691 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 692 | if (uuid->size != 32) |
| 693 | continue; |
| 694 | |
| 695 | if (!uuids_start) { |
| 696 | uuids_start = ptr; |
| 697 | uuids_start[0] = 1; |
| 698 | uuids_start[1] = EIR_UUID32_ALL; |
| 699 | ptr += 2; |
| 700 | } |
| 701 | |
| 702 | /* Stop if not enough space to put next UUID */ |
| 703 | if ((ptr - data) + sizeof(u32) > len) { |
| 704 | uuids_start[1] = EIR_UUID32_SOME; |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 709 | ptr += sizeof(u32); |
| 710 | uuids_start[0] += sizeof(u32); |
| 711 | } |
| 712 | |
| 713 | return ptr; |
| 714 | } |
| 715 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 716 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 717 | { |
| 718 | u8 *ptr = data, *uuids_start = NULL; |
| 719 | struct bt_uuid *uuid; |
| 720 | |
| 721 | if (len < 18) |
| 722 | return ptr; |
| 723 | |
| 724 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 725 | if (uuid->size != 128) |
| 726 | continue; |
| 727 | |
| 728 | if (!uuids_start) { |
| 729 | uuids_start = ptr; |
| 730 | uuids_start[0] = 1; |
| 731 | uuids_start[1] = EIR_UUID128_ALL; |
| 732 | ptr += 2; |
| 733 | } |
| 734 | |
| 735 | /* Stop if not enough space to put next UUID */ |
| 736 | if ((ptr - data) + 16 > len) { |
| 737 | uuids_start[1] = EIR_UUID128_SOME; |
| 738 | break; |
| 739 | } |
| 740 | |
| 741 | memcpy(ptr, uuid->uuid, 16); |
| 742 | ptr += 16; |
| 743 | uuids_start[0] += 16; |
| 744 | } |
| 745 | |
| 746 | return ptr; |
| 747 | } |
| 748 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 749 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 750 | { |
| 751 | struct pending_cmd *cmd; |
| 752 | |
| 753 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 754 | if (cmd->opcode == opcode) |
| 755 | return cmd; |
| 756 | } |
| 757 | |
| 758 | return NULL; |
| 759 | } |
| 760 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 761 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 762 | struct hci_dev *hdev, |
| 763 | const void *data) |
| 764 | { |
| 765 | struct pending_cmd *cmd; |
| 766 | |
| 767 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 768 | if (cmd->user_data != data) |
| 769 | continue; |
| 770 | if (cmd->opcode == opcode) |
| 771 | return cmd; |
| 772 | } |
| 773 | |
| 774 | return NULL; |
| 775 | } |
| 776 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 777 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 778 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 779 | u8 ad_len = 0; |
| 780 | size_t name_len; |
| 781 | |
| 782 | name_len = strlen(hdev->dev_name); |
| 783 | if (name_len > 0) { |
| 784 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 785 | |
| 786 | if (name_len > max_len) { |
| 787 | name_len = max_len; |
| 788 | ptr[1] = EIR_NAME_SHORT; |
| 789 | } else |
| 790 | ptr[1] = EIR_NAME_COMPLETE; |
| 791 | |
| 792 | ptr[0] = name_len + 1; |
| 793 | |
| 794 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 795 | |
| 796 | ad_len += (name_len + 2); |
| 797 | ptr += (name_len + 2); |
| 798 | } |
| 799 | |
| 800 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | static void update_scan_rsp_data(struct hci_request *req) |
| 804 | { |
| 805 | struct hci_dev *hdev = req->hdev; |
| 806 | struct hci_cp_le_set_scan_rsp_data cp; |
| 807 | u8 len; |
| 808 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 809 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 810 | return; |
| 811 | |
| 812 | memset(&cp, 0, sizeof(cp)); |
| 813 | |
| 814 | len = create_scan_rsp_data(hdev, cp.data); |
| 815 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 816 | if (hdev->scan_rsp_data_len == len && |
| 817 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 818 | return; |
| 819 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 820 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 821 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 822 | |
| 823 | cp.length = len; |
| 824 | |
| 825 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 826 | } |
| 827 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 828 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 829 | { |
| 830 | struct pending_cmd *cmd; |
| 831 | |
| 832 | /* If there's a pending mgmt command the flags will not yet have |
| 833 | * their final values, so check for this first. |
| 834 | */ |
| 835 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 836 | if (cmd) { |
| 837 | struct mgmt_mode *cp = cmd->param; |
| 838 | if (cp->val == 0x01) |
| 839 | return LE_AD_GENERAL; |
| 840 | else if (cp->val == 0x02) |
| 841 | return LE_AD_LIMITED; |
| 842 | } else { |
| 843 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 844 | return LE_AD_LIMITED; |
| 845 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 846 | return LE_AD_GENERAL; |
| 847 | } |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 852 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 853 | { |
| 854 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 855 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 856 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 857 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 858 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 859 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 860 | |
| 861 | if (flags) { |
| 862 | BT_DBG("adv flags 0x%02x", flags); |
| 863 | |
| 864 | ptr[0] = 2; |
| 865 | ptr[1] = EIR_FLAGS; |
| 866 | ptr[2] = flags; |
| 867 | |
| 868 | ad_len += 3; |
| 869 | ptr += 3; |
| 870 | } |
| 871 | |
| 872 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 873 | ptr[0] = 2; |
| 874 | ptr[1] = EIR_TX_POWER; |
| 875 | ptr[2] = (u8) hdev->adv_tx_power; |
| 876 | |
| 877 | ad_len += 3; |
| 878 | ptr += 3; |
| 879 | } |
| 880 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 881 | return ad_len; |
| 882 | } |
| 883 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 884 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 885 | { |
| 886 | struct hci_dev *hdev = req->hdev; |
| 887 | struct hci_cp_le_set_adv_data cp; |
| 888 | u8 len; |
| 889 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 890 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 891 | return; |
| 892 | |
| 893 | memset(&cp, 0, sizeof(cp)); |
| 894 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 895 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 896 | |
| 897 | if (hdev->adv_data_len == len && |
| 898 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 899 | return; |
| 900 | |
| 901 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 902 | hdev->adv_data_len = len; |
| 903 | |
| 904 | cp.length = len; |
| 905 | |
| 906 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 907 | } |
| 908 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 909 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 910 | { |
| 911 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 912 | size_t name_len; |
| 913 | |
| 914 | name_len = strlen(hdev->dev_name); |
| 915 | |
| 916 | if (name_len > 0) { |
| 917 | /* EIR Data type */ |
| 918 | if (name_len > 48) { |
| 919 | name_len = 48; |
| 920 | ptr[1] = EIR_NAME_SHORT; |
| 921 | } else |
| 922 | ptr[1] = EIR_NAME_COMPLETE; |
| 923 | |
| 924 | /* EIR Data length */ |
| 925 | ptr[0] = name_len + 1; |
| 926 | |
| 927 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 928 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 929 | ptr += (name_len + 2); |
| 930 | } |
| 931 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 932 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 933 | ptr[0] = 2; |
| 934 | ptr[1] = EIR_TX_POWER; |
| 935 | ptr[2] = (u8) hdev->inq_tx_power; |
| 936 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 937 | ptr += 3; |
| 938 | } |
| 939 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 940 | if (hdev->devid_source > 0) { |
| 941 | ptr[0] = 9; |
| 942 | ptr[1] = EIR_DEVICE_ID; |
| 943 | |
| 944 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 945 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 946 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 947 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 948 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 949 | ptr += 10; |
| 950 | } |
| 951 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 952 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 953 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 954 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 955 | } |
| 956 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 957 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 958 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 959 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 960 | struct hci_cp_write_eir cp; |
| 961 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 962 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 963 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 964 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 965 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 966 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 967 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 968 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 969 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 970 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 971 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 972 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 973 | |
| 974 | memset(&cp, 0, sizeof(cp)); |
| 975 | |
| 976 | create_eir(hdev, cp.data); |
| 977 | |
| 978 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 979 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 980 | |
| 981 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 982 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 983 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 984 | } |
| 985 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 986 | static u8 get_service_classes(struct hci_dev *hdev) |
| 987 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 988 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 989 | u8 val = 0; |
| 990 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 991 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 992 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 993 | |
| 994 | return val; |
| 995 | } |
| 996 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 997 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 998 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 999 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1000 | u8 cod[3]; |
| 1001 | |
| 1002 | BT_DBG("%s", hdev->name); |
| 1003 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 1004 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1005 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 1006 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 1007 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1008 | return; |
| 1009 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1010 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1011 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1012 | |
| 1013 | cod[0] = hdev->minor_class; |
| 1014 | cod[1] = hdev->major_class; |
| 1015 | cod[2] = get_service_classes(hdev); |
| 1016 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 1017 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 1018 | cod[1] |= 0x20; |
| 1019 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1020 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1021 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1022 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1023 | 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] | 1024 | } |
| 1025 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1026 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1027 | { |
| 1028 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1029 | |
| 1030 | /* If there's a pending mgmt command the flag will not yet have |
| 1031 | * it's final value, so check for this first. |
| 1032 | */ |
| 1033 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1034 | if (cmd) { |
| 1035 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1036 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1039 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1042 | static void disable_advertising(struct hci_request *req) |
| 1043 | { |
| 1044 | u8 enable = 0x00; |
| 1045 | |
| 1046 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1047 | } |
| 1048 | |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1049 | static void enable_advertising(struct hci_request *req) |
| 1050 | { |
| 1051 | struct hci_dev *hdev = req->hdev; |
| 1052 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1053 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1054 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1055 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1056 | if (hci_conn_num(hdev, LE_LINK) > 0) |
| 1057 | return; |
| 1058 | |
| 1059 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
| 1060 | disable_advertising(req); |
| 1061 | |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1062 | /* Clear the HCI_LE_ADV bit temporarily so that the |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1063 | * hci_update_random_address knows that it's safe to go ahead |
| 1064 | * and write a new random address. The flag will be set back on |
| 1065 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 1066 | */ |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1067 | clear_bit(HCI_LE_ADV, &hdev->dev_flags); |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1068 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1069 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1070 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1071 | /* Set require_privacy to true only when non-connectable |
| 1072 | * advertising is used. In that case it is fine to use a |
| 1073 | * non-resolvable private address. |
| 1074 | */ |
| 1075 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1076 | return; |
| 1077 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 1078 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1079 | cp.min_interval = cpu_to_le16(0x0800); |
| 1080 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1081 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1082 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1083 | cp.channel_map = hdev->le_adv_channel_map; |
| 1084 | |
| 1085 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1086 | |
| 1087 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1088 | } |
| 1089 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1090 | static void service_cache_off(struct work_struct *work) |
| 1091 | { |
| 1092 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1093 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1094 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1095 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1096 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1097 | return; |
| 1098 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1099 | hci_req_init(&req, hdev); |
| 1100 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1101 | hci_dev_lock(hdev); |
| 1102 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1103 | update_eir(&req); |
| 1104 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1105 | |
| 1106 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1107 | |
| 1108 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1109 | } |
| 1110 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1111 | static void rpa_expired(struct work_struct *work) |
| 1112 | { |
| 1113 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1114 | rpa_expired.work); |
| 1115 | struct hci_request req; |
| 1116 | |
| 1117 | BT_DBG(""); |
| 1118 | |
| 1119 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 1120 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1121 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1122 | return; |
| 1123 | |
| 1124 | /* The generation of a new RPA and programming it into the |
| 1125 | * controller happens in the enable_advertising() function. |
| 1126 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1127 | hci_req_init(&req, hdev); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1128 | enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1129 | hci_req_run(&req, NULL); |
| 1130 | } |
| 1131 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1132 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1133 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1134 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1135 | return; |
| 1136 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1137 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1138 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1139 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1140 | /* Non-mgmt controlled devices get this bit set |
| 1141 | * implicitly so that pairing works for them, however |
| 1142 | * for mgmt we require user-space to explicitly enable |
| 1143 | * it |
| 1144 | */ |
| 1145 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1148 | 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] | 1149 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1150 | { |
| 1151 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1152 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1153 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1154 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1155 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1156 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1157 | memset(&rp, 0, sizeof(rp)); |
| 1158 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1159 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1160 | |
| 1161 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1162 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1163 | |
| 1164 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1165 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1166 | |
| 1167 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1168 | |
| 1169 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1170 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1171 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1172 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1173 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1174 | 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] | 1175 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1179 | { |
| 1180 | sock_put(cmd->sk); |
| 1181 | kfree(cmd->param); |
| 1182 | kfree(cmd); |
| 1183 | } |
| 1184 | |
| 1185 | 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] | 1186 | struct hci_dev *hdev, void *data, |
| 1187 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1188 | { |
| 1189 | struct pending_cmd *cmd; |
| 1190 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1191 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1192 | if (!cmd) |
| 1193 | return NULL; |
| 1194 | |
| 1195 | cmd->opcode = opcode; |
| 1196 | cmd->index = hdev->id; |
| 1197 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1198 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1199 | if (!cmd->param) { |
| 1200 | kfree(cmd); |
| 1201 | return NULL; |
| 1202 | } |
| 1203 | |
| 1204 | if (data) |
| 1205 | memcpy(cmd->param, data, len); |
| 1206 | |
| 1207 | cmd->sk = sk; |
| 1208 | sock_hold(sk); |
| 1209 | |
| 1210 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1211 | |
| 1212 | return cmd; |
| 1213 | } |
| 1214 | |
| 1215 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1216 | void (*cb)(struct pending_cmd *cmd, |
| 1217 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1218 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1219 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1220 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1221 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1222 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1223 | if (opcode > 0 && cmd->opcode != opcode) |
| 1224 | continue; |
| 1225 | |
| 1226 | cb(cmd, data); |
| 1227 | } |
| 1228 | } |
| 1229 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1230 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1231 | { |
| 1232 | list_del(&cmd->list); |
| 1233 | mgmt_pending_free(cmd); |
| 1234 | } |
| 1235 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1236 | 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] | 1237 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1238 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1239 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1240 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1241 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1244 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1245 | { |
| 1246 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1247 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1248 | if (hci_conn_count(hdev) == 0) { |
| 1249 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1250 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1251 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1252 | } |
| 1253 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1254 | static bool hci_stop_discovery(struct hci_request *req) |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1255 | { |
| 1256 | struct hci_dev *hdev = req->hdev; |
| 1257 | struct hci_cp_remote_name_req_cancel cp; |
| 1258 | struct inquiry_entry *e; |
| 1259 | |
| 1260 | switch (hdev->discovery.state) { |
| 1261 | case DISCOVERY_FINDING: |
| 1262 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1263 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1264 | } else { |
| 1265 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1266 | hci_req_add_le_scan_disable(req); |
| 1267 | } |
| 1268 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1269 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1270 | |
| 1271 | case DISCOVERY_RESOLVING: |
| 1272 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1273 | NAME_PENDING); |
| 1274 | if (!e) |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1275 | break; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1276 | |
| 1277 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1278 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1279 | &cp); |
| 1280 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1281 | return true; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1282 | |
| 1283 | default: |
| 1284 | /* Passive scanning */ |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1285 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1286 | hci_req_add_le_scan_disable(req); |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1287 | return true; |
| 1288 | } |
| 1289 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1290 | break; |
| 1291 | } |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1292 | |
| 1293 | return false; |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1294 | } |
| 1295 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1296 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1297 | { |
| 1298 | struct hci_request req; |
| 1299 | struct hci_conn *conn; |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1300 | bool discov_stopped; |
| 1301 | int err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1302 | |
| 1303 | hci_req_init(&req, hdev); |
| 1304 | |
| 1305 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1306 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1307 | u8 scan = 0x00; |
| 1308 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1309 | } |
| 1310 | |
Johan Hedberg | 73e082f | 2014-07-08 15:07:51 +0300 | [diff] [blame] | 1311 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1312 | disable_advertising(&req); |
| 1313 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1314 | discov_stopped = hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1315 | |
| 1316 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1317 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1318 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1319 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1320 | switch (conn->state) { |
| 1321 | case BT_CONNECTED: |
| 1322 | case BT_CONFIG: |
| 1323 | dc.handle = cpu_to_le16(conn->handle); |
| 1324 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1325 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1326 | break; |
| 1327 | case BT_CONNECT: |
| 1328 | if (conn->type == LE_LINK) |
| 1329 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1330 | 0, NULL); |
| 1331 | else if (conn->type == ACL_LINK) |
| 1332 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1333 | 6, &conn->dst); |
| 1334 | break; |
| 1335 | case BT_CONNECT2: |
| 1336 | bacpy(&rej.bdaddr, &conn->dst); |
| 1337 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1338 | if (conn->type == ACL_LINK) |
| 1339 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1340 | sizeof(rej), &rej); |
| 1341 | else if (conn->type == SCO_LINK) |
| 1342 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1343 | sizeof(rej), &rej); |
| 1344 | break; |
| 1345 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1346 | } |
| 1347 | |
Johan Hedberg | 23a4809 | 2014-07-08 16:05:06 +0300 | [diff] [blame] | 1348 | err = hci_req_run(&req, clean_up_hci_complete); |
| 1349 | if (!err && discov_stopped) |
| 1350 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
| 1351 | |
| 1352 | return err; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1355 | 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] | 1356 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1357 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1358 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1359 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1360 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1361 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1362 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1363 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1364 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1365 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1366 | MGMT_STATUS_INVALID_PARAMS); |
| 1367 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1368 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1369 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1370 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1371 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1372 | MGMT_STATUS_BUSY); |
| 1373 | goto failed; |
| 1374 | } |
| 1375 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1376 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1377 | cancel_delayed_work(&hdev->power_off); |
| 1378 | |
| 1379 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1380 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1381 | data, len); |
| 1382 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1383 | goto failed; |
| 1384 | } |
| 1385 | } |
| 1386 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1387 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1388 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1389 | goto failed; |
| 1390 | } |
| 1391 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1392 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1393 | if (!cmd) { |
| 1394 | err = -ENOMEM; |
| 1395 | goto failed; |
| 1396 | } |
| 1397 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1398 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1399 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1400 | err = 0; |
| 1401 | } else { |
| 1402 | /* Disconnect connections, stop scans, etc */ |
| 1403 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1404 | if (!err) |
| 1405 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1406 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1407 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1408 | /* ENODATA means there were no HCI commands queued */ |
| 1409 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1410 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1411 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1412 | err = 0; |
| 1413 | } |
| 1414 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1415 | |
| 1416 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1417 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1418 | return err; |
| 1419 | } |
| 1420 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1421 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1422 | { |
| 1423 | __le32 ev; |
| 1424 | |
| 1425 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1426 | |
| 1427 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1428 | } |
| 1429 | |
Johan Hedberg | 91a668b | 2014-07-09 13:28:26 +0300 | [diff] [blame] | 1430 | int mgmt_new_settings(struct hci_dev *hdev) |
| 1431 | { |
| 1432 | return new_settings(hdev, NULL); |
| 1433 | } |
| 1434 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1435 | struct cmd_lookup { |
| 1436 | struct sock *sk; |
| 1437 | struct hci_dev *hdev; |
| 1438 | u8 mgmt_status; |
| 1439 | }; |
| 1440 | |
| 1441 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1442 | { |
| 1443 | struct cmd_lookup *match = data; |
| 1444 | |
| 1445 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1446 | |
| 1447 | list_del(&cmd->list); |
| 1448 | |
| 1449 | if (match->sk == NULL) { |
| 1450 | match->sk = cmd->sk; |
| 1451 | sock_hold(match->sk); |
| 1452 | } |
| 1453 | |
| 1454 | mgmt_pending_free(cmd); |
| 1455 | } |
| 1456 | |
| 1457 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1458 | { |
| 1459 | u8 *status = data; |
| 1460 | |
| 1461 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1462 | mgmt_pending_remove(cmd); |
| 1463 | } |
| 1464 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1465 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1466 | { |
| 1467 | if (!lmp_bredr_capable(hdev)) |
| 1468 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1469 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1470 | return MGMT_STATUS_REJECTED; |
| 1471 | else |
| 1472 | return MGMT_STATUS_SUCCESS; |
| 1473 | } |
| 1474 | |
| 1475 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1476 | { |
| 1477 | if (!lmp_le_capable(hdev)) |
| 1478 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1479 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1480 | return MGMT_STATUS_REJECTED; |
| 1481 | else |
| 1482 | return MGMT_STATUS_SUCCESS; |
| 1483 | } |
| 1484 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1485 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1486 | { |
| 1487 | struct pending_cmd *cmd; |
| 1488 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1489 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1490 | bool changed; |
| 1491 | |
| 1492 | BT_DBG("status 0x%02x", status); |
| 1493 | |
| 1494 | hci_dev_lock(hdev); |
| 1495 | |
| 1496 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1497 | if (!cmd) |
| 1498 | goto unlock; |
| 1499 | |
| 1500 | if (status) { |
| 1501 | u8 mgmt_err = mgmt_status(status); |
| 1502 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1503 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1504 | goto remove_cmd; |
| 1505 | } |
| 1506 | |
| 1507 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1508 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1509 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1510 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1511 | |
| 1512 | if (hdev->discov_timeout > 0) { |
| 1513 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1514 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1515 | to); |
| 1516 | } |
| 1517 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1518 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1519 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1520 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1521 | |
| 1522 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1523 | |
| 1524 | if (changed) |
| 1525 | new_settings(hdev, cmd->sk); |
| 1526 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1527 | /* When the discoverable mode gets changed, make sure |
| 1528 | * that class of device has the limited discoverable |
| 1529 | * bit correctly set. |
| 1530 | */ |
| 1531 | hci_req_init(&req, hdev); |
| 1532 | update_class(&req); |
| 1533 | hci_req_run(&req, NULL); |
| 1534 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1535 | remove_cmd: |
| 1536 | mgmt_pending_remove(cmd); |
| 1537 | |
| 1538 | unlock: |
| 1539 | hci_dev_unlock(hdev); |
| 1540 | } |
| 1541 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1542 | 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] | 1543 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1544 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1545 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1546 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1547 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1548 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1549 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1550 | int err; |
| 1551 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1552 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1553 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1554 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1555 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1556 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1557 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1558 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1559 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1560 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1561 | MGMT_STATUS_INVALID_PARAMS); |
| 1562 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1563 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1564 | |
| 1565 | /* Disabling discoverable requires that no timeout is set, |
| 1566 | * and enabling limited discoverable requires a timeout. |
| 1567 | */ |
| 1568 | if ((cp->val == 0x00 && timeout > 0) || |
| 1569 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1570 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1571 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1572 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1573 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1574 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1575 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1576 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1577 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1578 | goto failed; |
| 1579 | } |
| 1580 | |
| 1581 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1582 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1583 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1584 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1585 | goto failed; |
| 1586 | } |
| 1587 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1588 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1589 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1590 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1591 | goto failed; |
| 1592 | } |
| 1593 | |
| 1594 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1595 | bool changed = false; |
| 1596 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1597 | /* Setting limited discoverable when powered off is |
| 1598 | * not a valid operation since it requires a timeout |
| 1599 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1600 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1601 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1602 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1603 | changed = true; |
| 1604 | } |
| 1605 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1606 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1607 | if (err < 0) |
| 1608 | goto failed; |
| 1609 | |
| 1610 | if (changed) |
| 1611 | err = new_settings(hdev, sk); |
| 1612 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1613 | goto failed; |
| 1614 | } |
| 1615 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1616 | /* If the current mode is the same, then just update the timeout |
| 1617 | * value with the new value. And if only the timeout gets updated, |
| 1618 | * then no need for any HCI transactions. |
| 1619 | */ |
| 1620 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1621 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1622 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1623 | cancel_delayed_work(&hdev->discov_off); |
| 1624 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1625 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1626 | if (cp->val && hdev->discov_timeout > 0) { |
| 1627 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1628 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1629 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1630 | } |
| 1631 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1632 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1633 | goto failed; |
| 1634 | } |
| 1635 | |
| 1636 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1637 | if (!cmd) { |
| 1638 | err = -ENOMEM; |
| 1639 | goto failed; |
| 1640 | } |
| 1641 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1642 | /* Cancel any potential discoverable timeout that might be |
| 1643 | * still active and store new timeout value. The arming of |
| 1644 | * the timeout happens in the complete handler. |
| 1645 | */ |
| 1646 | cancel_delayed_work(&hdev->discov_off); |
| 1647 | hdev->discov_timeout = timeout; |
| 1648 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1649 | /* Limited discoverable mode */ |
| 1650 | if (cp->val == 0x02) |
| 1651 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1652 | else |
| 1653 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1654 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1655 | hci_req_init(&req, hdev); |
| 1656 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1657 | /* The procedure for LE-only controllers is much simpler - just |
| 1658 | * update the advertising data. |
| 1659 | */ |
| 1660 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1661 | goto update_ad; |
| 1662 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1663 | scan = SCAN_PAGE; |
| 1664 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1665 | if (cp->val) { |
| 1666 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1667 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1668 | if (cp->val == 0x02) { |
| 1669 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1670 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1671 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1672 | hci_cp.iac_lap[1] = 0x8b; |
| 1673 | hci_cp.iac_lap[2] = 0x9e; |
| 1674 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1675 | hci_cp.iac_lap[4] = 0x8b; |
| 1676 | hci_cp.iac_lap[5] = 0x9e; |
| 1677 | } else { |
| 1678 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1679 | hci_cp.num_iac = 1; |
| 1680 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1681 | hci_cp.iac_lap[1] = 0x8b; |
| 1682 | hci_cp.iac_lap[2] = 0x9e; |
| 1683 | } |
| 1684 | |
| 1685 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1686 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1687 | |
| 1688 | scan |= SCAN_INQUIRY; |
| 1689 | } else { |
| 1690 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1691 | } |
| 1692 | |
| 1693 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1694 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1695 | update_ad: |
| 1696 | update_adv_data(&req); |
| 1697 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1698 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1699 | if (err < 0) |
| 1700 | mgmt_pending_remove(cmd); |
| 1701 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1702 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1703 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1704 | return err; |
| 1705 | } |
| 1706 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1707 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1708 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1709 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1710 | struct hci_cp_write_page_scan_activity acp; |
| 1711 | u8 type; |
| 1712 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1713 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1714 | return; |
| 1715 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1716 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1717 | return; |
| 1718 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1719 | if (enable) { |
| 1720 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1721 | |
| 1722 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1723 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1724 | } else { |
| 1725 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1726 | |
| 1727 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1728 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1729 | } |
| 1730 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1731 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1732 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1733 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1734 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1735 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1736 | sizeof(acp), &acp); |
| 1737 | |
| 1738 | if (hdev->page_scan_type != type) |
| 1739 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1740 | } |
| 1741 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1742 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1743 | { |
| 1744 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1745 | struct mgmt_mode *cp; |
| 1746 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1747 | |
| 1748 | BT_DBG("status 0x%02x", status); |
| 1749 | |
| 1750 | hci_dev_lock(hdev); |
| 1751 | |
| 1752 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1753 | if (!cmd) |
| 1754 | goto unlock; |
| 1755 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1756 | if (status) { |
| 1757 | u8 mgmt_err = mgmt_status(status); |
| 1758 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1759 | goto remove_cmd; |
| 1760 | } |
| 1761 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1762 | cp = cmd->param; |
| 1763 | if (cp->val) |
| 1764 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1765 | else |
| 1766 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1767 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1768 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1769 | |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1770 | if (changed) { |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1771 | new_settings(hdev, cmd->sk); |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1772 | hci_update_background_scan(hdev); |
| 1773 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1774 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1775 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1776 | mgmt_pending_remove(cmd); |
| 1777 | |
| 1778 | unlock: |
| 1779 | hci_dev_unlock(hdev); |
| 1780 | } |
| 1781 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1782 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1783 | struct sock *sk, u8 val) |
| 1784 | { |
| 1785 | bool changed = false; |
| 1786 | int err; |
| 1787 | |
| 1788 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1789 | changed = true; |
| 1790 | |
| 1791 | if (val) { |
| 1792 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1793 | } else { |
| 1794 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1795 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1796 | } |
| 1797 | |
| 1798 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1799 | if (err < 0) |
| 1800 | return err; |
| 1801 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1802 | if (changed) { |
| 1803 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1804 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1805 | } |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1806 | |
| 1807 | return 0; |
| 1808 | } |
| 1809 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1810 | 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] | 1811 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1812 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1813 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1814 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1815 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1816 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1817 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1818 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1819 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1820 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1821 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1822 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1823 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1824 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1825 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1826 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1827 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1828 | MGMT_STATUS_INVALID_PARAMS); |
| 1829 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1830 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1831 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1832 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1833 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1834 | goto failed; |
| 1835 | } |
| 1836 | |
| 1837 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1838 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1839 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1840 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1841 | goto failed; |
| 1842 | } |
| 1843 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1844 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1845 | if (!cmd) { |
| 1846 | err = -ENOMEM; |
| 1847 | goto failed; |
| 1848 | } |
| 1849 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1850 | hci_req_init(&req, hdev); |
| 1851 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1852 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1853 | * by-product of disabling connectable, we need to update the |
| 1854 | * advertising flags. |
| 1855 | */ |
| 1856 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1857 | if (!cp->val) { |
| 1858 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1859 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1860 | } |
| 1861 | update_adv_data(&req); |
| 1862 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1863 | if (cp->val) { |
| 1864 | scan = SCAN_PAGE; |
| 1865 | } else { |
| 1866 | scan = 0; |
| 1867 | |
| 1868 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1869 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1870 | cancel_delayed_work(&hdev->discov_off); |
| 1871 | } |
| 1872 | |
| 1873 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1874 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1875 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1876 | /* If we're going from non-connectable to connectable or |
| 1877 | * vice-versa when fast connectable is enabled ensure that fast |
| 1878 | * connectable gets disabled. write_fast_connectable won't do |
| 1879 | * anything if the page scan parameters are already what they |
| 1880 | * should be. |
| 1881 | */ |
| 1882 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1883 | write_fast_connectable(&req, false); |
| 1884 | |
Johan Hedberg | e8b1202 | 2014-07-10 10:51:27 +0300 | [diff] [blame^] | 1885 | /* Update the advertising parameters if necessary */ |
| 1886 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1887 | enable_advertising(&req); |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1888 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1889 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1890 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1891 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1892 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1893 | err = set_connectable_update_settings(hdev, sk, |
| 1894 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1895 | goto failed; |
| 1896 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1897 | |
| 1898 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1899 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1900 | return err; |
| 1901 | } |
| 1902 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1903 | static int set_pairable(struct sock *sk, struct hci_dev *hdev, void *data, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1904 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1905 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1906 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1907 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1908 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1909 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1910 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1911 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1912 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1913 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1914 | MGMT_STATUS_INVALID_PARAMS); |
| 1915 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1916 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1917 | |
| 1918 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1919 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1920 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1921 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1922 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1923 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1924 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1925 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1926 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1927 | if (changed) |
| 1928 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1929 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1930 | unlock: |
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 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1934 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1935 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1936 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1937 | { |
| 1938 | struct mgmt_mode *cp = data; |
| 1939 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1940 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1941 | int err; |
| 1942 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1943 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1944 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1945 | status = mgmt_bredr_support(hdev); |
| 1946 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1947 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1948 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1949 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1950 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1951 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1952 | MGMT_STATUS_INVALID_PARAMS); |
| 1953 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1954 | hci_dev_lock(hdev); |
| 1955 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1956 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1957 | bool changed = false; |
| 1958 | |
| 1959 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1960 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1961 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1962 | changed = true; |
| 1963 | } |
| 1964 | |
| 1965 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1966 | if (err < 0) |
| 1967 | goto failed; |
| 1968 | |
| 1969 | if (changed) |
| 1970 | err = new_settings(hdev, sk); |
| 1971 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1972 | goto failed; |
| 1973 | } |
| 1974 | |
| 1975 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1976 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1977 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1978 | goto failed; |
| 1979 | } |
| 1980 | |
| 1981 | val = !!cp->val; |
| 1982 | |
| 1983 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1984 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1985 | goto failed; |
| 1986 | } |
| 1987 | |
| 1988 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1989 | if (!cmd) { |
| 1990 | err = -ENOMEM; |
| 1991 | goto failed; |
| 1992 | } |
| 1993 | |
| 1994 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1995 | if (err < 0) { |
| 1996 | mgmt_pending_remove(cmd); |
| 1997 | goto failed; |
| 1998 | } |
| 1999 | |
| 2000 | failed: |
| 2001 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 2002 | return err; |
| 2003 | } |
| 2004 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2005 | 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] | 2006 | { |
| 2007 | struct mgmt_mode *cp = data; |
| 2008 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2009 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2010 | int err; |
| 2011 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2012 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2013 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 2014 | status = mgmt_bredr_support(hdev); |
| 2015 | if (status) |
| 2016 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 2017 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2018 | if (!lmp_ssp_capable(hdev)) |
| 2019 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2020 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2021 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2022 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2023 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2024 | MGMT_STATUS_INVALID_PARAMS); |
| 2025 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2026 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 2027 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2028 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2029 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2030 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2031 | if (cp->val) { |
| 2032 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 2033 | &hdev->dev_flags); |
| 2034 | } else { |
| 2035 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 2036 | &hdev->dev_flags); |
| 2037 | if (!changed) |
| 2038 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 2039 | &hdev->dev_flags); |
| 2040 | else |
| 2041 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2045 | if (err < 0) |
| 2046 | goto failed; |
| 2047 | |
| 2048 | if (changed) |
| 2049 | err = new_settings(hdev, sk); |
| 2050 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2051 | goto failed; |
| 2052 | } |
| 2053 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2054 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 2055 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 2056 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2057 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2058 | goto failed; |
| 2059 | } |
| 2060 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2061 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2062 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2063 | goto failed; |
| 2064 | } |
| 2065 | |
| 2066 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 2067 | if (!cmd) { |
| 2068 | err = -ENOMEM; |
| 2069 | goto failed; |
| 2070 | } |
| 2071 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 2072 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 2073 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2074 | sizeof(cp->val), &cp->val); |
| 2075 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2076 | 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] | 2077 | if (err < 0) { |
| 2078 | mgmt_pending_remove(cmd); |
| 2079 | goto failed; |
| 2080 | } |
| 2081 | |
| 2082 | failed: |
| 2083 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2084 | return err; |
| 2085 | } |
| 2086 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2087 | 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] | 2088 | { |
| 2089 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2090 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2091 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2092 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2093 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2094 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2095 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2096 | status = mgmt_bredr_support(hdev); |
| 2097 | if (status) |
| 2098 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2099 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2100 | if (!lmp_ssp_capable(hdev)) |
| 2101 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2102 | MGMT_STATUS_NOT_SUPPORTED); |
| 2103 | |
| 2104 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 2105 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2106 | MGMT_STATUS_REJECTED); |
| 2107 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2108 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2109 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2110 | MGMT_STATUS_INVALID_PARAMS); |
| 2111 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2112 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2113 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2114 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2115 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2116 | } else { |
| 2117 | if (hdev_is_powered(hdev)) { |
| 2118 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2119 | MGMT_STATUS_REJECTED); |
| 2120 | goto unlock; |
| 2121 | } |
| 2122 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2123 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2124 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2125 | |
| 2126 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2127 | if (err < 0) |
| 2128 | goto unlock; |
| 2129 | |
| 2130 | if (changed) |
| 2131 | err = new_settings(hdev, sk); |
| 2132 | |
| 2133 | unlock: |
| 2134 | hci_dev_unlock(hdev); |
| 2135 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2136 | } |
| 2137 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2138 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 2139 | { |
| 2140 | struct cmd_lookup match = { NULL, hdev }; |
| 2141 | |
| 2142 | if (status) { |
| 2143 | u8 mgmt_err = mgmt_status(status); |
| 2144 | |
| 2145 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2146 | &mgmt_err); |
| 2147 | return; |
| 2148 | } |
| 2149 | |
| 2150 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2151 | |
| 2152 | new_settings(hdev, match.sk); |
| 2153 | |
| 2154 | if (match.sk) |
| 2155 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2156 | |
| 2157 | /* Make sure the controller has a good default for |
| 2158 | * advertising data. Restrict the update to when LE |
| 2159 | * has actually been enabled. During power on, the |
| 2160 | * update in powered_update_hci will take care of it. |
| 2161 | */ |
| 2162 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2163 | struct hci_request req; |
| 2164 | |
| 2165 | hci_dev_lock(hdev); |
| 2166 | |
| 2167 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2168 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2169 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2170 | hci_req_run(&req, NULL); |
| 2171 | |
Johan Hedberg | a70f4b5 | 2014-07-07 15:19:50 +0300 | [diff] [blame] | 2172 | hci_update_background_scan(hdev); |
| 2173 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2174 | hci_dev_unlock(hdev); |
| 2175 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2176 | } |
| 2177 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2178 | 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] | 2179 | { |
| 2180 | struct mgmt_mode *cp = data; |
| 2181 | struct hci_cp_write_le_host_supported hci_cp; |
| 2182 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2183 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2184 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2185 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2186 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2187 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2188 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2189 | if (!lmp_le_capable(hdev)) |
| 2190 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2191 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2192 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2193 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2194 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2195 | MGMT_STATUS_INVALID_PARAMS); |
| 2196 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2197 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2198 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2199 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2200 | MGMT_STATUS_REJECTED); |
| 2201 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2202 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2203 | |
| 2204 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2205 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2206 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2207 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2208 | bool changed = false; |
| 2209 | |
| 2210 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2211 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2212 | changed = true; |
| 2213 | } |
| 2214 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2215 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2216 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2217 | changed = true; |
| 2218 | } |
| 2219 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2220 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2221 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2222 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2223 | |
| 2224 | if (changed) |
| 2225 | err = new_settings(hdev, sk); |
| 2226 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2227 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2228 | } |
| 2229 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2230 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2231 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2232 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2233 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2234 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2238 | if (!cmd) { |
| 2239 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2240 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2241 | } |
| 2242 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2243 | hci_req_init(&req, hdev); |
| 2244 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2245 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2246 | |
| 2247 | if (val) { |
| 2248 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2249 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2250 | } else { |
Johan Hedberg | 73e082f | 2014-07-08 15:07:51 +0300 | [diff] [blame] | 2251 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2252 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2253 | } |
| 2254 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2255 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2256 | &hci_cp); |
| 2257 | |
| 2258 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2259 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2260 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2261 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2262 | unlock: |
| 2263 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2264 | return err; |
| 2265 | } |
| 2266 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2267 | /* This is a helper function to test for pending mgmt commands that can |
| 2268 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2269 | * mgmt command at a time since otherwise we cannot easily track what |
| 2270 | * the current values are, will be, and based on that calculate if a new |
| 2271 | * HCI command needs to be sent and if yes with what value. |
| 2272 | */ |
| 2273 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2274 | { |
| 2275 | struct pending_cmd *cmd; |
| 2276 | |
| 2277 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2278 | switch (cmd->opcode) { |
| 2279 | case MGMT_OP_ADD_UUID: |
| 2280 | case MGMT_OP_REMOVE_UUID: |
| 2281 | case MGMT_OP_SET_DEV_CLASS: |
| 2282 | case MGMT_OP_SET_POWERED: |
| 2283 | return true; |
| 2284 | } |
| 2285 | } |
| 2286 | |
| 2287 | return false; |
| 2288 | } |
| 2289 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2290 | static const u8 bluetooth_base_uuid[] = { |
| 2291 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2292 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2293 | }; |
| 2294 | |
| 2295 | static u8 get_uuid_size(const u8 *uuid) |
| 2296 | { |
| 2297 | u32 val; |
| 2298 | |
| 2299 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2300 | return 128; |
| 2301 | |
| 2302 | val = get_unaligned_le32(&uuid[12]); |
| 2303 | if (val > 0xffff) |
| 2304 | return 32; |
| 2305 | |
| 2306 | return 16; |
| 2307 | } |
| 2308 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2309 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2310 | { |
| 2311 | struct pending_cmd *cmd; |
| 2312 | |
| 2313 | hci_dev_lock(hdev); |
| 2314 | |
| 2315 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2316 | if (!cmd) |
| 2317 | goto unlock; |
| 2318 | |
| 2319 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2320 | hdev->dev_class, 3); |
| 2321 | |
| 2322 | mgmt_pending_remove(cmd); |
| 2323 | |
| 2324 | unlock: |
| 2325 | hci_dev_unlock(hdev); |
| 2326 | } |
| 2327 | |
| 2328 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2329 | { |
| 2330 | BT_DBG("status 0x%02x", status); |
| 2331 | |
| 2332 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2333 | } |
| 2334 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2335 | 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] | 2336 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2337 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2338 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2339 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2340 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2341 | int err; |
| 2342 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2343 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2344 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2345 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2346 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2347 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2348 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2349 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2350 | goto failed; |
| 2351 | } |
| 2352 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2353 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2354 | if (!uuid) { |
| 2355 | err = -ENOMEM; |
| 2356 | goto failed; |
| 2357 | } |
| 2358 | |
| 2359 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2360 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2361 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2362 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2363 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2364 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2365 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2366 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2367 | update_class(&req); |
| 2368 | update_eir(&req); |
| 2369 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2370 | err = hci_req_run(&req, add_uuid_complete); |
| 2371 | if (err < 0) { |
| 2372 | if (err != -ENODATA) |
| 2373 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2374 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2375 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2376 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2377 | goto failed; |
| 2378 | } |
| 2379 | |
| 2380 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2381 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2382 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2383 | goto failed; |
| 2384 | } |
| 2385 | |
| 2386 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2387 | |
| 2388 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2389 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2390 | return err; |
| 2391 | } |
| 2392 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2393 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2394 | { |
| 2395 | if (!hdev_is_powered(hdev)) |
| 2396 | return false; |
| 2397 | |
| 2398 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2399 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2400 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2401 | return true; |
| 2402 | } |
| 2403 | |
| 2404 | return false; |
| 2405 | } |
| 2406 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2407 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2408 | { |
| 2409 | BT_DBG("status 0x%02x", status); |
| 2410 | |
| 2411 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2412 | } |
| 2413 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2414 | 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] | 2415 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2416 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2417 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2418 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2419 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2420 | 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] | 2421 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2422 | int err, found; |
| 2423 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2424 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2425 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2426 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2427 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2428 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2429 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2430 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2431 | goto unlock; |
| 2432 | } |
| 2433 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2434 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2435 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2436 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2437 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2438 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2439 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2440 | goto unlock; |
| 2441 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2442 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2443 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | found = 0; |
| 2447 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2448 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2449 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2450 | continue; |
| 2451 | |
| 2452 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2453 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2454 | found++; |
| 2455 | } |
| 2456 | |
| 2457 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2458 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2459 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2460 | goto unlock; |
| 2461 | } |
| 2462 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2463 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2464 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2465 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2466 | update_class(&req); |
| 2467 | update_eir(&req); |
| 2468 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2469 | err = hci_req_run(&req, remove_uuid_complete); |
| 2470 | if (err < 0) { |
| 2471 | if (err != -ENODATA) |
| 2472 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2473 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2474 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2475 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2476 | goto unlock; |
| 2477 | } |
| 2478 | |
| 2479 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2480 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2481 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2482 | goto unlock; |
| 2483 | } |
| 2484 | |
| 2485 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2486 | |
| 2487 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2488 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2489 | return err; |
| 2490 | } |
| 2491 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2492 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2493 | { |
| 2494 | BT_DBG("status 0x%02x", status); |
| 2495 | |
| 2496 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2497 | } |
| 2498 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2499 | 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] | 2500 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2501 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2502 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2503 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2504 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2505 | int err; |
| 2506 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2507 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2508 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2509 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2510 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2511 | MGMT_STATUS_NOT_SUPPORTED); |
| 2512 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2513 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2514 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2515 | if (pending_eir_or_class(hdev)) { |
| 2516 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2517 | MGMT_STATUS_BUSY); |
| 2518 | goto unlock; |
| 2519 | } |
| 2520 | |
| 2521 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2522 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2523 | MGMT_STATUS_INVALID_PARAMS); |
| 2524 | goto unlock; |
| 2525 | } |
| 2526 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2527 | hdev->major_class = cp->major; |
| 2528 | hdev->minor_class = cp->minor; |
| 2529 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2530 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2531 | 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] | 2532 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2533 | goto unlock; |
| 2534 | } |
| 2535 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2536 | hci_req_init(&req, hdev); |
| 2537 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2538 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2539 | hci_dev_unlock(hdev); |
| 2540 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2541 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2542 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2543 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2544 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2545 | update_class(&req); |
| 2546 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2547 | err = hci_req_run(&req, set_class_complete); |
| 2548 | if (err < 0) { |
| 2549 | if (err != -ENODATA) |
| 2550 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2551 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2552 | 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] | 2553 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2554 | goto unlock; |
| 2555 | } |
| 2556 | |
| 2557 | 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] | 2558 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2559 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2560 | goto unlock; |
| 2561 | } |
| 2562 | |
| 2563 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2564 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2565 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2566 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2567 | return err; |
| 2568 | } |
| 2569 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2570 | 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] | 2571 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2572 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2573 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2574 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2575 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2576 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2577 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2578 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2579 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2580 | BT_DBG("request for %s", hdev->name); |
| 2581 | |
| 2582 | if (!lmp_bredr_capable(hdev)) |
| 2583 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2584 | MGMT_STATUS_NOT_SUPPORTED); |
| 2585 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2586 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2587 | if (key_count > max_key_count) { |
| 2588 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2589 | key_count); |
| 2590 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2591 | MGMT_STATUS_INVALID_PARAMS); |
| 2592 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2593 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2594 | expected_len = sizeof(*cp) + key_count * |
| 2595 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2596 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2597 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2598 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2599 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2600 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2601 | } |
| 2602 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2603 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2604 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2605 | MGMT_STATUS_INVALID_PARAMS); |
| 2606 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2607 | 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] | 2608 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2609 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2610 | for (i = 0; i < key_count; i++) { |
| 2611 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2612 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2613 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2614 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2615 | MGMT_STATUS_INVALID_PARAMS); |
| 2616 | } |
| 2617 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2618 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2619 | |
| 2620 | hci_link_keys_clear(hdev); |
| 2621 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2622 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2623 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2624 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2625 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2626 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2627 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2628 | |
| 2629 | if (changed) |
| 2630 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2631 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2632 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2633 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2634 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2635 | /* Always ignore debug keys and require a new pairing if |
| 2636 | * the user wants to use them. |
| 2637 | */ |
| 2638 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2639 | continue; |
| 2640 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2641 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2642 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2643 | } |
| 2644 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2645 | 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] | 2646 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2647 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2648 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2649 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2650 | } |
| 2651 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2652 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2653 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2654 | { |
| 2655 | struct mgmt_ev_device_unpaired ev; |
| 2656 | |
| 2657 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2658 | ev.addr.type = addr_type; |
| 2659 | |
| 2660 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2661 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2662 | } |
| 2663 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2664 | 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] | 2665 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2666 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2667 | struct mgmt_cp_unpair_device *cp = data; |
| 2668 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2669 | struct hci_cp_disconnect dc; |
| 2670 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2671 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2672 | int err; |
| 2673 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2674 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2675 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2676 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2677 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2678 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2679 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2680 | MGMT_STATUS_INVALID_PARAMS, |
| 2681 | &rp, sizeof(rp)); |
| 2682 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2683 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2684 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2685 | MGMT_STATUS_INVALID_PARAMS, |
| 2686 | &rp, sizeof(rp)); |
| 2687 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2688 | hci_dev_lock(hdev); |
| 2689 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2690 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2691 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2692 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2693 | goto unlock; |
| 2694 | } |
| 2695 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2696 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2697 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2698 | } else { |
| 2699 | u8 addr_type; |
| 2700 | |
| 2701 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2702 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2703 | else |
| 2704 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2705 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2706 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2707 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2708 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2709 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2710 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2711 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2712 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2713 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2714 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2715 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2716 | goto unlock; |
| 2717 | } |
| 2718 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2719 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2720 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2721 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2722 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2723 | else |
| 2724 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2725 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2726 | } else { |
| 2727 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2728 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2729 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2730 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2731 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2732 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2733 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2734 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2735 | } |
| 2736 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2737 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2738 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2739 | if (!cmd) { |
| 2740 | err = -ENOMEM; |
| 2741 | goto unlock; |
| 2742 | } |
| 2743 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2744 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2745 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2746 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2747 | if (err < 0) |
| 2748 | mgmt_pending_remove(cmd); |
| 2749 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2750 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2751 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2752 | return err; |
| 2753 | } |
| 2754 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2755 | 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] | 2756 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2757 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2758 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2759 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2760 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2761 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2762 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2763 | int err; |
| 2764 | |
| 2765 | BT_DBG(""); |
| 2766 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2767 | memset(&rp, 0, sizeof(rp)); |
| 2768 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2769 | rp.addr.type = cp->addr.type; |
| 2770 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2771 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2772 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2773 | MGMT_STATUS_INVALID_PARAMS, |
| 2774 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2775 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2776 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2777 | |
| 2778 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2779 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2780 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2781 | goto failed; |
| 2782 | } |
| 2783 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2784 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2785 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2786 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2787 | goto failed; |
| 2788 | } |
| 2789 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2790 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2791 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2792 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2793 | else |
| 2794 | 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] | 2795 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2796 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2797 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2798 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2799 | goto failed; |
| 2800 | } |
| 2801 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2802 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2803 | if (!cmd) { |
| 2804 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2805 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2806 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2807 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2808 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2809 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2810 | |
| 2811 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2812 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2813 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2814 | |
| 2815 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2816 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2817 | return err; |
| 2818 | } |
| 2819 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2820 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2821 | { |
| 2822 | switch (link_type) { |
| 2823 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2824 | switch (addr_type) { |
| 2825 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2826 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2827 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2828 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2829 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2830 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2831 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2832 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2833 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2834 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2835 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2836 | } |
| 2837 | } |
| 2838 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2839 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2840 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2841 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2842 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2843 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2844 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2845 | int err; |
| 2846 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2847 | |
| 2848 | BT_DBG(""); |
| 2849 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2850 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2851 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2852 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2853 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2854 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2855 | goto unlock; |
| 2856 | } |
| 2857 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2858 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2859 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2860 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2861 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2862 | } |
| 2863 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2864 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2865 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2866 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2867 | err = -ENOMEM; |
| 2868 | goto unlock; |
| 2869 | } |
| 2870 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2871 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2872 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2873 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2874 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2875 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2876 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2877 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2878 | continue; |
| 2879 | i++; |
| 2880 | } |
| 2881 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2882 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2883 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2884 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2885 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2886 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2887 | 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] | 2888 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2889 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2890 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2891 | |
| 2892 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2893 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2894 | return err; |
| 2895 | } |
| 2896 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2897 | 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] | 2898 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2899 | { |
| 2900 | struct pending_cmd *cmd; |
| 2901 | int err; |
| 2902 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2903 | 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] | 2904 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2905 | if (!cmd) |
| 2906 | return -ENOMEM; |
| 2907 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2908 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2909 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2910 | if (err < 0) |
| 2911 | mgmt_pending_remove(cmd); |
| 2912 | |
| 2913 | return err; |
| 2914 | } |
| 2915 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2916 | 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] | 2917 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2918 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2919 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2920 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2921 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2922 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2923 | int err; |
| 2924 | |
| 2925 | BT_DBG(""); |
| 2926 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2927 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2928 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2929 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2930 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2931 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2932 | goto failed; |
| 2933 | } |
| 2934 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2935 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2936 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2937 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2938 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2939 | goto failed; |
| 2940 | } |
| 2941 | |
| 2942 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2943 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2944 | |
| 2945 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2946 | |
| 2947 | BT_ERR("PIN code is not 16 bytes long"); |
| 2948 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2949 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2950 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2951 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2952 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2953 | |
| 2954 | goto failed; |
| 2955 | } |
| 2956 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2957 | 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] | 2958 | if (!cmd) { |
| 2959 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2960 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2961 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2962 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2963 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2964 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2965 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2966 | |
| 2967 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2968 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2969 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2970 | |
| 2971 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2972 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2973 | return err; |
| 2974 | } |
| 2975 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2976 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2977 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2978 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2979 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2980 | |
| 2981 | BT_DBG(""); |
| 2982 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2983 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2984 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2985 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2986 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2987 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2988 | |
| 2989 | hdev->io_capability = cp->io_capability; |
| 2990 | |
| 2991 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2992 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2993 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2994 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2995 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2996 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2997 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2998 | } |
| 2999 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 3000 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3001 | { |
| 3002 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 3003 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3004 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3005 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3006 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 3007 | continue; |
| 3008 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3009 | if (cmd->user_data != conn) |
| 3010 | continue; |
| 3011 | |
| 3012 | return cmd; |
| 3013 | } |
| 3014 | |
| 3015 | return NULL; |
| 3016 | } |
| 3017 | |
| 3018 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 3019 | { |
| 3020 | struct mgmt_rp_pair_device rp; |
| 3021 | struct hci_conn *conn = cmd->user_data; |
| 3022 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 3023 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 3024 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3025 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3026 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3027 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3028 | |
| 3029 | /* So we don't get further callbacks for this connection */ |
| 3030 | conn->connect_cfm_cb = NULL; |
| 3031 | conn->security_cfm_cb = NULL; |
| 3032 | conn->disconn_cfm_cb = NULL; |
| 3033 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3034 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3035 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3036 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3037 | } |
| 3038 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3039 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 3040 | { |
| 3041 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 3042 | struct pending_cmd *cmd; |
| 3043 | |
| 3044 | cmd = find_pairing(conn); |
| 3045 | if (cmd) |
| 3046 | pairing_complete(cmd, status); |
| 3047 | } |
| 3048 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3049 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 3050 | { |
| 3051 | struct pending_cmd *cmd; |
| 3052 | |
| 3053 | BT_DBG("status %u", status); |
| 3054 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3055 | cmd = find_pairing(conn); |
| 3056 | if (!cmd) |
| 3057 | BT_DBG("Unable to find a pending command"); |
| 3058 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 3059 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3060 | } |
| 3061 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3062 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3063 | { |
| 3064 | struct pending_cmd *cmd; |
| 3065 | |
| 3066 | BT_DBG("status %u", status); |
| 3067 | |
| 3068 | if (!status) |
| 3069 | return; |
| 3070 | |
| 3071 | cmd = find_pairing(conn); |
| 3072 | if (!cmd) |
| 3073 | BT_DBG("Unable to find a pending command"); |
| 3074 | else |
| 3075 | pairing_complete(cmd, mgmt_status(status)); |
| 3076 | } |
| 3077 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3078 | 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] | 3079 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3080 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3081 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3082 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3083 | struct pending_cmd *cmd; |
| 3084 | u8 sec_level, auth_type; |
| 3085 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3086 | int err; |
| 3087 | |
| 3088 | BT_DBG(""); |
| 3089 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3090 | memset(&rp, 0, sizeof(rp)); |
| 3091 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3092 | rp.addr.type = cp->addr.type; |
| 3093 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3094 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 3095 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3096 | MGMT_STATUS_INVALID_PARAMS, |
| 3097 | &rp, sizeof(rp)); |
| 3098 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3099 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 3100 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3101 | MGMT_STATUS_INVALID_PARAMS, |
| 3102 | &rp, sizeof(rp)); |
| 3103 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3104 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3105 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3106 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3107 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3108 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3109 | goto unlock; |
| 3110 | } |
| 3111 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3112 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3113 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3114 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3115 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3116 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3117 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3118 | } else { |
| 3119 | u8 addr_type; |
| 3120 | |
| 3121 | /* Convert from L2CAP channel address type to HCI address type |
| 3122 | */ |
| 3123 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3124 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3125 | else |
| 3126 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3127 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3128 | /* When pairing a new device, it is expected to remember |
| 3129 | * this device for future connections. Adding the connection |
| 3130 | * parameter information ahead of time allows tracking |
| 3131 | * of the slave preferred values and will speed up any |
| 3132 | * further connection establishment. |
| 3133 | * |
| 3134 | * If connection parameters already exist, then they |
| 3135 | * will be kept and this function does nothing. |
| 3136 | */ |
| 3137 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3138 | |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 3139 | /* Request a connection with master = true role */ |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3140 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 3141 | sec_level, HCI_LE_CONN_TIMEOUT, true); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3142 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3143 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3144 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3145 | int status; |
| 3146 | |
| 3147 | if (PTR_ERR(conn) == -EBUSY) |
| 3148 | status = MGMT_STATUS_BUSY; |
| 3149 | else |
| 3150 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3151 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3152 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3153 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3154 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3155 | goto unlock; |
| 3156 | } |
| 3157 | |
| 3158 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3159 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3160 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3161 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3162 | goto unlock; |
| 3163 | } |
| 3164 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3165 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3166 | if (!cmd) { |
| 3167 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3168 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3169 | goto unlock; |
| 3170 | } |
| 3171 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3172 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3173 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3174 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3175 | conn->security_cfm_cb = pairing_complete_cb; |
| 3176 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3177 | } else { |
| 3178 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3179 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3180 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3181 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3182 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3183 | conn->io_capability = cp->io_cap; |
| 3184 | cmd->user_data = conn; |
| 3185 | |
| 3186 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3187 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3188 | pairing_complete(cmd, 0); |
| 3189 | |
| 3190 | err = 0; |
| 3191 | |
| 3192 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3193 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3194 | return err; |
| 3195 | } |
| 3196 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3197 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3198 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3199 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3200 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3201 | struct pending_cmd *cmd; |
| 3202 | struct hci_conn *conn; |
| 3203 | int err; |
| 3204 | |
| 3205 | BT_DBG(""); |
| 3206 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3207 | hci_dev_lock(hdev); |
| 3208 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3209 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3210 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3211 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3212 | goto unlock; |
| 3213 | } |
| 3214 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3215 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3216 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3217 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3218 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3219 | goto unlock; |
| 3220 | } |
| 3221 | |
| 3222 | conn = cmd->user_data; |
| 3223 | |
| 3224 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3225 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3226 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3227 | goto unlock; |
| 3228 | } |
| 3229 | |
| 3230 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3231 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3232 | 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] | 3233 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3234 | unlock: |
| 3235 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3236 | return err; |
| 3237 | } |
| 3238 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3239 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3240 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3241 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3242 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3243 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3244 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3245 | int err; |
| 3246 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3247 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3248 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3249 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3250 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3251 | MGMT_STATUS_NOT_POWERED, addr, |
| 3252 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3253 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3254 | } |
| 3255 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3256 | if (addr->type == BDADDR_BREDR) |
| 3257 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3258 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3259 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3260 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3261 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3262 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3263 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3264 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3265 | goto done; |
| 3266 | } |
| 3267 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3268 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3269 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3270 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3271 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3272 | MGMT_STATUS_SUCCESS, addr, |
| 3273 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3274 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3275 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3276 | MGMT_STATUS_FAILED, addr, |
| 3277 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3278 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3279 | goto done; |
| 3280 | } |
| 3281 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3282 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3283 | if (!cmd) { |
| 3284 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3285 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3286 | } |
| 3287 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3288 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3289 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3290 | struct hci_cp_user_passkey_reply cp; |
| 3291 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3292 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3293 | cp.passkey = passkey; |
| 3294 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3295 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3296 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3297 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3298 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3299 | if (err < 0) |
| 3300 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3301 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3302 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3303 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3304 | return err; |
| 3305 | } |
| 3306 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3307 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3308 | void *data, u16 len) |
| 3309 | { |
| 3310 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3311 | |
| 3312 | BT_DBG(""); |
| 3313 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3314 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3315 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3316 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3317 | } |
| 3318 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3319 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3320 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3321 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3322 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3323 | |
| 3324 | BT_DBG(""); |
| 3325 | |
| 3326 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3327 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3328 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3329 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3330 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3331 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3332 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3333 | } |
| 3334 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3335 | 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] | 3336 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3337 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3338 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3339 | |
| 3340 | BT_DBG(""); |
| 3341 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3342 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3343 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3344 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3345 | } |
| 3346 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3347 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3348 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3349 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3350 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3351 | |
| 3352 | BT_DBG(""); |
| 3353 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3354 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3355 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3356 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3357 | } |
| 3358 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3359 | 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] | 3360 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3361 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3362 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3363 | |
| 3364 | BT_DBG(""); |
| 3365 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3366 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3367 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3368 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3369 | } |
| 3370 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3371 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3372 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3373 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3374 | struct hci_cp_write_local_name cp; |
| 3375 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3376 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3377 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3378 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3379 | } |
| 3380 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3381 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3382 | { |
| 3383 | struct mgmt_cp_set_local_name *cp; |
| 3384 | struct pending_cmd *cmd; |
| 3385 | |
| 3386 | BT_DBG("status 0x%02x", status); |
| 3387 | |
| 3388 | hci_dev_lock(hdev); |
| 3389 | |
| 3390 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3391 | if (!cmd) |
| 3392 | goto unlock; |
| 3393 | |
| 3394 | cp = cmd->param; |
| 3395 | |
| 3396 | if (status) |
| 3397 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3398 | mgmt_status(status)); |
| 3399 | else |
| 3400 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3401 | cp, sizeof(*cp)); |
| 3402 | |
| 3403 | mgmt_pending_remove(cmd); |
| 3404 | |
| 3405 | unlock: |
| 3406 | hci_dev_unlock(hdev); |
| 3407 | } |
| 3408 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3409 | 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] | 3410 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3411 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3412 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3413 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3414 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3415 | int err; |
| 3416 | |
| 3417 | BT_DBG(""); |
| 3418 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3419 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3420 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3421 | /* If the old values are the same as the new ones just return a |
| 3422 | * direct command complete event. |
| 3423 | */ |
| 3424 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3425 | !memcmp(hdev->short_name, cp->short_name, |
| 3426 | sizeof(hdev->short_name))) { |
| 3427 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3428 | data, len); |
| 3429 | goto failed; |
| 3430 | } |
| 3431 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3432 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3433 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3434 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3435 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3436 | |
| 3437 | 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] | 3438 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3439 | if (err < 0) |
| 3440 | goto failed; |
| 3441 | |
| 3442 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3443 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3444 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3445 | goto failed; |
| 3446 | } |
| 3447 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3448 | 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] | 3449 | if (!cmd) { |
| 3450 | err = -ENOMEM; |
| 3451 | goto failed; |
| 3452 | } |
| 3453 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3454 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3455 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3456 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3457 | |
| 3458 | if (lmp_bredr_capable(hdev)) { |
| 3459 | update_name(&req); |
| 3460 | update_eir(&req); |
| 3461 | } |
| 3462 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3463 | /* The name is stored in the scan response data and so |
| 3464 | * no need to udpate the advertising data here. |
| 3465 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3466 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3467 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3468 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3469 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3470 | if (err < 0) |
| 3471 | mgmt_pending_remove(cmd); |
| 3472 | |
| 3473 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3474 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3475 | return err; |
| 3476 | } |
| 3477 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3478 | 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] | 3479 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3480 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3481 | struct pending_cmd *cmd; |
| 3482 | int err; |
| 3483 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3484 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3485 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3486 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3487 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3488 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3489 | 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] | 3490 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3491 | goto unlock; |
| 3492 | } |
| 3493 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3494 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3495 | 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] | 3496 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3497 | goto unlock; |
| 3498 | } |
| 3499 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3500 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3501 | 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] | 3502 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3503 | goto unlock; |
| 3504 | } |
| 3505 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3506 | 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] | 3507 | if (!cmd) { |
| 3508 | err = -ENOMEM; |
| 3509 | goto unlock; |
| 3510 | } |
| 3511 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3512 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3513 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3514 | 0, NULL); |
| 3515 | else |
| 3516 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3517 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3518 | if (err < 0) |
| 3519 | mgmt_pending_remove(cmd); |
| 3520 | |
| 3521 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3522 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3523 | return err; |
| 3524 | } |
| 3525 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3526 | 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] | 3527 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3528 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3529 | int err; |
| 3530 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3531 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3532 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3533 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3534 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3535 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3536 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3537 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3538 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3539 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3540 | cp->hash, cp->randomizer); |
| 3541 | if (err < 0) |
| 3542 | status = MGMT_STATUS_FAILED; |
| 3543 | else |
| 3544 | status = MGMT_STATUS_SUCCESS; |
| 3545 | |
| 3546 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3547 | status, &cp->addr, sizeof(cp->addr)); |
| 3548 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3549 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3550 | u8 status; |
| 3551 | |
| 3552 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3553 | cp->hash192, |
| 3554 | cp->randomizer192, |
| 3555 | cp->hash256, |
| 3556 | cp->randomizer256); |
| 3557 | if (err < 0) |
| 3558 | status = MGMT_STATUS_FAILED; |
| 3559 | else |
| 3560 | status = MGMT_STATUS_SUCCESS; |
| 3561 | |
| 3562 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3563 | status, &cp->addr, sizeof(cp->addr)); |
| 3564 | } else { |
| 3565 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3566 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3567 | MGMT_STATUS_INVALID_PARAMS); |
| 3568 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3569 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3570 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3571 | return err; |
| 3572 | } |
| 3573 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3574 | 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] | 3575 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3576 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3577 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3578 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3579 | int err; |
| 3580 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3581 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3582 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3583 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3584 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3585 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3586 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3587 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3588 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3589 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3590 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3591 | 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] | 3592 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3593 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3594 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3595 | return err; |
| 3596 | } |
| 3597 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3598 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3599 | { |
| 3600 | struct pending_cmd *cmd; |
| 3601 | u8 type; |
| 3602 | int err; |
| 3603 | |
| 3604 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3605 | |
| 3606 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3607 | if (!cmd) |
| 3608 | return -ENOENT; |
| 3609 | |
| 3610 | type = hdev->discovery.type; |
| 3611 | |
| 3612 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3613 | &type, sizeof(type)); |
| 3614 | mgmt_pending_remove(cmd); |
| 3615 | |
| 3616 | return err; |
| 3617 | } |
| 3618 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3619 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3620 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3621 | unsigned long timeout = 0; |
| 3622 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3623 | BT_DBG("status %d", status); |
| 3624 | |
| 3625 | if (status) { |
| 3626 | hci_dev_lock(hdev); |
| 3627 | mgmt_start_discovery_failed(hdev, status); |
| 3628 | hci_dev_unlock(hdev); |
| 3629 | return; |
| 3630 | } |
| 3631 | |
| 3632 | hci_dev_lock(hdev); |
| 3633 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3634 | hci_dev_unlock(hdev); |
| 3635 | |
| 3636 | switch (hdev->discovery.type) { |
| 3637 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3638 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3639 | break; |
| 3640 | |
| 3641 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3642 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3643 | break; |
| 3644 | |
| 3645 | case DISCOV_TYPE_BREDR: |
| 3646 | break; |
| 3647 | |
| 3648 | default: |
| 3649 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3650 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3651 | |
| 3652 | if (!timeout) |
| 3653 | return; |
| 3654 | |
| 3655 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3656 | } |
| 3657 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3658 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3659 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3660 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3661 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3662 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3663 | struct hci_cp_le_set_scan_param param_cp; |
| 3664 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3665 | struct hci_cp_inquiry inq_cp; |
| 3666 | struct hci_request req; |
| 3667 | /* General inquiry access code (GIAC) */ |
| 3668 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3669 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3670 | int err; |
| 3671 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3672 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3673 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3674 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3675 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3676 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3677 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3678 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3679 | goto failed; |
| 3680 | } |
| 3681 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3682 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3683 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3684 | MGMT_STATUS_BUSY); |
| 3685 | goto failed; |
| 3686 | } |
| 3687 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3688 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3689 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3690 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3691 | goto failed; |
| 3692 | } |
| 3693 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3694 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3695 | if (!cmd) { |
| 3696 | err = -ENOMEM; |
| 3697 | goto failed; |
| 3698 | } |
| 3699 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3700 | hdev->discovery.type = cp->type; |
| 3701 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3702 | hci_req_init(&req, hdev); |
| 3703 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3704 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3705 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3706 | status = mgmt_bredr_support(hdev); |
| 3707 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3708 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3709 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3710 | mgmt_pending_remove(cmd); |
| 3711 | goto failed; |
| 3712 | } |
| 3713 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3714 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3715 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3716 | MGMT_STATUS_BUSY); |
| 3717 | mgmt_pending_remove(cmd); |
| 3718 | goto failed; |
| 3719 | } |
| 3720 | |
| 3721 | hci_inquiry_cache_flush(hdev); |
| 3722 | |
| 3723 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3724 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3725 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3726 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3727 | break; |
| 3728 | |
| 3729 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3730 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3731 | status = mgmt_le_support(hdev); |
| 3732 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3733 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3734 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3735 | mgmt_pending_remove(cmd); |
| 3736 | goto failed; |
| 3737 | } |
| 3738 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3739 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3740 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3741 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3742 | MGMT_STATUS_NOT_SUPPORTED); |
| 3743 | mgmt_pending_remove(cmd); |
| 3744 | goto failed; |
| 3745 | } |
| 3746 | |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 3747 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) { |
| 3748 | /* Don't let discovery abort an outgoing |
| 3749 | * connection attempt that's using directed |
| 3750 | * advertising. |
| 3751 | */ |
| 3752 | if (hci_conn_hash_lookup_state(hdev, LE_LINK, |
| 3753 | BT_CONNECT)) { |
| 3754 | err = cmd_status(sk, hdev->id, |
| 3755 | MGMT_OP_START_DISCOVERY, |
| 3756 | MGMT_STATUS_REJECTED); |
| 3757 | mgmt_pending_remove(cmd); |
| 3758 | goto failed; |
| 3759 | } |
| 3760 | |
| 3761 | disable_advertising(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3762 | } |
| 3763 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3764 | /* If controller is scanning, it means the background scanning |
| 3765 | * is running. Thus, we should temporarily stop it in order to |
| 3766 | * set the discovery scanning parameters. |
| 3767 | */ |
| 3768 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3769 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3770 | |
| 3771 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3772 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3773 | /* All active scans will be done with either a resolvable |
| 3774 | * private address (when privacy feature has been enabled) |
| 3775 | * or unresolvable private address. |
| 3776 | */ |
| 3777 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3778 | if (err < 0) { |
| 3779 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3780 | MGMT_STATUS_FAILED); |
| 3781 | mgmt_pending_remove(cmd); |
| 3782 | goto failed; |
| 3783 | } |
| 3784 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3785 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3786 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3787 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3788 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3789 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3790 | ¶m_cp); |
| 3791 | |
| 3792 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3793 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3794 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3795 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3796 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3797 | break; |
| 3798 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3799 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3800 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3801 | MGMT_STATUS_INVALID_PARAMS); |
| 3802 | mgmt_pending_remove(cmd); |
| 3803 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3804 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3805 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3806 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3807 | if (err < 0) |
| 3808 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3809 | else |
| 3810 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3811 | |
| 3812 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3813 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3814 | return err; |
| 3815 | } |
| 3816 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3817 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3818 | { |
| 3819 | struct pending_cmd *cmd; |
| 3820 | int err; |
| 3821 | |
| 3822 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3823 | if (!cmd) |
| 3824 | return -ENOENT; |
| 3825 | |
| 3826 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3827 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3828 | mgmt_pending_remove(cmd); |
| 3829 | |
| 3830 | return err; |
| 3831 | } |
| 3832 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3833 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3834 | { |
| 3835 | BT_DBG("status %d", status); |
| 3836 | |
| 3837 | hci_dev_lock(hdev); |
| 3838 | |
| 3839 | if (status) { |
| 3840 | mgmt_stop_discovery_failed(hdev, status); |
| 3841 | goto unlock; |
| 3842 | } |
| 3843 | |
| 3844 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3845 | |
| 3846 | unlock: |
| 3847 | hci_dev_unlock(hdev); |
| 3848 | } |
| 3849 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3850 | 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] | 3851 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3852 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3853 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3854 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3855 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3856 | int err; |
| 3857 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3858 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3859 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3860 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3861 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3862 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3863 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3864 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3865 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3866 | goto unlock; |
| 3867 | } |
| 3868 | |
| 3869 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3870 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3871 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3872 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3873 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3874 | } |
| 3875 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3876 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3877 | if (!cmd) { |
| 3878 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3879 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3880 | } |
| 3881 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3882 | hci_req_init(&req, hdev); |
| 3883 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3884 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3885 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3886 | err = hci_req_run(&req, stop_discovery_complete); |
| 3887 | if (!err) { |
| 3888 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3889 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3890 | } |
| 3891 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3892 | mgmt_pending_remove(cmd); |
| 3893 | |
| 3894 | /* If no HCI commands were sent we're done */ |
| 3895 | if (err == -ENODATA) { |
| 3896 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3897 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3898 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3899 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3900 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3901 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3902 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3903 | return err; |
| 3904 | } |
| 3905 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3906 | 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] | 3907 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3908 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3909 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3910 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3911 | int err; |
| 3912 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3913 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3914 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3915 | hci_dev_lock(hdev); |
| 3916 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3917 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3918 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3919 | MGMT_STATUS_FAILED, &cp->addr, |
| 3920 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3921 | goto failed; |
| 3922 | } |
| 3923 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3924 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3925 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3926 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3927 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3928 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3929 | goto failed; |
| 3930 | } |
| 3931 | |
| 3932 | if (cp->name_known) { |
| 3933 | e->name_state = NAME_KNOWN; |
| 3934 | list_del(&e->list); |
| 3935 | } else { |
| 3936 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3937 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3938 | } |
| 3939 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3940 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3941 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3942 | |
| 3943 | failed: |
| 3944 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3945 | return err; |
| 3946 | } |
| 3947 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3948 | 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] | 3949 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3950 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3951 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3952 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3953 | int err; |
| 3954 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3955 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3956 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3957 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3958 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3959 | MGMT_STATUS_INVALID_PARAMS, |
| 3960 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3961 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3962 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3963 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 3964 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 3965 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3966 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3967 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3968 | goto done; |
| 3969 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3970 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3971 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3972 | sk); |
| 3973 | status = MGMT_STATUS_SUCCESS; |
| 3974 | |
| 3975 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3976 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3977 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3978 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3979 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3980 | |
| 3981 | return err; |
| 3982 | } |
| 3983 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3984 | 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] | 3985 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3986 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3987 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3988 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3989 | int err; |
| 3990 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3991 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3992 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3993 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3994 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3995 | MGMT_STATUS_INVALID_PARAMS, |
| 3996 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3997 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3998 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3999 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 4000 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 4001 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4002 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4003 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4004 | goto done; |
| 4005 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4006 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4007 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4008 | sk); |
| 4009 | status = MGMT_STATUS_SUCCESS; |
| 4010 | |
| 4011 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4012 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4013 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4014 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4015 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4016 | |
| 4017 | return err; |
| 4018 | } |
| 4019 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4020 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4021 | u16 len) |
| 4022 | { |
| 4023 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4024 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4025 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4026 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4027 | |
| 4028 | BT_DBG("%s", hdev->name); |
| 4029 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4030 | source = __le16_to_cpu(cp->source); |
| 4031 | |
| 4032 | if (source > 0x0002) |
| 4033 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4034 | MGMT_STATUS_INVALID_PARAMS); |
| 4035 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4036 | hci_dev_lock(hdev); |
| 4037 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4038 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4039 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4040 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4041 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4042 | |
| 4043 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 4044 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4045 | hci_req_init(&req, hdev); |
| 4046 | update_eir(&req); |
| 4047 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4048 | |
| 4049 | hci_dev_unlock(hdev); |
| 4050 | |
| 4051 | return err; |
| 4052 | } |
| 4053 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4054 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 4055 | { |
| 4056 | struct cmd_lookup match = { NULL, hdev }; |
| 4057 | |
| 4058 | if (status) { |
| 4059 | u8 mgmt_err = mgmt_status(status); |
| 4060 | |
| 4061 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4062 | cmd_status_rsp, &mgmt_err); |
| 4063 | return; |
| 4064 | } |
| 4065 | |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4066 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
| 4067 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4068 | else |
| 4069 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4070 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4071 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4072 | &match); |
| 4073 | |
| 4074 | new_settings(hdev, match.sk); |
| 4075 | |
| 4076 | if (match.sk) |
| 4077 | sock_put(match.sk); |
| 4078 | } |
| 4079 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4080 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4081 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4082 | { |
| 4083 | struct mgmt_mode *cp = data; |
| 4084 | struct pending_cmd *cmd; |
| 4085 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4086 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4087 | int err; |
| 4088 | |
| 4089 | BT_DBG("request for %s", hdev->name); |
| 4090 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4091 | status = mgmt_le_support(hdev); |
| 4092 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4093 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4094 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4095 | |
| 4096 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4097 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4098 | MGMT_STATUS_INVALID_PARAMS); |
| 4099 | |
| 4100 | hci_dev_lock(hdev); |
| 4101 | |
| 4102 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4103 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4104 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4105 | /* The following conditions are ones which mean that we should |
| 4106 | * not do any HCI communication but directly send a mgmt |
| 4107 | * response to user space (after toggling the flag if |
| 4108 | * necessary). |
| 4109 | */ |
| 4110 | if (!hdev_is_powered(hdev) || val == enabled || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4111 | hci_conn_num(hdev, LE_LINK) > 0 || |
| 4112 | (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4113 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4114 | bool changed = false; |
| 4115 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4116 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 4117 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4118 | changed = true; |
| 4119 | } |
| 4120 | |
| 4121 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4122 | if (err < 0) |
| 4123 | goto unlock; |
| 4124 | |
| 4125 | if (changed) |
| 4126 | err = new_settings(hdev, sk); |
| 4127 | |
| 4128 | goto unlock; |
| 4129 | } |
| 4130 | |
| 4131 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4132 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 4133 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4134 | MGMT_STATUS_BUSY); |
| 4135 | goto unlock; |
| 4136 | } |
| 4137 | |
| 4138 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4139 | if (!cmd) { |
| 4140 | err = -ENOMEM; |
| 4141 | goto unlock; |
| 4142 | } |
| 4143 | |
| 4144 | hci_req_init(&req, hdev); |
| 4145 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4146 | if (val) |
| 4147 | enable_advertising(&req); |
| 4148 | else |
| 4149 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4150 | |
| 4151 | err = hci_req_run(&req, set_advertising_complete); |
| 4152 | if (err < 0) |
| 4153 | mgmt_pending_remove(cmd); |
| 4154 | |
| 4155 | unlock: |
| 4156 | hci_dev_unlock(hdev); |
| 4157 | return err; |
| 4158 | } |
| 4159 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4160 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4161 | void *data, u16 len) |
| 4162 | { |
| 4163 | struct mgmt_cp_set_static_address *cp = data; |
| 4164 | int err; |
| 4165 | |
| 4166 | BT_DBG("%s", hdev->name); |
| 4167 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4168 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4169 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4170 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4171 | |
| 4172 | if (hdev_is_powered(hdev)) |
| 4173 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4174 | MGMT_STATUS_REJECTED); |
| 4175 | |
| 4176 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4177 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 4178 | return cmd_status(sk, hdev->id, |
| 4179 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4180 | MGMT_STATUS_INVALID_PARAMS); |
| 4181 | |
| 4182 | /* Two most significant bits shall be set */ |
| 4183 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4184 | return cmd_status(sk, hdev->id, |
| 4185 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4186 | MGMT_STATUS_INVALID_PARAMS); |
| 4187 | } |
| 4188 | |
| 4189 | hci_dev_lock(hdev); |
| 4190 | |
| 4191 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4192 | |
| 4193 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4194 | |
| 4195 | hci_dev_unlock(hdev); |
| 4196 | |
| 4197 | return err; |
| 4198 | } |
| 4199 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4200 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4201 | void *data, u16 len) |
| 4202 | { |
| 4203 | struct mgmt_cp_set_scan_params *cp = data; |
| 4204 | __u16 interval, window; |
| 4205 | int err; |
| 4206 | |
| 4207 | BT_DBG("%s", hdev->name); |
| 4208 | |
| 4209 | if (!lmp_le_capable(hdev)) |
| 4210 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4211 | MGMT_STATUS_NOT_SUPPORTED); |
| 4212 | |
| 4213 | interval = __le16_to_cpu(cp->interval); |
| 4214 | |
| 4215 | if (interval < 0x0004 || interval > 0x4000) |
| 4216 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4217 | MGMT_STATUS_INVALID_PARAMS); |
| 4218 | |
| 4219 | window = __le16_to_cpu(cp->window); |
| 4220 | |
| 4221 | if (window < 0x0004 || window > 0x4000) |
| 4222 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4223 | MGMT_STATUS_INVALID_PARAMS); |
| 4224 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4225 | if (window > interval) |
| 4226 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4227 | MGMT_STATUS_INVALID_PARAMS); |
| 4228 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4229 | hci_dev_lock(hdev); |
| 4230 | |
| 4231 | hdev->le_scan_interval = interval; |
| 4232 | hdev->le_scan_window = window; |
| 4233 | |
| 4234 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4235 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4236 | /* If background scan is running, restart it so new parameters are |
| 4237 | * loaded. |
| 4238 | */ |
| 4239 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4240 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4241 | struct hci_request req; |
| 4242 | |
| 4243 | hci_req_init(&req, hdev); |
| 4244 | |
| 4245 | hci_req_add_le_scan_disable(&req); |
| 4246 | hci_req_add_le_passive_scan(&req); |
| 4247 | |
| 4248 | hci_req_run(&req, NULL); |
| 4249 | } |
| 4250 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4251 | hci_dev_unlock(hdev); |
| 4252 | |
| 4253 | return err; |
| 4254 | } |
| 4255 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4256 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4257 | { |
| 4258 | struct pending_cmd *cmd; |
| 4259 | |
| 4260 | BT_DBG("status 0x%02x", status); |
| 4261 | |
| 4262 | hci_dev_lock(hdev); |
| 4263 | |
| 4264 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4265 | if (!cmd) |
| 4266 | goto unlock; |
| 4267 | |
| 4268 | if (status) { |
| 4269 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4270 | mgmt_status(status)); |
| 4271 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4272 | struct mgmt_mode *cp = cmd->param; |
| 4273 | |
| 4274 | if (cp->val) |
| 4275 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4276 | else |
| 4277 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4278 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4279 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4280 | new_settings(hdev, cmd->sk); |
| 4281 | } |
| 4282 | |
| 4283 | mgmt_pending_remove(cmd); |
| 4284 | |
| 4285 | unlock: |
| 4286 | hci_dev_unlock(hdev); |
| 4287 | } |
| 4288 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4289 | 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] | 4290 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4291 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4292 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4293 | struct pending_cmd *cmd; |
| 4294 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4295 | int err; |
| 4296 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4297 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4298 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4299 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4300 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4301 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4302 | MGMT_STATUS_NOT_SUPPORTED); |
| 4303 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4304 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4305 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4306 | MGMT_STATUS_INVALID_PARAMS); |
| 4307 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4308 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4309 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4310 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4311 | |
| 4312 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4313 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4314 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4315 | |
| 4316 | hci_dev_lock(hdev); |
| 4317 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4318 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4319 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4320 | MGMT_STATUS_BUSY); |
| 4321 | goto unlock; |
| 4322 | } |
| 4323 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4324 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4325 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4326 | hdev); |
| 4327 | goto unlock; |
| 4328 | } |
| 4329 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4330 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4331 | data, len); |
| 4332 | if (!cmd) { |
| 4333 | err = -ENOMEM; |
| 4334 | goto unlock; |
| 4335 | } |
| 4336 | |
| 4337 | hci_req_init(&req, hdev); |
| 4338 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4339 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4340 | |
| 4341 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4342 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4343 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4344 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4345 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4346 | } |
| 4347 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4348 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4349 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4350 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4351 | return err; |
| 4352 | } |
| 4353 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4354 | static void set_bredr_scan(struct hci_request *req) |
| 4355 | { |
| 4356 | struct hci_dev *hdev = req->hdev; |
| 4357 | u8 scan = 0; |
| 4358 | |
| 4359 | /* Ensure that fast connectable is disabled. This function will |
| 4360 | * not do anything if the page scan parameters are already what |
| 4361 | * they should be. |
| 4362 | */ |
| 4363 | write_fast_connectable(req, false); |
| 4364 | |
Johan Hedberg | 6cebb9e7 | 2014-07-09 12:59:18 +0300 | [diff] [blame] | 4365 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags) || |
| 4366 | !list_empty(&hdev->whitelist)) |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4367 | scan |= SCAN_PAGE; |
| 4368 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4369 | scan |= SCAN_INQUIRY; |
| 4370 | |
| 4371 | if (scan) |
| 4372 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4373 | } |
| 4374 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4375 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4376 | { |
| 4377 | struct pending_cmd *cmd; |
| 4378 | |
| 4379 | BT_DBG("status 0x%02x", status); |
| 4380 | |
| 4381 | hci_dev_lock(hdev); |
| 4382 | |
| 4383 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4384 | if (!cmd) |
| 4385 | goto unlock; |
| 4386 | |
| 4387 | if (status) { |
| 4388 | u8 mgmt_err = mgmt_status(status); |
| 4389 | |
| 4390 | /* We need to restore the flag if related HCI commands |
| 4391 | * failed. |
| 4392 | */ |
| 4393 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4394 | |
| 4395 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4396 | } else { |
| 4397 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4398 | new_settings(hdev, cmd->sk); |
| 4399 | } |
| 4400 | |
| 4401 | mgmt_pending_remove(cmd); |
| 4402 | |
| 4403 | unlock: |
| 4404 | hci_dev_unlock(hdev); |
| 4405 | } |
| 4406 | |
| 4407 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4408 | { |
| 4409 | struct mgmt_mode *cp = data; |
| 4410 | struct pending_cmd *cmd; |
| 4411 | struct hci_request req; |
| 4412 | int err; |
| 4413 | |
| 4414 | BT_DBG("request for %s", hdev->name); |
| 4415 | |
| 4416 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4417 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4418 | MGMT_STATUS_NOT_SUPPORTED); |
| 4419 | |
| 4420 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4421 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4422 | MGMT_STATUS_REJECTED); |
| 4423 | |
| 4424 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4425 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4426 | MGMT_STATUS_INVALID_PARAMS); |
| 4427 | |
| 4428 | hci_dev_lock(hdev); |
| 4429 | |
| 4430 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4431 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4432 | goto unlock; |
| 4433 | } |
| 4434 | |
| 4435 | if (!hdev_is_powered(hdev)) { |
| 4436 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4437 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4438 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4439 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4440 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4441 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4442 | } |
| 4443 | |
| 4444 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4445 | |
| 4446 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4447 | if (err < 0) |
| 4448 | goto unlock; |
| 4449 | |
| 4450 | err = new_settings(hdev, sk); |
| 4451 | goto unlock; |
| 4452 | } |
| 4453 | |
| 4454 | /* Reject disabling when powered on */ |
| 4455 | if (!cp->val) { |
| 4456 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4457 | MGMT_STATUS_REJECTED); |
| 4458 | goto unlock; |
| 4459 | } |
| 4460 | |
| 4461 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4462 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4463 | MGMT_STATUS_BUSY); |
| 4464 | goto unlock; |
| 4465 | } |
| 4466 | |
| 4467 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4468 | if (!cmd) { |
| 4469 | err = -ENOMEM; |
| 4470 | goto unlock; |
| 4471 | } |
| 4472 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4473 | /* 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] | 4474 | * generates the correct flags. |
| 4475 | */ |
| 4476 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4477 | |
| 4478 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4479 | |
Johan Hedberg | 6cebb9e7 | 2014-07-09 12:59:18 +0300 | [diff] [blame] | 4480 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags) || |
| 4481 | !list_empty(&hdev->whitelist)) |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4482 | set_bredr_scan(&req); |
| 4483 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4484 | /* Since only the advertising data flags will change, there |
| 4485 | * is no need to update the scan response data. |
| 4486 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4487 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4488 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4489 | err = hci_req_run(&req, set_bredr_complete); |
| 4490 | if (err < 0) |
| 4491 | mgmt_pending_remove(cmd); |
| 4492 | |
| 4493 | unlock: |
| 4494 | hci_dev_unlock(hdev); |
| 4495 | return err; |
| 4496 | } |
| 4497 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4498 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4499 | void *data, u16 len) |
| 4500 | { |
| 4501 | struct mgmt_mode *cp = data; |
| 4502 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4503 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4504 | int err; |
| 4505 | |
| 4506 | BT_DBG("request for %s", hdev->name); |
| 4507 | |
| 4508 | status = mgmt_bredr_support(hdev); |
| 4509 | if (status) |
| 4510 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4511 | status); |
| 4512 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4513 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4514 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4515 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4516 | MGMT_STATUS_NOT_SUPPORTED); |
| 4517 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4518 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4519 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4520 | MGMT_STATUS_INVALID_PARAMS); |
| 4521 | |
| 4522 | hci_dev_lock(hdev); |
| 4523 | |
| 4524 | if (!hdev_is_powered(hdev)) { |
| 4525 | bool changed; |
| 4526 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4527 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4528 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4529 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4530 | if (cp->val == 0x02) |
| 4531 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4532 | else |
| 4533 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4534 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4535 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4536 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4537 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4538 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4539 | |
| 4540 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4541 | if (err < 0) |
| 4542 | goto failed; |
| 4543 | |
| 4544 | if (changed) |
| 4545 | err = new_settings(hdev, sk); |
| 4546 | |
| 4547 | goto failed; |
| 4548 | } |
| 4549 | |
| 4550 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4551 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4552 | MGMT_STATUS_BUSY); |
| 4553 | goto failed; |
| 4554 | } |
| 4555 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4556 | val = !!cp->val; |
| 4557 | |
| 4558 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4559 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4560 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4561 | goto failed; |
| 4562 | } |
| 4563 | |
| 4564 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4565 | if (!cmd) { |
| 4566 | err = -ENOMEM; |
| 4567 | goto failed; |
| 4568 | } |
| 4569 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4570 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4571 | if (err < 0) { |
| 4572 | mgmt_pending_remove(cmd); |
| 4573 | goto failed; |
| 4574 | } |
| 4575 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4576 | if (cp->val == 0x02) |
| 4577 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4578 | else |
| 4579 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4580 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4581 | failed: |
| 4582 | hci_dev_unlock(hdev); |
| 4583 | return err; |
| 4584 | } |
| 4585 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4586 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4587 | void *data, u16 len) |
| 4588 | { |
| 4589 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4590 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4591 | int err; |
| 4592 | |
| 4593 | BT_DBG("request for %s", hdev->name); |
| 4594 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4595 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4596 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4597 | MGMT_STATUS_INVALID_PARAMS); |
| 4598 | |
| 4599 | hci_dev_lock(hdev); |
| 4600 | |
| 4601 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4602 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4603 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4604 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4605 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4606 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4607 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4608 | if (cp->val == 0x02) |
| 4609 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4610 | &hdev->dev_flags); |
| 4611 | else |
| 4612 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4613 | &hdev->dev_flags); |
| 4614 | |
| 4615 | if (hdev_is_powered(hdev) && use_changed && |
| 4616 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4617 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4618 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4619 | sizeof(mode), &mode); |
| 4620 | } |
| 4621 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4622 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4623 | if (err < 0) |
| 4624 | goto unlock; |
| 4625 | |
| 4626 | if (changed) |
| 4627 | err = new_settings(hdev, sk); |
| 4628 | |
| 4629 | unlock: |
| 4630 | hci_dev_unlock(hdev); |
| 4631 | return err; |
| 4632 | } |
| 4633 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4634 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4635 | u16 len) |
| 4636 | { |
| 4637 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4638 | bool changed; |
| 4639 | int err; |
| 4640 | |
| 4641 | BT_DBG("request for %s", hdev->name); |
| 4642 | |
| 4643 | if (!lmp_le_capable(hdev)) |
| 4644 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4645 | MGMT_STATUS_NOT_SUPPORTED); |
| 4646 | |
| 4647 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4648 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4649 | MGMT_STATUS_INVALID_PARAMS); |
| 4650 | |
| 4651 | if (hdev_is_powered(hdev)) |
| 4652 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4653 | MGMT_STATUS_REJECTED); |
| 4654 | |
| 4655 | hci_dev_lock(hdev); |
| 4656 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4657 | /* If user space supports this command it is also expected to |
| 4658 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4659 | */ |
| 4660 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4661 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4662 | if (cp->privacy) { |
| 4663 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4664 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4665 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4666 | } else { |
| 4667 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4668 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4669 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4670 | } |
| 4671 | |
| 4672 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4673 | if (err < 0) |
| 4674 | goto unlock; |
| 4675 | |
| 4676 | if (changed) |
| 4677 | err = new_settings(hdev, sk); |
| 4678 | |
| 4679 | unlock: |
| 4680 | hci_dev_unlock(hdev); |
| 4681 | return err; |
| 4682 | } |
| 4683 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4684 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4685 | { |
| 4686 | switch (irk->addr.type) { |
| 4687 | case BDADDR_LE_PUBLIC: |
| 4688 | return true; |
| 4689 | |
| 4690 | case BDADDR_LE_RANDOM: |
| 4691 | /* Two most significant bits shall be set */ |
| 4692 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4693 | return false; |
| 4694 | return true; |
| 4695 | } |
| 4696 | |
| 4697 | return false; |
| 4698 | } |
| 4699 | |
| 4700 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4701 | u16 len) |
| 4702 | { |
| 4703 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4704 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4705 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4706 | u16 irk_count, expected_len; |
| 4707 | int i, err; |
| 4708 | |
| 4709 | BT_DBG("request for %s", hdev->name); |
| 4710 | |
| 4711 | if (!lmp_le_capable(hdev)) |
| 4712 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4713 | MGMT_STATUS_NOT_SUPPORTED); |
| 4714 | |
| 4715 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4716 | if (irk_count > max_irk_count) { |
| 4717 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
| 4718 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4719 | MGMT_STATUS_INVALID_PARAMS); |
| 4720 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4721 | |
| 4722 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4723 | if (expected_len != len) { |
| 4724 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4725 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4726 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4727 | MGMT_STATUS_INVALID_PARAMS); |
| 4728 | } |
| 4729 | |
| 4730 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4731 | |
| 4732 | for (i = 0; i < irk_count; i++) { |
| 4733 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4734 | |
| 4735 | if (!irk_is_valid(key)) |
| 4736 | return cmd_status(sk, hdev->id, |
| 4737 | MGMT_OP_LOAD_IRKS, |
| 4738 | MGMT_STATUS_INVALID_PARAMS); |
| 4739 | } |
| 4740 | |
| 4741 | hci_dev_lock(hdev); |
| 4742 | |
| 4743 | hci_smp_irks_clear(hdev); |
| 4744 | |
| 4745 | for (i = 0; i < irk_count; i++) { |
| 4746 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4747 | u8 addr_type; |
| 4748 | |
| 4749 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4750 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4751 | else |
| 4752 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4753 | |
| 4754 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4755 | BDADDR_ANY); |
| 4756 | } |
| 4757 | |
| 4758 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4759 | |
| 4760 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4761 | |
| 4762 | hci_dev_unlock(hdev); |
| 4763 | |
| 4764 | return err; |
| 4765 | } |
| 4766 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4767 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4768 | { |
| 4769 | if (key->master != 0x00 && key->master != 0x01) |
| 4770 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4771 | |
| 4772 | switch (key->addr.type) { |
| 4773 | case BDADDR_LE_PUBLIC: |
| 4774 | return true; |
| 4775 | |
| 4776 | case BDADDR_LE_RANDOM: |
| 4777 | /* Two most significant bits shall be set */ |
| 4778 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4779 | return false; |
| 4780 | return true; |
| 4781 | } |
| 4782 | |
| 4783 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4784 | } |
| 4785 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4786 | 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] | 4787 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4788 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4789 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4790 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4791 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4792 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4793 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4794 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4795 | BT_DBG("request for %s", hdev->name); |
| 4796 | |
| 4797 | if (!lmp_le_capable(hdev)) |
| 4798 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4799 | MGMT_STATUS_NOT_SUPPORTED); |
| 4800 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4801 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4802 | if (key_count > max_key_count) { |
| 4803 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
| 4804 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4805 | MGMT_STATUS_INVALID_PARAMS); |
| 4806 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4807 | |
| 4808 | expected_len = sizeof(*cp) + key_count * |
| 4809 | sizeof(struct mgmt_ltk_info); |
| 4810 | if (expected_len != len) { |
| 4811 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4812 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4813 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4814 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4815 | } |
| 4816 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4817 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4818 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4819 | for (i = 0; i < key_count; i++) { |
| 4820 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4821 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4822 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4823 | return cmd_status(sk, hdev->id, |
| 4824 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4825 | MGMT_STATUS_INVALID_PARAMS); |
| 4826 | } |
| 4827 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4828 | hci_dev_lock(hdev); |
| 4829 | |
| 4830 | hci_smp_ltks_clear(hdev); |
| 4831 | |
| 4832 | for (i = 0; i < key_count; i++) { |
| 4833 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4834 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4835 | |
| 4836 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4837 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4838 | else |
| 4839 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4840 | |
| 4841 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4842 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4843 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4844 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4845 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4846 | switch (key->type) { |
| 4847 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4848 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4849 | break; |
| 4850 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4851 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4852 | break; |
| 4853 | default: |
| 4854 | continue; |
| 4855 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4856 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4857 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4858 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4859 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4860 | } |
| 4861 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4862 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4863 | NULL, 0); |
| 4864 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4865 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4866 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4867 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4868 | } |
| 4869 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4870 | struct cmd_conn_lookup { |
| 4871 | struct hci_conn *conn; |
| 4872 | bool valid_tx_power; |
| 4873 | u8 mgmt_status; |
| 4874 | }; |
| 4875 | |
| 4876 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4877 | { |
| 4878 | struct cmd_conn_lookup *match = data; |
| 4879 | struct mgmt_cp_get_conn_info *cp; |
| 4880 | struct mgmt_rp_get_conn_info rp; |
| 4881 | struct hci_conn *conn = cmd->user_data; |
| 4882 | |
| 4883 | if (conn != match->conn) |
| 4884 | return; |
| 4885 | |
| 4886 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4887 | |
| 4888 | memset(&rp, 0, sizeof(rp)); |
| 4889 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4890 | rp.addr.type = cp->addr.type; |
| 4891 | |
| 4892 | if (!match->mgmt_status) { |
| 4893 | rp.rssi = conn->rssi; |
| 4894 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4895 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4896 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4897 | rp.max_tx_power = conn->max_tx_power; |
| 4898 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4899 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4900 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4901 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4905 | match->mgmt_status, &rp, sizeof(rp)); |
| 4906 | |
| 4907 | hci_conn_drop(conn); |
| 4908 | |
| 4909 | mgmt_pending_remove(cmd); |
| 4910 | } |
| 4911 | |
| 4912 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4913 | { |
| 4914 | struct hci_cp_read_rssi *cp; |
| 4915 | struct hci_conn *conn; |
| 4916 | struct cmd_conn_lookup match; |
| 4917 | u16 handle; |
| 4918 | |
| 4919 | BT_DBG("status 0x%02x", status); |
| 4920 | |
| 4921 | hci_dev_lock(hdev); |
| 4922 | |
| 4923 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4924 | * otherwise we assume it's not valid. At the moment we assume that |
| 4925 | * either both or none of current and max values are valid to keep code |
| 4926 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4927 | */ |
| 4928 | match.valid_tx_power = !status; |
| 4929 | |
| 4930 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4931 | * Level so we check which one was last sent to retrieve connection |
| 4932 | * handle. Both commands have handle as first parameter so it's safe to |
| 4933 | * cast data on the same command struct. |
| 4934 | * |
| 4935 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4936 | * In other case we simply override error to indicate success as we |
| 4937 | * already remembered if TX power value is actually valid. |
| 4938 | */ |
| 4939 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4940 | if (!cp) { |
| 4941 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4942 | status = 0; |
| 4943 | } |
| 4944 | |
| 4945 | if (!cp) { |
| 4946 | BT_ERR("invalid sent_cmd in response"); |
| 4947 | goto unlock; |
| 4948 | } |
| 4949 | |
| 4950 | handle = __le16_to_cpu(cp->handle); |
| 4951 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4952 | if (!conn) { |
| 4953 | BT_ERR("unknown handle (%d) in response", handle); |
| 4954 | goto unlock; |
| 4955 | } |
| 4956 | |
| 4957 | match.conn = conn; |
| 4958 | match.mgmt_status = mgmt_status(status); |
| 4959 | |
| 4960 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4961 | * connection only. |
| 4962 | */ |
| 4963 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4964 | get_conn_info_complete, &match); |
| 4965 | |
| 4966 | unlock: |
| 4967 | hci_dev_unlock(hdev); |
| 4968 | } |
| 4969 | |
| 4970 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4971 | u16 len) |
| 4972 | { |
| 4973 | struct mgmt_cp_get_conn_info *cp = data; |
| 4974 | struct mgmt_rp_get_conn_info rp; |
| 4975 | struct hci_conn *conn; |
| 4976 | unsigned long conn_info_age; |
| 4977 | int err = 0; |
| 4978 | |
| 4979 | BT_DBG("%s", hdev->name); |
| 4980 | |
| 4981 | memset(&rp, 0, sizeof(rp)); |
| 4982 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4983 | rp.addr.type = cp->addr.type; |
| 4984 | |
| 4985 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4986 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4987 | MGMT_STATUS_INVALID_PARAMS, |
| 4988 | &rp, sizeof(rp)); |
| 4989 | |
| 4990 | hci_dev_lock(hdev); |
| 4991 | |
| 4992 | if (!hdev_is_powered(hdev)) { |
| 4993 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4994 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4995 | goto unlock; |
| 4996 | } |
| 4997 | |
| 4998 | if (cp->addr.type == BDADDR_BREDR) |
| 4999 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5000 | &cp->addr.bdaddr); |
| 5001 | else |
| 5002 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 5003 | |
| 5004 | if (!conn || conn->state != BT_CONNECTED) { |
| 5005 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5006 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 5007 | goto unlock; |
| 5008 | } |
| 5009 | |
| 5010 | /* To avoid client trying to guess when to poll again for information we |
| 5011 | * calculate conn info age as random value between min/max set in hdev. |
| 5012 | */ |
| 5013 | conn_info_age = hdev->conn_info_min_age + |
| 5014 | prandom_u32_max(hdev->conn_info_max_age - |
| 5015 | hdev->conn_info_min_age); |
| 5016 | |
| 5017 | /* Query controller to refresh cached values if they are too old or were |
| 5018 | * never read. |
| 5019 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5020 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5021 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5022 | !conn->conn_info_timestamp) { |
| 5023 | struct hci_request req; |
| 5024 | struct hci_cp_read_tx_power req_txp_cp; |
| 5025 | struct hci_cp_read_rssi req_rssi_cp; |
| 5026 | struct pending_cmd *cmd; |
| 5027 | |
| 5028 | hci_req_init(&req, hdev); |
| 5029 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5030 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5031 | &req_rssi_cp); |
| 5032 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5033 | /* For LE links TX power does not change thus we don't need to |
| 5034 | * query for it once value is known. |
| 5035 | */ |
| 5036 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5037 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5038 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5039 | req_txp_cp.type = 0x00; |
| 5040 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5041 | sizeof(req_txp_cp), &req_txp_cp); |
| 5042 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5043 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5044 | /* Max TX power needs to be read only once per connection */ |
| 5045 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5046 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5047 | req_txp_cp.type = 0x01; |
| 5048 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5049 | sizeof(req_txp_cp), &req_txp_cp); |
| 5050 | } |
| 5051 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5052 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5053 | if (err < 0) |
| 5054 | goto unlock; |
| 5055 | |
| 5056 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5057 | data, len); |
| 5058 | if (!cmd) { |
| 5059 | err = -ENOMEM; |
| 5060 | goto unlock; |
| 5061 | } |
| 5062 | |
| 5063 | hci_conn_hold(conn); |
| 5064 | cmd->user_data = conn; |
| 5065 | |
| 5066 | conn->conn_info_timestamp = jiffies; |
| 5067 | } else { |
| 5068 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5069 | rp.rssi = conn->rssi; |
| 5070 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5071 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5072 | |
| 5073 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5074 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 5075 | } |
| 5076 | |
| 5077 | unlock: |
| 5078 | hci_dev_unlock(hdev); |
| 5079 | return err; |
| 5080 | } |
| 5081 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5082 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 5083 | { |
| 5084 | struct mgmt_cp_get_clock_info *cp; |
| 5085 | struct mgmt_rp_get_clock_info rp; |
| 5086 | struct hci_cp_read_clock *hci_cp; |
| 5087 | struct pending_cmd *cmd; |
| 5088 | struct hci_conn *conn; |
| 5089 | |
| 5090 | BT_DBG("%s status %u", hdev->name, status); |
| 5091 | |
| 5092 | hci_dev_lock(hdev); |
| 5093 | |
| 5094 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5095 | if (!hci_cp) |
| 5096 | goto unlock; |
| 5097 | |
| 5098 | if (hci_cp->which) { |
| 5099 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5100 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5101 | } else { |
| 5102 | conn = NULL; |
| 5103 | } |
| 5104 | |
| 5105 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5106 | if (!cmd) |
| 5107 | goto unlock; |
| 5108 | |
| 5109 | cp = cmd->param; |
| 5110 | |
| 5111 | memset(&rp, 0, sizeof(rp)); |
| 5112 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 5113 | |
| 5114 | if (status) |
| 5115 | goto send_rsp; |
| 5116 | |
| 5117 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5118 | |
| 5119 | if (conn) { |
| 5120 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5121 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5122 | } |
| 5123 | |
| 5124 | send_rsp: |
| 5125 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 5126 | &rp, sizeof(rp)); |
| 5127 | mgmt_pending_remove(cmd); |
| 5128 | if (conn) |
| 5129 | hci_conn_drop(conn); |
| 5130 | |
| 5131 | unlock: |
| 5132 | hci_dev_unlock(hdev); |
| 5133 | } |
| 5134 | |
| 5135 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5136 | u16 len) |
| 5137 | { |
| 5138 | struct mgmt_cp_get_clock_info *cp = data; |
| 5139 | struct mgmt_rp_get_clock_info rp; |
| 5140 | struct hci_cp_read_clock hci_cp; |
| 5141 | struct pending_cmd *cmd; |
| 5142 | struct hci_request req; |
| 5143 | struct hci_conn *conn; |
| 5144 | int err; |
| 5145 | |
| 5146 | BT_DBG("%s", hdev->name); |
| 5147 | |
| 5148 | memset(&rp, 0, sizeof(rp)); |
| 5149 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5150 | rp.addr.type = cp->addr.type; |
| 5151 | |
| 5152 | if (cp->addr.type != BDADDR_BREDR) |
| 5153 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5154 | MGMT_STATUS_INVALID_PARAMS, |
| 5155 | &rp, sizeof(rp)); |
| 5156 | |
| 5157 | hci_dev_lock(hdev); |
| 5158 | |
| 5159 | if (!hdev_is_powered(hdev)) { |
| 5160 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5161 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5162 | goto unlock; |
| 5163 | } |
| 5164 | |
| 5165 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5166 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5167 | &cp->addr.bdaddr); |
| 5168 | if (!conn || conn->state != BT_CONNECTED) { |
| 5169 | err = cmd_complete(sk, hdev->id, |
| 5170 | MGMT_OP_GET_CLOCK_INFO, |
| 5171 | MGMT_STATUS_NOT_CONNECTED, |
| 5172 | &rp, sizeof(rp)); |
| 5173 | goto unlock; |
| 5174 | } |
| 5175 | } else { |
| 5176 | conn = NULL; |
| 5177 | } |
| 5178 | |
| 5179 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5180 | if (!cmd) { |
| 5181 | err = -ENOMEM; |
| 5182 | goto unlock; |
| 5183 | } |
| 5184 | |
| 5185 | hci_req_init(&req, hdev); |
| 5186 | |
| 5187 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5188 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5189 | |
| 5190 | if (conn) { |
| 5191 | hci_conn_hold(conn); |
| 5192 | cmd->user_data = conn; |
| 5193 | |
| 5194 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5195 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5196 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5197 | } |
| 5198 | |
| 5199 | err = hci_req_run(&req, get_clock_info_complete); |
| 5200 | if (err < 0) |
| 5201 | mgmt_pending_remove(cmd); |
| 5202 | |
| 5203 | unlock: |
| 5204 | hci_dev_unlock(hdev); |
| 5205 | return err; |
| 5206 | } |
| 5207 | |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5208 | /* Helper for Add/Remove Device commands */ |
| 5209 | static void update_page_scan(struct hci_dev *hdev, u8 scan) |
| 5210 | { |
| 5211 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5212 | return; |
| 5213 | |
| 5214 | if (!hdev_is_powered(hdev)) |
| 5215 | return; |
| 5216 | |
| 5217 | /* If HCI_CONNECTABLE is set then Add/Remove Device should not |
| 5218 | * make any changes to page scanning. |
| 5219 | */ |
| 5220 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 5221 | return; |
| 5222 | |
| 5223 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 5224 | scan |= SCAN_INQUIRY; |
| 5225 | |
| 5226 | hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 5227 | } |
| 5228 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5229 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5230 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5231 | { |
| 5232 | struct mgmt_ev_device_added ev; |
| 5233 | |
| 5234 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5235 | ev.addr.type = type; |
| 5236 | ev.action = action; |
| 5237 | |
| 5238 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5239 | } |
| 5240 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5241 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5242 | void *data, u16 len) |
| 5243 | { |
| 5244 | struct mgmt_cp_add_device *cp = data; |
| 5245 | u8 auto_conn, addr_type; |
| 5246 | int err; |
| 5247 | |
| 5248 | BT_DBG("%s", hdev->name); |
| 5249 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5250 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5251 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5252 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5253 | MGMT_STATUS_INVALID_PARAMS, |
| 5254 | &cp->addr, sizeof(cp->addr)); |
| 5255 | |
| 5256 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5257 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5258 | MGMT_STATUS_INVALID_PARAMS, |
| 5259 | &cp->addr, sizeof(cp->addr)); |
| 5260 | |
| 5261 | hci_dev_lock(hdev); |
| 5262 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5263 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5264 | bool update_scan; |
| 5265 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5266 | /* Only "connect" action supported for now */ |
| 5267 | if (cp->action != 0x01) { |
| 5268 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5269 | MGMT_STATUS_INVALID_PARAMS, |
| 5270 | &cp->addr, sizeof(cp->addr)); |
| 5271 | goto unlock; |
| 5272 | } |
| 5273 | |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5274 | update_scan = list_empty(&hdev->whitelist); |
| 5275 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5276 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5277 | cp->addr.type); |
| 5278 | if (err) |
| 5279 | goto unlock; |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5280 | |
| 5281 | if (update_scan) |
| 5282 | update_page_scan(hdev, SCAN_PAGE); |
| 5283 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5284 | goto added; |
| 5285 | } |
| 5286 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5287 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5288 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5289 | else |
| 5290 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5291 | |
| 5292 | if (cp->action) |
| 5293 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5294 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5295 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5296 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5297 | /* If the connection parameters don't exist for this device, |
| 5298 | * they will be created and configured with defaults. |
| 5299 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5300 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5301 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5302 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5303 | MGMT_STATUS_FAILED, |
| 5304 | &cp->addr, sizeof(cp->addr)); |
| 5305 | goto unlock; |
| 5306 | } |
| 5307 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5308 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5309 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5310 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5311 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5312 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5313 | |
| 5314 | unlock: |
| 5315 | hci_dev_unlock(hdev); |
| 5316 | return err; |
| 5317 | } |
| 5318 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5319 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5320 | bdaddr_t *bdaddr, u8 type) |
| 5321 | { |
| 5322 | struct mgmt_ev_device_removed ev; |
| 5323 | |
| 5324 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5325 | ev.addr.type = type; |
| 5326 | |
| 5327 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5328 | } |
| 5329 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5330 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5331 | void *data, u16 len) |
| 5332 | { |
| 5333 | struct mgmt_cp_remove_device *cp = data; |
| 5334 | int err; |
| 5335 | |
| 5336 | BT_DBG("%s", hdev->name); |
| 5337 | |
| 5338 | hci_dev_lock(hdev); |
| 5339 | |
| 5340 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5341 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5342 | u8 addr_type; |
| 5343 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5344 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5345 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5346 | MGMT_STATUS_INVALID_PARAMS, |
| 5347 | &cp->addr, sizeof(cp->addr)); |
| 5348 | goto unlock; |
| 5349 | } |
| 5350 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5351 | if (cp->addr.type == BDADDR_BREDR) { |
| 5352 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5353 | &cp->addr.bdaddr, |
| 5354 | cp->addr.type); |
| 5355 | if (err) { |
| 5356 | err = cmd_complete(sk, hdev->id, |
| 5357 | MGMT_OP_REMOVE_DEVICE, |
| 5358 | MGMT_STATUS_INVALID_PARAMS, |
| 5359 | &cp->addr, sizeof(cp->addr)); |
| 5360 | goto unlock; |
| 5361 | } |
| 5362 | |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5363 | if (list_empty(&hdev->whitelist)) |
| 5364 | update_page_scan(hdev, SCAN_DISABLED); |
| 5365 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5366 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5367 | cp->addr.type); |
| 5368 | goto complete; |
| 5369 | } |
| 5370 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5371 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5372 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5373 | else |
| 5374 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5375 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5376 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5377 | addr_type); |
| 5378 | if (!params) { |
| 5379 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5380 | MGMT_STATUS_INVALID_PARAMS, |
| 5381 | &cp->addr, sizeof(cp->addr)); |
| 5382 | goto unlock; |
| 5383 | } |
| 5384 | |
| 5385 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5386 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5387 | MGMT_STATUS_INVALID_PARAMS, |
| 5388 | &cp->addr, sizeof(cp->addr)); |
| 5389 | goto unlock; |
| 5390 | } |
| 5391 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5392 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5393 | list_del(¶ms->list); |
| 5394 | kfree(params); |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame] | 5395 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5396 | |
| 5397 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5398 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5399 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5400 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5401 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5402 | if (cp->addr.type) { |
| 5403 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5404 | MGMT_STATUS_INVALID_PARAMS, |
| 5405 | &cp->addr, sizeof(cp->addr)); |
| 5406 | goto unlock; |
| 5407 | } |
| 5408 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5409 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 5410 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 5411 | list_del(&b->list); |
| 5412 | kfree(b); |
| 5413 | } |
| 5414 | |
Johan Hedberg | a397407 | 2014-07-09 12:59:15 +0300 | [diff] [blame] | 5415 | update_page_scan(hdev, SCAN_DISABLED); |
| 5416 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5417 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5418 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5419 | continue; |
| 5420 | device_removed(sk, hdev, &p->addr, p->addr_type); |
| 5421 | list_del(&p->action); |
| 5422 | list_del(&p->list); |
| 5423 | kfree(p); |
| 5424 | } |
| 5425 | |
| 5426 | BT_DBG("All LE connection parameters were removed"); |
| 5427 | |
| 5428 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5429 | } |
| 5430 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame] | 5431 | complete: |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5432 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5433 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5434 | |
| 5435 | unlock: |
| 5436 | hci_dev_unlock(hdev); |
| 5437 | return err; |
| 5438 | } |
| 5439 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5440 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5441 | u16 len) |
| 5442 | { |
| 5443 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5444 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5445 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5446 | u16 param_count, expected_len; |
| 5447 | int i; |
| 5448 | |
| 5449 | if (!lmp_le_capable(hdev)) |
| 5450 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5451 | MGMT_STATUS_NOT_SUPPORTED); |
| 5452 | |
| 5453 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5454 | if (param_count > max_param_count) { |
| 5455 | BT_ERR("load_conn_param: too big param_count value %u", |
| 5456 | param_count); |
| 5457 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5458 | MGMT_STATUS_INVALID_PARAMS); |
| 5459 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5460 | |
| 5461 | expected_len = sizeof(*cp) + param_count * |
| 5462 | sizeof(struct mgmt_conn_param); |
| 5463 | if (expected_len != len) { |
| 5464 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5465 | expected_len, len); |
| 5466 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5467 | MGMT_STATUS_INVALID_PARAMS); |
| 5468 | } |
| 5469 | |
| 5470 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5471 | |
| 5472 | hci_dev_lock(hdev); |
| 5473 | |
| 5474 | hci_conn_params_clear_disabled(hdev); |
| 5475 | |
| 5476 | for (i = 0; i < param_count; i++) { |
| 5477 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5478 | struct hci_conn_params *hci_param; |
| 5479 | u16 min, max, latency, timeout; |
| 5480 | u8 addr_type; |
| 5481 | |
| 5482 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5483 | param->addr.type); |
| 5484 | |
| 5485 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5486 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5487 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5488 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5489 | } else { |
| 5490 | BT_ERR("Ignoring invalid connection parameters"); |
| 5491 | continue; |
| 5492 | } |
| 5493 | |
| 5494 | min = le16_to_cpu(param->min_interval); |
| 5495 | max = le16_to_cpu(param->max_interval); |
| 5496 | latency = le16_to_cpu(param->latency); |
| 5497 | timeout = le16_to_cpu(param->timeout); |
| 5498 | |
| 5499 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5500 | min, max, latency, timeout); |
| 5501 | |
| 5502 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5503 | BT_ERR("Ignoring invalid connection parameters"); |
| 5504 | continue; |
| 5505 | } |
| 5506 | |
| 5507 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5508 | addr_type); |
| 5509 | if (!hci_param) { |
| 5510 | BT_ERR("Failed to add connection parameters"); |
| 5511 | continue; |
| 5512 | } |
| 5513 | |
| 5514 | hci_param->conn_min_interval = min; |
| 5515 | hci_param->conn_max_interval = max; |
| 5516 | hci_param->conn_latency = latency; |
| 5517 | hci_param->supervision_timeout = timeout; |
| 5518 | } |
| 5519 | |
| 5520 | hci_dev_unlock(hdev); |
| 5521 | |
| 5522 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5523 | } |
| 5524 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5525 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5526 | void *data, u16 len) |
| 5527 | { |
| 5528 | struct mgmt_cp_set_external_config *cp = data; |
| 5529 | bool changed; |
| 5530 | int err; |
| 5531 | |
| 5532 | BT_DBG("%s", hdev->name); |
| 5533 | |
| 5534 | if (hdev_is_powered(hdev)) |
| 5535 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5536 | MGMT_STATUS_REJECTED); |
| 5537 | |
| 5538 | if (cp->config != 0x00 && cp->config != 0x01) |
| 5539 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5540 | MGMT_STATUS_INVALID_PARAMS); |
| 5541 | |
| 5542 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 5543 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5544 | MGMT_STATUS_NOT_SUPPORTED); |
| 5545 | |
| 5546 | hci_dev_lock(hdev); |
| 5547 | |
| 5548 | if (cp->config) |
| 5549 | changed = !test_and_set_bit(HCI_EXT_CONFIGURED, |
| 5550 | &hdev->dev_flags); |
| 5551 | else |
| 5552 | changed = test_and_clear_bit(HCI_EXT_CONFIGURED, |
| 5553 | &hdev->dev_flags); |
| 5554 | |
| 5555 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5556 | if (err < 0) |
| 5557 | goto unlock; |
| 5558 | |
| 5559 | if (!changed) |
| 5560 | goto unlock; |
| 5561 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 5562 | err = new_options(hdev, sk); |
| 5563 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5564 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) { |
| 5565 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5566 | |
| 5567 | if (test_and_change_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { |
| 5568 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5569 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5570 | |
| 5571 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5572 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 5573 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5574 | mgmt_index_added(hdev); |
| 5575 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5576 | } |
| 5577 | |
| 5578 | unlock: |
| 5579 | hci_dev_unlock(hdev); |
| 5580 | return err; |
| 5581 | } |
| 5582 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5583 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 5584 | void *data, u16 len) |
| 5585 | { |
| 5586 | struct mgmt_cp_set_public_address *cp = data; |
| 5587 | bool changed; |
| 5588 | int err; |
| 5589 | |
| 5590 | BT_DBG("%s", hdev->name); |
| 5591 | |
| 5592 | if (hdev_is_powered(hdev)) |
| 5593 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5594 | MGMT_STATUS_REJECTED); |
| 5595 | |
| 5596 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
| 5597 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5598 | MGMT_STATUS_INVALID_PARAMS); |
| 5599 | |
| 5600 | if (!hdev->set_bdaddr) |
| 5601 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5602 | MGMT_STATUS_NOT_SUPPORTED); |
| 5603 | |
| 5604 | hci_dev_lock(hdev); |
| 5605 | |
| 5606 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 5607 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 5608 | |
| 5609 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 5610 | if (err < 0) |
| 5611 | goto unlock; |
| 5612 | |
| 5613 | if (!changed) |
| 5614 | goto unlock; |
| 5615 | |
| 5616 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5617 | err = new_options(hdev, sk); |
| 5618 | |
| 5619 | if (is_configured(hdev)) { |
| 5620 | mgmt_index_removed(hdev); |
| 5621 | |
| 5622 | clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags); |
| 5623 | |
| 5624 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5625 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5626 | |
| 5627 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5628 | } |
| 5629 | |
| 5630 | unlock: |
| 5631 | hci_dev_unlock(hdev); |
| 5632 | return err; |
| 5633 | } |
| 5634 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5635 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5636 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5637 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5638 | bool var_len; |
| 5639 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5640 | } mgmt_handlers[] = { |
| 5641 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5642 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5643 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5644 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5645 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5646 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5647 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5648 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5649 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5650 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5651 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5652 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5653 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5654 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5655 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5656 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5657 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5658 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5659 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5660 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5661 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5662 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5663 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5664 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5665 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5666 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5667 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5668 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5669 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5670 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5671 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5672 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5673 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5674 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5675 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5676 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5677 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5678 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5679 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5680 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5681 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5682 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5683 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5684 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5685 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5686 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5687 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5688 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5689 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5690 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5691 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5692 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5693 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5694 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
| 5695 | { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE }, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 5696 | { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE }, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5697 | { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE }, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5698 | { set_public_address, false, MGMT_SET_PUBLIC_ADDRESS_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5699 | }; |
| 5700 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5701 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5702 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5703 | void *buf; |
| 5704 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5705 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5706 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5707 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5708 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5709 | int err; |
| 5710 | |
| 5711 | BT_DBG("got %zu bytes", msglen); |
| 5712 | |
| 5713 | if (msglen < sizeof(*hdr)) |
| 5714 | return -EINVAL; |
| 5715 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5716 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5717 | if (!buf) |
| 5718 | return -ENOMEM; |
| 5719 | |
| 5720 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5721 | err = -EFAULT; |
| 5722 | goto done; |
| 5723 | } |
| 5724 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5725 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5726 | opcode = __le16_to_cpu(hdr->opcode); |
| 5727 | index = __le16_to_cpu(hdr->index); |
| 5728 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5729 | |
| 5730 | if (len != msglen - sizeof(*hdr)) { |
| 5731 | err = -EINVAL; |
| 5732 | goto done; |
| 5733 | } |
| 5734 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5735 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5736 | hdev = hci_dev_get(index); |
| 5737 | if (!hdev) { |
| 5738 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5739 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5740 | goto done; |
| 5741 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5742 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5743 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5744 | test_bit(HCI_CONFIG, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5745 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5746 | err = cmd_status(sk, index, opcode, |
| 5747 | MGMT_STATUS_INVALID_INDEX); |
| 5748 | goto done; |
| 5749 | } |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5750 | |
| 5751 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5752 | opcode != MGMT_OP_READ_CONFIG_INFO && |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5753 | opcode != MGMT_OP_SET_EXTERNAL_CONFIG && |
| 5754 | opcode != MGMT_OP_SET_PUBLIC_ADDRESS) { |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5755 | err = cmd_status(sk, index, opcode, |
| 5756 | MGMT_STATUS_INVALID_INDEX); |
| 5757 | goto done; |
| 5758 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5759 | } |
| 5760 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5761 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5762 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5763 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5764 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5765 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5766 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5767 | } |
| 5768 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5769 | if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || |
| 5770 | opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
| 5771 | err = cmd_status(sk, index, opcode, |
| 5772 | MGMT_STATUS_INVALID_INDEX); |
| 5773 | goto done; |
| 5774 | } |
| 5775 | |
| 5776 | if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && |
| 5777 | opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5778 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5779 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5780 | goto done; |
| 5781 | } |
| 5782 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5783 | handler = &mgmt_handlers[opcode]; |
| 5784 | |
| 5785 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5786 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5787 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5788 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5789 | goto done; |
| 5790 | } |
| 5791 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5792 | if (hdev) |
| 5793 | mgmt_init_hdev(sk, hdev); |
| 5794 | |
| 5795 | cp = buf + sizeof(*hdr); |
| 5796 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5797 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5798 | if (err < 0) |
| 5799 | goto done; |
| 5800 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5801 | err = msglen; |
| 5802 | |
| 5803 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5804 | if (hdev) |
| 5805 | hci_dev_put(hdev); |
| 5806 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5807 | kfree(buf); |
| 5808 | return err; |
| 5809 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5810 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5811 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5812 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5813 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5814 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5815 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5816 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5817 | return; |
| 5818 | |
| 5819 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5820 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5821 | else |
| 5822 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5823 | } |
| 5824 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5825 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5826 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5827 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5828 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5829 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5830 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5831 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5832 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5833 | return; |
| 5834 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5835 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5836 | |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 5837 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5838 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5839 | else |
| 5840 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5841 | } |
| 5842 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5843 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5844 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5845 | { |
| 5846 | struct hci_conn_params *p; |
| 5847 | |
| 5848 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5849 | /* Needed for AUTO_OFF case where might not "really" |
| 5850 | * have been powered off. |
| 5851 | */ |
| 5852 | list_del_init(&p->action); |
| 5853 | |
| 5854 | switch (p->auto_connect) { |
| 5855 | case HCI_AUTO_CONN_ALWAYS: |
| 5856 | list_add(&p->action, &hdev->pend_le_conns); |
| 5857 | break; |
| 5858 | case HCI_AUTO_CONN_REPORT: |
| 5859 | list_add(&p->action, &hdev->pend_le_reports); |
| 5860 | break; |
| 5861 | default: |
| 5862 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5863 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5864 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5865 | |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5866 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5867 | } |
| 5868 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5869 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5870 | { |
| 5871 | struct cmd_lookup match = { NULL, hdev }; |
| 5872 | |
| 5873 | BT_DBG("status 0x%02x", status); |
| 5874 | |
| 5875 | hci_dev_lock(hdev); |
| 5876 | |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5877 | restart_le_actions(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5878 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5879 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5880 | |
| 5881 | new_settings(hdev, match.sk); |
| 5882 | |
| 5883 | hci_dev_unlock(hdev); |
| 5884 | |
| 5885 | if (match.sk) |
| 5886 | sock_put(match.sk); |
| 5887 | } |
| 5888 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5889 | static int powered_update_hci(struct hci_dev *hdev) |
| 5890 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5891 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5892 | u8 link_sec; |
| 5893 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5894 | hci_req_init(&req, hdev); |
| 5895 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5896 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5897 | !lmp_host_ssp_capable(hdev)) { |
| 5898 | u8 ssp = 1; |
| 5899 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5900 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5901 | } |
| 5902 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5903 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5904 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5905 | struct hci_cp_write_le_host_supported cp; |
| 5906 | |
| 5907 | cp.le = 1; |
| 5908 | cp.simul = lmp_le_br_capable(hdev); |
| 5909 | |
| 5910 | /* Check first if we already have the right |
| 5911 | * host state (host features set) |
| 5912 | */ |
| 5913 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5914 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5915 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5916 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5917 | } |
| 5918 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5919 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5920 | /* Make sure the controller has a good default for |
| 5921 | * advertising data. This also applies to the case |
| 5922 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5923 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5924 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5925 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5926 | update_scan_rsp_data(&req); |
| 5927 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5928 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5929 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5930 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5931 | } |
| 5932 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5933 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5934 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5935 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5936 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5937 | |
| 5938 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5939 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5940 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5941 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5942 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5943 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5944 | } |
| 5945 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5946 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5947 | } |
| 5948 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5949 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5950 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5951 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5952 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5953 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5954 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5955 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5956 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5957 | return 0; |
| 5958 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5959 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5960 | if (powered_update_hci(hdev) == 0) |
| 5961 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5962 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5963 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5964 | &match); |
| 5965 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5966 | } |
| 5967 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5968 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5969 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5970 | |
| 5971 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5972 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5973 | zero_cod, sizeof(zero_cod), NULL); |
| 5974 | |
| 5975 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5976 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5977 | |
| 5978 | if (match.sk) |
| 5979 | sock_put(match.sk); |
| 5980 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5981 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5982 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5983 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5984 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5985 | { |
| 5986 | struct pending_cmd *cmd; |
| 5987 | u8 status; |
| 5988 | |
| 5989 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5990 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5991 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5992 | |
| 5993 | if (err == -ERFKILL) |
| 5994 | status = MGMT_STATUS_RFKILLED; |
| 5995 | else |
| 5996 | status = MGMT_STATUS_FAILED; |
| 5997 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5998 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5999 | |
| 6000 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 6001 | } |
| 6002 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6003 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 6004 | { |
| 6005 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6006 | |
| 6007 | hci_dev_lock(hdev); |
| 6008 | |
| 6009 | /* When discoverable timeout triggers, then just make sure |
| 6010 | * the limited discoverable flag is cleared. Even in the case |
| 6011 | * of a timeout triggered from general discoverable, it is |
| 6012 | * safe to unconditionally clear the flag. |
| 6013 | */ |
| 6014 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6015 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6016 | |
| 6017 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 6018 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 6019 | u8 scan = SCAN_PAGE; |
| 6020 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 6021 | sizeof(scan), &scan); |
| 6022 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6023 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6024 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6025 | hci_req_run(&req, NULL); |
| 6026 | |
| 6027 | hdev->discov_timeout = 0; |
| 6028 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6029 | new_settings(hdev, NULL); |
| 6030 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 6031 | hci_dev_unlock(hdev); |
| 6032 | } |
| 6033 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6034 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6035 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6036 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6037 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 6038 | /* Nothing needed here if there's a pending command since that |
| 6039 | * commands request completion callback takes care of everything |
| 6040 | * necessary. |
| 6041 | */ |
| 6042 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6043 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 6044 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 6045 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 6046 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 6047 | return; |
| 6048 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6049 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6050 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6051 | } else { |
| 6052 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6053 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6054 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6055 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6056 | if (changed) { |
| 6057 | struct hci_request req; |
| 6058 | |
| 6059 | /* In case this change in discoverable was triggered by |
| 6060 | * a disabling of connectable there could be a need to |
| 6061 | * update the advertising flags. |
| 6062 | */ |
| 6063 | hci_req_init(&req, hdev); |
| 6064 | update_adv_data(&req); |
| 6065 | hci_req_run(&req, NULL); |
| 6066 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6067 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6068 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6069 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6070 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6071 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6072 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6073 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6074 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 6075 | /* Nothing needed here if there's a pending command since that |
| 6076 | * commands request completion callback takes care of everything |
| 6077 | * necessary. |
| 6078 | */ |
| 6079 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6080 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 6081 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 6082 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 6083 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 6084 | return; |
| 6085 | |
Johan Hedberg | dee58c1 | 2014-07-09 12:59:16 +0300 | [diff] [blame] | 6086 | /* If something else than mgmt changed the page scan state we |
| 6087 | * can't differentiate this from a change triggered by adding |
| 6088 | * the first element to the whitelist. Therefore, avoid |
| 6089 | * incorrectly setting HCI_CONNECTABLE. |
| 6090 | */ |
| 6091 | if (connectable && !list_empty(&hdev->whitelist)) |
| 6092 | return; |
| 6093 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6094 | if (connectable) |
| 6095 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 6096 | else |
| 6097 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6098 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 6099 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6100 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6101 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6102 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 6103 | void mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status) |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 6104 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6105 | u8 mgmt_err = mgmt_status(status); |
| 6106 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 6107 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6108 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6109 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 6110 | |
| 6111 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6112 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6113 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 6114 | } |
| 6115 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6116 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 6117 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6118 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 6119 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6120 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6121 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6122 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6123 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 6124 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6125 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6126 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 6127 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6128 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6129 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6130 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6131 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6132 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6133 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 6134 | { |
| 6135 | if (ltk->authenticated) |
| 6136 | return MGMT_LTK_AUTHENTICATED; |
| 6137 | |
| 6138 | return MGMT_LTK_UNAUTHENTICATED; |
| 6139 | } |
| 6140 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6141 | 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] | 6142 | { |
| 6143 | struct mgmt_ev_new_long_term_key ev; |
| 6144 | |
| 6145 | memset(&ev, 0, sizeof(ev)); |
| 6146 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6147 | /* Devices using resolvable or non-resolvable random addresses |
| 6148 | * without providing an indentity resolving key don't require |
| 6149 | * to store long term keys. Their addresses will change the |
| 6150 | * next time around. |
| 6151 | * |
| 6152 | * Only when a remote device provides an identity address |
| 6153 | * make sure the long term key is stored. If the remote |
| 6154 | * identity is known, the long term keys are internally |
| 6155 | * mapped to the identity address. So allow static random |
| 6156 | * and public addresses here. |
| 6157 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6158 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6159 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6160 | ev.store_hint = 0x00; |
| 6161 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6162 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6163 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6164 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6165 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6166 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6167 | ev.key.enc_size = key->enc_size; |
| 6168 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 6169 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6170 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 6171 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6172 | ev.key.master = 1; |
| 6173 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6174 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 6175 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 6176 | 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] | 6177 | } |
| 6178 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6179 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 6180 | { |
| 6181 | struct mgmt_ev_new_irk ev; |
| 6182 | |
| 6183 | memset(&ev, 0, sizeof(ev)); |
| 6184 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6185 | /* For identity resolving keys from devices that are already |
| 6186 | * using a public address or static random address, do not |
| 6187 | * ask for storing this key. The identity resolving key really |
| 6188 | * is only mandatory for devices using resovlable random |
| 6189 | * addresses. |
| 6190 | * |
| 6191 | * Storing all identity resolving keys has the downside that |
| 6192 | * they will be also loaded on next boot of they system. More |
| 6193 | * identity resolving keys, means more time during scanning is |
| 6194 | * needed to actually resolve these addresses. |
| 6195 | */ |
| 6196 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 6197 | ev.store_hint = 0x01; |
| 6198 | else |
| 6199 | ev.store_hint = 0x00; |
| 6200 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6201 | bacpy(&ev.rpa, &irk->rpa); |
| 6202 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6203 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6204 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6205 | |
| 6206 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6207 | } |
| 6208 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6209 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6210 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6211 | { |
| 6212 | struct mgmt_ev_new_csrk ev; |
| 6213 | |
| 6214 | memset(&ev, 0, sizeof(ev)); |
| 6215 | |
| 6216 | /* Devices using resolvable or non-resolvable random addresses |
| 6217 | * without providing an indentity resolving key don't require |
| 6218 | * to store signature resolving keys. Their addresses will change |
| 6219 | * the next time around. |
| 6220 | * |
| 6221 | * Only when a remote device provides an identity address |
| 6222 | * make sure the signature resolving key is stored. So allow |
| 6223 | * static random and public addresses here. |
| 6224 | */ |
| 6225 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6226 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6227 | ev.store_hint = 0x00; |
| 6228 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6229 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6230 | |
| 6231 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6232 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 6233 | ev.key.master = csrk->master; |
| 6234 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6235 | |
| 6236 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6237 | } |
| 6238 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6239 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6240 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6241 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6242 | { |
| 6243 | struct mgmt_ev_new_conn_param ev; |
| 6244 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6245 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6246 | return; |
| 6247 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6248 | memset(&ev, 0, sizeof(ev)); |
| 6249 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6250 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6251 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6252 | ev.min_interval = cpu_to_le16(min_interval); |
| 6253 | ev.max_interval = cpu_to_le16(max_interval); |
| 6254 | ev.latency = cpu_to_le16(latency); |
| 6255 | ev.timeout = cpu_to_le16(timeout); |
| 6256 | |
| 6257 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6258 | } |
| 6259 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 6260 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 6261 | u8 data_len) |
| 6262 | { |
| 6263 | eir[eir_len++] = sizeof(type) + data_len; |
| 6264 | eir[eir_len++] = type; |
| 6265 | memcpy(&eir[eir_len], data, data_len); |
| 6266 | eir_len += data_len; |
| 6267 | |
| 6268 | return eir_len; |
| 6269 | } |
| 6270 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6271 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6272 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 6273 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6274 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6275 | char buf[512]; |
| 6276 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6277 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6278 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6279 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6280 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6281 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6282 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6283 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6284 | if (name_len > 0) |
| 6285 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6286 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6287 | |
| 6288 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 6289 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6290 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6291 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6292 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6293 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6294 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6295 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6296 | } |
| 6297 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6298 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 6299 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 6300 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6301 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 6302 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6303 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6304 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6305 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6306 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6307 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6308 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6309 | |
| 6310 | *sk = cmd->sk; |
| 6311 | sock_hold(*sk); |
| 6312 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6313 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6314 | } |
| 6315 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6316 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6317 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6318 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6319 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 6320 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6321 | |
| 6322 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6323 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6324 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6325 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6326 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6327 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6328 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6329 | |
| 6330 | mgmt_pending_remove(cmd); |
| 6331 | } |
| 6332 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6333 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6334 | u8 link_type, u8 addr_type, u8 reason, |
| 6335 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6336 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6337 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6338 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6339 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6340 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6341 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6342 | if (power_off) { |
| 6343 | struct mgmt_mode *cp = power_off->param; |
| 6344 | |
| 6345 | /* The connection is still in hci_conn_hash so test for 1 |
| 6346 | * instead of 0 to know if this is the last one. |
| 6347 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6348 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6349 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6350 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6351 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6352 | } |
| 6353 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6354 | if (!mgmt_connected) |
| 6355 | return; |
| 6356 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6357 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6358 | return; |
| 6359 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6360 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6361 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6362 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6363 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6364 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6365 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6366 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6367 | |
| 6368 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6369 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6370 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6371 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6372 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6373 | } |
| 6374 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6375 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6376 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6377 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6378 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6379 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6380 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6381 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6382 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6383 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6384 | hdev); |
| 6385 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6386 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6387 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6388 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6389 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6390 | cp = cmd->param; |
| 6391 | |
| 6392 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6393 | return; |
| 6394 | |
| 6395 | if (cp->addr.type != bdaddr_type) |
| 6396 | return; |
| 6397 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6398 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6399 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 6400 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6401 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 6402 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6403 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6404 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6405 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6406 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6407 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6408 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6409 | { |
| 6410 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6411 | struct pending_cmd *power_off; |
| 6412 | |
| 6413 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6414 | if (power_off) { |
| 6415 | struct mgmt_mode *cp = power_off->param; |
| 6416 | |
| 6417 | /* The connection is still in hci_conn_hash so test for 1 |
| 6418 | * instead of 0 to know if this is the last one. |
| 6419 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6420 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6421 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6422 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6423 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6424 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6425 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6426 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6427 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6428 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6429 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6430 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6431 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6432 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6433 | 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] | 6434 | { |
| 6435 | struct mgmt_ev_pin_code_request ev; |
| 6436 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6437 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6438 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6439 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6440 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6441 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6442 | } |
| 6443 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6444 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6445 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6446 | { |
| 6447 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6448 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6449 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6450 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6451 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6452 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6453 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6454 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6455 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6456 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6457 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6458 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6459 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6460 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6461 | } |
| 6462 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6463 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6464 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6465 | { |
| 6466 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6467 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6468 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6469 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6470 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6471 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6472 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6473 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6474 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6475 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6476 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6477 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6478 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6479 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6480 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6481 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6482 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6483 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6484 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6485 | { |
| 6486 | struct mgmt_ev_user_confirm_request ev; |
| 6487 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6488 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6489 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6490 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6491 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6492 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6493 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6494 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6495 | 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] | 6496 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6497 | } |
| 6498 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6499 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6500 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6501 | { |
| 6502 | struct mgmt_ev_user_passkey_request ev; |
| 6503 | |
| 6504 | BT_DBG("%s", hdev->name); |
| 6505 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6506 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6507 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6508 | |
| 6509 | 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] | 6510 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6511 | } |
| 6512 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6513 | 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] | 6514 | u8 link_type, u8 addr_type, u8 status, |
| 6515 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6516 | { |
| 6517 | struct pending_cmd *cmd; |
| 6518 | struct mgmt_rp_user_confirm_reply rp; |
| 6519 | int err; |
| 6520 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6521 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6522 | if (!cmd) |
| 6523 | return -ENOENT; |
| 6524 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6525 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6526 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6527 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6528 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6529 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6530 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6531 | |
| 6532 | return err; |
| 6533 | } |
| 6534 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6535 | 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] | 6536 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6537 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6538 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6539 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6540 | } |
| 6541 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6542 | 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] | 6543 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6544 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6545 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6546 | status, |
| 6547 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6548 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6549 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6550 | 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] | 6551 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6552 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6553 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6554 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6555 | } |
| 6556 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6557 | 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] | 6558 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6559 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6560 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6561 | status, |
| 6562 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6563 | } |
| 6564 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6565 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6566 | u8 link_type, u8 addr_type, u32 passkey, |
| 6567 | u8 entered) |
| 6568 | { |
| 6569 | struct mgmt_ev_passkey_notify ev; |
| 6570 | |
| 6571 | BT_DBG("%s", hdev->name); |
| 6572 | |
| 6573 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6574 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6575 | ev.passkey = __cpu_to_le32(passkey); |
| 6576 | ev.entered = entered; |
| 6577 | |
| 6578 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6579 | } |
| 6580 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6581 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6582 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6583 | { |
| 6584 | struct mgmt_ev_auth_failed ev; |
| 6585 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6586 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6587 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6588 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6589 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6590 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6591 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6592 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6593 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6594 | { |
| 6595 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6596 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6597 | |
| 6598 | if (status) { |
| 6599 | u8 mgmt_err = mgmt_status(status); |
| 6600 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6601 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6602 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6603 | } |
| 6604 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6605 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6606 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6607 | &hdev->dev_flags); |
| 6608 | else |
| 6609 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6610 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6611 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6612 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6613 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6614 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6615 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6616 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6617 | |
| 6618 | if (match.sk) |
| 6619 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6620 | } |
| 6621 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6622 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6623 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6624 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6625 | struct hci_cp_write_eir cp; |
| 6626 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6627 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6628 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6629 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6630 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6631 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6632 | memset(&cp, 0, sizeof(cp)); |
| 6633 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6634 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6635 | } |
| 6636 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6637 | 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] | 6638 | { |
| 6639 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6640 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6641 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6642 | |
| 6643 | if (status) { |
| 6644 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6645 | |
| 6646 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6647 | &hdev->dev_flags)) { |
| 6648 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6649 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6650 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6651 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6652 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6653 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6654 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6658 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6659 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6660 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6661 | if (!changed) |
| 6662 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6663 | &hdev->dev_flags); |
| 6664 | else |
| 6665 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6666 | } |
| 6667 | |
| 6668 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6669 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6670 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6671 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6672 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6673 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6674 | sock_put(match.sk); |
| 6675 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6676 | hci_req_init(&req, hdev); |
| 6677 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6678 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6679 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6680 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6681 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6682 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6683 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6684 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6685 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6686 | |
| 6687 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6688 | } |
| 6689 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6690 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6691 | { |
| 6692 | struct cmd_lookup match = { NULL, hdev }; |
| 6693 | bool changed = false; |
| 6694 | |
| 6695 | if (status) { |
| 6696 | u8 mgmt_err = mgmt_status(status); |
| 6697 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6698 | if (enable) { |
| 6699 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6700 | &hdev->dev_flags)) |
| 6701 | new_settings(hdev, NULL); |
| 6702 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6703 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6704 | |
| 6705 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6706 | cmd_status_rsp, &mgmt_err); |
| 6707 | return; |
| 6708 | } |
| 6709 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6710 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6711 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6712 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6713 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6714 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6715 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6716 | |
| 6717 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6718 | settings_rsp, &match); |
| 6719 | |
| 6720 | if (changed) |
| 6721 | new_settings(hdev, match.sk); |
| 6722 | |
| 6723 | if (match.sk) |
| 6724 | sock_put(match.sk); |
| 6725 | } |
| 6726 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6727 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6728 | { |
| 6729 | struct cmd_lookup *match = data; |
| 6730 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6731 | if (match->sk == NULL) { |
| 6732 | match->sk = cmd->sk; |
| 6733 | sock_hold(match->sk); |
| 6734 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6735 | } |
| 6736 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6737 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6738 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6739 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6740 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6741 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6742 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6743 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6744 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6745 | |
| 6746 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6747 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6748 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6749 | |
| 6750 | if (match.sk) |
| 6751 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6752 | } |
| 6753 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6754 | 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] | 6755 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6756 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6757 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6758 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6759 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6760 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6761 | |
| 6762 | memset(&ev, 0, sizeof(ev)); |
| 6763 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6764 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6765 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6766 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6767 | if (!cmd) { |
| 6768 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6769 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6770 | /* If this is a HCI command related to powering on the |
| 6771 | * HCI dev don't send any mgmt signals. |
| 6772 | */ |
| 6773 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6774 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6775 | } |
| 6776 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6777 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6778 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6779 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6780 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6781 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6782 | u8 *randomizer192, u8 *hash256, |
| 6783 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6784 | { |
| 6785 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6786 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6787 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6788 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6789 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6790 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6791 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6792 | |
| 6793 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6794 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6795 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6796 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6797 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6798 | hash256 && randomizer256) { |
| 6799 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6800 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6801 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6802 | memcpy(rp.randomizer192, randomizer192, |
| 6803 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6804 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6805 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6806 | memcpy(rp.randomizer256, randomizer256, |
| 6807 | sizeof(rp.randomizer256)); |
| 6808 | |
| 6809 | cmd_complete(cmd->sk, hdev->id, |
| 6810 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6811 | &rp, sizeof(rp)); |
| 6812 | } else { |
| 6813 | struct mgmt_rp_read_local_oob_data rp; |
| 6814 | |
| 6815 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6816 | memcpy(rp.randomizer, randomizer192, |
| 6817 | sizeof(rp.randomizer)); |
| 6818 | |
| 6819 | cmd_complete(cmd->sk, hdev->id, |
| 6820 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6821 | &rp, sizeof(rp)); |
| 6822 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6823 | } |
| 6824 | |
| 6825 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6826 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6827 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6828 | 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] | 6829 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6830 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6831 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6832 | char buf[512]; |
| 6833 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6834 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6835 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6836 | /* Don't send events for a non-kernel initiated discovery. With |
| 6837 | * LE one exception is if we have pend_le_reports > 0 in which |
| 6838 | * case we're doing passive scanning and want these events. |
| 6839 | */ |
| 6840 | if (!hci_discovery_active(hdev)) { |
| 6841 | if (link_type == ACL_LINK) |
| 6842 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 6843 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6844 | return; |
| 6845 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6846 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6847 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6848 | * are for the potential CoD field. |
| 6849 | */ |
| 6850 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6851 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6852 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6853 | memset(buf, 0, sizeof(buf)); |
| 6854 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 6855 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6856 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6857 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6858 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6859 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6860 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6861 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6862 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6863 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6864 | 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] | 6865 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6866 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6867 | if (scan_rsp_len > 0) |
| 6868 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6869 | |
| 6870 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6871 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6872 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6873 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6874 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6875 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6876 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6877 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6878 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6879 | struct mgmt_ev_device_found *ev; |
| 6880 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6881 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6882 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6883 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6884 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6885 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6886 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6887 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6888 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6889 | ev->rssi = rssi; |
| 6890 | |
| 6891 | 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] | 6892 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6893 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6894 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6895 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6896 | 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] | 6897 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6898 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6899 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6900 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6901 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6902 | struct pending_cmd *cmd; |
| 6903 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6904 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6905 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6906 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6907 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6908 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6909 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6910 | |
| 6911 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6912 | u8 type = hdev->discovery.type; |
| 6913 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6914 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6915 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6916 | mgmt_pending_remove(cmd); |
| 6917 | } |
| 6918 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6919 | memset(&ev, 0, sizeof(ev)); |
| 6920 | ev.type = hdev->discovery.type; |
| 6921 | ev.discovering = discovering; |
| 6922 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6923 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6924 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6925 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6926 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6927 | { |
| 6928 | BT_DBG("%s status %u", hdev->name, status); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6929 | } |
| 6930 | |
| 6931 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6932 | { |
| 6933 | struct hci_request req; |
| 6934 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6935 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6936 | return; |
| 6937 | |
| 6938 | hci_req_init(&req, hdev); |
| 6939 | enable_advertising(&req); |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 6940 | hci_req_run(&req, adv_enable_complete); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6941 | } |