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 | f7b64e6 | 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 | f7b64e6 | 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 | f7b64e6 | 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 | f7b64e6 | 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 | f7b64e6 | 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 | f7b64e6 | 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 | f7b64e6 | 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 | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Johan Hedberg | aee9b218 | 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 | aee9b218 | 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 | aee9b218 | 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 | aee9b218 | 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 | d603b76 | 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 | aee9b218 | 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 | d603b76 | 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 | 5afeac14 | 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 | f7b64e6 | 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 | f7b64e6 | 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 | f7b64e6 | 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 | f7b64e6 | 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 | aee9b218 | 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 | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1430 | struct cmd_lookup { |
| 1431 | struct sock *sk; |
| 1432 | struct hci_dev *hdev; |
| 1433 | u8 mgmt_status; |
| 1434 | }; |
| 1435 | |
| 1436 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1437 | { |
| 1438 | struct cmd_lookup *match = data; |
| 1439 | |
| 1440 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1441 | |
| 1442 | list_del(&cmd->list); |
| 1443 | |
| 1444 | if (match->sk == NULL) { |
| 1445 | match->sk = cmd->sk; |
| 1446 | sock_hold(match->sk); |
| 1447 | } |
| 1448 | |
| 1449 | mgmt_pending_free(cmd); |
| 1450 | } |
| 1451 | |
| 1452 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1453 | { |
| 1454 | u8 *status = data; |
| 1455 | |
| 1456 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1457 | mgmt_pending_remove(cmd); |
| 1458 | } |
| 1459 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1460 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1461 | { |
| 1462 | if (!lmp_bredr_capable(hdev)) |
| 1463 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1464 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1465 | return MGMT_STATUS_REJECTED; |
| 1466 | else |
| 1467 | return MGMT_STATUS_SUCCESS; |
| 1468 | } |
| 1469 | |
| 1470 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1471 | { |
| 1472 | if (!lmp_le_capable(hdev)) |
| 1473 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1474 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1475 | return MGMT_STATUS_REJECTED; |
| 1476 | else |
| 1477 | return MGMT_STATUS_SUCCESS; |
| 1478 | } |
| 1479 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1480 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1481 | { |
| 1482 | struct pending_cmd *cmd; |
| 1483 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1484 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1485 | bool changed; |
| 1486 | |
| 1487 | BT_DBG("status 0x%02x", status); |
| 1488 | |
| 1489 | hci_dev_lock(hdev); |
| 1490 | |
| 1491 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1492 | if (!cmd) |
| 1493 | goto unlock; |
| 1494 | |
| 1495 | if (status) { |
| 1496 | u8 mgmt_err = mgmt_status(status); |
| 1497 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1498 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1499 | goto remove_cmd; |
| 1500 | } |
| 1501 | |
| 1502 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1503 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1504 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1505 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1506 | |
| 1507 | if (hdev->discov_timeout > 0) { |
| 1508 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1509 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1510 | to); |
| 1511 | } |
| 1512 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1513 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1514 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1515 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1516 | |
| 1517 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1518 | |
| 1519 | if (changed) |
| 1520 | new_settings(hdev, cmd->sk); |
| 1521 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1522 | /* When the discoverable mode gets changed, make sure |
| 1523 | * that class of device has the limited discoverable |
| 1524 | * bit correctly set. |
| 1525 | */ |
| 1526 | hci_req_init(&req, hdev); |
| 1527 | update_class(&req); |
| 1528 | hci_req_run(&req, NULL); |
| 1529 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1530 | remove_cmd: |
| 1531 | mgmt_pending_remove(cmd); |
| 1532 | |
| 1533 | unlock: |
| 1534 | hci_dev_unlock(hdev); |
| 1535 | } |
| 1536 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1537 | 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] | 1538 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1539 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1540 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1541 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1542 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1543 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1544 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1545 | int err; |
| 1546 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1547 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1548 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1549 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1550 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1551 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1552 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1553 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1554 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1555 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1556 | MGMT_STATUS_INVALID_PARAMS); |
| 1557 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1558 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1559 | |
| 1560 | /* Disabling discoverable requires that no timeout is set, |
| 1561 | * and enabling limited discoverable requires a timeout. |
| 1562 | */ |
| 1563 | if ((cp->val == 0x00 && timeout > 0) || |
| 1564 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1565 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1566 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1567 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1568 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1569 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1570 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1571 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1572 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1573 | goto failed; |
| 1574 | } |
| 1575 | |
| 1576 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1577 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1578 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1579 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1580 | goto failed; |
| 1581 | } |
| 1582 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1583 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1584 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1585 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1586 | goto failed; |
| 1587 | } |
| 1588 | |
| 1589 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1590 | bool changed = false; |
| 1591 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1592 | /* Setting limited discoverable when powered off is |
| 1593 | * not a valid operation since it requires a timeout |
| 1594 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1595 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1596 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1597 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1598 | changed = true; |
| 1599 | } |
| 1600 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1601 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1602 | if (err < 0) |
| 1603 | goto failed; |
| 1604 | |
| 1605 | if (changed) |
| 1606 | err = new_settings(hdev, sk); |
| 1607 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1608 | goto failed; |
| 1609 | } |
| 1610 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1611 | /* If the current mode is the same, then just update the timeout |
| 1612 | * value with the new value. And if only the timeout gets updated, |
| 1613 | * then no need for any HCI transactions. |
| 1614 | */ |
| 1615 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1616 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1617 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1618 | cancel_delayed_work(&hdev->discov_off); |
| 1619 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1620 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1621 | if (cp->val && hdev->discov_timeout > 0) { |
| 1622 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1623 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1624 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1625 | } |
| 1626 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1627 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1628 | goto failed; |
| 1629 | } |
| 1630 | |
| 1631 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1632 | if (!cmd) { |
| 1633 | err = -ENOMEM; |
| 1634 | goto failed; |
| 1635 | } |
| 1636 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1637 | /* Cancel any potential discoverable timeout that might be |
| 1638 | * still active and store new timeout value. The arming of |
| 1639 | * the timeout happens in the complete handler. |
| 1640 | */ |
| 1641 | cancel_delayed_work(&hdev->discov_off); |
| 1642 | hdev->discov_timeout = timeout; |
| 1643 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1644 | /* Limited discoverable mode */ |
| 1645 | if (cp->val == 0x02) |
| 1646 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1647 | else |
| 1648 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1649 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1650 | hci_req_init(&req, hdev); |
| 1651 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1652 | /* The procedure for LE-only controllers is much simpler - just |
| 1653 | * update the advertising data. |
| 1654 | */ |
| 1655 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1656 | goto update_ad; |
| 1657 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1658 | scan = SCAN_PAGE; |
| 1659 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1660 | if (cp->val) { |
| 1661 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1662 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1663 | if (cp->val == 0x02) { |
| 1664 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1665 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1666 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1667 | hci_cp.iac_lap[1] = 0x8b; |
| 1668 | hci_cp.iac_lap[2] = 0x9e; |
| 1669 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1670 | hci_cp.iac_lap[4] = 0x8b; |
| 1671 | hci_cp.iac_lap[5] = 0x9e; |
| 1672 | } else { |
| 1673 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1674 | hci_cp.num_iac = 1; |
| 1675 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1676 | hci_cp.iac_lap[1] = 0x8b; |
| 1677 | hci_cp.iac_lap[2] = 0x9e; |
| 1678 | } |
| 1679 | |
| 1680 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1681 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1682 | |
| 1683 | scan |= SCAN_INQUIRY; |
| 1684 | } else { |
| 1685 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1686 | } |
| 1687 | |
| 1688 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1689 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1690 | update_ad: |
| 1691 | update_adv_data(&req); |
| 1692 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1693 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1694 | if (err < 0) |
| 1695 | mgmt_pending_remove(cmd); |
| 1696 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1697 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1698 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1699 | return err; |
| 1700 | } |
| 1701 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1702 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1703 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1704 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1705 | struct hci_cp_write_page_scan_activity acp; |
| 1706 | u8 type; |
| 1707 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1708 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1709 | return; |
| 1710 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1711 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1712 | return; |
| 1713 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1714 | if (enable) { |
| 1715 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1716 | |
| 1717 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1718 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1719 | } else { |
| 1720 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1721 | |
| 1722 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1723 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1724 | } |
| 1725 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1726 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1727 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1728 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1729 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1730 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1731 | sizeof(acp), &acp); |
| 1732 | |
| 1733 | if (hdev->page_scan_type != type) |
| 1734 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1735 | } |
| 1736 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1737 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1738 | { |
| 1739 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1740 | struct mgmt_mode *cp; |
| 1741 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1742 | |
| 1743 | BT_DBG("status 0x%02x", status); |
| 1744 | |
| 1745 | hci_dev_lock(hdev); |
| 1746 | |
| 1747 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1748 | if (!cmd) |
| 1749 | goto unlock; |
| 1750 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1751 | if (status) { |
| 1752 | u8 mgmt_err = mgmt_status(status); |
| 1753 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1754 | goto remove_cmd; |
| 1755 | } |
| 1756 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1757 | cp = cmd->param; |
| 1758 | if (cp->val) |
| 1759 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1760 | else |
| 1761 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1762 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1763 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1764 | |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1765 | if (changed) { |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1766 | new_settings(hdev, cmd->sk); |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1767 | hci_update_background_scan(hdev); |
| 1768 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1769 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1770 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1771 | mgmt_pending_remove(cmd); |
| 1772 | |
| 1773 | unlock: |
| 1774 | hci_dev_unlock(hdev); |
| 1775 | } |
| 1776 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1777 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1778 | struct sock *sk, u8 val) |
| 1779 | { |
| 1780 | bool changed = false; |
| 1781 | int err; |
| 1782 | |
| 1783 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1784 | changed = true; |
| 1785 | |
| 1786 | if (val) { |
| 1787 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1788 | } else { |
| 1789 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1790 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1791 | } |
| 1792 | |
| 1793 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1794 | if (err < 0) |
| 1795 | return err; |
| 1796 | |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1797 | if (changed) { |
| 1798 | hci_update_background_scan(hdev); |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1799 | return new_settings(hdev, sk); |
Johan Hedberg | 562064e | 2014-07-08 16:35:34 +0300 | [diff] [blame] | 1800 | } |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1801 | |
| 1802 | return 0; |
| 1803 | } |
| 1804 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1805 | 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] | 1806 | u16 len) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1807 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1808 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1809 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1810 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1811 | u8 scan; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1812 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1813 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1814 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1815 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1816 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1817 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1818 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1819 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1820 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1821 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1822 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1823 | MGMT_STATUS_INVALID_PARAMS); |
| 1824 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1825 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1826 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1827 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1828 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1829 | goto failed; |
| 1830 | } |
| 1831 | |
| 1832 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1833 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1834 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1835 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1836 | goto failed; |
| 1837 | } |
| 1838 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1839 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1840 | if (!cmd) { |
| 1841 | err = -ENOMEM; |
| 1842 | goto failed; |
| 1843 | } |
| 1844 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1845 | hci_req_init(&req, hdev); |
| 1846 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1847 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1848 | * by-product of disabling connectable, we need to update the |
| 1849 | * advertising flags. |
| 1850 | */ |
| 1851 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1852 | if (!cp->val) { |
| 1853 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1854 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1855 | } |
| 1856 | update_adv_data(&req); |
| 1857 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1858 | if (cp->val) { |
| 1859 | scan = SCAN_PAGE; |
| 1860 | } else { |
| 1861 | scan = 0; |
| 1862 | |
| 1863 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1864 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1865 | cancel_delayed_work(&hdev->discov_off); |
| 1866 | } |
| 1867 | |
| 1868 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1869 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1870 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1871 | /* If we're going from non-connectable to connectable or |
| 1872 | * vice-versa when fast connectable is enabled ensure that fast |
| 1873 | * connectable gets disabled. write_fast_connectable won't do |
| 1874 | * anything if the page scan parameters are already what they |
| 1875 | * should be. |
| 1876 | */ |
| 1877 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1878 | write_fast_connectable(&req, false); |
| 1879 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1880 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 1881 | !test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1882 | enable_advertising(&req); |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1883 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1884 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1885 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1886 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1887 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1888 | err = set_connectable_update_settings(hdev, sk, |
| 1889 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1890 | goto failed; |
| 1891 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1892 | |
| 1893 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1894 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1895 | return err; |
| 1896 | } |
| 1897 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1898 | 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] | 1899 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1900 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1901 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1902 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1903 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1904 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1905 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1906 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1907 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1908 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1909 | MGMT_STATUS_INVALID_PARAMS); |
| 1910 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1911 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1912 | |
| 1913 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1914 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1915 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1916 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1917 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1918 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1919 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1920 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1921 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1922 | if (changed) |
| 1923 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1924 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1925 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1926 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1927 | return err; |
| 1928 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1929 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1930 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1931 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1932 | { |
| 1933 | struct mgmt_mode *cp = data; |
| 1934 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1935 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1936 | int err; |
| 1937 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1938 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1939 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1940 | status = mgmt_bredr_support(hdev); |
| 1941 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1942 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1943 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1944 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1945 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1946 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1947 | MGMT_STATUS_INVALID_PARAMS); |
| 1948 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1949 | hci_dev_lock(hdev); |
| 1950 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1951 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1952 | bool changed = false; |
| 1953 | |
| 1954 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1955 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1956 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1957 | changed = true; |
| 1958 | } |
| 1959 | |
| 1960 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1961 | if (err < 0) |
| 1962 | goto failed; |
| 1963 | |
| 1964 | if (changed) |
| 1965 | err = new_settings(hdev, sk); |
| 1966 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1967 | goto failed; |
| 1968 | } |
| 1969 | |
| 1970 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1971 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1972 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1973 | goto failed; |
| 1974 | } |
| 1975 | |
| 1976 | val = !!cp->val; |
| 1977 | |
| 1978 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1979 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1980 | goto failed; |
| 1981 | } |
| 1982 | |
| 1983 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1984 | if (!cmd) { |
| 1985 | err = -ENOMEM; |
| 1986 | goto failed; |
| 1987 | } |
| 1988 | |
| 1989 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1990 | if (err < 0) { |
| 1991 | mgmt_pending_remove(cmd); |
| 1992 | goto failed; |
| 1993 | } |
| 1994 | |
| 1995 | failed: |
| 1996 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1997 | return err; |
| 1998 | } |
| 1999 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2000 | 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] | 2001 | { |
| 2002 | struct mgmt_mode *cp = data; |
| 2003 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2004 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2005 | int err; |
| 2006 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2007 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2008 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 2009 | status = mgmt_bredr_support(hdev); |
| 2010 | if (status) |
| 2011 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 2012 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2013 | if (!lmp_ssp_capable(hdev)) |
| 2014 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2015 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2016 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2017 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2018 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2019 | MGMT_STATUS_INVALID_PARAMS); |
| 2020 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2021 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 2022 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2023 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2024 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2025 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2026 | if (cp->val) { |
| 2027 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 2028 | &hdev->dev_flags); |
| 2029 | } else { |
| 2030 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 2031 | &hdev->dev_flags); |
| 2032 | if (!changed) |
| 2033 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 2034 | &hdev->dev_flags); |
| 2035 | else |
| 2036 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2040 | if (err < 0) |
| 2041 | goto failed; |
| 2042 | |
| 2043 | if (changed) |
| 2044 | err = new_settings(hdev, sk); |
| 2045 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2046 | goto failed; |
| 2047 | } |
| 2048 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2049 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 2050 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 2051 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2052 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2053 | goto failed; |
| 2054 | } |
| 2055 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2056 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2057 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2058 | goto failed; |
| 2059 | } |
| 2060 | |
| 2061 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 2062 | if (!cmd) { |
| 2063 | err = -ENOMEM; |
| 2064 | goto failed; |
| 2065 | } |
| 2066 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 2067 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 2068 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2069 | sizeof(cp->val), &cp->val); |
| 2070 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2071 | 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] | 2072 | if (err < 0) { |
| 2073 | mgmt_pending_remove(cmd); |
| 2074 | goto failed; |
| 2075 | } |
| 2076 | |
| 2077 | failed: |
| 2078 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2079 | return err; |
| 2080 | } |
| 2081 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2082 | 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] | 2083 | { |
| 2084 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2085 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2086 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2087 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2088 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2089 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2090 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2091 | status = mgmt_bredr_support(hdev); |
| 2092 | if (status) |
| 2093 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2094 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2095 | if (!lmp_ssp_capable(hdev)) |
| 2096 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2097 | MGMT_STATUS_NOT_SUPPORTED); |
| 2098 | |
| 2099 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 2100 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2101 | MGMT_STATUS_REJECTED); |
| 2102 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2103 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2104 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2105 | MGMT_STATUS_INVALID_PARAMS); |
| 2106 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2107 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2108 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2109 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2110 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2111 | } else { |
| 2112 | if (hdev_is_powered(hdev)) { |
| 2113 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2114 | MGMT_STATUS_REJECTED); |
| 2115 | goto unlock; |
| 2116 | } |
| 2117 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2118 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2119 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2120 | |
| 2121 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2122 | if (err < 0) |
| 2123 | goto unlock; |
| 2124 | |
| 2125 | if (changed) |
| 2126 | err = new_settings(hdev, sk); |
| 2127 | |
| 2128 | unlock: |
| 2129 | hci_dev_unlock(hdev); |
| 2130 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2131 | } |
| 2132 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2133 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 2134 | { |
| 2135 | struct cmd_lookup match = { NULL, hdev }; |
| 2136 | |
| 2137 | if (status) { |
| 2138 | u8 mgmt_err = mgmt_status(status); |
| 2139 | |
| 2140 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2141 | &mgmt_err); |
| 2142 | return; |
| 2143 | } |
| 2144 | |
| 2145 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2146 | |
| 2147 | new_settings(hdev, match.sk); |
| 2148 | |
| 2149 | if (match.sk) |
| 2150 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2151 | |
| 2152 | /* Make sure the controller has a good default for |
| 2153 | * advertising data. Restrict the update to when LE |
| 2154 | * has actually been enabled. During power on, the |
| 2155 | * update in powered_update_hci will take care of it. |
| 2156 | */ |
| 2157 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2158 | struct hci_request req; |
| 2159 | |
| 2160 | hci_dev_lock(hdev); |
| 2161 | |
| 2162 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2163 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2164 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2165 | hci_req_run(&req, NULL); |
| 2166 | |
Johan Hedberg | a70f4b5 | 2014-07-07 15:19:50 +0300 | [diff] [blame] | 2167 | hci_update_background_scan(hdev); |
| 2168 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2169 | hci_dev_unlock(hdev); |
| 2170 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2171 | } |
| 2172 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2173 | 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] | 2174 | { |
| 2175 | struct mgmt_mode *cp = data; |
| 2176 | struct hci_cp_write_le_host_supported hci_cp; |
| 2177 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2178 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2179 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2180 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2181 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2182 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2183 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2184 | if (!lmp_le_capable(hdev)) |
| 2185 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2186 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2187 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2188 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2189 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2190 | MGMT_STATUS_INVALID_PARAMS); |
| 2191 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2192 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2193 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2194 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2195 | MGMT_STATUS_REJECTED); |
| 2196 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2197 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2198 | |
| 2199 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2200 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2201 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2202 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2203 | bool changed = false; |
| 2204 | |
| 2205 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2206 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2207 | changed = true; |
| 2208 | } |
| 2209 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2210 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2211 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2212 | changed = true; |
| 2213 | } |
| 2214 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2215 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2216 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2217 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2218 | |
| 2219 | if (changed) |
| 2220 | err = new_settings(hdev, sk); |
| 2221 | |
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 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2225 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2226 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2227 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2228 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2229 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2233 | if (!cmd) { |
| 2234 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2235 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2236 | } |
| 2237 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2238 | hci_req_init(&req, hdev); |
| 2239 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2240 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2241 | |
| 2242 | if (val) { |
| 2243 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2244 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2245 | } else { |
Johan Hedberg | 73e082f | 2014-07-08 15:07:51 +0300 | [diff] [blame] | 2246 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2247 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2248 | } |
| 2249 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2250 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2251 | &hci_cp); |
| 2252 | |
| 2253 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2254 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2255 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2256 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2257 | unlock: |
| 2258 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2259 | return err; |
| 2260 | } |
| 2261 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2262 | /* This is a helper function to test for pending mgmt commands that can |
| 2263 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2264 | * mgmt command at a time since otherwise we cannot easily track what |
| 2265 | * the current values are, will be, and based on that calculate if a new |
| 2266 | * HCI command needs to be sent and if yes with what value. |
| 2267 | */ |
| 2268 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2269 | { |
| 2270 | struct pending_cmd *cmd; |
| 2271 | |
| 2272 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2273 | switch (cmd->opcode) { |
| 2274 | case MGMT_OP_ADD_UUID: |
| 2275 | case MGMT_OP_REMOVE_UUID: |
| 2276 | case MGMT_OP_SET_DEV_CLASS: |
| 2277 | case MGMT_OP_SET_POWERED: |
| 2278 | return true; |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | return false; |
| 2283 | } |
| 2284 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2285 | static const u8 bluetooth_base_uuid[] = { |
| 2286 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2287 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2288 | }; |
| 2289 | |
| 2290 | static u8 get_uuid_size(const u8 *uuid) |
| 2291 | { |
| 2292 | u32 val; |
| 2293 | |
| 2294 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2295 | return 128; |
| 2296 | |
| 2297 | val = get_unaligned_le32(&uuid[12]); |
| 2298 | if (val > 0xffff) |
| 2299 | return 32; |
| 2300 | |
| 2301 | return 16; |
| 2302 | } |
| 2303 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2304 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2305 | { |
| 2306 | struct pending_cmd *cmd; |
| 2307 | |
| 2308 | hci_dev_lock(hdev); |
| 2309 | |
| 2310 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2311 | if (!cmd) |
| 2312 | goto unlock; |
| 2313 | |
| 2314 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2315 | hdev->dev_class, 3); |
| 2316 | |
| 2317 | mgmt_pending_remove(cmd); |
| 2318 | |
| 2319 | unlock: |
| 2320 | hci_dev_unlock(hdev); |
| 2321 | } |
| 2322 | |
| 2323 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2324 | { |
| 2325 | BT_DBG("status 0x%02x", status); |
| 2326 | |
| 2327 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2328 | } |
| 2329 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2330 | 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] | 2331 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2332 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2333 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2334 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2335 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2336 | int err; |
| 2337 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2338 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2339 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2340 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2341 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2342 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2343 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2344 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2345 | goto failed; |
| 2346 | } |
| 2347 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2348 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2349 | if (!uuid) { |
| 2350 | err = -ENOMEM; |
| 2351 | goto failed; |
| 2352 | } |
| 2353 | |
| 2354 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2355 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2356 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2357 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2358 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2359 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2360 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2361 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2362 | update_class(&req); |
| 2363 | update_eir(&req); |
| 2364 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2365 | err = hci_req_run(&req, add_uuid_complete); |
| 2366 | if (err < 0) { |
| 2367 | if (err != -ENODATA) |
| 2368 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2369 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2370 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2371 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2372 | goto failed; |
| 2373 | } |
| 2374 | |
| 2375 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2376 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2377 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2378 | goto failed; |
| 2379 | } |
| 2380 | |
| 2381 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2382 | |
| 2383 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2384 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2385 | return err; |
| 2386 | } |
| 2387 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2388 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2389 | { |
| 2390 | if (!hdev_is_powered(hdev)) |
| 2391 | return false; |
| 2392 | |
| 2393 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2394 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2395 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2396 | return true; |
| 2397 | } |
| 2398 | |
| 2399 | return false; |
| 2400 | } |
| 2401 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2402 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2403 | { |
| 2404 | BT_DBG("status 0x%02x", status); |
| 2405 | |
| 2406 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2407 | } |
| 2408 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2409 | 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] | 2410 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2411 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2412 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2413 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2414 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2415 | 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] | 2416 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2417 | int err, found; |
| 2418 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2419 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2420 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2421 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2422 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2423 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2424 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2425 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2426 | goto unlock; |
| 2427 | } |
| 2428 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2429 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2430 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2431 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2432 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2433 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2434 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2435 | goto unlock; |
| 2436 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2437 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2438 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2439 | } |
| 2440 | |
| 2441 | found = 0; |
| 2442 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2443 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2444 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2445 | continue; |
| 2446 | |
| 2447 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2448 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2449 | found++; |
| 2450 | } |
| 2451 | |
| 2452 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2453 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2454 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2455 | goto unlock; |
| 2456 | } |
| 2457 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2458 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2459 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2460 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2461 | update_class(&req); |
| 2462 | update_eir(&req); |
| 2463 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2464 | err = hci_req_run(&req, remove_uuid_complete); |
| 2465 | if (err < 0) { |
| 2466 | if (err != -ENODATA) |
| 2467 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2468 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2469 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2470 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2471 | goto unlock; |
| 2472 | } |
| 2473 | |
| 2474 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2475 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2476 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2477 | goto unlock; |
| 2478 | } |
| 2479 | |
| 2480 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2481 | |
| 2482 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2483 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2484 | return err; |
| 2485 | } |
| 2486 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2487 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2488 | { |
| 2489 | BT_DBG("status 0x%02x", status); |
| 2490 | |
| 2491 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2492 | } |
| 2493 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2494 | 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] | 2495 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2496 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2497 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2498 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2499 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2500 | int err; |
| 2501 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2502 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2503 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2504 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2505 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2506 | MGMT_STATUS_NOT_SUPPORTED); |
| 2507 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2508 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2509 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2510 | if (pending_eir_or_class(hdev)) { |
| 2511 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2512 | MGMT_STATUS_BUSY); |
| 2513 | goto unlock; |
| 2514 | } |
| 2515 | |
| 2516 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2517 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2518 | MGMT_STATUS_INVALID_PARAMS); |
| 2519 | goto unlock; |
| 2520 | } |
| 2521 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2522 | hdev->major_class = cp->major; |
| 2523 | hdev->minor_class = cp->minor; |
| 2524 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2525 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2526 | 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] | 2527 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2528 | goto unlock; |
| 2529 | } |
| 2530 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2531 | hci_req_init(&req, hdev); |
| 2532 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2533 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2534 | hci_dev_unlock(hdev); |
| 2535 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2536 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2537 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2538 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2539 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2540 | update_class(&req); |
| 2541 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2542 | err = hci_req_run(&req, set_class_complete); |
| 2543 | if (err < 0) { |
| 2544 | if (err != -ENODATA) |
| 2545 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2546 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2547 | 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] | 2548 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2549 | goto unlock; |
| 2550 | } |
| 2551 | |
| 2552 | 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] | 2553 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2554 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2555 | goto unlock; |
| 2556 | } |
| 2557 | |
| 2558 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2559 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2560 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2561 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2562 | return err; |
| 2563 | } |
| 2564 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2565 | 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] | 2566 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2567 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2568 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2569 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2570 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2571 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2572 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2573 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2574 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2575 | BT_DBG("request for %s", hdev->name); |
| 2576 | |
| 2577 | if (!lmp_bredr_capable(hdev)) |
| 2578 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2579 | MGMT_STATUS_NOT_SUPPORTED); |
| 2580 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2581 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2582 | if (key_count > max_key_count) { |
| 2583 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2584 | key_count); |
| 2585 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2586 | MGMT_STATUS_INVALID_PARAMS); |
| 2587 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2588 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2589 | expected_len = sizeof(*cp) + key_count * |
| 2590 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2591 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2592 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2593 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2594 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2595 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2596 | } |
| 2597 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2598 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2599 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2600 | MGMT_STATUS_INVALID_PARAMS); |
| 2601 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2602 | 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] | 2603 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2604 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2605 | for (i = 0; i < key_count; i++) { |
| 2606 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2607 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2608 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2609 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2610 | MGMT_STATUS_INVALID_PARAMS); |
| 2611 | } |
| 2612 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2613 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2614 | |
| 2615 | hci_link_keys_clear(hdev); |
| 2616 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2617 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2618 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2619 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2620 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2621 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2622 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2623 | |
| 2624 | if (changed) |
| 2625 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2626 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2627 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2628 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2629 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2630 | /* Always ignore debug keys and require a new pairing if |
| 2631 | * the user wants to use them. |
| 2632 | */ |
| 2633 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2634 | continue; |
| 2635 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2636 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2637 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2638 | } |
| 2639 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2640 | 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] | 2641 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2642 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2643 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2644 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2647 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2648 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2649 | { |
| 2650 | struct mgmt_ev_device_unpaired ev; |
| 2651 | |
| 2652 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2653 | ev.addr.type = addr_type; |
| 2654 | |
| 2655 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2656 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2657 | } |
| 2658 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2659 | 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] | 2660 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2661 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2662 | struct mgmt_cp_unpair_device *cp = data; |
| 2663 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2664 | struct hci_cp_disconnect dc; |
| 2665 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2666 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2667 | int err; |
| 2668 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2669 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2670 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2671 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2672 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2673 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2674 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2675 | MGMT_STATUS_INVALID_PARAMS, |
| 2676 | &rp, sizeof(rp)); |
| 2677 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2678 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2679 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2680 | MGMT_STATUS_INVALID_PARAMS, |
| 2681 | &rp, sizeof(rp)); |
| 2682 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2683 | hci_dev_lock(hdev); |
| 2684 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2685 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2686 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2687 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2688 | goto unlock; |
| 2689 | } |
| 2690 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2691 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2692 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2693 | } else { |
| 2694 | u8 addr_type; |
| 2695 | |
| 2696 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2697 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2698 | else |
| 2699 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2700 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2701 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2702 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2703 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2704 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2705 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2706 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2707 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2708 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2709 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2710 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2711 | goto unlock; |
| 2712 | } |
| 2713 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2714 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2715 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2716 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2717 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2718 | else |
| 2719 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2720 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2721 | } else { |
| 2722 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2723 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2724 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2725 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2726 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2727 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2728 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2729 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2730 | } |
| 2731 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2732 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2733 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2734 | if (!cmd) { |
| 2735 | err = -ENOMEM; |
| 2736 | goto unlock; |
| 2737 | } |
| 2738 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2739 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2740 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2741 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2742 | if (err < 0) |
| 2743 | mgmt_pending_remove(cmd); |
| 2744 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2745 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2746 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2747 | return err; |
| 2748 | } |
| 2749 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2750 | 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] | 2751 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2752 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2753 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2754 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2755 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2756 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2757 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2758 | int err; |
| 2759 | |
| 2760 | BT_DBG(""); |
| 2761 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2762 | memset(&rp, 0, sizeof(rp)); |
| 2763 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2764 | rp.addr.type = cp->addr.type; |
| 2765 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2766 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2767 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2768 | MGMT_STATUS_INVALID_PARAMS, |
| 2769 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2770 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2771 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2772 | |
| 2773 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2774 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2775 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2776 | goto failed; |
| 2777 | } |
| 2778 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2779 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2780 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2781 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2782 | goto failed; |
| 2783 | } |
| 2784 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2785 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2786 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2787 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2788 | else |
| 2789 | 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] | 2790 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2791 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2792 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2793 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2794 | goto failed; |
| 2795 | } |
| 2796 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2797 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2798 | if (!cmd) { |
| 2799 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2800 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2801 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2802 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2803 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2804 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2805 | |
| 2806 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2807 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2808 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2809 | |
| 2810 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2811 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2812 | return err; |
| 2813 | } |
| 2814 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2815 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2816 | { |
| 2817 | switch (link_type) { |
| 2818 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2819 | switch (addr_type) { |
| 2820 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2821 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2822 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2823 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2824 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2825 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2826 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2827 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2828 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2829 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2830 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2834 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2835 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2836 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2837 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2838 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2839 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2840 | int err; |
| 2841 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2842 | |
| 2843 | BT_DBG(""); |
| 2844 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2845 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2846 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2847 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2848 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2849 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2850 | goto unlock; |
| 2851 | } |
| 2852 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2853 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2854 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2855 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2856 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2857 | } |
| 2858 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2859 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2860 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2861 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2862 | err = -ENOMEM; |
| 2863 | goto unlock; |
| 2864 | } |
| 2865 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2866 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2867 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2868 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2869 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2870 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2871 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2872 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2873 | continue; |
| 2874 | i++; |
| 2875 | } |
| 2876 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2877 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2878 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2879 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2880 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2881 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2882 | 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] | 2883 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2884 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2885 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2886 | |
| 2887 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2888 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2889 | return err; |
| 2890 | } |
| 2891 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2892 | 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] | 2893 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2894 | { |
| 2895 | struct pending_cmd *cmd; |
| 2896 | int err; |
| 2897 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2898 | 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] | 2899 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2900 | if (!cmd) |
| 2901 | return -ENOMEM; |
| 2902 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2903 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2904 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2905 | if (err < 0) |
| 2906 | mgmt_pending_remove(cmd); |
| 2907 | |
| 2908 | return err; |
| 2909 | } |
| 2910 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2911 | 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] | 2912 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2913 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2914 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2915 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2916 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2917 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2918 | int err; |
| 2919 | |
| 2920 | BT_DBG(""); |
| 2921 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2922 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2923 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2924 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2925 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2926 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2927 | goto failed; |
| 2928 | } |
| 2929 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2930 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2931 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2932 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2933 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2934 | goto failed; |
| 2935 | } |
| 2936 | |
| 2937 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2938 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2939 | |
| 2940 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2941 | |
| 2942 | BT_ERR("PIN code is not 16 bytes long"); |
| 2943 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2944 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2945 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2946 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2947 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2948 | |
| 2949 | goto failed; |
| 2950 | } |
| 2951 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2952 | 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] | 2953 | if (!cmd) { |
| 2954 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2955 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2956 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2957 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2958 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2959 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2960 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2961 | |
| 2962 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2963 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2964 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2965 | |
| 2966 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2967 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2968 | return err; |
| 2969 | } |
| 2970 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2971 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2972 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2973 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2974 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2975 | |
| 2976 | BT_DBG(""); |
| 2977 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2978 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2979 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2980 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2981 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2982 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2983 | |
| 2984 | hdev->io_capability = cp->io_capability; |
| 2985 | |
| 2986 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2987 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2988 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2989 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2990 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2991 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2992 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2993 | } |
| 2994 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2995 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2996 | { |
| 2997 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2998 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2999 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3000 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3001 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 3002 | continue; |
| 3003 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3004 | if (cmd->user_data != conn) |
| 3005 | continue; |
| 3006 | |
| 3007 | return cmd; |
| 3008 | } |
| 3009 | |
| 3010 | return NULL; |
| 3011 | } |
| 3012 | |
| 3013 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 3014 | { |
| 3015 | struct mgmt_rp_pair_device rp; |
| 3016 | struct hci_conn *conn = cmd->user_data; |
| 3017 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 3018 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 3019 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3020 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3021 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3022 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3023 | |
| 3024 | /* So we don't get further callbacks for this connection */ |
| 3025 | conn->connect_cfm_cb = NULL; |
| 3026 | conn->security_cfm_cb = NULL; |
| 3027 | conn->disconn_cfm_cb = NULL; |
| 3028 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3029 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3030 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3031 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3032 | } |
| 3033 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3034 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 3035 | { |
| 3036 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 3037 | struct pending_cmd *cmd; |
| 3038 | |
| 3039 | cmd = find_pairing(conn); |
| 3040 | if (cmd) |
| 3041 | pairing_complete(cmd, status); |
| 3042 | } |
| 3043 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3044 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 3045 | { |
| 3046 | struct pending_cmd *cmd; |
| 3047 | |
| 3048 | BT_DBG("status %u", status); |
| 3049 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3050 | cmd = find_pairing(conn); |
| 3051 | if (!cmd) |
| 3052 | BT_DBG("Unable to find a pending command"); |
| 3053 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 3054 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3055 | } |
| 3056 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3057 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3058 | { |
| 3059 | struct pending_cmd *cmd; |
| 3060 | |
| 3061 | BT_DBG("status %u", status); |
| 3062 | |
| 3063 | if (!status) |
| 3064 | return; |
| 3065 | |
| 3066 | cmd = find_pairing(conn); |
| 3067 | if (!cmd) |
| 3068 | BT_DBG("Unable to find a pending command"); |
| 3069 | else |
| 3070 | pairing_complete(cmd, mgmt_status(status)); |
| 3071 | } |
| 3072 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3073 | 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] | 3074 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3075 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3076 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3077 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3078 | struct pending_cmd *cmd; |
| 3079 | u8 sec_level, auth_type; |
| 3080 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3081 | int err; |
| 3082 | |
| 3083 | BT_DBG(""); |
| 3084 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3085 | memset(&rp, 0, sizeof(rp)); |
| 3086 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3087 | rp.addr.type = cp->addr.type; |
| 3088 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3089 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 3090 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3091 | MGMT_STATUS_INVALID_PARAMS, |
| 3092 | &rp, sizeof(rp)); |
| 3093 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3094 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 3095 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3096 | MGMT_STATUS_INVALID_PARAMS, |
| 3097 | &rp, sizeof(rp)); |
| 3098 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3099 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3100 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3101 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3102 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3103 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3104 | goto unlock; |
| 3105 | } |
| 3106 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3107 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3108 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3109 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3110 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3111 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3112 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3113 | } else { |
| 3114 | u8 addr_type; |
| 3115 | |
| 3116 | /* Convert from L2CAP channel address type to HCI address type |
| 3117 | */ |
| 3118 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3119 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3120 | else |
| 3121 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3122 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3123 | /* When pairing a new device, it is expected to remember |
| 3124 | * this device for future connections. Adding the connection |
| 3125 | * parameter information ahead of time allows tracking |
| 3126 | * of the slave preferred values and will speed up any |
| 3127 | * further connection establishment. |
| 3128 | * |
| 3129 | * If connection parameters already exist, then they |
| 3130 | * will be kept and this function does nothing. |
| 3131 | */ |
| 3132 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3133 | |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 3134 | /* Request a connection with master = true role */ |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3135 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 3136 | sec_level, HCI_LE_CONN_TIMEOUT, true); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3137 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3138 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3139 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3140 | int status; |
| 3141 | |
| 3142 | if (PTR_ERR(conn) == -EBUSY) |
| 3143 | status = MGMT_STATUS_BUSY; |
| 3144 | else |
| 3145 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3146 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3147 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3148 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3149 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3150 | goto unlock; |
| 3151 | } |
| 3152 | |
| 3153 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3154 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3155 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3156 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3157 | goto unlock; |
| 3158 | } |
| 3159 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3160 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3161 | if (!cmd) { |
| 3162 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3163 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3164 | goto unlock; |
| 3165 | } |
| 3166 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3167 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3168 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3169 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3170 | conn->security_cfm_cb = pairing_complete_cb; |
| 3171 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3172 | } else { |
| 3173 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3174 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3175 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3176 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3177 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3178 | conn->io_capability = cp->io_cap; |
| 3179 | cmd->user_data = conn; |
| 3180 | |
| 3181 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3182 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3183 | pairing_complete(cmd, 0); |
| 3184 | |
| 3185 | err = 0; |
| 3186 | |
| 3187 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3188 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3189 | return err; |
| 3190 | } |
| 3191 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3192 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3193 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3194 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3195 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3196 | struct pending_cmd *cmd; |
| 3197 | struct hci_conn *conn; |
| 3198 | int err; |
| 3199 | |
| 3200 | BT_DBG(""); |
| 3201 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3202 | hci_dev_lock(hdev); |
| 3203 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3204 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3205 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3206 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3207 | goto unlock; |
| 3208 | } |
| 3209 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3210 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3211 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3212 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3213 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3214 | goto unlock; |
| 3215 | } |
| 3216 | |
| 3217 | conn = cmd->user_data; |
| 3218 | |
| 3219 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3220 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3221 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3222 | goto unlock; |
| 3223 | } |
| 3224 | |
| 3225 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3226 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3227 | 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] | 3228 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3229 | unlock: |
| 3230 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3231 | return err; |
| 3232 | } |
| 3233 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3234 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3235 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3236 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3237 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3238 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3239 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3240 | int err; |
| 3241 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3242 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3243 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3244 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3245 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3246 | MGMT_STATUS_NOT_POWERED, addr, |
| 3247 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3248 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3249 | } |
| 3250 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3251 | if (addr->type == BDADDR_BREDR) |
| 3252 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3253 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3254 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3255 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3256 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3257 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3258 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3259 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3260 | goto done; |
| 3261 | } |
| 3262 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3263 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3264 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3265 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3266 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3267 | MGMT_STATUS_SUCCESS, addr, |
| 3268 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3269 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3270 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3271 | MGMT_STATUS_FAILED, addr, |
| 3272 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3273 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3274 | goto done; |
| 3275 | } |
| 3276 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3277 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3278 | if (!cmd) { |
| 3279 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3280 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3281 | } |
| 3282 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3283 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3284 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3285 | struct hci_cp_user_passkey_reply cp; |
| 3286 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3287 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3288 | cp.passkey = passkey; |
| 3289 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3290 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3291 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3292 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3293 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3294 | if (err < 0) |
| 3295 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3296 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3297 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3298 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3299 | return err; |
| 3300 | } |
| 3301 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3302 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3303 | void *data, u16 len) |
| 3304 | { |
| 3305 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3306 | |
| 3307 | BT_DBG(""); |
| 3308 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3309 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3310 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3311 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3312 | } |
| 3313 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3314 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3315 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3316 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3317 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3318 | |
| 3319 | BT_DBG(""); |
| 3320 | |
| 3321 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3322 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3323 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3324 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3325 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3326 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3327 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3328 | } |
| 3329 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3330 | 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] | 3331 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3332 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3333 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3334 | |
| 3335 | BT_DBG(""); |
| 3336 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3337 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3338 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3339 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3340 | } |
| 3341 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3342 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3343 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3344 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3345 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3346 | |
| 3347 | BT_DBG(""); |
| 3348 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3349 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3350 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3351 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3352 | } |
| 3353 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3354 | 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] | 3355 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3356 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3357 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3358 | |
| 3359 | BT_DBG(""); |
| 3360 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3361 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3362 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3363 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3364 | } |
| 3365 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3366 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3367 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3368 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3369 | struct hci_cp_write_local_name cp; |
| 3370 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3371 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3372 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3373 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3374 | } |
| 3375 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3376 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3377 | { |
| 3378 | struct mgmt_cp_set_local_name *cp; |
| 3379 | struct pending_cmd *cmd; |
| 3380 | |
| 3381 | BT_DBG("status 0x%02x", status); |
| 3382 | |
| 3383 | hci_dev_lock(hdev); |
| 3384 | |
| 3385 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3386 | if (!cmd) |
| 3387 | goto unlock; |
| 3388 | |
| 3389 | cp = cmd->param; |
| 3390 | |
| 3391 | if (status) |
| 3392 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3393 | mgmt_status(status)); |
| 3394 | else |
| 3395 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3396 | cp, sizeof(*cp)); |
| 3397 | |
| 3398 | mgmt_pending_remove(cmd); |
| 3399 | |
| 3400 | unlock: |
| 3401 | hci_dev_unlock(hdev); |
| 3402 | } |
| 3403 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3404 | 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] | 3405 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3406 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3407 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3408 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3409 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3410 | int err; |
| 3411 | |
| 3412 | BT_DBG(""); |
| 3413 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3414 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3415 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3416 | /* If the old values are the same as the new ones just return a |
| 3417 | * direct command complete event. |
| 3418 | */ |
| 3419 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3420 | !memcmp(hdev->short_name, cp->short_name, |
| 3421 | sizeof(hdev->short_name))) { |
| 3422 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3423 | data, len); |
| 3424 | goto failed; |
| 3425 | } |
| 3426 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3427 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3428 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3429 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3430 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3431 | |
| 3432 | 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] | 3433 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3434 | if (err < 0) |
| 3435 | goto failed; |
| 3436 | |
| 3437 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3438 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3439 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3440 | goto failed; |
| 3441 | } |
| 3442 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3443 | 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] | 3444 | if (!cmd) { |
| 3445 | err = -ENOMEM; |
| 3446 | goto failed; |
| 3447 | } |
| 3448 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3449 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3450 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3451 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3452 | |
| 3453 | if (lmp_bredr_capable(hdev)) { |
| 3454 | update_name(&req); |
| 3455 | update_eir(&req); |
| 3456 | } |
| 3457 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3458 | /* The name is stored in the scan response data and so |
| 3459 | * no need to udpate the advertising data here. |
| 3460 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3461 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3462 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3463 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3464 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3465 | if (err < 0) |
| 3466 | mgmt_pending_remove(cmd); |
| 3467 | |
| 3468 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3469 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3470 | return err; |
| 3471 | } |
| 3472 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3473 | 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] | 3474 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3475 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3476 | struct pending_cmd *cmd; |
| 3477 | int err; |
| 3478 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3479 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3480 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3481 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3482 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3483 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3484 | 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] | 3485 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3486 | goto unlock; |
| 3487 | } |
| 3488 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3489 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3490 | 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] | 3491 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3492 | goto unlock; |
| 3493 | } |
| 3494 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3495 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3496 | 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] | 3497 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3498 | goto unlock; |
| 3499 | } |
| 3500 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3501 | 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] | 3502 | if (!cmd) { |
| 3503 | err = -ENOMEM; |
| 3504 | goto unlock; |
| 3505 | } |
| 3506 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3507 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3508 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3509 | 0, NULL); |
| 3510 | else |
| 3511 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3512 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3513 | if (err < 0) |
| 3514 | mgmt_pending_remove(cmd); |
| 3515 | |
| 3516 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3517 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3518 | return err; |
| 3519 | } |
| 3520 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3521 | 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] | 3522 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3523 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3524 | int err; |
| 3525 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3526 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3527 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3528 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3529 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3530 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3531 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3532 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3533 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3534 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3535 | cp->hash, cp->randomizer); |
| 3536 | if (err < 0) |
| 3537 | status = MGMT_STATUS_FAILED; |
| 3538 | else |
| 3539 | status = MGMT_STATUS_SUCCESS; |
| 3540 | |
| 3541 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3542 | status, &cp->addr, sizeof(cp->addr)); |
| 3543 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3544 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3545 | u8 status; |
| 3546 | |
| 3547 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3548 | cp->hash192, |
| 3549 | cp->randomizer192, |
| 3550 | cp->hash256, |
| 3551 | cp->randomizer256); |
| 3552 | if (err < 0) |
| 3553 | status = MGMT_STATUS_FAILED; |
| 3554 | else |
| 3555 | status = MGMT_STATUS_SUCCESS; |
| 3556 | |
| 3557 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3558 | status, &cp->addr, sizeof(cp->addr)); |
| 3559 | } else { |
| 3560 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3561 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3562 | MGMT_STATUS_INVALID_PARAMS); |
| 3563 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3564 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3565 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3566 | return err; |
| 3567 | } |
| 3568 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3569 | 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] | 3570 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3571 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3572 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3573 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3574 | int err; |
| 3575 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3576 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3577 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3578 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3579 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3580 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3581 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3582 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3583 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3584 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3585 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3586 | 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] | 3587 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3588 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3589 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3590 | return err; |
| 3591 | } |
| 3592 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3593 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3594 | { |
| 3595 | struct pending_cmd *cmd; |
| 3596 | u8 type; |
| 3597 | int err; |
| 3598 | |
| 3599 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3600 | |
| 3601 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3602 | if (!cmd) |
| 3603 | return -ENOENT; |
| 3604 | |
| 3605 | type = hdev->discovery.type; |
| 3606 | |
| 3607 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3608 | &type, sizeof(type)); |
| 3609 | mgmt_pending_remove(cmd); |
| 3610 | |
| 3611 | return err; |
| 3612 | } |
| 3613 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3614 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3615 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3616 | unsigned long timeout = 0; |
| 3617 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3618 | BT_DBG("status %d", status); |
| 3619 | |
| 3620 | if (status) { |
| 3621 | hci_dev_lock(hdev); |
| 3622 | mgmt_start_discovery_failed(hdev, status); |
| 3623 | hci_dev_unlock(hdev); |
| 3624 | return; |
| 3625 | } |
| 3626 | |
| 3627 | hci_dev_lock(hdev); |
| 3628 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3629 | hci_dev_unlock(hdev); |
| 3630 | |
| 3631 | switch (hdev->discovery.type) { |
| 3632 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3633 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3634 | break; |
| 3635 | |
| 3636 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3637 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3638 | break; |
| 3639 | |
| 3640 | case DISCOV_TYPE_BREDR: |
| 3641 | break; |
| 3642 | |
| 3643 | default: |
| 3644 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3645 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3646 | |
| 3647 | if (!timeout) |
| 3648 | return; |
| 3649 | |
| 3650 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3651 | } |
| 3652 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3653 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3654 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3655 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3656 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3657 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3658 | struct hci_cp_le_set_scan_param param_cp; |
| 3659 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3660 | struct hci_cp_inquiry inq_cp; |
| 3661 | struct hci_request req; |
| 3662 | /* General inquiry access code (GIAC) */ |
| 3663 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3664 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3665 | int err; |
| 3666 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3667 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3668 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3669 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3670 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3671 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3672 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3673 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3674 | goto failed; |
| 3675 | } |
| 3676 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3677 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3678 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3679 | MGMT_STATUS_BUSY); |
| 3680 | goto failed; |
| 3681 | } |
| 3682 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3683 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3684 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3685 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3686 | goto failed; |
| 3687 | } |
| 3688 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3689 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3690 | if (!cmd) { |
| 3691 | err = -ENOMEM; |
| 3692 | goto failed; |
| 3693 | } |
| 3694 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3695 | hdev->discovery.type = cp->type; |
| 3696 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3697 | hci_req_init(&req, hdev); |
| 3698 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3699 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3700 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3701 | status = mgmt_bredr_support(hdev); |
| 3702 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3703 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3704 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3705 | mgmt_pending_remove(cmd); |
| 3706 | goto failed; |
| 3707 | } |
| 3708 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3709 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3710 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3711 | MGMT_STATUS_BUSY); |
| 3712 | mgmt_pending_remove(cmd); |
| 3713 | goto failed; |
| 3714 | } |
| 3715 | |
| 3716 | hci_inquiry_cache_flush(hdev); |
| 3717 | |
| 3718 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3719 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3720 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3721 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3722 | break; |
| 3723 | |
| 3724 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3725 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3726 | status = mgmt_le_support(hdev); |
| 3727 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3728 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3729 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3730 | mgmt_pending_remove(cmd); |
| 3731 | goto failed; |
| 3732 | } |
| 3733 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3734 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3735 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3736 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3737 | MGMT_STATUS_NOT_SUPPORTED); |
| 3738 | mgmt_pending_remove(cmd); |
| 3739 | goto failed; |
| 3740 | } |
| 3741 | |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 3742 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) { |
| 3743 | /* Don't let discovery abort an outgoing |
| 3744 | * connection attempt that's using directed |
| 3745 | * advertising. |
| 3746 | */ |
| 3747 | if (hci_conn_hash_lookup_state(hdev, LE_LINK, |
| 3748 | BT_CONNECT)) { |
| 3749 | err = cmd_status(sk, hdev->id, |
| 3750 | MGMT_OP_START_DISCOVERY, |
| 3751 | MGMT_STATUS_REJECTED); |
| 3752 | mgmt_pending_remove(cmd); |
| 3753 | goto failed; |
| 3754 | } |
| 3755 | |
| 3756 | disable_advertising(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3757 | } |
| 3758 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3759 | /* If controller is scanning, it means the background scanning |
| 3760 | * is running. Thus, we should temporarily stop it in order to |
| 3761 | * set the discovery scanning parameters. |
| 3762 | */ |
| 3763 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3764 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3765 | |
| 3766 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3767 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3768 | /* All active scans will be done with either a resolvable |
| 3769 | * private address (when privacy feature has been enabled) |
| 3770 | * or unresolvable private address. |
| 3771 | */ |
| 3772 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3773 | if (err < 0) { |
| 3774 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3775 | MGMT_STATUS_FAILED); |
| 3776 | mgmt_pending_remove(cmd); |
| 3777 | goto failed; |
| 3778 | } |
| 3779 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3780 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3781 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3782 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3783 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3784 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3785 | ¶m_cp); |
| 3786 | |
| 3787 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3788 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3789 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3790 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3791 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3792 | break; |
| 3793 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3794 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3795 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3796 | MGMT_STATUS_INVALID_PARAMS); |
| 3797 | mgmt_pending_remove(cmd); |
| 3798 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3799 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3800 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3801 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3802 | if (err < 0) |
| 3803 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3804 | else |
| 3805 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3806 | |
| 3807 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3808 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3809 | return err; |
| 3810 | } |
| 3811 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3812 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3813 | { |
| 3814 | struct pending_cmd *cmd; |
| 3815 | int err; |
| 3816 | |
| 3817 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3818 | if (!cmd) |
| 3819 | return -ENOENT; |
| 3820 | |
| 3821 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3822 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3823 | mgmt_pending_remove(cmd); |
| 3824 | |
| 3825 | return err; |
| 3826 | } |
| 3827 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3828 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3829 | { |
| 3830 | BT_DBG("status %d", status); |
| 3831 | |
| 3832 | hci_dev_lock(hdev); |
| 3833 | |
| 3834 | if (status) { |
| 3835 | mgmt_stop_discovery_failed(hdev, status); |
| 3836 | goto unlock; |
| 3837 | } |
| 3838 | |
| 3839 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3840 | |
| 3841 | unlock: |
| 3842 | hci_dev_unlock(hdev); |
| 3843 | } |
| 3844 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3845 | 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] | 3846 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3847 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3848 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3849 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3850 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3851 | int err; |
| 3852 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3853 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3854 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3855 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3856 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3857 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3858 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3859 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3860 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3861 | goto unlock; |
| 3862 | } |
| 3863 | |
| 3864 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3865 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3866 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3867 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3868 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3869 | } |
| 3870 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3871 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3872 | if (!cmd) { |
| 3873 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3874 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3875 | } |
| 3876 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3877 | hci_req_init(&req, hdev); |
| 3878 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3879 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3880 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3881 | err = hci_req_run(&req, stop_discovery_complete); |
| 3882 | if (!err) { |
| 3883 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3884 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3885 | } |
| 3886 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3887 | mgmt_pending_remove(cmd); |
| 3888 | |
| 3889 | /* If no HCI commands were sent we're done */ |
| 3890 | if (err == -ENODATA) { |
| 3891 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3892 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3893 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3894 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3895 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3896 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3897 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3898 | return err; |
| 3899 | } |
| 3900 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3901 | 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] | 3902 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3903 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3904 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3905 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3906 | int err; |
| 3907 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3908 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3909 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3910 | hci_dev_lock(hdev); |
| 3911 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3912 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3913 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3914 | MGMT_STATUS_FAILED, &cp->addr, |
| 3915 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3916 | goto failed; |
| 3917 | } |
| 3918 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3919 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3920 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3921 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3922 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3923 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3924 | goto failed; |
| 3925 | } |
| 3926 | |
| 3927 | if (cp->name_known) { |
| 3928 | e->name_state = NAME_KNOWN; |
| 3929 | list_del(&e->list); |
| 3930 | } else { |
| 3931 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3932 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3933 | } |
| 3934 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3935 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3936 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3937 | |
| 3938 | failed: |
| 3939 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3940 | return err; |
| 3941 | } |
| 3942 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3943 | 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] | 3944 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3945 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3946 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3947 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3948 | int err; |
| 3949 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3950 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3951 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3952 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3953 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3954 | MGMT_STATUS_INVALID_PARAMS, |
| 3955 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3956 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3957 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3958 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 3959 | err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr, |
| 3960 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3961 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3962 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3963 | goto done; |
| 3964 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3965 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3966 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3967 | sk); |
| 3968 | status = MGMT_STATUS_SUCCESS; |
| 3969 | |
| 3970 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3971 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3972 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3973 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3974 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3975 | |
| 3976 | return err; |
| 3977 | } |
| 3978 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3979 | 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] | 3980 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3981 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3982 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3983 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3984 | int err; |
| 3985 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3986 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3987 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3988 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3989 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3990 | MGMT_STATUS_INVALID_PARAMS, |
| 3991 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3992 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3993 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3994 | |
Johan Hedberg | dcc36c1 | 2014-07-09 12:59:13 +0300 | [diff] [blame] | 3995 | err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr, |
| 3996 | cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3997 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3998 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3999 | goto done; |
| 4000 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 4001 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 4002 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 4003 | sk); |
| 4004 | status = MGMT_STATUS_SUCCESS; |
| 4005 | |
| 4006 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4007 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4008 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 4009 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 4010 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 4011 | |
| 4012 | return err; |
| 4013 | } |
| 4014 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4015 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4016 | u16 len) |
| 4017 | { |
| 4018 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4019 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4020 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4021 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4022 | |
| 4023 | BT_DBG("%s", hdev->name); |
| 4024 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4025 | source = __le16_to_cpu(cp->source); |
| 4026 | |
| 4027 | if (source > 0x0002) |
| 4028 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4029 | MGMT_STATUS_INVALID_PARAMS); |
| 4030 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4031 | hci_dev_lock(hdev); |
| 4032 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4033 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4034 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4035 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4036 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4037 | |
| 4038 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 4039 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4040 | hci_req_init(&req, hdev); |
| 4041 | update_eir(&req); |
| 4042 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4043 | |
| 4044 | hci_dev_unlock(hdev); |
| 4045 | |
| 4046 | return err; |
| 4047 | } |
| 4048 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4049 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 4050 | { |
| 4051 | struct cmd_lookup match = { NULL, hdev }; |
| 4052 | |
| 4053 | if (status) { |
| 4054 | u8 mgmt_err = mgmt_status(status); |
| 4055 | |
| 4056 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4057 | cmd_status_rsp, &mgmt_err); |
| 4058 | return; |
| 4059 | } |
| 4060 | |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4061 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
| 4062 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4063 | else |
| 4064 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4065 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4066 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4067 | &match); |
| 4068 | |
| 4069 | new_settings(hdev, match.sk); |
| 4070 | |
| 4071 | if (match.sk) |
| 4072 | sock_put(match.sk); |
| 4073 | } |
| 4074 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4075 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4076 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4077 | { |
| 4078 | struct mgmt_mode *cp = data; |
| 4079 | struct pending_cmd *cmd; |
| 4080 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4081 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4082 | int err; |
| 4083 | |
| 4084 | BT_DBG("request for %s", hdev->name); |
| 4085 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4086 | status = mgmt_le_support(hdev); |
| 4087 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4088 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4089 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4090 | |
| 4091 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4092 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4093 | MGMT_STATUS_INVALID_PARAMS); |
| 4094 | |
| 4095 | hci_dev_lock(hdev); |
| 4096 | |
| 4097 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4098 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4099 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4100 | /* The following conditions are ones which mean that we should |
| 4101 | * not do any HCI communication but directly send a mgmt |
| 4102 | * response to user space (after toggling the flag if |
| 4103 | * necessary). |
| 4104 | */ |
| 4105 | if (!hdev_is_powered(hdev) || val == enabled || |
Johan Hedberg | e8bb6b9 | 2014-07-08 15:07:53 +0300 | [diff] [blame] | 4106 | hci_conn_num(hdev, LE_LINK) > 0 || |
| 4107 | (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4108 | hdev->le_scan_type == LE_SCAN_ACTIVE)) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4109 | bool changed = false; |
| 4110 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4111 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 4112 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4113 | changed = true; |
| 4114 | } |
| 4115 | |
| 4116 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4117 | if (err < 0) |
| 4118 | goto unlock; |
| 4119 | |
| 4120 | if (changed) |
| 4121 | err = new_settings(hdev, sk); |
| 4122 | |
| 4123 | goto unlock; |
| 4124 | } |
| 4125 | |
| 4126 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4127 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 4128 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4129 | MGMT_STATUS_BUSY); |
| 4130 | goto unlock; |
| 4131 | } |
| 4132 | |
| 4133 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4134 | if (!cmd) { |
| 4135 | err = -ENOMEM; |
| 4136 | goto unlock; |
| 4137 | } |
| 4138 | |
| 4139 | hci_req_init(&req, hdev); |
| 4140 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4141 | if (val) |
| 4142 | enable_advertising(&req); |
| 4143 | else |
| 4144 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4145 | |
| 4146 | err = hci_req_run(&req, set_advertising_complete); |
| 4147 | if (err < 0) |
| 4148 | mgmt_pending_remove(cmd); |
| 4149 | |
| 4150 | unlock: |
| 4151 | hci_dev_unlock(hdev); |
| 4152 | return err; |
| 4153 | } |
| 4154 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4155 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4156 | void *data, u16 len) |
| 4157 | { |
| 4158 | struct mgmt_cp_set_static_address *cp = data; |
| 4159 | int err; |
| 4160 | |
| 4161 | BT_DBG("%s", hdev->name); |
| 4162 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4163 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4164 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4165 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4166 | |
| 4167 | if (hdev_is_powered(hdev)) |
| 4168 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4169 | MGMT_STATUS_REJECTED); |
| 4170 | |
| 4171 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4172 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 4173 | return cmd_status(sk, hdev->id, |
| 4174 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4175 | MGMT_STATUS_INVALID_PARAMS); |
| 4176 | |
| 4177 | /* Two most significant bits shall be set */ |
| 4178 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4179 | return cmd_status(sk, hdev->id, |
| 4180 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4181 | MGMT_STATUS_INVALID_PARAMS); |
| 4182 | } |
| 4183 | |
| 4184 | hci_dev_lock(hdev); |
| 4185 | |
| 4186 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4187 | |
| 4188 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4189 | |
| 4190 | hci_dev_unlock(hdev); |
| 4191 | |
| 4192 | return err; |
| 4193 | } |
| 4194 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4195 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4196 | void *data, u16 len) |
| 4197 | { |
| 4198 | struct mgmt_cp_set_scan_params *cp = data; |
| 4199 | __u16 interval, window; |
| 4200 | int err; |
| 4201 | |
| 4202 | BT_DBG("%s", hdev->name); |
| 4203 | |
| 4204 | if (!lmp_le_capable(hdev)) |
| 4205 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4206 | MGMT_STATUS_NOT_SUPPORTED); |
| 4207 | |
| 4208 | interval = __le16_to_cpu(cp->interval); |
| 4209 | |
| 4210 | if (interval < 0x0004 || interval > 0x4000) |
| 4211 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4212 | MGMT_STATUS_INVALID_PARAMS); |
| 4213 | |
| 4214 | window = __le16_to_cpu(cp->window); |
| 4215 | |
| 4216 | if (window < 0x0004 || window > 0x4000) |
| 4217 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4218 | MGMT_STATUS_INVALID_PARAMS); |
| 4219 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4220 | if (window > interval) |
| 4221 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4222 | MGMT_STATUS_INVALID_PARAMS); |
| 4223 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4224 | hci_dev_lock(hdev); |
| 4225 | |
| 4226 | hdev->le_scan_interval = interval; |
| 4227 | hdev->le_scan_window = window; |
| 4228 | |
| 4229 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4230 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4231 | /* If background scan is running, restart it so new parameters are |
| 4232 | * loaded. |
| 4233 | */ |
| 4234 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4235 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4236 | struct hci_request req; |
| 4237 | |
| 4238 | hci_req_init(&req, hdev); |
| 4239 | |
| 4240 | hci_req_add_le_scan_disable(&req); |
| 4241 | hci_req_add_le_passive_scan(&req); |
| 4242 | |
| 4243 | hci_req_run(&req, NULL); |
| 4244 | } |
| 4245 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4246 | hci_dev_unlock(hdev); |
| 4247 | |
| 4248 | return err; |
| 4249 | } |
| 4250 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4251 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4252 | { |
| 4253 | struct pending_cmd *cmd; |
| 4254 | |
| 4255 | BT_DBG("status 0x%02x", status); |
| 4256 | |
| 4257 | hci_dev_lock(hdev); |
| 4258 | |
| 4259 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4260 | if (!cmd) |
| 4261 | goto unlock; |
| 4262 | |
| 4263 | if (status) { |
| 4264 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4265 | mgmt_status(status)); |
| 4266 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4267 | struct mgmt_mode *cp = cmd->param; |
| 4268 | |
| 4269 | if (cp->val) |
| 4270 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4271 | else |
| 4272 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4273 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4274 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4275 | new_settings(hdev, cmd->sk); |
| 4276 | } |
| 4277 | |
| 4278 | mgmt_pending_remove(cmd); |
| 4279 | |
| 4280 | unlock: |
| 4281 | hci_dev_unlock(hdev); |
| 4282 | } |
| 4283 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4284 | 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] | 4285 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4286 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4287 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4288 | struct pending_cmd *cmd; |
| 4289 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4290 | int err; |
| 4291 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4292 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4293 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4294 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4295 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4296 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4297 | MGMT_STATUS_NOT_SUPPORTED); |
| 4298 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4299 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4300 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4301 | MGMT_STATUS_INVALID_PARAMS); |
| 4302 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4303 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4304 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4305 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4306 | |
| 4307 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4308 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4309 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4310 | |
| 4311 | hci_dev_lock(hdev); |
| 4312 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4313 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4314 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4315 | MGMT_STATUS_BUSY); |
| 4316 | goto unlock; |
| 4317 | } |
| 4318 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4319 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4320 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4321 | hdev); |
| 4322 | goto unlock; |
| 4323 | } |
| 4324 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4325 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4326 | data, len); |
| 4327 | if (!cmd) { |
| 4328 | err = -ENOMEM; |
| 4329 | goto unlock; |
| 4330 | } |
| 4331 | |
| 4332 | hci_req_init(&req, hdev); |
| 4333 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4334 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4335 | |
| 4336 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4337 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4338 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4339 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4340 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4341 | } |
| 4342 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4343 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4344 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4345 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4346 | return err; |
| 4347 | } |
| 4348 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4349 | static void set_bredr_scan(struct hci_request *req) |
| 4350 | { |
| 4351 | struct hci_dev *hdev = req->hdev; |
| 4352 | u8 scan = 0; |
| 4353 | |
| 4354 | /* Ensure that fast connectable is disabled. This function will |
| 4355 | * not do anything if the page scan parameters are already what |
| 4356 | * they should be. |
| 4357 | */ |
| 4358 | write_fast_connectable(req, false); |
| 4359 | |
| 4360 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4361 | scan |= SCAN_PAGE; |
| 4362 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4363 | scan |= SCAN_INQUIRY; |
| 4364 | |
| 4365 | if (scan) |
| 4366 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4367 | } |
| 4368 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4369 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4370 | { |
| 4371 | struct pending_cmd *cmd; |
| 4372 | |
| 4373 | BT_DBG("status 0x%02x", status); |
| 4374 | |
| 4375 | hci_dev_lock(hdev); |
| 4376 | |
| 4377 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4378 | if (!cmd) |
| 4379 | goto unlock; |
| 4380 | |
| 4381 | if (status) { |
| 4382 | u8 mgmt_err = mgmt_status(status); |
| 4383 | |
| 4384 | /* We need to restore the flag if related HCI commands |
| 4385 | * failed. |
| 4386 | */ |
| 4387 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4388 | |
| 4389 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4390 | } else { |
| 4391 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4392 | new_settings(hdev, cmd->sk); |
| 4393 | } |
| 4394 | |
| 4395 | mgmt_pending_remove(cmd); |
| 4396 | |
| 4397 | unlock: |
| 4398 | hci_dev_unlock(hdev); |
| 4399 | } |
| 4400 | |
| 4401 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4402 | { |
| 4403 | struct mgmt_mode *cp = data; |
| 4404 | struct pending_cmd *cmd; |
| 4405 | struct hci_request req; |
| 4406 | int err; |
| 4407 | |
| 4408 | BT_DBG("request for %s", hdev->name); |
| 4409 | |
| 4410 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4411 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4412 | MGMT_STATUS_NOT_SUPPORTED); |
| 4413 | |
| 4414 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4415 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4416 | MGMT_STATUS_REJECTED); |
| 4417 | |
| 4418 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4419 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4420 | MGMT_STATUS_INVALID_PARAMS); |
| 4421 | |
| 4422 | hci_dev_lock(hdev); |
| 4423 | |
| 4424 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4425 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4426 | goto unlock; |
| 4427 | } |
| 4428 | |
| 4429 | if (!hdev_is_powered(hdev)) { |
| 4430 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4431 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4432 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4433 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4434 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4435 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4436 | } |
| 4437 | |
| 4438 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4439 | |
| 4440 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4441 | if (err < 0) |
| 4442 | goto unlock; |
| 4443 | |
| 4444 | err = new_settings(hdev, sk); |
| 4445 | goto unlock; |
| 4446 | } |
| 4447 | |
| 4448 | /* Reject disabling when powered on */ |
| 4449 | if (!cp->val) { |
| 4450 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4451 | MGMT_STATUS_REJECTED); |
| 4452 | goto unlock; |
| 4453 | } |
| 4454 | |
| 4455 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4456 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4457 | MGMT_STATUS_BUSY); |
| 4458 | goto unlock; |
| 4459 | } |
| 4460 | |
| 4461 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4462 | if (!cmd) { |
| 4463 | err = -ENOMEM; |
| 4464 | goto unlock; |
| 4465 | } |
| 4466 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4467 | /* 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] | 4468 | * generates the correct flags. |
| 4469 | */ |
| 4470 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4471 | |
| 4472 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4473 | |
| 4474 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4475 | set_bredr_scan(&req); |
| 4476 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4477 | /* Since only the advertising data flags will change, there |
| 4478 | * is no need to update the scan response data. |
| 4479 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4480 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4481 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4482 | err = hci_req_run(&req, set_bredr_complete); |
| 4483 | if (err < 0) |
| 4484 | mgmt_pending_remove(cmd); |
| 4485 | |
| 4486 | unlock: |
| 4487 | hci_dev_unlock(hdev); |
| 4488 | return err; |
| 4489 | } |
| 4490 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4491 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4492 | void *data, u16 len) |
| 4493 | { |
| 4494 | struct mgmt_mode *cp = data; |
| 4495 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4496 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4497 | int err; |
| 4498 | |
| 4499 | BT_DBG("request for %s", hdev->name); |
| 4500 | |
| 4501 | status = mgmt_bredr_support(hdev); |
| 4502 | if (status) |
| 4503 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4504 | status); |
| 4505 | |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4506 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4507 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4508 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4509 | MGMT_STATUS_NOT_SUPPORTED); |
| 4510 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4511 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4512 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4513 | MGMT_STATUS_INVALID_PARAMS); |
| 4514 | |
| 4515 | hci_dev_lock(hdev); |
| 4516 | |
| 4517 | if (!hdev_is_powered(hdev)) { |
| 4518 | bool changed; |
| 4519 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4520 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4521 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4522 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4523 | if (cp->val == 0x02) |
| 4524 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4525 | else |
| 4526 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4527 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4528 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4529 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4530 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4531 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4532 | |
| 4533 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4534 | if (err < 0) |
| 4535 | goto failed; |
| 4536 | |
| 4537 | if (changed) |
| 4538 | err = new_settings(hdev, sk); |
| 4539 | |
| 4540 | goto failed; |
| 4541 | } |
| 4542 | |
| 4543 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4544 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4545 | MGMT_STATUS_BUSY); |
| 4546 | goto failed; |
| 4547 | } |
| 4548 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4549 | val = !!cp->val; |
| 4550 | |
| 4551 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4552 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4553 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4554 | goto failed; |
| 4555 | } |
| 4556 | |
| 4557 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4558 | if (!cmd) { |
| 4559 | err = -ENOMEM; |
| 4560 | goto failed; |
| 4561 | } |
| 4562 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4563 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4564 | if (err < 0) { |
| 4565 | mgmt_pending_remove(cmd); |
| 4566 | goto failed; |
| 4567 | } |
| 4568 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4569 | if (cp->val == 0x02) |
| 4570 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4571 | else |
| 4572 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4573 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4574 | failed: |
| 4575 | hci_dev_unlock(hdev); |
| 4576 | return err; |
| 4577 | } |
| 4578 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4579 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4580 | void *data, u16 len) |
| 4581 | { |
| 4582 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4583 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4584 | int err; |
| 4585 | |
| 4586 | BT_DBG("request for %s", hdev->name); |
| 4587 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4588 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4589 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4590 | MGMT_STATUS_INVALID_PARAMS); |
| 4591 | |
| 4592 | hci_dev_lock(hdev); |
| 4593 | |
| 4594 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4595 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4596 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4597 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4598 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4599 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4600 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4601 | if (cp->val == 0x02) |
| 4602 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4603 | &hdev->dev_flags); |
| 4604 | else |
| 4605 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4606 | &hdev->dev_flags); |
| 4607 | |
| 4608 | if (hdev_is_powered(hdev) && use_changed && |
| 4609 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4610 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4611 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4612 | sizeof(mode), &mode); |
| 4613 | } |
| 4614 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4615 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4616 | if (err < 0) |
| 4617 | goto unlock; |
| 4618 | |
| 4619 | if (changed) |
| 4620 | err = new_settings(hdev, sk); |
| 4621 | |
| 4622 | unlock: |
| 4623 | hci_dev_unlock(hdev); |
| 4624 | return err; |
| 4625 | } |
| 4626 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4627 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4628 | u16 len) |
| 4629 | { |
| 4630 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4631 | bool changed; |
| 4632 | int err; |
| 4633 | |
| 4634 | BT_DBG("request for %s", hdev->name); |
| 4635 | |
| 4636 | if (!lmp_le_capable(hdev)) |
| 4637 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4638 | MGMT_STATUS_NOT_SUPPORTED); |
| 4639 | |
| 4640 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4641 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4642 | MGMT_STATUS_INVALID_PARAMS); |
| 4643 | |
| 4644 | if (hdev_is_powered(hdev)) |
| 4645 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4646 | MGMT_STATUS_REJECTED); |
| 4647 | |
| 4648 | hci_dev_lock(hdev); |
| 4649 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4650 | /* If user space supports this command it is also expected to |
| 4651 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4652 | */ |
| 4653 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4654 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4655 | if (cp->privacy) { |
| 4656 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4657 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4658 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4659 | } else { |
| 4660 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4661 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4662 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4663 | } |
| 4664 | |
| 4665 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4666 | if (err < 0) |
| 4667 | goto unlock; |
| 4668 | |
| 4669 | if (changed) |
| 4670 | err = new_settings(hdev, sk); |
| 4671 | |
| 4672 | unlock: |
| 4673 | hci_dev_unlock(hdev); |
| 4674 | return err; |
| 4675 | } |
| 4676 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4677 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4678 | { |
| 4679 | switch (irk->addr.type) { |
| 4680 | case BDADDR_LE_PUBLIC: |
| 4681 | return true; |
| 4682 | |
| 4683 | case BDADDR_LE_RANDOM: |
| 4684 | /* Two most significant bits shall be set */ |
| 4685 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4686 | return false; |
| 4687 | return true; |
| 4688 | } |
| 4689 | |
| 4690 | return false; |
| 4691 | } |
| 4692 | |
| 4693 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4694 | u16 len) |
| 4695 | { |
| 4696 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4697 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4698 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4699 | u16 irk_count, expected_len; |
| 4700 | int i, err; |
| 4701 | |
| 4702 | BT_DBG("request for %s", hdev->name); |
| 4703 | |
| 4704 | if (!lmp_le_capable(hdev)) |
| 4705 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4706 | MGMT_STATUS_NOT_SUPPORTED); |
| 4707 | |
| 4708 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4709 | if (irk_count > max_irk_count) { |
| 4710 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
| 4711 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4712 | MGMT_STATUS_INVALID_PARAMS); |
| 4713 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4714 | |
| 4715 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4716 | if (expected_len != len) { |
| 4717 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4718 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4719 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4720 | MGMT_STATUS_INVALID_PARAMS); |
| 4721 | } |
| 4722 | |
| 4723 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4724 | |
| 4725 | for (i = 0; i < irk_count; i++) { |
| 4726 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4727 | |
| 4728 | if (!irk_is_valid(key)) |
| 4729 | return cmd_status(sk, hdev->id, |
| 4730 | MGMT_OP_LOAD_IRKS, |
| 4731 | MGMT_STATUS_INVALID_PARAMS); |
| 4732 | } |
| 4733 | |
| 4734 | hci_dev_lock(hdev); |
| 4735 | |
| 4736 | hci_smp_irks_clear(hdev); |
| 4737 | |
| 4738 | for (i = 0; i < irk_count; i++) { |
| 4739 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4740 | u8 addr_type; |
| 4741 | |
| 4742 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4743 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4744 | else |
| 4745 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4746 | |
| 4747 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4748 | BDADDR_ANY); |
| 4749 | } |
| 4750 | |
| 4751 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4752 | |
| 4753 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4754 | |
| 4755 | hci_dev_unlock(hdev); |
| 4756 | |
| 4757 | return err; |
| 4758 | } |
| 4759 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4760 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4761 | { |
| 4762 | if (key->master != 0x00 && key->master != 0x01) |
| 4763 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4764 | |
| 4765 | switch (key->addr.type) { |
| 4766 | case BDADDR_LE_PUBLIC: |
| 4767 | return true; |
| 4768 | |
| 4769 | case BDADDR_LE_RANDOM: |
| 4770 | /* Two most significant bits shall be set */ |
| 4771 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4772 | return false; |
| 4773 | return true; |
| 4774 | } |
| 4775 | |
| 4776 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4777 | } |
| 4778 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4779 | 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] | 4780 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4781 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4782 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4783 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4784 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4785 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4786 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4787 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4788 | BT_DBG("request for %s", hdev->name); |
| 4789 | |
| 4790 | if (!lmp_le_capable(hdev)) |
| 4791 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4792 | MGMT_STATUS_NOT_SUPPORTED); |
| 4793 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4794 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4795 | if (key_count > max_key_count) { |
| 4796 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
| 4797 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4798 | MGMT_STATUS_INVALID_PARAMS); |
| 4799 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4800 | |
| 4801 | expected_len = sizeof(*cp) + key_count * |
| 4802 | sizeof(struct mgmt_ltk_info); |
| 4803 | if (expected_len != len) { |
| 4804 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4805 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4806 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4807 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4808 | } |
| 4809 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4810 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4811 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4812 | for (i = 0; i < key_count; i++) { |
| 4813 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4814 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4815 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4816 | return cmd_status(sk, hdev->id, |
| 4817 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4818 | MGMT_STATUS_INVALID_PARAMS); |
| 4819 | } |
| 4820 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4821 | hci_dev_lock(hdev); |
| 4822 | |
| 4823 | hci_smp_ltks_clear(hdev); |
| 4824 | |
| 4825 | for (i = 0; i < key_count; i++) { |
| 4826 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4827 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4828 | |
| 4829 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4830 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4831 | else |
| 4832 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4833 | |
| 4834 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4835 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4836 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4837 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4838 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4839 | switch (key->type) { |
| 4840 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4841 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4842 | break; |
| 4843 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4844 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4845 | break; |
| 4846 | default: |
| 4847 | continue; |
| 4848 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4849 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4850 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4851 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4852 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4853 | } |
| 4854 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4855 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4856 | NULL, 0); |
| 4857 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4858 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4859 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4860 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4861 | } |
| 4862 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4863 | struct cmd_conn_lookup { |
| 4864 | struct hci_conn *conn; |
| 4865 | bool valid_tx_power; |
| 4866 | u8 mgmt_status; |
| 4867 | }; |
| 4868 | |
| 4869 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4870 | { |
| 4871 | struct cmd_conn_lookup *match = data; |
| 4872 | struct mgmt_cp_get_conn_info *cp; |
| 4873 | struct mgmt_rp_get_conn_info rp; |
| 4874 | struct hci_conn *conn = cmd->user_data; |
| 4875 | |
| 4876 | if (conn != match->conn) |
| 4877 | return; |
| 4878 | |
| 4879 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4880 | |
| 4881 | memset(&rp, 0, sizeof(rp)); |
| 4882 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4883 | rp.addr.type = cp->addr.type; |
| 4884 | |
| 4885 | if (!match->mgmt_status) { |
| 4886 | rp.rssi = conn->rssi; |
| 4887 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4888 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4889 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4890 | rp.max_tx_power = conn->max_tx_power; |
| 4891 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4892 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4893 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4894 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4898 | match->mgmt_status, &rp, sizeof(rp)); |
| 4899 | |
| 4900 | hci_conn_drop(conn); |
| 4901 | |
| 4902 | mgmt_pending_remove(cmd); |
| 4903 | } |
| 4904 | |
| 4905 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4906 | { |
| 4907 | struct hci_cp_read_rssi *cp; |
| 4908 | struct hci_conn *conn; |
| 4909 | struct cmd_conn_lookup match; |
| 4910 | u16 handle; |
| 4911 | |
| 4912 | BT_DBG("status 0x%02x", status); |
| 4913 | |
| 4914 | hci_dev_lock(hdev); |
| 4915 | |
| 4916 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4917 | * otherwise we assume it's not valid. At the moment we assume that |
| 4918 | * either both or none of current and max values are valid to keep code |
| 4919 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4920 | */ |
| 4921 | match.valid_tx_power = !status; |
| 4922 | |
| 4923 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4924 | * Level so we check which one was last sent to retrieve connection |
| 4925 | * handle. Both commands have handle as first parameter so it's safe to |
| 4926 | * cast data on the same command struct. |
| 4927 | * |
| 4928 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4929 | * In other case we simply override error to indicate success as we |
| 4930 | * already remembered if TX power value is actually valid. |
| 4931 | */ |
| 4932 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4933 | if (!cp) { |
| 4934 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4935 | status = 0; |
| 4936 | } |
| 4937 | |
| 4938 | if (!cp) { |
| 4939 | BT_ERR("invalid sent_cmd in response"); |
| 4940 | goto unlock; |
| 4941 | } |
| 4942 | |
| 4943 | handle = __le16_to_cpu(cp->handle); |
| 4944 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4945 | if (!conn) { |
| 4946 | BT_ERR("unknown handle (%d) in response", handle); |
| 4947 | goto unlock; |
| 4948 | } |
| 4949 | |
| 4950 | match.conn = conn; |
| 4951 | match.mgmt_status = mgmt_status(status); |
| 4952 | |
| 4953 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4954 | * connection only. |
| 4955 | */ |
| 4956 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4957 | get_conn_info_complete, &match); |
| 4958 | |
| 4959 | unlock: |
| 4960 | hci_dev_unlock(hdev); |
| 4961 | } |
| 4962 | |
| 4963 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4964 | u16 len) |
| 4965 | { |
| 4966 | struct mgmt_cp_get_conn_info *cp = data; |
| 4967 | struct mgmt_rp_get_conn_info rp; |
| 4968 | struct hci_conn *conn; |
| 4969 | unsigned long conn_info_age; |
| 4970 | int err = 0; |
| 4971 | |
| 4972 | BT_DBG("%s", hdev->name); |
| 4973 | |
| 4974 | memset(&rp, 0, sizeof(rp)); |
| 4975 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4976 | rp.addr.type = cp->addr.type; |
| 4977 | |
| 4978 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4979 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4980 | MGMT_STATUS_INVALID_PARAMS, |
| 4981 | &rp, sizeof(rp)); |
| 4982 | |
| 4983 | hci_dev_lock(hdev); |
| 4984 | |
| 4985 | if (!hdev_is_powered(hdev)) { |
| 4986 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4987 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4988 | goto unlock; |
| 4989 | } |
| 4990 | |
| 4991 | if (cp->addr.type == BDADDR_BREDR) |
| 4992 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4993 | &cp->addr.bdaddr); |
| 4994 | else |
| 4995 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4996 | |
| 4997 | if (!conn || conn->state != BT_CONNECTED) { |
| 4998 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4999 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 5000 | goto unlock; |
| 5001 | } |
| 5002 | |
| 5003 | /* To avoid client trying to guess when to poll again for information we |
| 5004 | * calculate conn info age as random value between min/max set in hdev. |
| 5005 | */ |
| 5006 | conn_info_age = hdev->conn_info_min_age + |
| 5007 | prandom_u32_max(hdev->conn_info_max_age - |
| 5008 | hdev->conn_info_min_age); |
| 5009 | |
| 5010 | /* Query controller to refresh cached values if they are too old or were |
| 5011 | * never read. |
| 5012 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 5013 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 5014 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5015 | !conn->conn_info_timestamp) { |
| 5016 | struct hci_request req; |
| 5017 | struct hci_cp_read_tx_power req_txp_cp; |
| 5018 | struct hci_cp_read_rssi req_rssi_cp; |
| 5019 | struct pending_cmd *cmd; |
| 5020 | |
| 5021 | hci_req_init(&req, hdev); |
| 5022 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 5023 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 5024 | &req_rssi_cp); |
| 5025 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 5026 | /* For LE links TX power does not change thus we don't need to |
| 5027 | * query for it once value is known. |
| 5028 | */ |
| 5029 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5030 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5031 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5032 | req_txp_cp.type = 0x00; |
| 5033 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5034 | sizeof(req_txp_cp), &req_txp_cp); |
| 5035 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5036 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5037 | /* Max TX power needs to be read only once per connection */ |
| 5038 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5039 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5040 | req_txp_cp.type = 0x01; |
| 5041 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5042 | sizeof(req_txp_cp), &req_txp_cp); |
| 5043 | } |
| 5044 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5045 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5046 | if (err < 0) |
| 5047 | goto unlock; |
| 5048 | |
| 5049 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5050 | data, len); |
| 5051 | if (!cmd) { |
| 5052 | err = -ENOMEM; |
| 5053 | goto unlock; |
| 5054 | } |
| 5055 | |
| 5056 | hci_conn_hold(conn); |
| 5057 | cmd->user_data = conn; |
| 5058 | |
| 5059 | conn->conn_info_timestamp = jiffies; |
| 5060 | } else { |
| 5061 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5062 | rp.rssi = conn->rssi; |
| 5063 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5064 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5065 | |
| 5066 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5067 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 5068 | } |
| 5069 | |
| 5070 | unlock: |
| 5071 | hci_dev_unlock(hdev); |
| 5072 | return err; |
| 5073 | } |
| 5074 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5075 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 5076 | { |
| 5077 | struct mgmt_cp_get_clock_info *cp; |
| 5078 | struct mgmt_rp_get_clock_info rp; |
| 5079 | struct hci_cp_read_clock *hci_cp; |
| 5080 | struct pending_cmd *cmd; |
| 5081 | struct hci_conn *conn; |
| 5082 | |
| 5083 | BT_DBG("%s status %u", hdev->name, status); |
| 5084 | |
| 5085 | hci_dev_lock(hdev); |
| 5086 | |
| 5087 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5088 | if (!hci_cp) |
| 5089 | goto unlock; |
| 5090 | |
| 5091 | if (hci_cp->which) { |
| 5092 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5093 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5094 | } else { |
| 5095 | conn = NULL; |
| 5096 | } |
| 5097 | |
| 5098 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5099 | if (!cmd) |
| 5100 | goto unlock; |
| 5101 | |
| 5102 | cp = cmd->param; |
| 5103 | |
| 5104 | memset(&rp, 0, sizeof(rp)); |
| 5105 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 5106 | |
| 5107 | if (status) |
| 5108 | goto send_rsp; |
| 5109 | |
| 5110 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5111 | |
| 5112 | if (conn) { |
| 5113 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5114 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5115 | } |
| 5116 | |
| 5117 | send_rsp: |
| 5118 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 5119 | &rp, sizeof(rp)); |
| 5120 | mgmt_pending_remove(cmd); |
| 5121 | if (conn) |
| 5122 | hci_conn_drop(conn); |
| 5123 | |
| 5124 | unlock: |
| 5125 | hci_dev_unlock(hdev); |
| 5126 | } |
| 5127 | |
| 5128 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5129 | u16 len) |
| 5130 | { |
| 5131 | struct mgmt_cp_get_clock_info *cp = data; |
| 5132 | struct mgmt_rp_get_clock_info rp; |
| 5133 | struct hci_cp_read_clock hci_cp; |
| 5134 | struct pending_cmd *cmd; |
| 5135 | struct hci_request req; |
| 5136 | struct hci_conn *conn; |
| 5137 | int err; |
| 5138 | |
| 5139 | BT_DBG("%s", hdev->name); |
| 5140 | |
| 5141 | memset(&rp, 0, sizeof(rp)); |
| 5142 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5143 | rp.addr.type = cp->addr.type; |
| 5144 | |
| 5145 | if (cp->addr.type != BDADDR_BREDR) |
| 5146 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5147 | MGMT_STATUS_INVALID_PARAMS, |
| 5148 | &rp, sizeof(rp)); |
| 5149 | |
| 5150 | hci_dev_lock(hdev); |
| 5151 | |
| 5152 | if (!hdev_is_powered(hdev)) { |
| 5153 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5154 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5155 | goto unlock; |
| 5156 | } |
| 5157 | |
| 5158 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5159 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5160 | &cp->addr.bdaddr); |
| 5161 | if (!conn || conn->state != BT_CONNECTED) { |
| 5162 | err = cmd_complete(sk, hdev->id, |
| 5163 | MGMT_OP_GET_CLOCK_INFO, |
| 5164 | MGMT_STATUS_NOT_CONNECTED, |
| 5165 | &rp, sizeof(rp)); |
| 5166 | goto unlock; |
| 5167 | } |
| 5168 | } else { |
| 5169 | conn = NULL; |
| 5170 | } |
| 5171 | |
| 5172 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5173 | if (!cmd) { |
| 5174 | err = -ENOMEM; |
| 5175 | goto unlock; |
| 5176 | } |
| 5177 | |
| 5178 | hci_req_init(&req, hdev); |
| 5179 | |
| 5180 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5181 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5182 | |
| 5183 | if (conn) { |
| 5184 | hci_conn_hold(conn); |
| 5185 | cmd->user_data = conn; |
| 5186 | |
| 5187 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5188 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5189 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5190 | } |
| 5191 | |
| 5192 | err = hci_req_run(&req, get_clock_info_complete); |
| 5193 | if (err < 0) |
| 5194 | mgmt_pending_remove(cmd); |
| 5195 | |
| 5196 | unlock: |
| 5197 | hci_dev_unlock(hdev); |
| 5198 | return err; |
| 5199 | } |
| 5200 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5201 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5202 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5203 | { |
| 5204 | struct mgmt_ev_device_added ev; |
| 5205 | |
| 5206 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5207 | ev.addr.type = type; |
| 5208 | ev.action = action; |
| 5209 | |
| 5210 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5211 | } |
| 5212 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5213 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5214 | void *data, u16 len) |
| 5215 | { |
| 5216 | struct mgmt_cp_add_device *cp = data; |
| 5217 | u8 auto_conn, addr_type; |
| 5218 | int err; |
| 5219 | |
| 5220 | BT_DBG("%s", hdev->name); |
| 5221 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame^] | 5222 | if (!bdaddr_type_is_valid(cp->addr.type) || |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5223 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5224 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5225 | MGMT_STATUS_INVALID_PARAMS, |
| 5226 | &cp->addr, sizeof(cp->addr)); |
| 5227 | |
| 5228 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5229 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5230 | MGMT_STATUS_INVALID_PARAMS, |
| 5231 | &cp->addr, sizeof(cp->addr)); |
| 5232 | |
| 5233 | hci_dev_lock(hdev); |
| 5234 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame^] | 5235 | if (cp->addr.type == BDADDR_BREDR) { |
| 5236 | /* Only "connect" action supported for now */ |
| 5237 | if (cp->action != 0x01) { |
| 5238 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5239 | MGMT_STATUS_INVALID_PARAMS, |
| 5240 | &cp->addr, sizeof(cp->addr)); |
| 5241 | goto unlock; |
| 5242 | } |
| 5243 | |
| 5244 | err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr, |
| 5245 | cp->addr.type); |
| 5246 | if (err) |
| 5247 | goto unlock; |
| 5248 | goto added; |
| 5249 | } |
| 5250 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5251 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5252 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5253 | else |
| 5254 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5255 | |
| 5256 | if (cp->action) |
| 5257 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5258 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5259 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5260 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5261 | /* If the connection parameters don't exist for this device, |
| 5262 | * they will be created and configured with defaults. |
| 5263 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5264 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5265 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5266 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5267 | MGMT_STATUS_FAILED, |
| 5268 | &cp->addr, sizeof(cp->addr)); |
| 5269 | goto unlock; |
| 5270 | } |
| 5271 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame^] | 5272 | added: |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5273 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5274 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5275 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5276 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5277 | |
| 5278 | unlock: |
| 5279 | hci_dev_unlock(hdev); |
| 5280 | return err; |
| 5281 | } |
| 5282 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5283 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5284 | bdaddr_t *bdaddr, u8 type) |
| 5285 | { |
| 5286 | struct mgmt_ev_device_removed ev; |
| 5287 | |
| 5288 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5289 | ev.addr.type = type; |
| 5290 | |
| 5291 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5292 | } |
| 5293 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5294 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5295 | void *data, u16 len) |
| 5296 | { |
| 5297 | struct mgmt_cp_remove_device *cp = data; |
| 5298 | int err; |
| 5299 | |
| 5300 | BT_DBG("%s", hdev->name); |
| 5301 | |
| 5302 | hci_dev_lock(hdev); |
| 5303 | |
| 5304 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5305 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5306 | u8 addr_type; |
| 5307 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame^] | 5308 | if (!bdaddr_type_is_valid(cp->addr.type)) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5309 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5310 | MGMT_STATUS_INVALID_PARAMS, |
| 5311 | &cp->addr, sizeof(cp->addr)); |
| 5312 | goto unlock; |
| 5313 | } |
| 5314 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame^] | 5315 | if (cp->addr.type == BDADDR_BREDR) { |
| 5316 | err = hci_bdaddr_list_del(&hdev->whitelist, |
| 5317 | &cp->addr.bdaddr, |
| 5318 | cp->addr.type); |
| 5319 | if (err) { |
| 5320 | err = cmd_complete(sk, hdev->id, |
| 5321 | MGMT_OP_REMOVE_DEVICE, |
| 5322 | MGMT_STATUS_INVALID_PARAMS, |
| 5323 | &cp->addr, sizeof(cp->addr)); |
| 5324 | goto unlock; |
| 5325 | } |
| 5326 | |
| 5327 | device_removed(sk, hdev, &cp->addr.bdaddr, |
| 5328 | cp->addr.type); |
| 5329 | goto complete; |
| 5330 | } |
| 5331 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5332 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5333 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5334 | else |
| 5335 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5336 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5337 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5338 | addr_type); |
| 5339 | if (!params) { |
| 5340 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5341 | MGMT_STATUS_INVALID_PARAMS, |
| 5342 | &cp->addr, sizeof(cp->addr)); |
| 5343 | goto unlock; |
| 5344 | } |
| 5345 | |
| 5346 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5347 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5348 | MGMT_STATUS_INVALID_PARAMS, |
| 5349 | &cp->addr, sizeof(cp->addr)); |
| 5350 | goto unlock; |
| 5351 | } |
| 5352 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5353 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5354 | list_del(¶ms->list); |
| 5355 | kfree(params); |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame] | 5356 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5357 | |
| 5358 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5359 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5360 | struct hci_conn_params *p, *tmp; |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame^] | 5361 | struct bdaddr_list *b, *btmp; |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5362 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5363 | if (cp->addr.type) { |
| 5364 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5365 | MGMT_STATUS_INVALID_PARAMS, |
| 5366 | &cp->addr, sizeof(cp->addr)); |
| 5367 | goto unlock; |
| 5368 | } |
| 5369 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame^] | 5370 | list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) { |
| 5371 | device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type); |
| 5372 | list_del(&b->list); |
| 5373 | kfree(b); |
| 5374 | } |
| 5375 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5376 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5377 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5378 | continue; |
| 5379 | device_removed(sk, hdev, &p->addr, p->addr_type); |
| 5380 | list_del(&p->action); |
| 5381 | list_del(&p->list); |
| 5382 | kfree(p); |
| 5383 | } |
| 5384 | |
| 5385 | BT_DBG("All LE connection parameters were removed"); |
| 5386 | |
| 5387 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5388 | } |
| 5389 | |
Johan Hedberg | 6659358 | 2014-07-09 12:59:14 +0300 | [diff] [blame^] | 5390 | complete: |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5391 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5392 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5393 | |
| 5394 | unlock: |
| 5395 | hci_dev_unlock(hdev); |
| 5396 | return err; |
| 5397 | } |
| 5398 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5399 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5400 | u16 len) |
| 5401 | { |
| 5402 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5403 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5404 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5405 | u16 param_count, expected_len; |
| 5406 | int i; |
| 5407 | |
| 5408 | if (!lmp_le_capable(hdev)) |
| 5409 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5410 | MGMT_STATUS_NOT_SUPPORTED); |
| 5411 | |
| 5412 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5413 | if (param_count > max_param_count) { |
| 5414 | BT_ERR("load_conn_param: too big param_count value %u", |
| 5415 | param_count); |
| 5416 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5417 | MGMT_STATUS_INVALID_PARAMS); |
| 5418 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5419 | |
| 5420 | expected_len = sizeof(*cp) + param_count * |
| 5421 | sizeof(struct mgmt_conn_param); |
| 5422 | if (expected_len != len) { |
| 5423 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5424 | expected_len, len); |
| 5425 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5426 | MGMT_STATUS_INVALID_PARAMS); |
| 5427 | } |
| 5428 | |
| 5429 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5430 | |
| 5431 | hci_dev_lock(hdev); |
| 5432 | |
| 5433 | hci_conn_params_clear_disabled(hdev); |
| 5434 | |
| 5435 | for (i = 0; i < param_count; i++) { |
| 5436 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5437 | struct hci_conn_params *hci_param; |
| 5438 | u16 min, max, latency, timeout; |
| 5439 | u8 addr_type; |
| 5440 | |
| 5441 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5442 | param->addr.type); |
| 5443 | |
| 5444 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5445 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5446 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5447 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5448 | } else { |
| 5449 | BT_ERR("Ignoring invalid connection parameters"); |
| 5450 | continue; |
| 5451 | } |
| 5452 | |
| 5453 | min = le16_to_cpu(param->min_interval); |
| 5454 | max = le16_to_cpu(param->max_interval); |
| 5455 | latency = le16_to_cpu(param->latency); |
| 5456 | timeout = le16_to_cpu(param->timeout); |
| 5457 | |
| 5458 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5459 | min, max, latency, timeout); |
| 5460 | |
| 5461 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5462 | BT_ERR("Ignoring invalid connection parameters"); |
| 5463 | continue; |
| 5464 | } |
| 5465 | |
| 5466 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5467 | addr_type); |
| 5468 | if (!hci_param) { |
| 5469 | BT_ERR("Failed to add connection parameters"); |
| 5470 | continue; |
| 5471 | } |
| 5472 | |
| 5473 | hci_param->conn_min_interval = min; |
| 5474 | hci_param->conn_max_interval = max; |
| 5475 | hci_param->conn_latency = latency; |
| 5476 | hci_param->supervision_timeout = timeout; |
| 5477 | } |
| 5478 | |
| 5479 | hci_dev_unlock(hdev); |
| 5480 | |
| 5481 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5482 | } |
| 5483 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5484 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5485 | void *data, u16 len) |
| 5486 | { |
| 5487 | struct mgmt_cp_set_external_config *cp = data; |
| 5488 | bool changed; |
| 5489 | int err; |
| 5490 | |
| 5491 | BT_DBG("%s", hdev->name); |
| 5492 | |
| 5493 | if (hdev_is_powered(hdev)) |
| 5494 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5495 | MGMT_STATUS_REJECTED); |
| 5496 | |
| 5497 | if (cp->config != 0x00 && cp->config != 0x01) |
| 5498 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5499 | MGMT_STATUS_INVALID_PARAMS); |
| 5500 | |
| 5501 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 5502 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5503 | MGMT_STATUS_NOT_SUPPORTED); |
| 5504 | |
| 5505 | hci_dev_lock(hdev); |
| 5506 | |
| 5507 | if (cp->config) |
| 5508 | changed = !test_and_set_bit(HCI_EXT_CONFIGURED, |
| 5509 | &hdev->dev_flags); |
| 5510 | else |
| 5511 | changed = test_and_clear_bit(HCI_EXT_CONFIGURED, |
| 5512 | &hdev->dev_flags); |
| 5513 | |
| 5514 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5515 | if (err < 0) |
| 5516 | goto unlock; |
| 5517 | |
| 5518 | if (!changed) |
| 5519 | goto unlock; |
| 5520 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 5521 | err = new_options(hdev, sk); |
| 5522 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5523 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) { |
| 5524 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5525 | |
| 5526 | if (test_and_change_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { |
| 5527 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5528 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5529 | |
| 5530 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5531 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 5532 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5533 | mgmt_index_added(hdev); |
| 5534 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | unlock: |
| 5538 | hci_dev_unlock(hdev); |
| 5539 | return err; |
| 5540 | } |
| 5541 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5542 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 5543 | void *data, u16 len) |
| 5544 | { |
| 5545 | struct mgmt_cp_set_public_address *cp = data; |
| 5546 | bool changed; |
| 5547 | int err; |
| 5548 | |
| 5549 | BT_DBG("%s", hdev->name); |
| 5550 | |
| 5551 | if (hdev_is_powered(hdev)) |
| 5552 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5553 | MGMT_STATUS_REJECTED); |
| 5554 | |
| 5555 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
| 5556 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5557 | MGMT_STATUS_INVALID_PARAMS); |
| 5558 | |
| 5559 | if (!hdev->set_bdaddr) |
| 5560 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5561 | MGMT_STATUS_NOT_SUPPORTED); |
| 5562 | |
| 5563 | hci_dev_lock(hdev); |
| 5564 | |
| 5565 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 5566 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 5567 | |
| 5568 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 5569 | if (err < 0) |
| 5570 | goto unlock; |
| 5571 | |
| 5572 | if (!changed) |
| 5573 | goto unlock; |
| 5574 | |
| 5575 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5576 | err = new_options(hdev, sk); |
| 5577 | |
| 5578 | if (is_configured(hdev)) { |
| 5579 | mgmt_index_removed(hdev); |
| 5580 | |
| 5581 | clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags); |
| 5582 | |
| 5583 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5584 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5585 | |
| 5586 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5587 | } |
| 5588 | |
| 5589 | unlock: |
| 5590 | hci_dev_unlock(hdev); |
| 5591 | return err; |
| 5592 | } |
| 5593 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5594 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5595 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5596 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5597 | bool var_len; |
| 5598 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5599 | } mgmt_handlers[] = { |
| 5600 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5601 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5602 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5603 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5604 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5605 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5606 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5607 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5608 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5609 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5610 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5611 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5612 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5613 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5614 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5615 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5616 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5617 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5618 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5619 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5620 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5621 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5622 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5623 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5624 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5625 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5626 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5627 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5628 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5629 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5630 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5631 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5632 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5633 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5634 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5635 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5636 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5637 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5638 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5639 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5640 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5641 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5642 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5643 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5644 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5645 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5646 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5647 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5648 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5649 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5650 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5651 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5652 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5653 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
| 5654 | { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE }, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 5655 | { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE }, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5656 | { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE }, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5657 | { set_public_address, false, MGMT_SET_PUBLIC_ADDRESS_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5658 | }; |
| 5659 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5660 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5661 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5662 | void *buf; |
| 5663 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5664 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5665 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5666 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5667 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5668 | int err; |
| 5669 | |
| 5670 | BT_DBG("got %zu bytes", msglen); |
| 5671 | |
| 5672 | if (msglen < sizeof(*hdr)) |
| 5673 | return -EINVAL; |
| 5674 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5675 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5676 | if (!buf) |
| 5677 | return -ENOMEM; |
| 5678 | |
| 5679 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5680 | err = -EFAULT; |
| 5681 | goto done; |
| 5682 | } |
| 5683 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5684 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5685 | opcode = __le16_to_cpu(hdr->opcode); |
| 5686 | index = __le16_to_cpu(hdr->index); |
| 5687 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5688 | |
| 5689 | if (len != msglen - sizeof(*hdr)) { |
| 5690 | err = -EINVAL; |
| 5691 | goto done; |
| 5692 | } |
| 5693 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5694 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5695 | hdev = hci_dev_get(index); |
| 5696 | if (!hdev) { |
| 5697 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5698 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5699 | goto done; |
| 5700 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5701 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5702 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | d603b76 | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5703 | test_bit(HCI_CONFIG, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5704 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5705 | err = cmd_status(sk, index, opcode, |
| 5706 | MGMT_STATUS_INVALID_INDEX); |
| 5707 | goto done; |
| 5708 | } |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5709 | |
| 5710 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5711 | opcode != MGMT_OP_READ_CONFIG_INFO && |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5712 | opcode != MGMT_OP_SET_EXTERNAL_CONFIG && |
| 5713 | opcode != MGMT_OP_SET_PUBLIC_ADDRESS) { |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5714 | err = cmd_status(sk, index, opcode, |
| 5715 | MGMT_STATUS_INVALID_INDEX); |
| 5716 | goto done; |
| 5717 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5718 | } |
| 5719 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5720 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5721 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5722 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5723 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5724 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5725 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5726 | } |
| 5727 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5728 | if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || |
| 5729 | opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
| 5730 | err = cmd_status(sk, index, opcode, |
| 5731 | MGMT_STATUS_INVALID_INDEX); |
| 5732 | goto done; |
| 5733 | } |
| 5734 | |
| 5735 | if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && |
| 5736 | opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5737 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5738 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5739 | goto done; |
| 5740 | } |
| 5741 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5742 | handler = &mgmt_handlers[opcode]; |
| 5743 | |
| 5744 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5745 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5746 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5747 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5748 | goto done; |
| 5749 | } |
| 5750 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5751 | if (hdev) |
| 5752 | mgmt_init_hdev(sk, hdev); |
| 5753 | |
| 5754 | cp = buf + sizeof(*hdr); |
| 5755 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5756 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5757 | if (err < 0) |
| 5758 | goto done; |
| 5759 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5760 | err = msglen; |
| 5761 | |
| 5762 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5763 | if (hdev) |
| 5764 | hci_dev_put(hdev); |
| 5765 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5766 | kfree(buf); |
| 5767 | return err; |
| 5768 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5769 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5770 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5771 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5772 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5773 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5774 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5775 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5776 | return; |
| 5777 | |
| 5778 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5779 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5780 | else |
| 5781 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5782 | } |
| 5783 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5784 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5785 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5786 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5787 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5788 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5789 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5790 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5791 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5792 | return; |
| 5793 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5794 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5795 | |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 5796 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5797 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5798 | else |
| 5799 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5800 | } |
| 5801 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5802 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5803 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5804 | { |
| 5805 | struct hci_conn_params *p; |
| 5806 | |
| 5807 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5808 | /* Needed for AUTO_OFF case where might not "really" |
| 5809 | * have been powered off. |
| 5810 | */ |
| 5811 | list_del_init(&p->action); |
| 5812 | |
| 5813 | switch (p->auto_connect) { |
| 5814 | case HCI_AUTO_CONN_ALWAYS: |
| 5815 | list_add(&p->action, &hdev->pend_le_conns); |
| 5816 | break; |
| 5817 | case HCI_AUTO_CONN_REPORT: |
| 5818 | list_add(&p->action, &hdev->pend_le_reports); |
| 5819 | break; |
| 5820 | default: |
| 5821 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5822 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5823 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5824 | |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5825 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5826 | } |
| 5827 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5828 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5829 | { |
| 5830 | struct cmd_lookup match = { NULL, hdev }; |
| 5831 | |
| 5832 | BT_DBG("status 0x%02x", status); |
| 5833 | |
| 5834 | hci_dev_lock(hdev); |
| 5835 | |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5836 | restart_le_actions(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5837 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5838 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5839 | |
| 5840 | new_settings(hdev, match.sk); |
| 5841 | |
| 5842 | hci_dev_unlock(hdev); |
| 5843 | |
| 5844 | if (match.sk) |
| 5845 | sock_put(match.sk); |
| 5846 | } |
| 5847 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5848 | static int powered_update_hci(struct hci_dev *hdev) |
| 5849 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5850 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5851 | u8 link_sec; |
| 5852 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5853 | hci_req_init(&req, hdev); |
| 5854 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5855 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5856 | !lmp_host_ssp_capable(hdev)) { |
| 5857 | u8 ssp = 1; |
| 5858 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5859 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5860 | } |
| 5861 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5862 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5863 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5864 | struct hci_cp_write_le_host_supported cp; |
| 5865 | |
| 5866 | cp.le = 1; |
| 5867 | cp.simul = lmp_le_br_capable(hdev); |
| 5868 | |
| 5869 | /* Check first if we already have the right |
| 5870 | * host state (host features set) |
| 5871 | */ |
| 5872 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5873 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5874 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5875 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5876 | } |
| 5877 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5878 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5879 | /* Make sure the controller has a good default for |
| 5880 | * advertising data. This also applies to the case |
| 5881 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5882 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5883 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5884 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5885 | update_scan_rsp_data(&req); |
| 5886 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5887 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5888 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5889 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5890 | } |
| 5891 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5892 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5893 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5894 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5895 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5896 | |
| 5897 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5898 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5899 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5900 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5901 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5902 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5903 | } |
| 5904 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5905 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5906 | } |
| 5907 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5908 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5909 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5910 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5911 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5912 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5913 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5914 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5915 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5916 | return 0; |
| 5917 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5918 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5919 | if (powered_update_hci(hdev) == 0) |
| 5920 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5921 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5922 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5923 | &match); |
| 5924 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5925 | } |
| 5926 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5927 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5928 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5929 | |
| 5930 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5931 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5932 | zero_cod, sizeof(zero_cod), NULL); |
| 5933 | |
| 5934 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5935 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5936 | |
| 5937 | if (match.sk) |
| 5938 | sock_put(match.sk); |
| 5939 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5940 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5941 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5942 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5943 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5944 | { |
| 5945 | struct pending_cmd *cmd; |
| 5946 | u8 status; |
| 5947 | |
| 5948 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5949 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5950 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5951 | |
| 5952 | if (err == -ERFKILL) |
| 5953 | status = MGMT_STATUS_RFKILLED; |
| 5954 | else |
| 5955 | status = MGMT_STATUS_FAILED; |
| 5956 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5957 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5958 | |
| 5959 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5960 | } |
| 5961 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5962 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5963 | { |
| 5964 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5965 | |
| 5966 | hci_dev_lock(hdev); |
| 5967 | |
| 5968 | /* When discoverable timeout triggers, then just make sure |
| 5969 | * the limited discoverable flag is cleared. Even in the case |
| 5970 | * of a timeout triggered from general discoverable, it is |
| 5971 | * safe to unconditionally clear the flag. |
| 5972 | */ |
| 5973 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5974 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5975 | |
| 5976 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5977 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5978 | u8 scan = SCAN_PAGE; |
| 5979 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5980 | sizeof(scan), &scan); |
| 5981 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5982 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5983 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5984 | hci_req_run(&req, NULL); |
| 5985 | |
| 5986 | hdev->discov_timeout = 0; |
| 5987 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5988 | new_settings(hdev, NULL); |
| 5989 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5990 | hci_dev_unlock(hdev); |
| 5991 | } |
| 5992 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5993 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5994 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5995 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5996 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5997 | /* Nothing needed here if there's a pending command since that |
| 5998 | * commands request completion callback takes care of everything |
| 5999 | * necessary. |
| 6000 | */ |
| 6001 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6002 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 6003 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 6004 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 6005 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 6006 | return; |
| 6007 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6008 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6009 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6010 | } else { |
| 6011 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6012 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6013 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6014 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6015 | if (changed) { |
| 6016 | struct hci_request req; |
| 6017 | |
| 6018 | /* In case this change in discoverable was triggered by |
| 6019 | * a disabling of connectable there could be a need to |
| 6020 | * update the advertising flags. |
| 6021 | */ |
| 6022 | hci_req_init(&req, hdev); |
| 6023 | update_adv_data(&req); |
| 6024 | hci_req_run(&req, NULL); |
| 6025 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 6026 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 6027 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 6028 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6029 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6030 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6031 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6032 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6033 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 6034 | /* Nothing needed here if there's a pending command since that |
| 6035 | * commands request completion callback takes care of everything |
| 6036 | * necessary. |
| 6037 | */ |
| 6038 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6039 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 6040 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 6041 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 6042 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 6043 | return; |
| 6044 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6045 | if (connectable) |
| 6046 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 6047 | else |
| 6048 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6049 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 6050 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 6051 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 6052 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6053 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 6054 | 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] | 6055 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6056 | u8 mgmt_err = mgmt_status(status); |
| 6057 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 6058 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6059 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6060 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 6061 | |
| 6062 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6063 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6064 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 6065 | } |
| 6066 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6067 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 6068 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6069 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 6070 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6071 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6072 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6073 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6074 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 6075 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6076 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6077 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 6078 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6079 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6080 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6081 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6082 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6083 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6084 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 6085 | { |
| 6086 | if (ltk->authenticated) |
| 6087 | return MGMT_LTK_AUTHENTICATED; |
| 6088 | |
| 6089 | return MGMT_LTK_UNAUTHENTICATED; |
| 6090 | } |
| 6091 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6092 | 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] | 6093 | { |
| 6094 | struct mgmt_ev_new_long_term_key ev; |
| 6095 | |
| 6096 | memset(&ev, 0, sizeof(ev)); |
| 6097 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6098 | /* Devices using resolvable or non-resolvable random addresses |
| 6099 | * without providing an indentity resolving key don't require |
| 6100 | * to store long term keys. Their addresses will change the |
| 6101 | * next time around. |
| 6102 | * |
| 6103 | * Only when a remote device provides an identity address |
| 6104 | * make sure the long term key is stored. If the remote |
| 6105 | * identity is known, the long term keys are internally |
| 6106 | * mapped to the identity address. So allow static random |
| 6107 | * and public addresses here. |
| 6108 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6109 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6110 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6111 | ev.store_hint = 0x00; |
| 6112 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6113 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6114 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6115 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6116 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6117 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6118 | ev.key.enc_size = key->enc_size; |
| 6119 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 6120 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6121 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 6122 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6123 | ev.key.master = 1; |
| 6124 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6125 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 6126 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 6127 | 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] | 6128 | } |
| 6129 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6130 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 6131 | { |
| 6132 | struct mgmt_ev_new_irk ev; |
| 6133 | |
| 6134 | memset(&ev, 0, sizeof(ev)); |
| 6135 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6136 | /* For identity resolving keys from devices that are already |
| 6137 | * using a public address or static random address, do not |
| 6138 | * ask for storing this key. The identity resolving key really |
| 6139 | * is only mandatory for devices using resovlable random |
| 6140 | * addresses. |
| 6141 | * |
| 6142 | * Storing all identity resolving keys has the downside that |
| 6143 | * they will be also loaded on next boot of they system. More |
| 6144 | * identity resolving keys, means more time during scanning is |
| 6145 | * needed to actually resolve these addresses. |
| 6146 | */ |
| 6147 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 6148 | ev.store_hint = 0x01; |
| 6149 | else |
| 6150 | ev.store_hint = 0x00; |
| 6151 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6152 | bacpy(&ev.rpa, &irk->rpa); |
| 6153 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6154 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6155 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6156 | |
| 6157 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6158 | } |
| 6159 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6160 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6161 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6162 | { |
| 6163 | struct mgmt_ev_new_csrk ev; |
| 6164 | |
| 6165 | memset(&ev, 0, sizeof(ev)); |
| 6166 | |
| 6167 | /* Devices using resolvable or non-resolvable random addresses |
| 6168 | * without providing an indentity resolving key don't require |
| 6169 | * to store signature resolving keys. Their addresses will change |
| 6170 | * the next time around. |
| 6171 | * |
| 6172 | * Only when a remote device provides an identity address |
| 6173 | * make sure the signature resolving key is stored. So allow |
| 6174 | * static random and public addresses here. |
| 6175 | */ |
| 6176 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6177 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6178 | ev.store_hint = 0x00; |
| 6179 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6180 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6181 | |
| 6182 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6183 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 6184 | ev.key.master = csrk->master; |
| 6185 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6186 | |
| 6187 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6188 | } |
| 6189 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6190 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6191 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6192 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6193 | { |
| 6194 | struct mgmt_ev_new_conn_param ev; |
| 6195 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6196 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6197 | return; |
| 6198 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6199 | memset(&ev, 0, sizeof(ev)); |
| 6200 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6201 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6202 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6203 | ev.min_interval = cpu_to_le16(min_interval); |
| 6204 | ev.max_interval = cpu_to_le16(max_interval); |
| 6205 | ev.latency = cpu_to_le16(latency); |
| 6206 | ev.timeout = cpu_to_le16(timeout); |
| 6207 | |
| 6208 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6209 | } |
| 6210 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 6211 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 6212 | u8 data_len) |
| 6213 | { |
| 6214 | eir[eir_len++] = sizeof(type) + data_len; |
| 6215 | eir[eir_len++] = type; |
| 6216 | memcpy(&eir[eir_len], data, data_len); |
| 6217 | eir_len += data_len; |
| 6218 | |
| 6219 | return eir_len; |
| 6220 | } |
| 6221 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6222 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6223 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 6224 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6225 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6226 | char buf[512]; |
| 6227 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6228 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6229 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6230 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6231 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6232 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6233 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6234 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6235 | if (name_len > 0) |
| 6236 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6237 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6238 | |
| 6239 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 6240 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6241 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6242 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6243 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6244 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6245 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6246 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6247 | } |
| 6248 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6249 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 6250 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 6251 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6252 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 6253 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6254 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6255 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6256 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6257 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6258 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6259 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6260 | |
| 6261 | *sk = cmd->sk; |
| 6262 | sock_hold(*sk); |
| 6263 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6264 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6265 | } |
| 6266 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6267 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6268 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6269 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6270 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 6271 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6272 | |
| 6273 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6274 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6275 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6276 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6277 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6278 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6279 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6280 | |
| 6281 | mgmt_pending_remove(cmd); |
| 6282 | } |
| 6283 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6284 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6285 | u8 link_type, u8 addr_type, u8 reason, |
| 6286 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6287 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6288 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6289 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6290 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6291 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6292 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6293 | if (power_off) { |
| 6294 | struct mgmt_mode *cp = power_off->param; |
| 6295 | |
| 6296 | /* The connection is still in hci_conn_hash so test for 1 |
| 6297 | * instead of 0 to know if this is the last one. |
| 6298 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6299 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6300 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6301 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6302 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6303 | } |
| 6304 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6305 | if (!mgmt_connected) |
| 6306 | return; |
| 6307 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6308 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6309 | return; |
| 6310 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6311 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6312 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6313 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6314 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6315 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6316 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6317 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6318 | |
| 6319 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6320 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6321 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6322 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6323 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6324 | } |
| 6325 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6326 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6327 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6328 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6329 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6330 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6331 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6332 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6333 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6334 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6335 | hdev); |
| 6336 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6337 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6338 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6339 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6340 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6341 | cp = cmd->param; |
| 6342 | |
| 6343 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6344 | return; |
| 6345 | |
| 6346 | if (cp->addr.type != bdaddr_type) |
| 6347 | return; |
| 6348 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6349 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6350 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 6351 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6352 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 6353 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6354 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6355 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6356 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6357 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6358 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6359 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6360 | { |
| 6361 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6362 | struct pending_cmd *power_off; |
| 6363 | |
| 6364 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6365 | if (power_off) { |
| 6366 | struct mgmt_mode *cp = power_off->param; |
| 6367 | |
| 6368 | /* The connection is still in hci_conn_hash so test for 1 |
| 6369 | * instead of 0 to know if this is the last one. |
| 6370 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6371 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6372 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6373 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6374 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6375 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6376 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6377 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6378 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6379 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6380 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6381 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6382 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6383 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6384 | 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] | 6385 | { |
| 6386 | struct mgmt_ev_pin_code_request ev; |
| 6387 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6388 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6389 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6390 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6391 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6392 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6393 | } |
| 6394 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6395 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6396 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6397 | { |
| 6398 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6399 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6400 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6401 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6402 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6403 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6404 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6405 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6406 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6407 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6408 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6409 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6410 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6411 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6412 | } |
| 6413 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6414 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6415 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6416 | { |
| 6417 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6418 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6419 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6420 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6421 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6422 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6423 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6424 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6425 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6426 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6427 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6428 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6429 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6430 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6431 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6432 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6433 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6434 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6435 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6436 | { |
| 6437 | struct mgmt_ev_user_confirm_request ev; |
| 6438 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6439 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6440 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6441 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6442 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6443 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6444 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6445 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6446 | 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] | 6447 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6448 | } |
| 6449 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6450 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6451 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6452 | { |
| 6453 | struct mgmt_ev_user_passkey_request ev; |
| 6454 | |
| 6455 | BT_DBG("%s", hdev->name); |
| 6456 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6457 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6458 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6459 | |
| 6460 | 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] | 6461 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6462 | } |
| 6463 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6464 | 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] | 6465 | u8 link_type, u8 addr_type, u8 status, |
| 6466 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6467 | { |
| 6468 | struct pending_cmd *cmd; |
| 6469 | struct mgmt_rp_user_confirm_reply rp; |
| 6470 | int err; |
| 6471 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6472 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6473 | if (!cmd) |
| 6474 | return -ENOENT; |
| 6475 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6476 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6477 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6478 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6479 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6480 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6481 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6482 | |
| 6483 | return err; |
| 6484 | } |
| 6485 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6486 | 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] | 6487 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6488 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6489 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6490 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6491 | } |
| 6492 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6493 | 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] | 6494 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6495 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6496 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6497 | status, |
| 6498 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6499 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6500 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6501 | 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] | 6502 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6503 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6504 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6505 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6506 | } |
| 6507 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6508 | 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] | 6509 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6510 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6511 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6512 | status, |
| 6513 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6514 | } |
| 6515 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6516 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6517 | u8 link_type, u8 addr_type, u32 passkey, |
| 6518 | u8 entered) |
| 6519 | { |
| 6520 | struct mgmt_ev_passkey_notify ev; |
| 6521 | |
| 6522 | BT_DBG("%s", hdev->name); |
| 6523 | |
| 6524 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6525 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6526 | ev.passkey = __cpu_to_le32(passkey); |
| 6527 | ev.entered = entered; |
| 6528 | |
| 6529 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6530 | } |
| 6531 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6532 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6533 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6534 | { |
| 6535 | struct mgmt_ev_auth_failed ev; |
| 6536 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6537 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6538 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6539 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6540 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6541 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6542 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6543 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6544 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6545 | { |
| 6546 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6547 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6548 | |
| 6549 | if (status) { |
| 6550 | u8 mgmt_err = mgmt_status(status); |
| 6551 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6552 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6553 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6554 | } |
| 6555 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6556 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6557 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6558 | &hdev->dev_flags); |
| 6559 | else |
| 6560 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6561 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6562 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6563 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6564 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6565 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6566 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6567 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6568 | |
| 6569 | if (match.sk) |
| 6570 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6571 | } |
| 6572 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6573 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6574 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6575 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6576 | struct hci_cp_write_eir cp; |
| 6577 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6578 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6579 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6580 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6581 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6582 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6583 | memset(&cp, 0, sizeof(cp)); |
| 6584 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6585 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6586 | } |
| 6587 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6588 | 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] | 6589 | { |
| 6590 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6591 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6592 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6593 | |
| 6594 | if (status) { |
| 6595 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6596 | |
| 6597 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6598 | &hdev->dev_flags)) { |
| 6599 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6600 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6601 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6602 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6603 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6604 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6605 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6606 | } |
| 6607 | |
| 6608 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6609 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6610 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6611 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6612 | if (!changed) |
| 6613 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6614 | &hdev->dev_flags); |
| 6615 | else |
| 6616 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6617 | } |
| 6618 | |
| 6619 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6620 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6621 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6622 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6623 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6624 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6625 | sock_put(match.sk); |
| 6626 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6627 | hci_req_init(&req, hdev); |
| 6628 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6629 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6630 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6631 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6632 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6633 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6634 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6635 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6636 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6637 | |
| 6638 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6639 | } |
| 6640 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6641 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6642 | { |
| 6643 | struct cmd_lookup match = { NULL, hdev }; |
| 6644 | bool changed = false; |
| 6645 | |
| 6646 | if (status) { |
| 6647 | u8 mgmt_err = mgmt_status(status); |
| 6648 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6649 | if (enable) { |
| 6650 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6651 | &hdev->dev_flags)) |
| 6652 | new_settings(hdev, NULL); |
| 6653 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6654 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6655 | |
| 6656 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6657 | cmd_status_rsp, &mgmt_err); |
| 6658 | return; |
| 6659 | } |
| 6660 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6661 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6662 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6663 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6664 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6665 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6666 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6667 | |
| 6668 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6669 | settings_rsp, &match); |
| 6670 | |
| 6671 | if (changed) |
| 6672 | new_settings(hdev, match.sk); |
| 6673 | |
| 6674 | if (match.sk) |
| 6675 | sock_put(match.sk); |
| 6676 | } |
| 6677 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6678 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6679 | { |
| 6680 | struct cmd_lookup *match = data; |
| 6681 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6682 | if (match->sk == NULL) { |
| 6683 | match->sk = cmd->sk; |
| 6684 | sock_hold(match->sk); |
| 6685 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6686 | } |
| 6687 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6688 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6689 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6690 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6691 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6692 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6693 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6694 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6695 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6696 | |
| 6697 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6698 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6699 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6700 | |
| 6701 | if (match.sk) |
| 6702 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6703 | } |
| 6704 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6705 | 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] | 6706 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6707 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6708 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6709 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6710 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6711 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6712 | |
| 6713 | memset(&ev, 0, sizeof(ev)); |
| 6714 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6715 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6716 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6717 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6718 | if (!cmd) { |
| 6719 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6720 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6721 | /* If this is a HCI command related to powering on the |
| 6722 | * HCI dev don't send any mgmt signals. |
| 6723 | */ |
| 6724 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6725 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6726 | } |
| 6727 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6728 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6729 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6730 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6731 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6732 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6733 | u8 *randomizer192, u8 *hash256, |
| 6734 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6735 | { |
| 6736 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6737 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6738 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6739 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6740 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6741 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6742 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6743 | |
| 6744 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6745 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6746 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6747 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6748 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6749 | hash256 && randomizer256) { |
| 6750 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6751 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6752 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6753 | memcpy(rp.randomizer192, randomizer192, |
| 6754 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6755 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6756 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6757 | memcpy(rp.randomizer256, randomizer256, |
| 6758 | sizeof(rp.randomizer256)); |
| 6759 | |
| 6760 | cmd_complete(cmd->sk, hdev->id, |
| 6761 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6762 | &rp, sizeof(rp)); |
| 6763 | } else { |
| 6764 | struct mgmt_rp_read_local_oob_data rp; |
| 6765 | |
| 6766 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6767 | memcpy(rp.randomizer, randomizer192, |
| 6768 | sizeof(rp.randomizer)); |
| 6769 | |
| 6770 | cmd_complete(cmd->sk, hdev->id, |
| 6771 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6772 | &rp, sizeof(rp)); |
| 6773 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6774 | } |
| 6775 | |
| 6776 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6777 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6778 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6779 | 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] | 6780 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6781 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6782 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6783 | char buf[512]; |
| 6784 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6785 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6786 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6787 | /* Don't send events for a non-kernel initiated discovery. With |
| 6788 | * LE one exception is if we have pend_le_reports > 0 in which |
| 6789 | * case we're doing passive scanning and want these events. |
| 6790 | */ |
| 6791 | if (!hci_discovery_active(hdev)) { |
| 6792 | if (link_type == ACL_LINK) |
| 6793 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 6794 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6795 | return; |
| 6796 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6797 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6798 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6799 | * are for the potential CoD field. |
| 6800 | */ |
| 6801 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6802 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6803 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6804 | memset(buf, 0, sizeof(buf)); |
| 6805 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 6806 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6807 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6808 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6809 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6810 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6811 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6812 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6813 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6814 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6815 | 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] | 6816 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6817 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6818 | if (scan_rsp_len > 0) |
| 6819 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6820 | |
| 6821 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6822 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6823 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6824 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6825 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6826 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6827 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6828 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6829 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6830 | struct mgmt_ev_device_found *ev; |
| 6831 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6832 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6833 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6834 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6835 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6836 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6837 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6838 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6839 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6840 | ev->rssi = rssi; |
| 6841 | |
| 6842 | 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] | 6843 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6844 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6845 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6846 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6847 | 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] | 6848 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6849 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6850 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6851 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6852 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6853 | struct pending_cmd *cmd; |
| 6854 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6855 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6856 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6857 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6858 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6859 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6860 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6861 | |
| 6862 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6863 | u8 type = hdev->discovery.type; |
| 6864 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6865 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6866 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6867 | mgmt_pending_remove(cmd); |
| 6868 | } |
| 6869 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6870 | memset(&ev, 0, sizeof(ev)); |
| 6871 | ev.type = hdev->discovery.type; |
| 6872 | ev.discovering = discovering; |
| 6873 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6874 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6875 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6876 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6877 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6878 | { |
| 6879 | BT_DBG("%s status %u", hdev->name, status); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6880 | } |
| 6881 | |
| 6882 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6883 | { |
| 6884 | struct hci_request req; |
| 6885 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6886 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6887 | return; |
| 6888 | |
| 6889 | hci_req_init(&req, hdev); |
| 6890 | enable_advertising(&req); |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame] | 6891 | hci_req_run(&req, adv_enable_complete); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6892 | } |