Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 3 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4 | Copyright (C) 2010 Nokia Corporation |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 5 | Copyright (C) 2011-2012 Intel Corporation |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
| 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI Management interface */ |
| 26 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 32 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 33 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 34 | |
| 35 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 36 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 37 | #define MGMT_VERSION 1 |
Marcel Holtmann | 730f091 | 2014-06-28 12:36:10 +0200 | [diff] [blame] | 38 | #define MGMT_REVISION 7 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 39 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 40 | static const u16 mgmt_commands[] = { |
| 41 | MGMT_OP_READ_INDEX_LIST, |
| 42 | MGMT_OP_READ_INFO, |
| 43 | MGMT_OP_SET_POWERED, |
| 44 | MGMT_OP_SET_DISCOVERABLE, |
| 45 | MGMT_OP_SET_CONNECTABLE, |
| 46 | MGMT_OP_SET_FAST_CONNECTABLE, |
| 47 | MGMT_OP_SET_PAIRABLE, |
| 48 | MGMT_OP_SET_LINK_SECURITY, |
| 49 | MGMT_OP_SET_SSP, |
| 50 | MGMT_OP_SET_HS, |
| 51 | MGMT_OP_SET_LE, |
| 52 | MGMT_OP_SET_DEV_CLASS, |
| 53 | MGMT_OP_SET_LOCAL_NAME, |
| 54 | MGMT_OP_ADD_UUID, |
| 55 | MGMT_OP_REMOVE_UUID, |
| 56 | MGMT_OP_LOAD_LINK_KEYS, |
| 57 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 58 | MGMT_OP_DISCONNECT, |
| 59 | MGMT_OP_GET_CONNECTIONS, |
| 60 | MGMT_OP_PIN_CODE_REPLY, |
| 61 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 62 | MGMT_OP_SET_IO_CAPABILITY, |
| 63 | MGMT_OP_PAIR_DEVICE, |
| 64 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 65 | MGMT_OP_UNPAIR_DEVICE, |
| 66 | MGMT_OP_USER_CONFIRM_REPLY, |
| 67 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 68 | MGMT_OP_USER_PASSKEY_REPLY, |
| 69 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 70 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 71 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 72 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 73 | MGMT_OP_START_DISCOVERY, |
| 74 | MGMT_OP_STOP_DISCOVERY, |
| 75 | MGMT_OP_CONFIRM_NAME, |
| 76 | MGMT_OP_BLOCK_DEVICE, |
| 77 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 78 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 79 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 80 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 82 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 83 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 84 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 85 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 86 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 87 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 88 | MGMT_OP_GET_CLOCK_INFO, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 89 | MGMT_OP_ADD_DEVICE, |
| 90 | MGMT_OP_REMOVE_DEVICE, |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 91 | MGMT_OP_LOAD_CONN_PARAM, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 92 | MGMT_OP_READ_UNCONF_INDEX_LIST, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 93 | MGMT_OP_READ_CONFIG_INFO, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 94 | MGMT_OP_SET_EXTERNAL_CONFIG, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 95 | MGMT_OP_SET_PUBLIC_ADDRESS, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | static const u16 mgmt_events[] = { |
| 99 | MGMT_EV_CONTROLLER_ERROR, |
| 100 | MGMT_EV_INDEX_ADDED, |
| 101 | MGMT_EV_INDEX_REMOVED, |
| 102 | MGMT_EV_NEW_SETTINGS, |
| 103 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 104 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 105 | MGMT_EV_NEW_LINK_KEY, |
| 106 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 107 | MGMT_EV_DEVICE_CONNECTED, |
| 108 | MGMT_EV_DEVICE_DISCONNECTED, |
| 109 | MGMT_EV_CONNECT_FAILED, |
| 110 | MGMT_EV_PIN_CODE_REQUEST, |
| 111 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 112 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 113 | MGMT_EV_AUTH_FAILED, |
| 114 | MGMT_EV_DEVICE_FOUND, |
| 115 | MGMT_EV_DISCOVERING, |
| 116 | MGMT_EV_DEVICE_BLOCKED, |
| 117 | MGMT_EV_DEVICE_UNBLOCKED, |
| 118 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 119 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 120 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 121 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 122 | MGMT_EV_DEVICE_ADDED, |
| 123 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 124 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 125 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 126 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 127 | MGMT_EV_NEW_CONFIG_OPTIONS, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 130 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 131 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 132 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 133 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 134 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 135 | struct pending_cmd { |
| 136 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 137 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 138 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 139 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 140 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 141 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 142 | }; |
| 143 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 144 | /* HCI to MGMT error code conversion table */ |
| 145 | static u8 mgmt_status_table[] = { |
| 146 | MGMT_STATUS_SUCCESS, |
| 147 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 148 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 149 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 150 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 151 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 152 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 153 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 154 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 155 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 156 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 157 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 158 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 159 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 160 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 161 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 162 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 163 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 164 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 165 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 166 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 167 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 168 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 169 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 170 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 171 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 172 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 173 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 174 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 175 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 176 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 177 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 178 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 179 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 180 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 181 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 182 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 183 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 184 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 185 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 186 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 187 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 188 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 189 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 190 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 191 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 192 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 193 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 194 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 195 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 196 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 197 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 198 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 199 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 200 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 201 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 202 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 203 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 204 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 205 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 206 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 207 | }; |
| 208 | |
| 209 | static u8 mgmt_status(u8 hci_status) |
| 210 | { |
| 211 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 212 | return mgmt_status_table[hci_status]; |
| 213 | |
| 214 | return MGMT_STATUS_FAILED; |
| 215 | } |
| 216 | |
Marcel Holtmann | 04c60f05 | 2014-07-04 19:06:22 +0200 | [diff] [blame] | 217 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 218 | struct sock *skip_sk) |
| 219 | { |
| 220 | struct sk_buff *skb; |
| 221 | struct mgmt_hdr *hdr; |
| 222 | |
| 223 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
| 224 | if (!skb) |
| 225 | return -ENOMEM; |
| 226 | |
| 227 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 228 | hdr->opcode = cpu_to_le16(event); |
| 229 | if (hdev) |
| 230 | hdr->index = cpu_to_le16(hdev->id); |
| 231 | else |
| 232 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
| 233 | hdr->len = cpu_to_le16(data_len); |
| 234 | |
| 235 | if (data) |
| 236 | memcpy(skb_put(skb, data_len), data, data_len); |
| 237 | |
| 238 | /* Time stamp */ |
| 239 | __net_timestamp(skb); |
| 240 | |
| 241 | hci_send_to_control(skb, skip_sk); |
| 242 | kfree_skb(skb); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 247 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 248 | { |
| 249 | struct sk_buff *skb; |
| 250 | struct mgmt_hdr *hdr; |
| 251 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 252 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 253 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 254 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 255 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 256 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 257 | if (!skb) |
| 258 | return -ENOMEM; |
| 259 | |
| 260 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 261 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 262 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 263 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 264 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 265 | |
| 266 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 267 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 268 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 269 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 270 | err = sock_queue_rcv_skb(sk, skb); |
| 271 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 272 | kfree_skb(skb); |
| 273 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 274 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 277 | static int cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 278 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 279 | { |
| 280 | struct sk_buff *skb; |
| 281 | struct mgmt_hdr *hdr; |
| 282 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 283 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 284 | |
| 285 | BT_DBG("sock %p", sk); |
| 286 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 287 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 288 | if (!skb) |
| 289 | return -ENOMEM; |
| 290 | |
| 291 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 292 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 293 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 294 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 295 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 296 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 297 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 298 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 299 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 300 | |
| 301 | if (rp) |
| 302 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 303 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 304 | err = sock_queue_rcv_skb(sk, skb); |
| 305 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 306 | kfree_skb(skb); |
| 307 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 308 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 311 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 312 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 313 | { |
| 314 | struct mgmt_rp_read_version rp; |
| 315 | |
| 316 | BT_DBG("sock %p", sk); |
| 317 | |
| 318 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 319 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 320 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 321 | return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 322 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 325 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 326 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 327 | { |
| 328 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 329 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 330 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 331 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 332 | size_t rp_size; |
| 333 | int i, err; |
| 334 | |
| 335 | BT_DBG("sock %p", sk); |
| 336 | |
| 337 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 338 | |
| 339 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 340 | if (!rp) |
| 341 | return -ENOMEM; |
| 342 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 343 | rp->num_commands = cpu_to_le16(num_commands); |
| 344 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 345 | |
| 346 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 347 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 348 | |
| 349 | for (i = 0; i < num_events; i++, opcode++) |
| 350 | put_unaligned_le16(mgmt_events[i], opcode); |
| 351 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 352 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 353 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 354 | kfree(rp); |
| 355 | |
| 356 | return err; |
| 357 | } |
| 358 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 359 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 360 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 361 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 362 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 363 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 364 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 365 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 366 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 367 | |
| 368 | BT_DBG("sock %p", sk); |
| 369 | |
| 370 | read_lock(&hci_dev_list_lock); |
| 371 | |
| 372 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 373 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 374 | if (d->dev_type == HCI_BREDR && |
| 375 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 376 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 379 | rp_len = sizeof(*rp) + (2 * count); |
| 380 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 381 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 382 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 383 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 384 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 385 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 386 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 387 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 388 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 389 | test_bit(HCI_CONFIG, &d->dev_flags) || |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 390 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 391 | continue; |
| 392 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 393 | /* Devices marked as raw-only are neither configured |
| 394 | * nor unconfigured controllers. |
| 395 | */ |
| 396 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 397 | continue; |
| 398 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 399 | if (d->dev_type == HCI_BREDR && |
| 400 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 401 | rp->index[count++] = cpu_to_le16(d->id); |
| 402 | BT_DBG("Added hci%u", d->id); |
| 403 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 406 | rp->num_controllers = cpu_to_le16(count); |
| 407 | rp_len = sizeof(*rp) + (2 * count); |
| 408 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 409 | read_unlock(&hci_dev_list_lock); |
| 410 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 411 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, 0, rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 412 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 413 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 414 | kfree(rp); |
| 415 | |
| 416 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 419 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 420 | void *data, u16 data_len) |
| 421 | { |
| 422 | struct mgmt_rp_read_unconf_index_list *rp; |
| 423 | struct hci_dev *d; |
| 424 | size_t rp_len; |
| 425 | u16 count; |
| 426 | int err; |
| 427 | |
| 428 | BT_DBG("sock %p", sk); |
| 429 | |
| 430 | read_lock(&hci_dev_list_lock); |
| 431 | |
| 432 | count = 0; |
| 433 | list_for_each_entry(d, &hci_dev_list, list) { |
| 434 | if (d->dev_type == HCI_BREDR && |
| 435 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
| 436 | count++; |
| 437 | } |
| 438 | |
| 439 | rp_len = sizeof(*rp) + (2 * count); |
| 440 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 441 | if (!rp) { |
| 442 | read_unlock(&hci_dev_list_lock); |
| 443 | return -ENOMEM; |
| 444 | } |
| 445 | |
| 446 | count = 0; |
| 447 | list_for_each_entry(d, &hci_dev_list, list) { |
| 448 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 449 | test_bit(HCI_CONFIG, &d->dev_flags) || |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 450 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 451 | continue; |
| 452 | |
| 453 | /* Devices marked as raw-only are neither configured |
| 454 | * nor unconfigured controllers. |
| 455 | */ |
| 456 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 457 | continue; |
| 458 | |
| 459 | if (d->dev_type == HCI_BREDR && |
| 460 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
| 461 | rp->index[count++] = cpu_to_le16(d->id); |
| 462 | BT_DBG("Added hci%u", d->id); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | rp->num_controllers = cpu_to_le16(count); |
| 467 | rp_len = sizeof(*rp) + (2 * count); |
| 468 | |
| 469 | read_unlock(&hci_dev_list_lock); |
| 470 | |
| 471 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 472 | 0, rp, rp_len); |
| 473 | |
| 474 | kfree(rp); |
| 475 | |
| 476 | return err; |
| 477 | } |
| 478 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 479 | static bool is_configured(struct hci_dev *hdev) |
| 480 | { |
| 481 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 482 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
| 483 | return false; |
| 484 | |
| 485 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 486 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 487 | return false; |
| 488 | |
| 489 | return true; |
| 490 | } |
| 491 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 492 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 493 | { |
| 494 | u32 options = 0; |
| 495 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 496 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 497 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 498 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 499 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 500 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 501 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 502 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 503 | |
| 504 | return cpu_to_le32(options); |
| 505 | } |
| 506 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 507 | static int new_options(struct hci_dev *hdev, struct sock *skip) |
| 508 | { |
| 509 | __le32 options = get_missing_options(hdev); |
| 510 | |
| 511 | return mgmt_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, |
| 512 | sizeof(options), skip); |
| 513 | } |
| 514 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 515 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 516 | { |
| 517 | __le32 options = get_missing_options(hdev); |
| 518 | |
| 519 | return cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 520 | sizeof(options)); |
| 521 | } |
| 522 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 523 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 524 | void *data, u16 data_len) |
| 525 | { |
| 526 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 527 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 528 | |
| 529 | BT_DBG("sock %p %s", sk, hdev->name); |
| 530 | |
| 531 | hci_dev_lock(hdev); |
| 532 | |
| 533 | memset(&rp, 0, sizeof(rp)); |
| 534 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 535 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 536 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 537 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 538 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 539 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 540 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 541 | |
| 542 | rp.supported_options = cpu_to_le32(options); |
| 543 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 544 | |
| 545 | hci_dev_unlock(hdev); |
| 546 | |
| 547 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp, |
| 548 | sizeof(rp)); |
| 549 | } |
| 550 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 551 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 552 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 553 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 554 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 555 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 556 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 557 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 558 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 559 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 560 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 561 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 562 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 563 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 564 | settings |= MGMT_SETTING_BREDR; |
| 565 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 566 | |
| 567 | if (lmp_ssp_capable(hdev)) { |
| 568 | settings |= MGMT_SETTING_SSP; |
| 569 | settings |= MGMT_SETTING_HS; |
| 570 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 571 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 572 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 573 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 574 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 575 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 576 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 577 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 578 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 579 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 580 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 581 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 582 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 583 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 584 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 585 | settings |= MGMT_SETTING_CONFIGURATION; |
| 586 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 587 | return settings; |
| 588 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 589 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 590 | static u32 get_current_settings(struct hci_dev *hdev) |
| 591 | { |
| 592 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 593 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 594 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 595 | settings |= MGMT_SETTING_POWERED; |
| 596 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 597 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 598 | settings |= MGMT_SETTING_CONNECTABLE; |
| 599 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 600 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 601 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 602 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 603 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 604 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 605 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 606 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 607 | settings |= MGMT_SETTING_PAIRABLE; |
| 608 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 609 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 610 | settings |= MGMT_SETTING_BREDR; |
| 611 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 612 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 613 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 614 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 615 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 616 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 617 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 618 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 619 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 620 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 621 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 622 | settings |= MGMT_SETTING_HS; |
| 623 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 624 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 625 | settings |= MGMT_SETTING_ADVERTISING; |
| 626 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 627 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 628 | settings |= MGMT_SETTING_SECURE_CONN; |
| 629 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 630 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 631 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 632 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 633 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 634 | settings |= MGMT_SETTING_PRIVACY; |
| 635 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 636 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 637 | } |
| 638 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 639 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 640 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 641 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 642 | { |
| 643 | u8 *ptr = data, *uuids_start = NULL; |
| 644 | struct bt_uuid *uuid; |
| 645 | |
| 646 | if (len < 4) |
| 647 | return ptr; |
| 648 | |
| 649 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 650 | u16 uuid16; |
| 651 | |
| 652 | if (uuid->size != 16) |
| 653 | continue; |
| 654 | |
| 655 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 656 | if (uuid16 < 0x1100) |
| 657 | continue; |
| 658 | |
| 659 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 660 | continue; |
| 661 | |
| 662 | if (!uuids_start) { |
| 663 | uuids_start = ptr; |
| 664 | uuids_start[0] = 1; |
| 665 | uuids_start[1] = EIR_UUID16_ALL; |
| 666 | ptr += 2; |
| 667 | } |
| 668 | |
| 669 | /* Stop if not enough space to put next UUID */ |
| 670 | if ((ptr - data) + sizeof(u16) > len) { |
| 671 | uuids_start[1] = EIR_UUID16_SOME; |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | *ptr++ = (uuid16 & 0x00ff); |
| 676 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 677 | uuids_start[0] += sizeof(uuid16); |
| 678 | } |
| 679 | |
| 680 | return ptr; |
| 681 | } |
| 682 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 683 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 684 | { |
| 685 | u8 *ptr = data, *uuids_start = NULL; |
| 686 | struct bt_uuid *uuid; |
| 687 | |
| 688 | if (len < 6) |
| 689 | return ptr; |
| 690 | |
| 691 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 692 | if (uuid->size != 32) |
| 693 | continue; |
| 694 | |
| 695 | if (!uuids_start) { |
| 696 | uuids_start = ptr; |
| 697 | uuids_start[0] = 1; |
| 698 | uuids_start[1] = EIR_UUID32_ALL; |
| 699 | ptr += 2; |
| 700 | } |
| 701 | |
| 702 | /* Stop if not enough space to put next UUID */ |
| 703 | if ((ptr - data) + sizeof(u32) > len) { |
| 704 | uuids_start[1] = EIR_UUID32_SOME; |
| 705 | break; |
| 706 | } |
| 707 | |
| 708 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 709 | ptr += sizeof(u32); |
| 710 | uuids_start[0] += sizeof(u32); |
| 711 | } |
| 712 | |
| 713 | return ptr; |
| 714 | } |
| 715 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 716 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 717 | { |
| 718 | u8 *ptr = data, *uuids_start = NULL; |
| 719 | struct bt_uuid *uuid; |
| 720 | |
| 721 | if (len < 18) |
| 722 | return ptr; |
| 723 | |
| 724 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 725 | if (uuid->size != 128) |
| 726 | continue; |
| 727 | |
| 728 | if (!uuids_start) { |
| 729 | uuids_start = ptr; |
| 730 | uuids_start[0] = 1; |
| 731 | uuids_start[1] = EIR_UUID128_ALL; |
| 732 | ptr += 2; |
| 733 | } |
| 734 | |
| 735 | /* Stop if not enough space to put next UUID */ |
| 736 | if ((ptr - data) + 16 > len) { |
| 737 | uuids_start[1] = EIR_UUID128_SOME; |
| 738 | break; |
| 739 | } |
| 740 | |
| 741 | memcpy(ptr, uuid->uuid, 16); |
| 742 | ptr += 16; |
| 743 | uuids_start[0] += 16; |
| 744 | } |
| 745 | |
| 746 | return ptr; |
| 747 | } |
| 748 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 749 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 750 | { |
| 751 | struct pending_cmd *cmd; |
| 752 | |
| 753 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 754 | if (cmd->opcode == opcode) |
| 755 | return cmd; |
| 756 | } |
| 757 | |
| 758 | return NULL; |
| 759 | } |
| 760 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 761 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 762 | struct hci_dev *hdev, |
| 763 | const void *data) |
| 764 | { |
| 765 | struct pending_cmd *cmd; |
| 766 | |
| 767 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 768 | if (cmd->user_data != data) |
| 769 | continue; |
| 770 | if (cmd->opcode == opcode) |
| 771 | return cmd; |
| 772 | } |
| 773 | |
| 774 | return NULL; |
| 775 | } |
| 776 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 777 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 778 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 779 | u8 ad_len = 0; |
| 780 | size_t name_len; |
| 781 | |
| 782 | name_len = strlen(hdev->dev_name); |
| 783 | if (name_len > 0) { |
| 784 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 785 | |
| 786 | if (name_len > max_len) { |
| 787 | name_len = max_len; |
| 788 | ptr[1] = EIR_NAME_SHORT; |
| 789 | } else |
| 790 | ptr[1] = EIR_NAME_COMPLETE; |
| 791 | |
| 792 | ptr[0] = name_len + 1; |
| 793 | |
| 794 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 795 | |
| 796 | ad_len += (name_len + 2); |
| 797 | ptr += (name_len + 2); |
| 798 | } |
| 799 | |
| 800 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | static void update_scan_rsp_data(struct hci_request *req) |
| 804 | { |
| 805 | struct hci_dev *hdev = req->hdev; |
| 806 | struct hci_cp_le_set_scan_rsp_data cp; |
| 807 | u8 len; |
| 808 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 809 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 810 | return; |
| 811 | |
| 812 | memset(&cp, 0, sizeof(cp)); |
| 813 | |
| 814 | len = create_scan_rsp_data(hdev, cp.data); |
| 815 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 816 | if (hdev->scan_rsp_data_len == len && |
| 817 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 818 | return; |
| 819 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 820 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 821 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 822 | |
| 823 | cp.length = len; |
| 824 | |
| 825 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 826 | } |
| 827 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 828 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 829 | { |
| 830 | struct pending_cmd *cmd; |
| 831 | |
| 832 | /* If there's a pending mgmt command the flags will not yet have |
| 833 | * their final values, so check for this first. |
| 834 | */ |
| 835 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 836 | if (cmd) { |
| 837 | struct mgmt_mode *cp = cmd->param; |
| 838 | if (cp->val == 0x01) |
| 839 | return LE_AD_GENERAL; |
| 840 | else if (cp->val == 0x02) |
| 841 | return LE_AD_LIMITED; |
| 842 | } else { |
| 843 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 844 | return LE_AD_LIMITED; |
| 845 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 846 | return LE_AD_GENERAL; |
| 847 | } |
| 848 | |
| 849 | return 0; |
| 850 | } |
| 851 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 852 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 853 | { |
| 854 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 855 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 856 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 857 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 858 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 859 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 860 | |
| 861 | if (flags) { |
| 862 | BT_DBG("adv flags 0x%02x", flags); |
| 863 | |
| 864 | ptr[0] = 2; |
| 865 | ptr[1] = EIR_FLAGS; |
| 866 | ptr[2] = flags; |
| 867 | |
| 868 | ad_len += 3; |
| 869 | ptr += 3; |
| 870 | } |
| 871 | |
| 872 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 873 | ptr[0] = 2; |
| 874 | ptr[1] = EIR_TX_POWER; |
| 875 | ptr[2] = (u8) hdev->adv_tx_power; |
| 876 | |
| 877 | ad_len += 3; |
| 878 | ptr += 3; |
| 879 | } |
| 880 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 881 | return ad_len; |
| 882 | } |
| 883 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 884 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 885 | { |
| 886 | struct hci_dev *hdev = req->hdev; |
| 887 | struct hci_cp_le_set_adv_data cp; |
| 888 | u8 len; |
| 889 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 890 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 891 | return; |
| 892 | |
| 893 | memset(&cp, 0, sizeof(cp)); |
| 894 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 895 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 896 | |
| 897 | if (hdev->adv_data_len == len && |
| 898 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 899 | return; |
| 900 | |
| 901 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 902 | hdev->adv_data_len = len; |
| 903 | |
| 904 | cp.length = len; |
| 905 | |
| 906 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 907 | } |
| 908 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 909 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 910 | { |
| 911 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 912 | size_t name_len; |
| 913 | |
| 914 | name_len = strlen(hdev->dev_name); |
| 915 | |
| 916 | if (name_len > 0) { |
| 917 | /* EIR Data type */ |
| 918 | if (name_len > 48) { |
| 919 | name_len = 48; |
| 920 | ptr[1] = EIR_NAME_SHORT; |
| 921 | } else |
| 922 | ptr[1] = EIR_NAME_COMPLETE; |
| 923 | |
| 924 | /* EIR Data length */ |
| 925 | ptr[0] = name_len + 1; |
| 926 | |
| 927 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 928 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 929 | ptr += (name_len + 2); |
| 930 | } |
| 931 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 932 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 933 | ptr[0] = 2; |
| 934 | ptr[1] = EIR_TX_POWER; |
| 935 | ptr[2] = (u8) hdev->inq_tx_power; |
| 936 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 937 | ptr += 3; |
| 938 | } |
| 939 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 940 | if (hdev->devid_source > 0) { |
| 941 | ptr[0] = 9; |
| 942 | ptr[1] = EIR_DEVICE_ID; |
| 943 | |
| 944 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 945 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 946 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 947 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 948 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 949 | ptr += 10; |
| 950 | } |
| 951 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 952 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 953 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 954 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 955 | } |
| 956 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 957 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 958 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 959 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 960 | struct hci_cp_write_eir cp; |
| 961 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 962 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 963 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 964 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 965 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 966 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 967 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 968 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 969 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 970 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 971 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 972 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 973 | |
| 974 | memset(&cp, 0, sizeof(cp)); |
| 975 | |
| 976 | create_eir(hdev, cp.data); |
| 977 | |
| 978 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 979 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 980 | |
| 981 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 982 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 983 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 984 | } |
| 985 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 986 | static u8 get_service_classes(struct hci_dev *hdev) |
| 987 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 988 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 989 | u8 val = 0; |
| 990 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 991 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 992 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 993 | |
| 994 | return val; |
| 995 | } |
| 996 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 997 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 998 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 999 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1000 | u8 cod[3]; |
| 1001 | |
| 1002 | BT_DBG("%s", hdev->name); |
| 1003 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 1004 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1005 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 1006 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 1007 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1008 | return; |
| 1009 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1010 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1011 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1012 | |
| 1013 | cod[0] = hdev->minor_class; |
| 1014 | cod[1] = hdev->major_class; |
| 1015 | cod[2] = get_service_classes(hdev); |
| 1016 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 1017 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 1018 | cod[1] |= 0x20; |
| 1019 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1020 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1021 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1022 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1023 | hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 1024 | } |
| 1025 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1026 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1027 | { |
| 1028 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1029 | |
| 1030 | /* If there's a pending mgmt command the flag will not yet have |
| 1031 | * it's final value, so check for this first. |
| 1032 | */ |
| 1033 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1034 | if (cmd) { |
| 1035 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1036 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1039 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame^] | 1042 | static void disable_advertising(struct hci_request *req) |
| 1043 | { |
| 1044 | u8 enable = 0x00; |
| 1045 | |
| 1046 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1047 | } |
| 1048 | |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1049 | static void enable_advertising(struct hci_request *req) |
| 1050 | { |
| 1051 | struct hci_dev *hdev = req->hdev; |
| 1052 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1053 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1054 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1055 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame^] | 1056 | if (hci_conn_num(hdev, LE_LINK) > 0) |
| 1057 | return; |
| 1058 | |
| 1059 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
| 1060 | disable_advertising(req); |
| 1061 | |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1062 | /* Clear the HCI_LE_ADV bit temporarily so that the |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1063 | * hci_update_random_address knows that it's safe to go ahead |
| 1064 | * and write a new random address. The flag will be set back on |
| 1065 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 1066 | */ |
Johan Hedberg | 5ce194c | 2014-07-08 15:07:49 +0300 | [diff] [blame] | 1067 | clear_bit(HCI_LE_ADV, &hdev->dev_flags); |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1068 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1069 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1070 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1071 | /* Set require_privacy to true only when non-connectable |
| 1072 | * advertising is used. In that case it is fine to use a |
| 1073 | * non-resolvable private address. |
| 1074 | */ |
| 1075 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1076 | return; |
| 1077 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 1078 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1079 | cp.min_interval = cpu_to_le16(0x0800); |
| 1080 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1081 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1082 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1083 | cp.channel_map = hdev->le_adv_channel_map; |
| 1084 | |
| 1085 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1086 | |
| 1087 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1088 | } |
| 1089 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1090 | static void service_cache_off(struct work_struct *work) |
| 1091 | { |
| 1092 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1093 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1094 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1095 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1096 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1097 | return; |
| 1098 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1099 | hci_req_init(&req, hdev); |
| 1100 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1101 | hci_dev_lock(hdev); |
| 1102 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1103 | update_eir(&req); |
| 1104 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1105 | |
| 1106 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1107 | |
| 1108 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1109 | } |
| 1110 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1111 | static void rpa_expired(struct work_struct *work) |
| 1112 | { |
| 1113 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1114 | rpa_expired.work); |
| 1115 | struct hci_request req; |
| 1116 | |
| 1117 | BT_DBG(""); |
| 1118 | |
| 1119 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 1120 | |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame^] | 1121 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1122 | return; |
| 1123 | |
| 1124 | /* The generation of a new RPA and programming it into the |
| 1125 | * controller happens in the enable_advertising() function. |
| 1126 | */ |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1127 | hci_req_init(&req, hdev); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1128 | enable_advertising(&req); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1129 | hci_req_run(&req, NULL); |
| 1130 | } |
| 1131 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1132 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1133 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1134 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1135 | return; |
| 1136 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1137 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1138 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1139 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1140 | /* Non-mgmt controlled devices get this bit set |
| 1141 | * implicitly so that pairing works for them, however |
| 1142 | * for mgmt we require user-space to explicitly enable |
| 1143 | * it |
| 1144 | */ |
| 1145 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1146 | } |
| 1147 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1148 | static int read_controller_info(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1149 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1150 | { |
| 1151 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1152 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1153 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1154 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1155 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1156 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1157 | memset(&rp, 0, sizeof(rp)); |
| 1158 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1159 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1160 | |
| 1161 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1162 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1163 | |
| 1164 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1165 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1166 | |
| 1167 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1168 | |
| 1169 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1170 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1171 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1172 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1173 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1174 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1175 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1179 | { |
| 1180 | sock_put(cmd->sk); |
| 1181 | kfree(cmd->param); |
| 1182 | kfree(cmd); |
| 1183 | } |
| 1184 | |
| 1185 | static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1186 | struct hci_dev *hdev, void *data, |
| 1187 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1188 | { |
| 1189 | struct pending_cmd *cmd; |
| 1190 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1191 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1192 | if (!cmd) |
| 1193 | return NULL; |
| 1194 | |
| 1195 | cmd->opcode = opcode; |
| 1196 | cmd->index = hdev->id; |
| 1197 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1198 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1199 | if (!cmd->param) { |
| 1200 | kfree(cmd); |
| 1201 | return NULL; |
| 1202 | } |
| 1203 | |
| 1204 | if (data) |
| 1205 | memcpy(cmd->param, data, len); |
| 1206 | |
| 1207 | cmd->sk = sk; |
| 1208 | sock_hold(sk); |
| 1209 | |
| 1210 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1211 | |
| 1212 | return cmd; |
| 1213 | } |
| 1214 | |
| 1215 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1216 | void (*cb)(struct pending_cmd *cmd, |
| 1217 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1218 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1219 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1220 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1221 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1222 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1223 | if (opcode > 0 && cmd->opcode != opcode) |
| 1224 | continue; |
| 1225 | |
| 1226 | cb(cmd, data); |
| 1227 | } |
| 1228 | } |
| 1229 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1230 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1231 | { |
| 1232 | list_del(&cmd->list); |
| 1233 | mgmt_pending_free(cmd); |
| 1234 | } |
| 1235 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1236 | static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1237 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1238 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1239 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1240 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1241 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1242 | } |
| 1243 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1244 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1245 | { |
| 1246 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1247 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1248 | if (hci_conn_count(hdev) == 0) { |
| 1249 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1250 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1251 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1252 | } |
| 1253 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1254 | static void hci_stop_discovery(struct hci_request *req) |
| 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 | |
| 1269 | break; |
| 1270 | |
| 1271 | case DISCOVERY_RESOLVING: |
| 1272 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1273 | NAME_PENDING); |
| 1274 | if (!e) |
| 1275 | return; |
| 1276 | |
| 1277 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1278 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1279 | &cp); |
| 1280 | |
| 1281 | break; |
| 1282 | |
| 1283 | default: |
| 1284 | /* Passive scanning */ |
| 1285 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1286 | hci_req_add_le_scan_disable(req); |
| 1287 | break; |
| 1288 | } |
| 1289 | } |
| 1290 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1291 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1292 | { |
| 1293 | struct hci_request req; |
| 1294 | struct hci_conn *conn; |
| 1295 | |
| 1296 | hci_req_init(&req, hdev); |
| 1297 | |
| 1298 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1299 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1300 | u8 scan = 0x00; |
| 1301 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1302 | } |
| 1303 | |
| 1304 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1305 | disable_advertising(&req); |
| 1306 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1307 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1308 | |
| 1309 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1310 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1311 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1312 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1313 | switch (conn->state) { |
| 1314 | case BT_CONNECTED: |
| 1315 | case BT_CONFIG: |
| 1316 | dc.handle = cpu_to_le16(conn->handle); |
| 1317 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1318 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1319 | break; |
| 1320 | case BT_CONNECT: |
| 1321 | if (conn->type == LE_LINK) |
| 1322 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1323 | 0, NULL); |
| 1324 | else if (conn->type == ACL_LINK) |
| 1325 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1326 | 6, &conn->dst); |
| 1327 | break; |
| 1328 | case BT_CONNECT2: |
| 1329 | bacpy(&rej.bdaddr, &conn->dst); |
| 1330 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1331 | if (conn->type == ACL_LINK) |
| 1332 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1333 | sizeof(rej), &rej); |
| 1334 | else if (conn->type == SCO_LINK) |
| 1335 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1336 | sizeof(rej), &rej); |
| 1337 | break; |
| 1338 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | return hci_req_run(&req, clean_up_hci_complete); |
| 1342 | } |
| 1343 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1344 | 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] | 1345 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1346 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1347 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1348 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1349 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1350 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1351 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1352 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1353 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1354 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1355 | MGMT_STATUS_INVALID_PARAMS); |
| 1356 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1357 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1358 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1359 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1360 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1361 | MGMT_STATUS_BUSY); |
| 1362 | goto failed; |
| 1363 | } |
| 1364 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1365 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1366 | cancel_delayed_work(&hdev->power_off); |
| 1367 | |
| 1368 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1369 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1370 | data, len); |
| 1371 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1372 | goto failed; |
| 1373 | } |
| 1374 | } |
| 1375 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1376 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1377 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1378 | goto failed; |
| 1379 | } |
| 1380 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1381 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1382 | if (!cmd) { |
| 1383 | err = -ENOMEM; |
| 1384 | goto failed; |
| 1385 | } |
| 1386 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1387 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1388 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1389 | err = 0; |
| 1390 | } else { |
| 1391 | /* Disconnect connections, stop scans, etc */ |
| 1392 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1393 | if (!err) |
| 1394 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1395 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1396 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1397 | /* ENODATA means there were no HCI commands queued */ |
| 1398 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1399 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1400 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1401 | err = 0; |
| 1402 | } |
| 1403 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1404 | |
| 1405 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1406 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1407 | return err; |
| 1408 | } |
| 1409 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1410 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1411 | { |
| 1412 | __le32 ev; |
| 1413 | |
| 1414 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1415 | |
| 1416 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1417 | } |
| 1418 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1419 | struct cmd_lookup { |
| 1420 | struct sock *sk; |
| 1421 | struct hci_dev *hdev; |
| 1422 | u8 mgmt_status; |
| 1423 | }; |
| 1424 | |
| 1425 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1426 | { |
| 1427 | struct cmd_lookup *match = data; |
| 1428 | |
| 1429 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1430 | |
| 1431 | list_del(&cmd->list); |
| 1432 | |
| 1433 | if (match->sk == NULL) { |
| 1434 | match->sk = cmd->sk; |
| 1435 | sock_hold(match->sk); |
| 1436 | } |
| 1437 | |
| 1438 | mgmt_pending_free(cmd); |
| 1439 | } |
| 1440 | |
| 1441 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1442 | { |
| 1443 | u8 *status = data; |
| 1444 | |
| 1445 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1446 | mgmt_pending_remove(cmd); |
| 1447 | } |
| 1448 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1449 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1450 | { |
| 1451 | if (!lmp_bredr_capable(hdev)) |
| 1452 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1453 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1454 | return MGMT_STATUS_REJECTED; |
| 1455 | else |
| 1456 | return MGMT_STATUS_SUCCESS; |
| 1457 | } |
| 1458 | |
| 1459 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1460 | { |
| 1461 | if (!lmp_le_capable(hdev)) |
| 1462 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1463 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1464 | return MGMT_STATUS_REJECTED; |
| 1465 | else |
| 1466 | return MGMT_STATUS_SUCCESS; |
| 1467 | } |
| 1468 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1469 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1470 | { |
| 1471 | struct pending_cmd *cmd; |
| 1472 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1473 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1474 | bool changed; |
| 1475 | |
| 1476 | BT_DBG("status 0x%02x", status); |
| 1477 | |
| 1478 | hci_dev_lock(hdev); |
| 1479 | |
| 1480 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1481 | if (!cmd) |
| 1482 | goto unlock; |
| 1483 | |
| 1484 | if (status) { |
| 1485 | u8 mgmt_err = mgmt_status(status); |
| 1486 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1487 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1488 | goto remove_cmd; |
| 1489 | } |
| 1490 | |
| 1491 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1492 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1493 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1494 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1495 | |
| 1496 | if (hdev->discov_timeout > 0) { |
| 1497 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1498 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1499 | to); |
| 1500 | } |
| 1501 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1502 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1503 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1504 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1505 | |
| 1506 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1507 | |
| 1508 | if (changed) |
| 1509 | new_settings(hdev, cmd->sk); |
| 1510 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1511 | /* When the discoverable mode gets changed, make sure |
| 1512 | * that class of device has the limited discoverable |
| 1513 | * bit correctly set. |
| 1514 | */ |
| 1515 | hci_req_init(&req, hdev); |
| 1516 | update_class(&req); |
| 1517 | hci_req_run(&req, NULL); |
| 1518 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1519 | remove_cmd: |
| 1520 | mgmt_pending_remove(cmd); |
| 1521 | |
| 1522 | unlock: |
| 1523 | hci_dev_unlock(hdev); |
| 1524 | } |
| 1525 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1526 | 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] | 1527 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1528 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1529 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1530 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1531 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1532 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1533 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1534 | int err; |
| 1535 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1536 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1537 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1538 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1539 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1540 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1541 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1542 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1543 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1544 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1545 | MGMT_STATUS_INVALID_PARAMS); |
| 1546 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1547 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1548 | |
| 1549 | /* Disabling discoverable requires that no timeout is set, |
| 1550 | * and enabling limited discoverable requires a timeout. |
| 1551 | */ |
| 1552 | if ((cp->val == 0x00 && timeout > 0) || |
| 1553 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1554 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1555 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1556 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1557 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1558 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1559 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1560 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1561 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1562 | goto failed; |
| 1563 | } |
| 1564 | |
| 1565 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1566 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1567 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1568 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1569 | goto failed; |
| 1570 | } |
| 1571 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1572 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1573 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1574 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1575 | goto failed; |
| 1576 | } |
| 1577 | |
| 1578 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1579 | bool changed = false; |
| 1580 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1581 | /* Setting limited discoverable when powered off is |
| 1582 | * not a valid operation since it requires a timeout |
| 1583 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1584 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1585 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1586 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1587 | changed = true; |
| 1588 | } |
| 1589 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1590 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1591 | if (err < 0) |
| 1592 | goto failed; |
| 1593 | |
| 1594 | if (changed) |
| 1595 | err = new_settings(hdev, sk); |
| 1596 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1597 | goto failed; |
| 1598 | } |
| 1599 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1600 | /* If the current mode is the same, then just update the timeout |
| 1601 | * value with the new value. And if only the timeout gets updated, |
| 1602 | * then no need for any HCI transactions. |
| 1603 | */ |
| 1604 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1605 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1606 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1607 | cancel_delayed_work(&hdev->discov_off); |
| 1608 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1609 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1610 | if (cp->val && hdev->discov_timeout > 0) { |
| 1611 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1612 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1613 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1614 | } |
| 1615 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1616 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1617 | goto failed; |
| 1618 | } |
| 1619 | |
| 1620 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1621 | if (!cmd) { |
| 1622 | err = -ENOMEM; |
| 1623 | goto failed; |
| 1624 | } |
| 1625 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1626 | /* Cancel any potential discoverable timeout that might be |
| 1627 | * still active and store new timeout value. The arming of |
| 1628 | * the timeout happens in the complete handler. |
| 1629 | */ |
| 1630 | cancel_delayed_work(&hdev->discov_off); |
| 1631 | hdev->discov_timeout = timeout; |
| 1632 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1633 | /* Limited discoverable mode */ |
| 1634 | if (cp->val == 0x02) |
| 1635 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1636 | else |
| 1637 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1638 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1639 | hci_req_init(&req, hdev); |
| 1640 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1641 | /* The procedure for LE-only controllers is much simpler - just |
| 1642 | * update the advertising data. |
| 1643 | */ |
| 1644 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1645 | goto update_ad; |
| 1646 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1647 | scan = SCAN_PAGE; |
| 1648 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1649 | if (cp->val) { |
| 1650 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1651 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1652 | if (cp->val == 0x02) { |
| 1653 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1654 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1655 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1656 | hci_cp.iac_lap[1] = 0x8b; |
| 1657 | hci_cp.iac_lap[2] = 0x9e; |
| 1658 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1659 | hci_cp.iac_lap[4] = 0x8b; |
| 1660 | hci_cp.iac_lap[5] = 0x9e; |
| 1661 | } else { |
| 1662 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1663 | hci_cp.num_iac = 1; |
| 1664 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1665 | hci_cp.iac_lap[1] = 0x8b; |
| 1666 | hci_cp.iac_lap[2] = 0x9e; |
| 1667 | } |
| 1668 | |
| 1669 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1670 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1671 | |
| 1672 | scan |= SCAN_INQUIRY; |
| 1673 | } else { |
| 1674 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1675 | } |
| 1676 | |
| 1677 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1678 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1679 | update_ad: |
| 1680 | update_adv_data(&req); |
| 1681 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1682 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1683 | if (err < 0) |
| 1684 | mgmt_pending_remove(cmd); |
| 1685 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1686 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1687 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1688 | return err; |
| 1689 | } |
| 1690 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1691 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1692 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1693 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1694 | struct hci_cp_write_page_scan_activity acp; |
| 1695 | u8 type; |
| 1696 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1697 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1698 | return; |
| 1699 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1700 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1701 | return; |
| 1702 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1703 | if (enable) { |
| 1704 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1705 | |
| 1706 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1707 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1708 | } else { |
| 1709 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1710 | |
| 1711 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1712 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1713 | } |
| 1714 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1715 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1716 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1717 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1718 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1719 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1720 | sizeof(acp), &acp); |
| 1721 | |
| 1722 | if (hdev->page_scan_type != type) |
| 1723 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1724 | } |
| 1725 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1726 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1727 | { |
| 1728 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1729 | struct mgmt_mode *cp; |
| 1730 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1731 | |
| 1732 | BT_DBG("status 0x%02x", status); |
| 1733 | |
| 1734 | hci_dev_lock(hdev); |
| 1735 | |
| 1736 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1737 | if (!cmd) |
| 1738 | goto unlock; |
| 1739 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1740 | if (status) { |
| 1741 | u8 mgmt_err = mgmt_status(status); |
| 1742 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1743 | goto remove_cmd; |
| 1744 | } |
| 1745 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1746 | cp = cmd->param; |
| 1747 | if (cp->val) |
| 1748 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1749 | else |
| 1750 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1751 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1752 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1753 | |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1754 | if (changed) { |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1755 | new_settings(hdev, cmd->sk); |
Johan Hedberg | 2b7be33 | 2014-07-07 14:40:22 +0300 | [diff] [blame] | 1756 | hci_update_background_scan(hdev); |
| 1757 | } |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1758 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1759 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1760 | mgmt_pending_remove(cmd); |
| 1761 | |
| 1762 | unlock: |
| 1763 | hci_dev_unlock(hdev); |
| 1764 | } |
| 1765 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1766 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1767 | struct sock *sk, u8 val) |
| 1768 | { |
| 1769 | bool changed = false; |
| 1770 | int err; |
| 1771 | |
| 1772 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1773 | changed = true; |
| 1774 | |
| 1775 | if (val) { |
| 1776 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1777 | } else { |
| 1778 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1779 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1780 | } |
| 1781 | |
| 1782 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1783 | if (err < 0) |
| 1784 | return err; |
| 1785 | |
| 1786 | if (changed) |
| 1787 | return new_settings(hdev, sk); |
| 1788 | |
| 1789 | return 0; |
| 1790 | } |
| 1791 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1792 | 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] | 1793 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1794 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1795 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1796 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1797 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1798 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1799 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1800 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1801 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1802 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1803 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1804 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1805 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1806 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1807 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1808 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1809 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1810 | MGMT_STATUS_INVALID_PARAMS); |
| 1811 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1812 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1813 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1814 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1815 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1816 | goto failed; |
| 1817 | } |
| 1818 | |
| 1819 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1820 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1821 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1822 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1823 | goto failed; |
| 1824 | } |
| 1825 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1826 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1827 | if (!cmd) { |
| 1828 | err = -ENOMEM; |
| 1829 | goto failed; |
| 1830 | } |
| 1831 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1832 | hci_req_init(&req, hdev); |
| 1833 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1834 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1835 | * by-product of disabling connectable, we need to update the |
| 1836 | * advertising flags. |
| 1837 | */ |
| 1838 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1839 | if (!cp->val) { |
| 1840 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1841 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1842 | } |
| 1843 | update_adv_data(&req); |
| 1844 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1845 | if (cp->val) { |
| 1846 | scan = SCAN_PAGE; |
| 1847 | } else { |
| 1848 | scan = 0; |
| 1849 | |
| 1850 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1851 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1852 | cancel_delayed_work(&hdev->discov_off); |
| 1853 | } |
| 1854 | |
| 1855 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1856 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1857 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1858 | /* If we're going from non-connectable to connectable or |
| 1859 | * vice-versa when fast connectable is enabled ensure that fast |
| 1860 | * connectable gets disabled. write_fast_connectable won't do |
| 1861 | * anything if the page scan parameters are already what they |
| 1862 | * should be. |
| 1863 | */ |
| 1864 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1865 | write_fast_connectable(&req, false); |
| 1866 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1867 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame^] | 1868 | !test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1869 | enable_advertising(&req); |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1870 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1871 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1872 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1873 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1874 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1875 | err = set_connectable_update_settings(hdev, sk, |
| 1876 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1877 | goto failed; |
| 1878 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1879 | |
| 1880 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1881 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1882 | return err; |
| 1883 | } |
| 1884 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1885 | 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] | 1886 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1887 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1888 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1889 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1890 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1891 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1892 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1893 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1894 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1895 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1896 | MGMT_STATUS_INVALID_PARAMS); |
| 1897 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1898 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1899 | |
| 1900 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1901 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1902 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1903 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1904 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1905 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1906 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1907 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1908 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1909 | if (changed) |
| 1910 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1911 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1912 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1913 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1914 | return err; |
| 1915 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1916 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1917 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1918 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1919 | { |
| 1920 | struct mgmt_mode *cp = data; |
| 1921 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1922 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1923 | int err; |
| 1924 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1925 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1926 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1927 | status = mgmt_bredr_support(hdev); |
| 1928 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1929 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1930 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1931 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1932 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1933 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1934 | MGMT_STATUS_INVALID_PARAMS); |
| 1935 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1936 | hci_dev_lock(hdev); |
| 1937 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1938 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1939 | bool changed = false; |
| 1940 | |
| 1941 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1942 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1943 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1944 | changed = true; |
| 1945 | } |
| 1946 | |
| 1947 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1948 | if (err < 0) |
| 1949 | goto failed; |
| 1950 | |
| 1951 | if (changed) |
| 1952 | err = new_settings(hdev, sk); |
| 1953 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1954 | goto failed; |
| 1955 | } |
| 1956 | |
| 1957 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1958 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1959 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1960 | goto failed; |
| 1961 | } |
| 1962 | |
| 1963 | val = !!cp->val; |
| 1964 | |
| 1965 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1966 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1967 | goto failed; |
| 1968 | } |
| 1969 | |
| 1970 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1971 | if (!cmd) { |
| 1972 | err = -ENOMEM; |
| 1973 | goto failed; |
| 1974 | } |
| 1975 | |
| 1976 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1977 | if (err < 0) { |
| 1978 | mgmt_pending_remove(cmd); |
| 1979 | goto failed; |
| 1980 | } |
| 1981 | |
| 1982 | failed: |
| 1983 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1984 | return err; |
| 1985 | } |
| 1986 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1987 | 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] | 1988 | { |
| 1989 | struct mgmt_mode *cp = data; |
| 1990 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1991 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1992 | int err; |
| 1993 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1994 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1995 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1996 | status = mgmt_bredr_support(hdev); |
| 1997 | if (status) |
| 1998 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1999 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2000 | if (!lmp_ssp_capable(hdev)) |
| 2001 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2002 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2003 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2004 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2005 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2006 | MGMT_STATUS_INVALID_PARAMS); |
| 2007 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2008 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 2009 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2010 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2011 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2012 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2013 | if (cp->val) { |
| 2014 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 2015 | &hdev->dev_flags); |
| 2016 | } else { |
| 2017 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 2018 | &hdev->dev_flags); |
| 2019 | if (!changed) |
| 2020 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 2021 | &hdev->dev_flags); |
| 2022 | else |
| 2023 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2027 | if (err < 0) |
| 2028 | goto failed; |
| 2029 | |
| 2030 | if (changed) |
| 2031 | err = new_settings(hdev, sk); |
| 2032 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2033 | goto failed; |
| 2034 | } |
| 2035 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2036 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 2037 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 2038 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2039 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2040 | goto failed; |
| 2041 | } |
| 2042 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2043 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2044 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2045 | goto failed; |
| 2046 | } |
| 2047 | |
| 2048 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 2049 | if (!cmd) { |
| 2050 | err = -ENOMEM; |
| 2051 | goto failed; |
| 2052 | } |
| 2053 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 2054 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 2055 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2056 | sizeof(cp->val), &cp->val); |
| 2057 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2058 | 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] | 2059 | if (err < 0) { |
| 2060 | mgmt_pending_remove(cmd); |
| 2061 | goto failed; |
| 2062 | } |
| 2063 | |
| 2064 | failed: |
| 2065 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2066 | return err; |
| 2067 | } |
| 2068 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2069 | 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] | 2070 | { |
| 2071 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2072 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2073 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2074 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2075 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2076 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2077 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2078 | status = mgmt_bredr_support(hdev); |
| 2079 | if (status) |
| 2080 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2081 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2082 | if (!lmp_ssp_capable(hdev)) |
| 2083 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2084 | MGMT_STATUS_NOT_SUPPORTED); |
| 2085 | |
| 2086 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 2087 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2088 | MGMT_STATUS_REJECTED); |
| 2089 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2090 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2091 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2092 | MGMT_STATUS_INVALID_PARAMS); |
| 2093 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2094 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2095 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2096 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2097 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2098 | } else { |
| 2099 | if (hdev_is_powered(hdev)) { |
| 2100 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2101 | MGMT_STATUS_REJECTED); |
| 2102 | goto unlock; |
| 2103 | } |
| 2104 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2105 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2106 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2107 | |
| 2108 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2109 | if (err < 0) |
| 2110 | goto unlock; |
| 2111 | |
| 2112 | if (changed) |
| 2113 | err = new_settings(hdev, sk); |
| 2114 | |
| 2115 | unlock: |
| 2116 | hci_dev_unlock(hdev); |
| 2117 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2118 | } |
| 2119 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2120 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 2121 | { |
| 2122 | struct cmd_lookup match = { NULL, hdev }; |
| 2123 | |
| 2124 | if (status) { |
| 2125 | u8 mgmt_err = mgmt_status(status); |
| 2126 | |
| 2127 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2128 | &mgmt_err); |
| 2129 | return; |
| 2130 | } |
| 2131 | |
| 2132 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2133 | |
| 2134 | new_settings(hdev, match.sk); |
| 2135 | |
| 2136 | if (match.sk) |
| 2137 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2138 | |
| 2139 | /* Make sure the controller has a good default for |
| 2140 | * advertising data. Restrict the update to when LE |
| 2141 | * has actually been enabled. During power on, the |
| 2142 | * update in powered_update_hci will take care of it. |
| 2143 | */ |
| 2144 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2145 | struct hci_request req; |
| 2146 | |
| 2147 | hci_dev_lock(hdev); |
| 2148 | |
| 2149 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2150 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2151 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2152 | hci_req_run(&req, NULL); |
| 2153 | |
Johan Hedberg | a70f4b5 | 2014-07-07 15:19:50 +0300 | [diff] [blame] | 2154 | hci_update_background_scan(hdev); |
| 2155 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2156 | hci_dev_unlock(hdev); |
| 2157 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2158 | } |
| 2159 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2160 | 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] | 2161 | { |
| 2162 | struct mgmt_mode *cp = data; |
| 2163 | struct hci_cp_write_le_host_supported hci_cp; |
| 2164 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2165 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2166 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2167 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2168 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2169 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2170 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2171 | if (!lmp_le_capable(hdev)) |
| 2172 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2173 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2174 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2175 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2176 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2177 | MGMT_STATUS_INVALID_PARAMS); |
| 2178 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2179 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2180 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2181 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2182 | MGMT_STATUS_REJECTED); |
| 2183 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2184 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2185 | |
| 2186 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2187 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2188 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2189 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2190 | bool changed = false; |
| 2191 | |
| 2192 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2193 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2194 | changed = true; |
| 2195 | } |
| 2196 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2197 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2198 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2199 | changed = true; |
| 2200 | } |
| 2201 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2202 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2203 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2204 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2205 | |
| 2206 | if (changed) |
| 2207 | err = new_settings(hdev, sk); |
| 2208 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2209 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2210 | } |
| 2211 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2212 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2213 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2214 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2215 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2216 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2220 | if (!cmd) { |
| 2221 | err = -ENOMEM; |
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 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2225 | hci_req_init(&req, hdev); |
| 2226 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2227 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2228 | |
| 2229 | if (val) { |
| 2230 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2231 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2232 | } else { |
| 2233 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2234 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2235 | } |
| 2236 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2237 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2238 | &hci_cp); |
| 2239 | |
| 2240 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2241 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2242 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2243 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2244 | unlock: |
| 2245 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2246 | return err; |
| 2247 | } |
| 2248 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2249 | /* This is a helper function to test for pending mgmt commands that can |
| 2250 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2251 | * mgmt command at a time since otherwise we cannot easily track what |
| 2252 | * the current values are, will be, and based on that calculate if a new |
| 2253 | * HCI command needs to be sent and if yes with what value. |
| 2254 | */ |
| 2255 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2256 | { |
| 2257 | struct pending_cmd *cmd; |
| 2258 | |
| 2259 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2260 | switch (cmd->opcode) { |
| 2261 | case MGMT_OP_ADD_UUID: |
| 2262 | case MGMT_OP_REMOVE_UUID: |
| 2263 | case MGMT_OP_SET_DEV_CLASS: |
| 2264 | case MGMT_OP_SET_POWERED: |
| 2265 | return true; |
| 2266 | } |
| 2267 | } |
| 2268 | |
| 2269 | return false; |
| 2270 | } |
| 2271 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2272 | static const u8 bluetooth_base_uuid[] = { |
| 2273 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2274 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2275 | }; |
| 2276 | |
| 2277 | static u8 get_uuid_size(const u8 *uuid) |
| 2278 | { |
| 2279 | u32 val; |
| 2280 | |
| 2281 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2282 | return 128; |
| 2283 | |
| 2284 | val = get_unaligned_le32(&uuid[12]); |
| 2285 | if (val > 0xffff) |
| 2286 | return 32; |
| 2287 | |
| 2288 | return 16; |
| 2289 | } |
| 2290 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2291 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2292 | { |
| 2293 | struct pending_cmd *cmd; |
| 2294 | |
| 2295 | hci_dev_lock(hdev); |
| 2296 | |
| 2297 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2298 | if (!cmd) |
| 2299 | goto unlock; |
| 2300 | |
| 2301 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2302 | hdev->dev_class, 3); |
| 2303 | |
| 2304 | mgmt_pending_remove(cmd); |
| 2305 | |
| 2306 | unlock: |
| 2307 | hci_dev_unlock(hdev); |
| 2308 | } |
| 2309 | |
| 2310 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2311 | { |
| 2312 | BT_DBG("status 0x%02x", status); |
| 2313 | |
| 2314 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2315 | } |
| 2316 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2317 | 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] | 2318 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2319 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2320 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2321 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2322 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2323 | int err; |
| 2324 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2325 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2326 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2327 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2328 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2329 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2330 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2331 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2332 | goto failed; |
| 2333 | } |
| 2334 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2335 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2336 | if (!uuid) { |
| 2337 | err = -ENOMEM; |
| 2338 | goto failed; |
| 2339 | } |
| 2340 | |
| 2341 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2342 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2343 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2344 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2345 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2346 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2347 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2348 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2349 | update_class(&req); |
| 2350 | update_eir(&req); |
| 2351 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2352 | err = hci_req_run(&req, add_uuid_complete); |
| 2353 | if (err < 0) { |
| 2354 | if (err != -ENODATA) |
| 2355 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2356 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2357 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2358 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2359 | goto failed; |
| 2360 | } |
| 2361 | |
| 2362 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2363 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2364 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2365 | goto failed; |
| 2366 | } |
| 2367 | |
| 2368 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2369 | |
| 2370 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2371 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2372 | return err; |
| 2373 | } |
| 2374 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2375 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2376 | { |
| 2377 | if (!hdev_is_powered(hdev)) |
| 2378 | return false; |
| 2379 | |
| 2380 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2381 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2382 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2383 | return true; |
| 2384 | } |
| 2385 | |
| 2386 | return false; |
| 2387 | } |
| 2388 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2389 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2390 | { |
| 2391 | BT_DBG("status 0x%02x", status); |
| 2392 | |
| 2393 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2394 | } |
| 2395 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2396 | 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] | 2397 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2398 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2399 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2400 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2401 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2402 | 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] | 2403 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2404 | int err, found; |
| 2405 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2406 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2407 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2408 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2409 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2410 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2411 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2412 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2413 | goto unlock; |
| 2414 | } |
| 2415 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2416 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2417 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2418 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2419 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2420 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2421 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2422 | goto unlock; |
| 2423 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2424 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2425 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | found = 0; |
| 2429 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2430 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2431 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2432 | continue; |
| 2433 | |
| 2434 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2435 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2436 | found++; |
| 2437 | } |
| 2438 | |
| 2439 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2440 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2441 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2442 | goto unlock; |
| 2443 | } |
| 2444 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2445 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2446 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2447 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2448 | update_class(&req); |
| 2449 | update_eir(&req); |
| 2450 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2451 | err = hci_req_run(&req, remove_uuid_complete); |
| 2452 | if (err < 0) { |
| 2453 | if (err != -ENODATA) |
| 2454 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2455 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2456 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2457 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2458 | goto unlock; |
| 2459 | } |
| 2460 | |
| 2461 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2462 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2463 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2464 | goto unlock; |
| 2465 | } |
| 2466 | |
| 2467 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2468 | |
| 2469 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2470 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2471 | return err; |
| 2472 | } |
| 2473 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2474 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2475 | { |
| 2476 | BT_DBG("status 0x%02x", status); |
| 2477 | |
| 2478 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2479 | } |
| 2480 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2481 | 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] | 2482 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2483 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2484 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2485 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2486 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2487 | int err; |
| 2488 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2489 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2490 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2491 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2492 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2493 | MGMT_STATUS_NOT_SUPPORTED); |
| 2494 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2495 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2496 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2497 | if (pending_eir_or_class(hdev)) { |
| 2498 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2499 | MGMT_STATUS_BUSY); |
| 2500 | goto unlock; |
| 2501 | } |
| 2502 | |
| 2503 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2504 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2505 | MGMT_STATUS_INVALID_PARAMS); |
| 2506 | goto unlock; |
| 2507 | } |
| 2508 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2509 | hdev->major_class = cp->major; |
| 2510 | hdev->minor_class = cp->minor; |
| 2511 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2512 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2513 | 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] | 2514 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2515 | goto unlock; |
| 2516 | } |
| 2517 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2518 | hci_req_init(&req, hdev); |
| 2519 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2520 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2521 | hci_dev_unlock(hdev); |
| 2522 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2523 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2524 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2525 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2526 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2527 | update_class(&req); |
| 2528 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2529 | err = hci_req_run(&req, set_class_complete); |
| 2530 | if (err < 0) { |
| 2531 | if (err != -ENODATA) |
| 2532 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2533 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2534 | 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] | 2535 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2536 | goto unlock; |
| 2537 | } |
| 2538 | |
| 2539 | 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] | 2540 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2541 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2542 | goto unlock; |
| 2543 | } |
| 2544 | |
| 2545 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2546 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2547 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2548 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2549 | return err; |
| 2550 | } |
| 2551 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2552 | 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] | 2553 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2554 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2555 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2556 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2557 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2558 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2559 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2560 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2561 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2562 | BT_DBG("request for %s", hdev->name); |
| 2563 | |
| 2564 | if (!lmp_bredr_capable(hdev)) |
| 2565 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2566 | MGMT_STATUS_NOT_SUPPORTED); |
| 2567 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2568 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2569 | if (key_count > max_key_count) { |
| 2570 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2571 | key_count); |
| 2572 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2573 | MGMT_STATUS_INVALID_PARAMS); |
| 2574 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2575 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2576 | expected_len = sizeof(*cp) + key_count * |
| 2577 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2578 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2579 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2580 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2581 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2582 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2583 | } |
| 2584 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2585 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2586 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2587 | MGMT_STATUS_INVALID_PARAMS); |
| 2588 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2589 | 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] | 2590 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2591 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2592 | for (i = 0; i < key_count; i++) { |
| 2593 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2594 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2595 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2596 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2597 | MGMT_STATUS_INVALID_PARAMS); |
| 2598 | } |
| 2599 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2600 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2601 | |
| 2602 | hci_link_keys_clear(hdev); |
| 2603 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2604 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2605 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2606 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2607 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2608 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2609 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2610 | |
| 2611 | if (changed) |
| 2612 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2613 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2614 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2615 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2616 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2617 | /* Always ignore debug keys and require a new pairing if |
| 2618 | * the user wants to use them. |
| 2619 | */ |
| 2620 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2621 | continue; |
| 2622 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2623 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2624 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2627 | 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] | 2628 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2629 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2630 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2631 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2632 | } |
| 2633 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2634 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2635 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2636 | { |
| 2637 | struct mgmt_ev_device_unpaired ev; |
| 2638 | |
| 2639 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2640 | ev.addr.type = addr_type; |
| 2641 | |
| 2642 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2643 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2644 | } |
| 2645 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2646 | 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] | 2647 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2648 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2649 | struct mgmt_cp_unpair_device *cp = data; |
| 2650 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2651 | struct hci_cp_disconnect dc; |
| 2652 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2653 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2654 | int err; |
| 2655 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2656 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2657 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2658 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2659 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2660 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2661 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2662 | MGMT_STATUS_INVALID_PARAMS, |
| 2663 | &rp, sizeof(rp)); |
| 2664 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2665 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2666 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2667 | MGMT_STATUS_INVALID_PARAMS, |
| 2668 | &rp, sizeof(rp)); |
| 2669 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2670 | hci_dev_lock(hdev); |
| 2671 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2672 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2673 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2674 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2675 | goto unlock; |
| 2676 | } |
| 2677 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2678 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2679 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2680 | } else { |
| 2681 | u8 addr_type; |
| 2682 | |
| 2683 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2684 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2685 | else |
| 2686 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2687 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2688 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2689 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2690 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2691 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2692 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2693 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2694 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2695 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2696 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2697 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2698 | goto unlock; |
| 2699 | } |
| 2700 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2701 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2702 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2703 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2704 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2705 | else |
| 2706 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2707 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2708 | } else { |
| 2709 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2710 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2711 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2712 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2713 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2714 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2715 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2716 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2717 | } |
| 2718 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2719 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2720 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2721 | if (!cmd) { |
| 2722 | err = -ENOMEM; |
| 2723 | goto unlock; |
| 2724 | } |
| 2725 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2726 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2727 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2728 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2729 | if (err < 0) |
| 2730 | mgmt_pending_remove(cmd); |
| 2731 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2732 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2733 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2734 | return err; |
| 2735 | } |
| 2736 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2737 | 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] | 2738 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2739 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2740 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2741 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2742 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2743 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2744 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2745 | int err; |
| 2746 | |
| 2747 | BT_DBG(""); |
| 2748 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2749 | memset(&rp, 0, sizeof(rp)); |
| 2750 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2751 | rp.addr.type = cp->addr.type; |
| 2752 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2753 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2754 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2755 | MGMT_STATUS_INVALID_PARAMS, |
| 2756 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2757 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2758 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2759 | |
| 2760 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2761 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2762 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2763 | goto failed; |
| 2764 | } |
| 2765 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2766 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2767 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2768 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2769 | goto failed; |
| 2770 | } |
| 2771 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2772 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2773 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2774 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2775 | else |
| 2776 | 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] | 2777 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2778 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2779 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2780 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2781 | goto failed; |
| 2782 | } |
| 2783 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2784 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2785 | if (!cmd) { |
| 2786 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2787 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2788 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2789 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2790 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2791 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2792 | |
| 2793 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2794 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2795 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2796 | |
| 2797 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2798 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2799 | return err; |
| 2800 | } |
| 2801 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2802 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2803 | { |
| 2804 | switch (link_type) { |
| 2805 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2806 | switch (addr_type) { |
| 2807 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2808 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2809 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2810 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2811 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2812 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2813 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2814 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2815 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2816 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2817 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2818 | } |
| 2819 | } |
| 2820 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2821 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2822 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2823 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2824 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2825 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2826 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2827 | int err; |
| 2828 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2829 | |
| 2830 | BT_DBG(""); |
| 2831 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2832 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2833 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2834 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2835 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2836 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2837 | goto unlock; |
| 2838 | } |
| 2839 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2840 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2841 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2842 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2843 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2844 | } |
| 2845 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2846 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2847 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2848 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2849 | err = -ENOMEM; |
| 2850 | goto unlock; |
| 2851 | } |
| 2852 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2853 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2854 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2855 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2856 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2857 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2858 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2859 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2860 | continue; |
| 2861 | i++; |
| 2862 | } |
| 2863 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2864 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2865 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2866 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2867 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2868 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2869 | 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] | 2870 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2871 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2872 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2873 | |
| 2874 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2875 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2876 | return err; |
| 2877 | } |
| 2878 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2879 | 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] | 2880 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2881 | { |
| 2882 | struct pending_cmd *cmd; |
| 2883 | int err; |
| 2884 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2885 | 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] | 2886 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2887 | if (!cmd) |
| 2888 | return -ENOMEM; |
| 2889 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2890 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2891 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2892 | if (err < 0) |
| 2893 | mgmt_pending_remove(cmd); |
| 2894 | |
| 2895 | return err; |
| 2896 | } |
| 2897 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2898 | 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] | 2899 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2900 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2901 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2902 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2903 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2904 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2905 | int err; |
| 2906 | |
| 2907 | BT_DBG(""); |
| 2908 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2909 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2910 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2911 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2912 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2913 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2914 | goto failed; |
| 2915 | } |
| 2916 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2917 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2918 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2919 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2920 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2921 | goto failed; |
| 2922 | } |
| 2923 | |
| 2924 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2925 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2926 | |
| 2927 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2928 | |
| 2929 | BT_ERR("PIN code is not 16 bytes long"); |
| 2930 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2931 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2932 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2933 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2934 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2935 | |
| 2936 | goto failed; |
| 2937 | } |
| 2938 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2939 | 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] | 2940 | if (!cmd) { |
| 2941 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2942 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2943 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2944 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2945 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2946 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2947 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2948 | |
| 2949 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2950 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2951 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2952 | |
| 2953 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2954 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2955 | return err; |
| 2956 | } |
| 2957 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2958 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2959 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2960 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2961 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2962 | |
| 2963 | BT_DBG(""); |
| 2964 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2965 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2966 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2967 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2968 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2969 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2970 | |
| 2971 | hdev->io_capability = cp->io_capability; |
| 2972 | |
| 2973 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2974 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2975 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2976 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2977 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2978 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2979 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2980 | } |
| 2981 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2982 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2983 | { |
| 2984 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2985 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2986 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2987 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2988 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2989 | continue; |
| 2990 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2991 | if (cmd->user_data != conn) |
| 2992 | continue; |
| 2993 | |
| 2994 | return cmd; |
| 2995 | } |
| 2996 | |
| 2997 | return NULL; |
| 2998 | } |
| 2999 | |
| 3000 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 3001 | { |
| 3002 | struct mgmt_rp_pair_device rp; |
| 3003 | struct hci_conn *conn = cmd->user_data; |
| 3004 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 3005 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 3006 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3007 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 3008 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3009 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3010 | |
| 3011 | /* So we don't get further callbacks for this connection */ |
| 3012 | conn->connect_cfm_cb = NULL; |
| 3013 | conn->security_cfm_cb = NULL; |
| 3014 | conn->disconn_cfm_cb = NULL; |
| 3015 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3016 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3017 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3018 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3019 | } |
| 3020 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3021 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 3022 | { |
| 3023 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 3024 | struct pending_cmd *cmd; |
| 3025 | |
| 3026 | cmd = find_pairing(conn); |
| 3027 | if (cmd) |
| 3028 | pairing_complete(cmd, status); |
| 3029 | } |
| 3030 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3031 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 3032 | { |
| 3033 | struct pending_cmd *cmd; |
| 3034 | |
| 3035 | BT_DBG("status %u", status); |
| 3036 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3037 | cmd = find_pairing(conn); |
| 3038 | if (!cmd) |
| 3039 | BT_DBG("Unable to find a pending command"); |
| 3040 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 3041 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3042 | } |
| 3043 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3044 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3045 | { |
| 3046 | struct pending_cmd *cmd; |
| 3047 | |
| 3048 | BT_DBG("status %u", status); |
| 3049 | |
| 3050 | if (!status) |
| 3051 | return; |
| 3052 | |
| 3053 | cmd = find_pairing(conn); |
| 3054 | if (!cmd) |
| 3055 | BT_DBG("Unable to find a pending command"); |
| 3056 | else |
| 3057 | pairing_complete(cmd, mgmt_status(status)); |
| 3058 | } |
| 3059 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3060 | 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] | 3061 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3062 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3063 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3064 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3065 | struct pending_cmd *cmd; |
| 3066 | u8 sec_level, auth_type; |
| 3067 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3068 | int err; |
| 3069 | |
| 3070 | BT_DBG(""); |
| 3071 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3072 | memset(&rp, 0, sizeof(rp)); |
| 3073 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3074 | rp.addr.type = cp->addr.type; |
| 3075 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3076 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 3077 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3078 | MGMT_STATUS_INVALID_PARAMS, |
| 3079 | &rp, sizeof(rp)); |
| 3080 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3081 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 3082 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3083 | MGMT_STATUS_INVALID_PARAMS, |
| 3084 | &rp, sizeof(rp)); |
| 3085 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3086 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3087 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3088 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3089 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3090 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3091 | goto unlock; |
| 3092 | } |
| 3093 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3094 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3095 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3096 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3097 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3098 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3099 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3100 | } else { |
| 3101 | u8 addr_type; |
| 3102 | |
| 3103 | /* Convert from L2CAP channel address type to HCI address type |
| 3104 | */ |
| 3105 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3106 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3107 | else |
| 3108 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3109 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3110 | /* When pairing a new device, it is expected to remember |
| 3111 | * this device for future connections. Adding the connection |
| 3112 | * parameter information ahead of time allows tracking |
| 3113 | * of the slave preferred values and will speed up any |
| 3114 | * further connection establishment. |
| 3115 | * |
| 3116 | * If connection parameters already exist, then they |
| 3117 | * will be kept and this function does nothing. |
| 3118 | */ |
| 3119 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3120 | |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 3121 | /* Request a connection with master = true role */ |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3122 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Johan Hedberg | cdd6275 | 2014-07-07 15:02:28 +0300 | [diff] [blame] | 3123 | sec_level, HCI_LE_CONN_TIMEOUT, true); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3124 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3125 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3126 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3127 | int status; |
| 3128 | |
| 3129 | if (PTR_ERR(conn) == -EBUSY) |
| 3130 | status = MGMT_STATUS_BUSY; |
| 3131 | else |
| 3132 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3133 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3134 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3135 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3136 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3137 | goto unlock; |
| 3138 | } |
| 3139 | |
| 3140 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3141 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3142 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3143 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3144 | goto unlock; |
| 3145 | } |
| 3146 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3147 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3148 | if (!cmd) { |
| 3149 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3150 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3151 | goto unlock; |
| 3152 | } |
| 3153 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3154 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3155 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3156 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3157 | conn->security_cfm_cb = pairing_complete_cb; |
| 3158 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3159 | } else { |
| 3160 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3161 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3162 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3163 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3164 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3165 | conn->io_capability = cp->io_cap; |
| 3166 | cmd->user_data = conn; |
| 3167 | |
| 3168 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3169 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3170 | pairing_complete(cmd, 0); |
| 3171 | |
| 3172 | err = 0; |
| 3173 | |
| 3174 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3175 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3176 | return err; |
| 3177 | } |
| 3178 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3179 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3180 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3181 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3182 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3183 | struct pending_cmd *cmd; |
| 3184 | struct hci_conn *conn; |
| 3185 | int err; |
| 3186 | |
| 3187 | BT_DBG(""); |
| 3188 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3189 | hci_dev_lock(hdev); |
| 3190 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3191 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3192 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3193 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3194 | goto unlock; |
| 3195 | } |
| 3196 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3197 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3198 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3199 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3200 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3201 | goto unlock; |
| 3202 | } |
| 3203 | |
| 3204 | conn = cmd->user_data; |
| 3205 | |
| 3206 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3207 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3208 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3209 | goto unlock; |
| 3210 | } |
| 3211 | |
| 3212 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3213 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3214 | 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] | 3215 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3216 | unlock: |
| 3217 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3218 | return err; |
| 3219 | } |
| 3220 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3221 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3222 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3223 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3224 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3225 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3226 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3227 | int err; |
| 3228 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3229 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3230 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3231 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3232 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3233 | MGMT_STATUS_NOT_POWERED, addr, |
| 3234 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3235 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3236 | } |
| 3237 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3238 | if (addr->type == BDADDR_BREDR) |
| 3239 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3240 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3241 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3242 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3243 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3244 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3245 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3246 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3247 | goto done; |
| 3248 | } |
| 3249 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3250 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3251 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3252 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3253 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3254 | MGMT_STATUS_SUCCESS, addr, |
| 3255 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3256 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3257 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3258 | MGMT_STATUS_FAILED, addr, |
| 3259 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3260 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3261 | goto done; |
| 3262 | } |
| 3263 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3264 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3265 | if (!cmd) { |
| 3266 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3267 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3268 | } |
| 3269 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3270 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3271 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3272 | struct hci_cp_user_passkey_reply cp; |
| 3273 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3274 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3275 | cp.passkey = passkey; |
| 3276 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3277 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3278 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3279 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3280 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3281 | if (err < 0) |
| 3282 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3283 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3284 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3285 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3286 | return err; |
| 3287 | } |
| 3288 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3289 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3290 | void *data, u16 len) |
| 3291 | { |
| 3292 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3293 | |
| 3294 | BT_DBG(""); |
| 3295 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3296 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3297 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3298 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3299 | } |
| 3300 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3301 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3302 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3303 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3304 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3305 | |
| 3306 | BT_DBG(""); |
| 3307 | |
| 3308 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3309 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3310 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3311 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3312 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3313 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3314 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3315 | } |
| 3316 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3317 | 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] | 3318 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3319 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3320 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3321 | |
| 3322 | BT_DBG(""); |
| 3323 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3324 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3325 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3326 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3327 | } |
| 3328 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3329 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3330 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3331 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3332 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3333 | |
| 3334 | BT_DBG(""); |
| 3335 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3336 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3337 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3338 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3339 | } |
| 3340 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3341 | 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] | 3342 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3343 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3344 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3345 | |
| 3346 | BT_DBG(""); |
| 3347 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3348 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3349 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3350 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3351 | } |
| 3352 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3353 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3354 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3355 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3356 | struct hci_cp_write_local_name cp; |
| 3357 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3358 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3359 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3360 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3361 | } |
| 3362 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3363 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3364 | { |
| 3365 | struct mgmt_cp_set_local_name *cp; |
| 3366 | struct pending_cmd *cmd; |
| 3367 | |
| 3368 | BT_DBG("status 0x%02x", status); |
| 3369 | |
| 3370 | hci_dev_lock(hdev); |
| 3371 | |
| 3372 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3373 | if (!cmd) |
| 3374 | goto unlock; |
| 3375 | |
| 3376 | cp = cmd->param; |
| 3377 | |
| 3378 | if (status) |
| 3379 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3380 | mgmt_status(status)); |
| 3381 | else |
| 3382 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3383 | cp, sizeof(*cp)); |
| 3384 | |
| 3385 | mgmt_pending_remove(cmd); |
| 3386 | |
| 3387 | unlock: |
| 3388 | hci_dev_unlock(hdev); |
| 3389 | } |
| 3390 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3391 | 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] | 3392 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3393 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3394 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3395 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3396 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3397 | int err; |
| 3398 | |
| 3399 | BT_DBG(""); |
| 3400 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3401 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3402 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3403 | /* If the old values are the same as the new ones just return a |
| 3404 | * direct command complete event. |
| 3405 | */ |
| 3406 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3407 | !memcmp(hdev->short_name, cp->short_name, |
| 3408 | sizeof(hdev->short_name))) { |
| 3409 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3410 | data, len); |
| 3411 | goto failed; |
| 3412 | } |
| 3413 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3414 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3415 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3416 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3417 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3418 | |
| 3419 | 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] | 3420 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3421 | if (err < 0) |
| 3422 | goto failed; |
| 3423 | |
| 3424 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3425 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3426 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3427 | goto failed; |
| 3428 | } |
| 3429 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3430 | 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] | 3431 | if (!cmd) { |
| 3432 | err = -ENOMEM; |
| 3433 | goto failed; |
| 3434 | } |
| 3435 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3436 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3437 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3438 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3439 | |
| 3440 | if (lmp_bredr_capable(hdev)) { |
| 3441 | update_name(&req); |
| 3442 | update_eir(&req); |
| 3443 | } |
| 3444 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3445 | /* The name is stored in the scan response data and so |
| 3446 | * no need to udpate the advertising data here. |
| 3447 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3448 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3449 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3450 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3451 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3452 | if (err < 0) |
| 3453 | mgmt_pending_remove(cmd); |
| 3454 | |
| 3455 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3456 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3457 | return err; |
| 3458 | } |
| 3459 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3460 | 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] | 3461 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3462 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3463 | struct pending_cmd *cmd; |
| 3464 | int err; |
| 3465 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3466 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3467 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3468 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3469 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3470 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3471 | 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] | 3472 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3473 | goto unlock; |
| 3474 | } |
| 3475 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3476 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3477 | 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] | 3478 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3479 | goto unlock; |
| 3480 | } |
| 3481 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3482 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3483 | 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] | 3484 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3485 | goto unlock; |
| 3486 | } |
| 3487 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3488 | 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] | 3489 | if (!cmd) { |
| 3490 | err = -ENOMEM; |
| 3491 | goto unlock; |
| 3492 | } |
| 3493 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3494 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3495 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3496 | 0, NULL); |
| 3497 | else |
| 3498 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3499 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3500 | if (err < 0) |
| 3501 | mgmt_pending_remove(cmd); |
| 3502 | |
| 3503 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3504 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3505 | return err; |
| 3506 | } |
| 3507 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3508 | 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] | 3509 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3510 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3511 | int err; |
| 3512 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3513 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3514 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3515 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3516 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3517 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3518 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3519 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3520 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3521 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3522 | cp->hash, cp->randomizer); |
| 3523 | if (err < 0) |
| 3524 | status = MGMT_STATUS_FAILED; |
| 3525 | else |
| 3526 | status = MGMT_STATUS_SUCCESS; |
| 3527 | |
| 3528 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3529 | status, &cp->addr, sizeof(cp->addr)); |
| 3530 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3531 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3532 | u8 status; |
| 3533 | |
| 3534 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3535 | cp->hash192, |
| 3536 | cp->randomizer192, |
| 3537 | cp->hash256, |
| 3538 | cp->randomizer256); |
| 3539 | if (err < 0) |
| 3540 | status = MGMT_STATUS_FAILED; |
| 3541 | else |
| 3542 | status = MGMT_STATUS_SUCCESS; |
| 3543 | |
| 3544 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3545 | status, &cp->addr, sizeof(cp->addr)); |
| 3546 | } else { |
| 3547 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3548 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3549 | MGMT_STATUS_INVALID_PARAMS); |
| 3550 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3551 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3552 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3553 | return err; |
| 3554 | } |
| 3555 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3556 | 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] | 3557 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3558 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3559 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3560 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3561 | int err; |
| 3562 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3563 | BT_DBG("%s", hdev->name); |
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_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3566 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3567 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3568 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3569 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3570 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3571 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3572 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3573 | 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] | 3574 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3575 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3576 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3577 | return err; |
| 3578 | } |
| 3579 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3580 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3581 | { |
| 3582 | struct pending_cmd *cmd; |
| 3583 | u8 type; |
| 3584 | int err; |
| 3585 | |
| 3586 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3587 | |
| 3588 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3589 | if (!cmd) |
| 3590 | return -ENOENT; |
| 3591 | |
| 3592 | type = hdev->discovery.type; |
| 3593 | |
| 3594 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3595 | &type, sizeof(type)); |
| 3596 | mgmt_pending_remove(cmd); |
| 3597 | |
| 3598 | return err; |
| 3599 | } |
| 3600 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3601 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3602 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3603 | unsigned long timeout = 0; |
| 3604 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3605 | BT_DBG("status %d", status); |
| 3606 | |
| 3607 | if (status) { |
| 3608 | hci_dev_lock(hdev); |
| 3609 | mgmt_start_discovery_failed(hdev, status); |
| 3610 | hci_dev_unlock(hdev); |
| 3611 | return; |
| 3612 | } |
| 3613 | |
| 3614 | hci_dev_lock(hdev); |
| 3615 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3616 | hci_dev_unlock(hdev); |
| 3617 | |
| 3618 | switch (hdev->discovery.type) { |
| 3619 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3620 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3621 | break; |
| 3622 | |
| 3623 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3624 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3625 | break; |
| 3626 | |
| 3627 | case DISCOV_TYPE_BREDR: |
| 3628 | break; |
| 3629 | |
| 3630 | default: |
| 3631 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3632 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3633 | |
| 3634 | if (!timeout) |
| 3635 | return; |
| 3636 | |
| 3637 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3638 | } |
| 3639 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3640 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3641 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3642 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3643 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3644 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3645 | struct hci_cp_le_set_scan_param param_cp; |
| 3646 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3647 | struct hci_cp_inquiry inq_cp; |
| 3648 | struct hci_request req; |
| 3649 | /* General inquiry access code (GIAC) */ |
| 3650 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3651 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3652 | int err; |
| 3653 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3654 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3655 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3656 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3657 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3658 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3659 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3660 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3661 | goto failed; |
| 3662 | } |
| 3663 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3664 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3665 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3666 | MGMT_STATUS_BUSY); |
| 3667 | goto failed; |
| 3668 | } |
| 3669 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3670 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3671 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3672 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3673 | goto failed; |
| 3674 | } |
| 3675 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3676 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3677 | if (!cmd) { |
| 3678 | err = -ENOMEM; |
| 3679 | goto failed; |
| 3680 | } |
| 3681 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3682 | hdev->discovery.type = cp->type; |
| 3683 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3684 | hci_req_init(&req, hdev); |
| 3685 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3686 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3687 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3688 | status = mgmt_bredr_support(hdev); |
| 3689 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3690 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3691 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3692 | mgmt_pending_remove(cmd); |
| 3693 | goto failed; |
| 3694 | } |
| 3695 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3696 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3697 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3698 | MGMT_STATUS_BUSY); |
| 3699 | mgmt_pending_remove(cmd); |
| 3700 | goto failed; |
| 3701 | } |
| 3702 | |
| 3703 | hci_inquiry_cache_flush(hdev); |
| 3704 | |
| 3705 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3706 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3707 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3708 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3709 | break; |
| 3710 | |
| 3711 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3712 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3713 | status = mgmt_le_support(hdev); |
| 3714 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3715 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3716 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3717 | mgmt_pending_remove(cmd); |
| 3718 | goto failed; |
| 3719 | } |
| 3720 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3721 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3722 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3723 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3724 | MGMT_STATUS_NOT_SUPPORTED); |
| 3725 | mgmt_pending_remove(cmd); |
| 3726 | goto failed; |
| 3727 | } |
| 3728 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3729 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3730 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3731 | MGMT_STATUS_REJECTED); |
| 3732 | mgmt_pending_remove(cmd); |
| 3733 | goto failed; |
| 3734 | } |
| 3735 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3736 | /* If controller is scanning, it means the background scanning |
| 3737 | * is running. Thus, we should temporarily stop it in order to |
| 3738 | * set the discovery scanning parameters. |
| 3739 | */ |
| 3740 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3741 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3742 | |
| 3743 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3744 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3745 | /* All active scans will be done with either a resolvable |
| 3746 | * private address (when privacy feature has been enabled) |
| 3747 | * or unresolvable private address. |
| 3748 | */ |
| 3749 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3750 | if (err < 0) { |
| 3751 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3752 | MGMT_STATUS_FAILED); |
| 3753 | mgmt_pending_remove(cmd); |
| 3754 | goto failed; |
| 3755 | } |
| 3756 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3757 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3758 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3759 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3760 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3761 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3762 | ¶m_cp); |
| 3763 | |
| 3764 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3765 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3766 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3767 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3768 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3769 | break; |
| 3770 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3771 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3772 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3773 | MGMT_STATUS_INVALID_PARAMS); |
| 3774 | mgmt_pending_remove(cmd); |
| 3775 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3776 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3777 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3778 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3779 | if (err < 0) |
| 3780 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3781 | else |
| 3782 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3783 | |
| 3784 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3785 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3786 | return err; |
| 3787 | } |
| 3788 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3789 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3790 | { |
| 3791 | struct pending_cmd *cmd; |
| 3792 | int err; |
| 3793 | |
| 3794 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3795 | if (!cmd) |
| 3796 | return -ENOENT; |
| 3797 | |
| 3798 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3799 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3800 | mgmt_pending_remove(cmd); |
| 3801 | |
| 3802 | return err; |
| 3803 | } |
| 3804 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3805 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3806 | { |
| 3807 | BT_DBG("status %d", status); |
| 3808 | |
| 3809 | hci_dev_lock(hdev); |
| 3810 | |
| 3811 | if (status) { |
| 3812 | mgmt_stop_discovery_failed(hdev, status); |
| 3813 | goto unlock; |
| 3814 | } |
| 3815 | |
| 3816 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3817 | |
| 3818 | unlock: |
| 3819 | hci_dev_unlock(hdev); |
| 3820 | } |
| 3821 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3822 | 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] | 3823 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3824 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3825 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3826 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3827 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3828 | int err; |
| 3829 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3830 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3831 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3832 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3833 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3834 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3835 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3836 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3837 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3838 | goto unlock; |
| 3839 | } |
| 3840 | |
| 3841 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3842 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3843 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3844 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3845 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3846 | } |
| 3847 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3848 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3849 | if (!cmd) { |
| 3850 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3851 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3852 | } |
| 3853 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3854 | hci_req_init(&req, hdev); |
| 3855 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3856 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3857 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3858 | err = hci_req_run(&req, stop_discovery_complete); |
| 3859 | if (!err) { |
| 3860 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3861 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3862 | } |
| 3863 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3864 | mgmt_pending_remove(cmd); |
| 3865 | |
| 3866 | /* If no HCI commands were sent we're done */ |
| 3867 | if (err == -ENODATA) { |
| 3868 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3869 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3870 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3871 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3872 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3873 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3874 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3875 | return err; |
| 3876 | } |
| 3877 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3878 | 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] | 3879 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3880 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3881 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3882 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3883 | int err; |
| 3884 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3885 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3886 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3887 | hci_dev_lock(hdev); |
| 3888 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3889 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3890 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3891 | MGMT_STATUS_FAILED, &cp->addr, |
| 3892 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3893 | goto failed; |
| 3894 | } |
| 3895 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3896 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3897 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3898 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3899 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3900 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3901 | goto failed; |
| 3902 | } |
| 3903 | |
| 3904 | if (cp->name_known) { |
| 3905 | e->name_state = NAME_KNOWN; |
| 3906 | list_del(&e->list); |
| 3907 | } else { |
| 3908 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3909 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3910 | } |
| 3911 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3912 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3913 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3914 | |
| 3915 | failed: |
| 3916 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3917 | return err; |
| 3918 | } |
| 3919 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3920 | 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] | 3921 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3922 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3923 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3924 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3925 | int err; |
| 3926 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3927 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3928 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3929 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3930 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3931 | MGMT_STATUS_INVALID_PARAMS, |
| 3932 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3933 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3934 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3935 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3936 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3937 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3938 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3939 | goto done; |
| 3940 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3941 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3942 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3943 | sk); |
| 3944 | status = MGMT_STATUS_SUCCESS; |
| 3945 | |
| 3946 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3947 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3948 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3949 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3950 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3951 | |
| 3952 | return err; |
| 3953 | } |
| 3954 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3955 | 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] | 3956 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3957 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3958 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3959 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3960 | int err; |
| 3961 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3962 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3963 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3964 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3965 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3966 | MGMT_STATUS_INVALID_PARAMS, |
| 3967 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3968 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3969 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3970 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3971 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3972 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3973 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3974 | goto done; |
| 3975 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3976 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3977 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3978 | sk); |
| 3979 | status = MGMT_STATUS_SUCCESS; |
| 3980 | |
| 3981 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3982 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3983 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3984 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3985 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3986 | |
| 3987 | return err; |
| 3988 | } |
| 3989 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3990 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3991 | u16 len) |
| 3992 | { |
| 3993 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3994 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3995 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3996 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3997 | |
| 3998 | BT_DBG("%s", hdev->name); |
| 3999 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4000 | source = __le16_to_cpu(cp->source); |
| 4001 | |
| 4002 | if (source > 0x0002) |
| 4003 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 4004 | MGMT_STATUS_INVALID_PARAMS); |
| 4005 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4006 | hci_dev_lock(hdev); |
| 4007 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 4008 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4009 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 4010 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 4011 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 4012 | |
| 4013 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 4014 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4015 | hci_req_init(&req, hdev); |
| 4016 | update_eir(&req); |
| 4017 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4018 | |
| 4019 | hci_dev_unlock(hdev); |
| 4020 | |
| 4021 | return err; |
| 4022 | } |
| 4023 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4024 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 4025 | { |
| 4026 | struct cmd_lookup match = { NULL, hdev }; |
| 4027 | |
| 4028 | if (status) { |
| 4029 | u8 mgmt_err = mgmt_status(status); |
| 4030 | |
| 4031 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4032 | cmd_status_rsp, &mgmt_err); |
| 4033 | return; |
| 4034 | } |
| 4035 | |
Johan Hedberg | c93bd15 | 2014-07-08 15:07:48 +0300 | [diff] [blame] | 4036 | if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) |
| 4037 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4038 | else |
| 4039 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 4040 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4041 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4042 | &match); |
| 4043 | |
| 4044 | new_settings(hdev, match.sk); |
| 4045 | |
| 4046 | if (match.sk) |
| 4047 | sock_put(match.sk); |
| 4048 | } |
| 4049 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4050 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4051 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4052 | { |
| 4053 | struct mgmt_mode *cp = data; |
| 4054 | struct pending_cmd *cmd; |
| 4055 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4056 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4057 | int err; |
| 4058 | |
| 4059 | BT_DBG("request for %s", hdev->name); |
| 4060 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4061 | status = mgmt_le_support(hdev); |
| 4062 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4063 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4064 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4065 | |
| 4066 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4067 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4068 | MGMT_STATUS_INVALID_PARAMS); |
| 4069 | |
| 4070 | hci_dev_lock(hdev); |
| 4071 | |
| 4072 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4073 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4074 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4075 | /* The following conditions are ones which mean that we should |
| 4076 | * not do any HCI communication but directly send a mgmt |
| 4077 | * response to user space (after toggling the flag if |
| 4078 | * necessary). |
| 4079 | */ |
| 4080 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 4081 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4082 | bool changed = false; |
| 4083 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4084 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 4085 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4086 | changed = true; |
| 4087 | } |
| 4088 | |
| 4089 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4090 | if (err < 0) |
| 4091 | goto unlock; |
| 4092 | |
| 4093 | if (changed) |
| 4094 | err = new_settings(hdev, sk); |
| 4095 | |
| 4096 | goto unlock; |
| 4097 | } |
| 4098 | |
| 4099 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4100 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 4101 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4102 | MGMT_STATUS_BUSY); |
| 4103 | goto unlock; |
| 4104 | } |
| 4105 | |
| 4106 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4107 | if (!cmd) { |
| 4108 | err = -ENOMEM; |
| 4109 | goto unlock; |
| 4110 | } |
| 4111 | |
| 4112 | hci_req_init(&req, hdev); |
| 4113 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4114 | if (val) |
| 4115 | enable_advertising(&req); |
| 4116 | else |
| 4117 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4118 | |
| 4119 | err = hci_req_run(&req, set_advertising_complete); |
| 4120 | if (err < 0) |
| 4121 | mgmt_pending_remove(cmd); |
| 4122 | |
| 4123 | unlock: |
| 4124 | hci_dev_unlock(hdev); |
| 4125 | return err; |
| 4126 | } |
| 4127 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4128 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4129 | void *data, u16 len) |
| 4130 | { |
| 4131 | struct mgmt_cp_set_static_address *cp = data; |
| 4132 | int err; |
| 4133 | |
| 4134 | BT_DBG("%s", hdev->name); |
| 4135 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4136 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4137 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4138 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4139 | |
| 4140 | if (hdev_is_powered(hdev)) |
| 4141 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4142 | MGMT_STATUS_REJECTED); |
| 4143 | |
| 4144 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4145 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 4146 | return cmd_status(sk, hdev->id, |
| 4147 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4148 | MGMT_STATUS_INVALID_PARAMS); |
| 4149 | |
| 4150 | /* Two most significant bits shall be set */ |
| 4151 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4152 | return cmd_status(sk, hdev->id, |
| 4153 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4154 | MGMT_STATUS_INVALID_PARAMS); |
| 4155 | } |
| 4156 | |
| 4157 | hci_dev_lock(hdev); |
| 4158 | |
| 4159 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4160 | |
| 4161 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4162 | |
| 4163 | hci_dev_unlock(hdev); |
| 4164 | |
| 4165 | return err; |
| 4166 | } |
| 4167 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4168 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4169 | void *data, u16 len) |
| 4170 | { |
| 4171 | struct mgmt_cp_set_scan_params *cp = data; |
| 4172 | __u16 interval, window; |
| 4173 | int err; |
| 4174 | |
| 4175 | BT_DBG("%s", hdev->name); |
| 4176 | |
| 4177 | if (!lmp_le_capable(hdev)) |
| 4178 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4179 | MGMT_STATUS_NOT_SUPPORTED); |
| 4180 | |
| 4181 | interval = __le16_to_cpu(cp->interval); |
| 4182 | |
| 4183 | if (interval < 0x0004 || interval > 0x4000) |
| 4184 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4185 | MGMT_STATUS_INVALID_PARAMS); |
| 4186 | |
| 4187 | window = __le16_to_cpu(cp->window); |
| 4188 | |
| 4189 | if (window < 0x0004 || window > 0x4000) |
| 4190 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4191 | MGMT_STATUS_INVALID_PARAMS); |
| 4192 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4193 | if (window > interval) |
| 4194 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4195 | MGMT_STATUS_INVALID_PARAMS); |
| 4196 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4197 | hci_dev_lock(hdev); |
| 4198 | |
| 4199 | hdev->le_scan_interval = interval; |
| 4200 | hdev->le_scan_window = window; |
| 4201 | |
| 4202 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4203 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4204 | /* If background scan is running, restart it so new parameters are |
| 4205 | * loaded. |
| 4206 | */ |
| 4207 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4208 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4209 | struct hci_request req; |
| 4210 | |
| 4211 | hci_req_init(&req, hdev); |
| 4212 | |
| 4213 | hci_req_add_le_scan_disable(&req); |
| 4214 | hci_req_add_le_passive_scan(&req); |
| 4215 | |
| 4216 | hci_req_run(&req, NULL); |
| 4217 | } |
| 4218 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4219 | hci_dev_unlock(hdev); |
| 4220 | |
| 4221 | return err; |
| 4222 | } |
| 4223 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4224 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4225 | { |
| 4226 | struct pending_cmd *cmd; |
| 4227 | |
| 4228 | BT_DBG("status 0x%02x", status); |
| 4229 | |
| 4230 | hci_dev_lock(hdev); |
| 4231 | |
| 4232 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4233 | if (!cmd) |
| 4234 | goto unlock; |
| 4235 | |
| 4236 | if (status) { |
| 4237 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4238 | mgmt_status(status)); |
| 4239 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4240 | struct mgmt_mode *cp = cmd->param; |
| 4241 | |
| 4242 | if (cp->val) |
| 4243 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4244 | else |
| 4245 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4246 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4247 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4248 | new_settings(hdev, cmd->sk); |
| 4249 | } |
| 4250 | |
| 4251 | mgmt_pending_remove(cmd); |
| 4252 | |
| 4253 | unlock: |
| 4254 | hci_dev_unlock(hdev); |
| 4255 | } |
| 4256 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4257 | 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] | 4258 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4259 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4260 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4261 | struct pending_cmd *cmd; |
| 4262 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4263 | int err; |
| 4264 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4265 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4266 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4267 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4268 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4269 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4270 | MGMT_STATUS_NOT_SUPPORTED); |
| 4271 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4272 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4273 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4274 | MGMT_STATUS_INVALID_PARAMS); |
| 4275 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4276 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4277 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4278 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4279 | |
| 4280 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4281 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4282 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4283 | |
| 4284 | hci_dev_lock(hdev); |
| 4285 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4286 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4287 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4288 | MGMT_STATUS_BUSY); |
| 4289 | goto unlock; |
| 4290 | } |
| 4291 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4292 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4293 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4294 | hdev); |
| 4295 | goto unlock; |
| 4296 | } |
| 4297 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4298 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4299 | data, len); |
| 4300 | if (!cmd) { |
| 4301 | err = -ENOMEM; |
| 4302 | goto unlock; |
| 4303 | } |
| 4304 | |
| 4305 | hci_req_init(&req, hdev); |
| 4306 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4307 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4308 | |
| 4309 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4310 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4311 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4312 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4313 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4314 | } |
| 4315 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4316 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4317 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4318 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4319 | return err; |
| 4320 | } |
| 4321 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4322 | static void set_bredr_scan(struct hci_request *req) |
| 4323 | { |
| 4324 | struct hci_dev *hdev = req->hdev; |
| 4325 | u8 scan = 0; |
| 4326 | |
| 4327 | /* Ensure that fast connectable is disabled. This function will |
| 4328 | * not do anything if the page scan parameters are already what |
| 4329 | * they should be. |
| 4330 | */ |
| 4331 | write_fast_connectable(req, false); |
| 4332 | |
| 4333 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4334 | scan |= SCAN_PAGE; |
| 4335 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4336 | scan |= SCAN_INQUIRY; |
| 4337 | |
| 4338 | if (scan) |
| 4339 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4340 | } |
| 4341 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4342 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4343 | { |
| 4344 | struct pending_cmd *cmd; |
| 4345 | |
| 4346 | BT_DBG("status 0x%02x", status); |
| 4347 | |
| 4348 | hci_dev_lock(hdev); |
| 4349 | |
| 4350 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4351 | if (!cmd) |
| 4352 | goto unlock; |
| 4353 | |
| 4354 | if (status) { |
| 4355 | u8 mgmt_err = mgmt_status(status); |
| 4356 | |
| 4357 | /* We need to restore the flag if related HCI commands |
| 4358 | * failed. |
| 4359 | */ |
| 4360 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4361 | |
| 4362 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4363 | } else { |
| 4364 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4365 | new_settings(hdev, cmd->sk); |
| 4366 | } |
| 4367 | |
| 4368 | mgmt_pending_remove(cmd); |
| 4369 | |
| 4370 | unlock: |
| 4371 | hci_dev_unlock(hdev); |
| 4372 | } |
| 4373 | |
| 4374 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4375 | { |
| 4376 | struct mgmt_mode *cp = data; |
| 4377 | struct pending_cmd *cmd; |
| 4378 | struct hci_request req; |
| 4379 | int err; |
| 4380 | |
| 4381 | BT_DBG("request for %s", hdev->name); |
| 4382 | |
| 4383 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4384 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4385 | MGMT_STATUS_NOT_SUPPORTED); |
| 4386 | |
| 4387 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4388 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4389 | MGMT_STATUS_REJECTED); |
| 4390 | |
| 4391 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4392 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4393 | MGMT_STATUS_INVALID_PARAMS); |
| 4394 | |
| 4395 | hci_dev_lock(hdev); |
| 4396 | |
| 4397 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4398 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4399 | goto unlock; |
| 4400 | } |
| 4401 | |
| 4402 | if (!hdev_is_powered(hdev)) { |
| 4403 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4404 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4405 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4406 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4407 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4408 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4409 | } |
| 4410 | |
| 4411 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4412 | |
| 4413 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4414 | if (err < 0) |
| 4415 | goto unlock; |
| 4416 | |
| 4417 | err = new_settings(hdev, sk); |
| 4418 | goto unlock; |
| 4419 | } |
| 4420 | |
| 4421 | /* Reject disabling when powered on */ |
| 4422 | if (!cp->val) { |
| 4423 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4424 | MGMT_STATUS_REJECTED); |
| 4425 | goto unlock; |
| 4426 | } |
| 4427 | |
| 4428 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4429 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4430 | MGMT_STATUS_BUSY); |
| 4431 | goto unlock; |
| 4432 | } |
| 4433 | |
| 4434 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4435 | if (!cmd) { |
| 4436 | err = -ENOMEM; |
| 4437 | goto unlock; |
| 4438 | } |
| 4439 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4440 | /* 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] | 4441 | * generates the correct flags. |
| 4442 | */ |
| 4443 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4444 | |
| 4445 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4446 | |
| 4447 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4448 | set_bredr_scan(&req); |
| 4449 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4450 | /* Since only the advertising data flags will change, there |
| 4451 | * is no need to update the scan response data. |
| 4452 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4453 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4454 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4455 | err = hci_req_run(&req, set_bredr_complete); |
| 4456 | if (err < 0) |
| 4457 | mgmt_pending_remove(cmd); |
| 4458 | |
| 4459 | unlock: |
| 4460 | hci_dev_unlock(hdev); |
| 4461 | return err; |
| 4462 | } |
| 4463 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4464 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4465 | void *data, u16 len) |
| 4466 | { |
| 4467 | struct mgmt_mode *cp = data; |
| 4468 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4469 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4470 | int err; |
| 4471 | |
| 4472 | BT_DBG("request for %s", hdev->name); |
| 4473 | |
| 4474 | status = mgmt_bredr_support(hdev); |
| 4475 | if (status) |
| 4476 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4477 | status); |
| 4478 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4479 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4480 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4481 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4482 | MGMT_STATUS_NOT_SUPPORTED); |
| 4483 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4484 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4485 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4486 | MGMT_STATUS_INVALID_PARAMS); |
| 4487 | |
| 4488 | hci_dev_lock(hdev); |
| 4489 | |
| 4490 | if (!hdev_is_powered(hdev)) { |
| 4491 | bool changed; |
| 4492 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4493 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4494 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4495 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4496 | if (cp->val == 0x02) |
| 4497 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4498 | else |
| 4499 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4500 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4501 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4502 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4503 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4504 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4505 | |
| 4506 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4507 | if (err < 0) |
| 4508 | goto failed; |
| 4509 | |
| 4510 | if (changed) |
| 4511 | err = new_settings(hdev, sk); |
| 4512 | |
| 4513 | goto failed; |
| 4514 | } |
| 4515 | |
| 4516 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4517 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4518 | MGMT_STATUS_BUSY); |
| 4519 | goto failed; |
| 4520 | } |
| 4521 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4522 | val = !!cp->val; |
| 4523 | |
| 4524 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4525 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4526 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4527 | goto failed; |
| 4528 | } |
| 4529 | |
| 4530 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4531 | if (!cmd) { |
| 4532 | err = -ENOMEM; |
| 4533 | goto failed; |
| 4534 | } |
| 4535 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4536 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4537 | if (err < 0) { |
| 4538 | mgmt_pending_remove(cmd); |
| 4539 | goto failed; |
| 4540 | } |
| 4541 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4542 | if (cp->val == 0x02) |
| 4543 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4544 | else |
| 4545 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4546 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4547 | failed: |
| 4548 | hci_dev_unlock(hdev); |
| 4549 | return err; |
| 4550 | } |
| 4551 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4552 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4553 | void *data, u16 len) |
| 4554 | { |
| 4555 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4556 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4557 | int err; |
| 4558 | |
| 4559 | BT_DBG("request for %s", hdev->name); |
| 4560 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4561 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4562 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4563 | MGMT_STATUS_INVALID_PARAMS); |
| 4564 | |
| 4565 | hci_dev_lock(hdev); |
| 4566 | |
| 4567 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4568 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4569 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4570 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4571 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4572 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4573 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4574 | if (cp->val == 0x02) |
| 4575 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4576 | &hdev->dev_flags); |
| 4577 | else |
| 4578 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4579 | &hdev->dev_flags); |
| 4580 | |
| 4581 | if (hdev_is_powered(hdev) && use_changed && |
| 4582 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4583 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4584 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4585 | sizeof(mode), &mode); |
| 4586 | } |
| 4587 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4588 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4589 | if (err < 0) |
| 4590 | goto unlock; |
| 4591 | |
| 4592 | if (changed) |
| 4593 | err = new_settings(hdev, sk); |
| 4594 | |
| 4595 | unlock: |
| 4596 | hci_dev_unlock(hdev); |
| 4597 | return err; |
| 4598 | } |
| 4599 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4600 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4601 | u16 len) |
| 4602 | { |
| 4603 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4604 | bool changed; |
| 4605 | int err; |
| 4606 | |
| 4607 | BT_DBG("request for %s", hdev->name); |
| 4608 | |
| 4609 | if (!lmp_le_capable(hdev)) |
| 4610 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4611 | MGMT_STATUS_NOT_SUPPORTED); |
| 4612 | |
| 4613 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4614 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4615 | MGMT_STATUS_INVALID_PARAMS); |
| 4616 | |
| 4617 | if (hdev_is_powered(hdev)) |
| 4618 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4619 | MGMT_STATUS_REJECTED); |
| 4620 | |
| 4621 | hci_dev_lock(hdev); |
| 4622 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4623 | /* If user space supports this command it is also expected to |
| 4624 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4625 | */ |
| 4626 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4627 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4628 | if (cp->privacy) { |
| 4629 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4630 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4631 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4632 | } else { |
| 4633 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4634 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4635 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4636 | } |
| 4637 | |
| 4638 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4639 | if (err < 0) |
| 4640 | goto unlock; |
| 4641 | |
| 4642 | if (changed) |
| 4643 | err = new_settings(hdev, sk); |
| 4644 | |
| 4645 | unlock: |
| 4646 | hci_dev_unlock(hdev); |
| 4647 | return err; |
| 4648 | } |
| 4649 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4650 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4651 | { |
| 4652 | switch (irk->addr.type) { |
| 4653 | case BDADDR_LE_PUBLIC: |
| 4654 | return true; |
| 4655 | |
| 4656 | case BDADDR_LE_RANDOM: |
| 4657 | /* Two most significant bits shall be set */ |
| 4658 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4659 | return false; |
| 4660 | return true; |
| 4661 | } |
| 4662 | |
| 4663 | return false; |
| 4664 | } |
| 4665 | |
| 4666 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4667 | u16 len) |
| 4668 | { |
| 4669 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4670 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4671 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4672 | u16 irk_count, expected_len; |
| 4673 | int i, err; |
| 4674 | |
| 4675 | BT_DBG("request for %s", hdev->name); |
| 4676 | |
| 4677 | if (!lmp_le_capable(hdev)) |
| 4678 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4679 | MGMT_STATUS_NOT_SUPPORTED); |
| 4680 | |
| 4681 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4682 | if (irk_count > max_irk_count) { |
| 4683 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
| 4684 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4685 | MGMT_STATUS_INVALID_PARAMS); |
| 4686 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4687 | |
| 4688 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4689 | if (expected_len != len) { |
| 4690 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4691 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4692 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4693 | MGMT_STATUS_INVALID_PARAMS); |
| 4694 | } |
| 4695 | |
| 4696 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4697 | |
| 4698 | for (i = 0; i < irk_count; i++) { |
| 4699 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4700 | |
| 4701 | if (!irk_is_valid(key)) |
| 4702 | return cmd_status(sk, hdev->id, |
| 4703 | MGMT_OP_LOAD_IRKS, |
| 4704 | MGMT_STATUS_INVALID_PARAMS); |
| 4705 | } |
| 4706 | |
| 4707 | hci_dev_lock(hdev); |
| 4708 | |
| 4709 | hci_smp_irks_clear(hdev); |
| 4710 | |
| 4711 | for (i = 0; i < irk_count; i++) { |
| 4712 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4713 | u8 addr_type; |
| 4714 | |
| 4715 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4716 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4717 | else |
| 4718 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4719 | |
| 4720 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4721 | BDADDR_ANY); |
| 4722 | } |
| 4723 | |
| 4724 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4725 | |
| 4726 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4727 | |
| 4728 | hci_dev_unlock(hdev); |
| 4729 | |
| 4730 | return err; |
| 4731 | } |
| 4732 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4733 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4734 | { |
| 4735 | if (key->master != 0x00 && key->master != 0x01) |
| 4736 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4737 | |
| 4738 | switch (key->addr.type) { |
| 4739 | case BDADDR_LE_PUBLIC: |
| 4740 | return true; |
| 4741 | |
| 4742 | case BDADDR_LE_RANDOM: |
| 4743 | /* Two most significant bits shall be set */ |
| 4744 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4745 | return false; |
| 4746 | return true; |
| 4747 | } |
| 4748 | |
| 4749 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4750 | } |
| 4751 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4752 | 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] | 4753 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4754 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4755 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4756 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4757 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4758 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4759 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4760 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4761 | BT_DBG("request for %s", hdev->name); |
| 4762 | |
| 4763 | if (!lmp_le_capable(hdev)) |
| 4764 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4765 | MGMT_STATUS_NOT_SUPPORTED); |
| 4766 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4767 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4768 | if (key_count > max_key_count) { |
| 4769 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
| 4770 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4771 | MGMT_STATUS_INVALID_PARAMS); |
| 4772 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4773 | |
| 4774 | expected_len = sizeof(*cp) + key_count * |
| 4775 | sizeof(struct mgmt_ltk_info); |
| 4776 | if (expected_len != len) { |
| 4777 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4778 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4779 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4780 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4781 | } |
| 4782 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4783 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4784 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4785 | for (i = 0; i < key_count; i++) { |
| 4786 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4787 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4788 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4789 | return cmd_status(sk, hdev->id, |
| 4790 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4791 | MGMT_STATUS_INVALID_PARAMS); |
| 4792 | } |
| 4793 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4794 | hci_dev_lock(hdev); |
| 4795 | |
| 4796 | hci_smp_ltks_clear(hdev); |
| 4797 | |
| 4798 | for (i = 0; i < key_count; i++) { |
| 4799 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4800 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4801 | |
| 4802 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4803 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4804 | else |
| 4805 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4806 | |
| 4807 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4808 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4809 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4810 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4811 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4812 | switch (key->type) { |
| 4813 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4814 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4815 | break; |
| 4816 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4817 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4818 | break; |
| 4819 | default: |
| 4820 | continue; |
| 4821 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4822 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4823 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4824 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4825 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4826 | } |
| 4827 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4828 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4829 | NULL, 0); |
| 4830 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4831 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4832 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4833 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4834 | } |
| 4835 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4836 | struct cmd_conn_lookup { |
| 4837 | struct hci_conn *conn; |
| 4838 | bool valid_tx_power; |
| 4839 | u8 mgmt_status; |
| 4840 | }; |
| 4841 | |
| 4842 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4843 | { |
| 4844 | struct cmd_conn_lookup *match = data; |
| 4845 | struct mgmt_cp_get_conn_info *cp; |
| 4846 | struct mgmt_rp_get_conn_info rp; |
| 4847 | struct hci_conn *conn = cmd->user_data; |
| 4848 | |
| 4849 | if (conn != match->conn) |
| 4850 | return; |
| 4851 | |
| 4852 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4853 | |
| 4854 | memset(&rp, 0, sizeof(rp)); |
| 4855 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4856 | rp.addr.type = cp->addr.type; |
| 4857 | |
| 4858 | if (!match->mgmt_status) { |
| 4859 | rp.rssi = conn->rssi; |
| 4860 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4861 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4862 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4863 | rp.max_tx_power = conn->max_tx_power; |
| 4864 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4865 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4866 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4867 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4871 | match->mgmt_status, &rp, sizeof(rp)); |
| 4872 | |
| 4873 | hci_conn_drop(conn); |
| 4874 | |
| 4875 | mgmt_pending_remove(cmd); |
| 4876 | } |
| 4877 | |
| 4878 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4879 | { |
| 4880 | struct hci_cp_read_rssi *cp; |
| 4881 | struct hci_conn *conn; |
| 4882 | struct cmd_conn_lookup match; |
| 4883 | u16 handle; |
| 4884 | |
| 4885 | BT_DBG("status 0x%02x", status); |
| 4886 | |
| 4887 | hci_dev_lock(hdev); |
| 4888 | |
| 4889 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4890 | * otherwise we assume it's not valid. At the moment we assume that |
| 4891 | * either both or none of current and max values are valid to keep code |
| 4892 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4893 | */ |
| 4894 | match.valid_tx_power = !status; |
| 4895 | |
| 4896 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4897 | * Level so we check which one was last sent to retrieve connection |
| 4898 | * handle. Both commands have handle as first parameter so it's safe to |
| 4899 | * cast data on the same command struct. |
| 4900 | * |
| 4901 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4902 | * In other case we simply override error to indicate success as we |
| 4903 | * already remembered if TX power value is actually valid. |
| 4904 | */ |
| 4905 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4906 | if (!cp) { |
| 4907 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4908 | status = 0; |
| 4909 | } |
| 4910 | |
| 4911 | if (!cp) { |
| 4912 | BT_ERR("invalid sent_cmd in response"); |
| 4913 | goto unlock; |
| 4914 | } |
| 4915 | |
| 4916 | handle = __le16_to_cpu(cp->handle); |
| 4917 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4918 | if (!conn) { |
| 4919 | BT_ERR("unknown handle (%d) in response", handle); |
| 4920 | goto unlock; |
| 4921 | } |
| 4922 | |
| 4923 | match.conn = conn; |
| 4924 | match.mgmt_status = mgmt_status(status); |
| 4925 | |
| 4926 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4927 | * connection only. |
| 4928 | */ |
| 4929 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4930 | get_conn_info_complete, &match); |
| 4931 | |
| 4932 | unlock: |
| 4933 | hci_dev_unlock(hdev); |
| 4934 | } |
| 4935 | |
| 4936 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4937 | u16 len) |
| 4938 | { |
| 4939 | struct mgmt_cp_get_conn_info *cp = data; |
| 4940 | struct mgmt_rp_get_conn_info rp; |
| 4941 | struct hci_conn *conn; |
| 4942 | unsigned long conn_info_age; |
| 4943 | int err = 0; |
| 4944 | |
| 4945 | BT_DBG("%s", hdev->name); |
| 4946 | |
| 4947 | memset(&rp, 0, sizeof(rp)); |
| 4948 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4949 | rp.addr.type = cp->addr.type; |
| 4950 | |
| 4951 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4952 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4953 | MGMT_STATUS_INVALID_PARAMS, |
| 4954 | &rp, sizeof(rp)); |
| 4955 | |
| 4956 | hci_dev_lock(hdev); |
| 4957 | |
| 4958 | if (!hdev_is_powered(hdev)) { |
| 4959 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4960 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4961 | goto unlock; |
| 4962 | } |
| 4963 | |
| 4964 | if (cp->addr.type == BDADDR_BREDR) |
| 4965 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4966 | &cp->addr.bdaddr); |
| 4967 | else |
| 4968 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4969 | |
| 4970 | if (!conn || conn->state != BT_CONNECTED) { |
| 4971 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4972 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4973 | goto unlock; |
| 4974 | } |
| 4975 | |
| 4976 | /* To avoid client trying to guess when to poll again for information we |
| 4977 | * calculate conn info age as random value between min/max set in hdev. |
| 4978 | */ |
| 4979 | conn_info_age = hdev->conn_info_min_age + |
| 4980 | prandom_u32_max(hdev->conn_info_max_age - |
| 4981 | hdev->conn_info_min_age); |
| 4982 | |
| 4983 | /* Query controller to refresh cached values if they are too old or were |
| 4984 | * never read. |
| 4985 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4986 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4987 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4988 | !conn->conn_info_timestamp) { |
| 4989 | struct hci_request req; |
| 4990 | struct hci_cp_read_tx_power req_txp_cp; |
| 4991 | struct hci_cp_read_rssi req_rssi_cp; |
| 4992 | struct pending_cmd *cmd; |
| 4993 | |
| 4994 | hci_req_init(&req, hdev); |
| 4995 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4996 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4997 | &req_rssi_cp); |
| 4998 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4999 | /* For LE links TX power does not change thus we don't need to |
| 5000 | * query for it once value is known. |
| 5001 | */ |
| 5002 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5003 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 5004 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5005 | req_txp_cp.type = 0x00; |
| 5006 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5007 | sizeof(req_txp_cp), &req_txp_cp); |
| 5008 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5009 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5010 | /* Max TX power needs to be read only once per connection */ |
| 5011 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 5012 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 5013 | req_txp_cp.type = 0x01; |
| 5014 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 5015 | sizeof(req_txp_cp), &req_txp_cp); |
| 5016 | } |
| 5017 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5018 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 5019 | if (err < 0) |
| 5020 | goto unlock; |
| 5021 | |
| 5022 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5023 | data, len); |
| 5024 | if (!cmd) { |
| 5025 | err = -ENOMEM; |
| 5026 | goto unlock; |
| 5027 | } |
| 5028 | |
| 5029 | hci_conn_hold(conn); |
| 5030 | cmd->user_data = conn; |
| 5031 | |
| 5032 | conn->conn_info_timestamp = jiffies; |
| 5033 | } else { |
| 5034 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5035 | rp.rssi = conn->rssi; |
| 5036 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5037 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5038 | |
| 5039 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5040 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 5041 | } |
| 5042 | |
| 5043 | unlock: |
| 5044 | hci_dev_unlock(hdev); |
| 5045 | return err; |
| 5046 | } |
| 5047 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5048 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 5049 | { |
| 5050 | struct mgmt_cp_get_clock_info *cp; |
| 5051 | struct mgmt_rp_get_clock_info rp; |
| 5052 | struct hci_cp_read_clock *hci_cp; |
| 5053 | struct pending_cmd *cmd; |
| 5054 | struct hci_conn *conn; |
| 5055 | |
| 5056 | BT_DBG("%s status %u", hdev->name, status); |
| 5057 | |
| 5058 | hci_dev_lock(hdev); |
| 5059 | |
| 5060 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5061 | if (!hci_cp) |
| 5062 | goto unlock; |
| 5063 | |
| 5064 | if (hci_cp->which) { |
| 5065 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5066 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5067 | } else { |
| 5068 | conn = NULL; |
| 5069 | } |
| 5070 | |
| 5071 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5072 | if (!cmd) |
| 5073 | goto unlock; |
| 5074 | |
| 5075 | cp = cmd->param; |
| 5076 | |
| 5077 | memset(&rp, 0, sizeof(rp)); |
| 5078 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 5079 | |
| 5080 | if (status) |
| 5081 | goto send_rsp; |
| 5082 | |
| 5083 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5084 | |
| 5085 | if (conn) { |
| 5086 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5087 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5088 | } |
| 5089 | |
| 5090 | send_rsp: |
| 5091 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 5092 | &rp, sizeof(rp)); |
| 5093 | mgmt_pending_remove(cmd); |
| 5094 | if (conn) |
| 5095 | hci_conn_drop(conn); |
| 5096 | |
| 5097 | unlock: |
| 5098 | hci_dev_unlock(hdev); |
| 5099 | } |
| 5100 | |
| 5101 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5102 | u16 len) |
| 5103 | { |
| 5104 | struct mgmt_cp_get_clock_info *cp = data; |
| 5105 | struct mgmt_rp_get_clock_info rp; |
| 5106 | struct hci_cp_read_clock hci_cp; |
| 5107 | struct pending_cmd *cmd; |
| 5108 | struct hci_request req; |
| 5109 | struct hci_conn *conn; |
| 5110 | int err; |
| 5111 | |
| 5112 | BT_DBG("%s", hdev->name); |
| 5113 | |
| 5114 | memset(&rp, 0, sizeof(rp)); |
| 5115 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5116 | rp.addr.type = cp->addr.type; |
| 5117 | |
| 5118 | if (cp->addr.type != BDADDR_BREDR) |
| 5119 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5120 | MGMT_STATUS_INVALID_PARAMS, |
| 5121 | &rp, sizeof(rp)); |
| 5122 | |
| 5123 | hci_dev_lock(hdev); |
| 5124 | |
| 5125 | if (!hdev_is_powered(hdev)) { |
| 5126 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5127 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5128 | goto unlock; |
| 5129 | } |
| 5130 | |
| 5131 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5132 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5133 | &cp->addr.bdaddr); |
| 5134 | if (!conn || conn->state != BT_CONNECTED) { |
| 5135 | err = cmd_complete(sk, hdev->id, |
| 5136 | MGMT_OP_GET_CLOCK_INFO, |
| 5137 | MGMT_STATUS_NOT_CONNECTED, |
| 5138 | &rp, sizeof(rp)); |
| 5139 | goto unlock; |
| 5140 | } |
| 5141 | } else { |
| 5142 | conn = NULL; |
| 5143 | } |
| 5144 | |
| 5145 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5146 | if (!cmd) { |
| 5147 | err = -ENOMEM; |
| 5148 | goto unlock; |
| 5149 | } |
| 5150 | |
| 5151 | hci_req_init(&req, hdev); |
| 5152 | |
| 5153 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5154 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5155 | |
| 5156 | if (conn) { |
| 5157 | hci_conn_hold(conn); |
| 5158 | cmd->user_data = conn; |
| 5159 | |
| 5160 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5161 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5162 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5163 | } |
| 5164 | |
| 5165 | err = hci_req_run(&req, get_clock_info_complete); |
| 5166 | if (err < 0) |
| 5167 | mgmt_pending_remove(cmd); |
| 5168 | |
| 5169 | unlock: |
| 5170 | hci_dev_unlock(hdev); |
| 5171 | return err; |
| 5172 | } |
| 5173 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5174 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5175 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5176 | { |
| 5177 | struct mgmt_ev_device_added ev; |
| 5178 | |
| 5179 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5180 | ev.addr.type = type; |
| 5181 | ev.action = action; |
| 5182 | |
| 5183 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5184 | } |
| 5185 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5186 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5187 | void *data, u16 len) |
| 5188 | { |
| 5189 | struct mgmt_cp_add_device *cp = data; |
| 5190 | u8 auto_conn, addr_type; |
| 5191 | int err; |
| 5192 | |
| 5193 | BT_DBG("%s", hdev->name); |
| 5194 | |
| 5195 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5196 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5197 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5198 | MGMT_STATUS_INVALID_PARAMS, |
| 5199 | &cp->addr, sizeof(cp->addr)); |
| 5200 | |
| 5201 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5202 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5203 | MGMT_STATUS_INVALID_PARAMS, |
| 5204 | &cp->addr, sizeof(cp->addr)); |
| 5205 | |
| 5206 | hci_dev_lock(hdev); |
| 5207 | |
| 5208 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5209 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5210 | else |
| 5211 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5212 | |
| 5213 | if (cp->action) |
| 5214 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5215 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5216 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5217 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5218 | /* If the connection parameters don't exist for this device, |
| 5219 | * they will be created and configured with defaults. |
| 5220 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5221 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5222 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5223 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5224 | MGMT_STATUS_FAILED, |
| 5225 | &cp->addr, sizeof(cp->addr)); |
| 5226 | goto unlock; |
| 5227 | } |
| 5228 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5229 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5230 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5231 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5232 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5233 | |
| 5234 | unlock: |
| 5235 | hci_dev_unlock(hdev); |
| 5236 | return err; |
| 5237 | } |
| 5238 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5239 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5240 | bdaddr_t *bdaddr, u8 type) |
| 5241 | { |
| 5242 | struct mgmt_ev_device_removed ev; |
| 5243 | |
| 5244 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5245 | ev.addr.type = type; |
| 5246 | |
| 5247 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5248 | } |
| 5249 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5250 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5251 | void *data, u16 len) |
| 5252 | { |
| 5253 | struct mgmt_cp_remove_device *cp = data; |
| 5254 | int err; |
| 5255 | |
| 5256 | BT_DBG("%s", hdev->name); |
| 5257 | |
| 5258 | hci_dev_lock(hdev); |
| 5259 | |
| 5260 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5261 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5262 | u8 addr_type; |
| 5263 | |
| 5264 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5265 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5266 | MGMT_STATUS_INVALID_PARAMS, |
| 5267 | &cp->addr, sizeof(cp->addr)); |
| 5268 | goto unlock; |
| 5269 | } |
| 5270 | |
| 5271 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5272 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5273 | else |
| 5274 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5275 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5276 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5277 | addr_type); |
| 5278 | if (!params) { |
| 5279 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5280 | MGMT_STATUS_INVALID_PARAMS, |
| 5281 | &cp->addr, sizeof(cp->addr)); |
| 5282 | goto unlock; |
| 5283 | } |
| 5284 | |
| 5285 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5286 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5287 | MGMT_STATUS_INVALID_PARAMS, |
| 5288 | &cp->addr, sizeof(cp->addr)); |
| 5289 | goto unlock; |
| 5290 | } |
| 5291 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5292 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5293 | list_del(¶ms->list); |
| 5294 | kfree(params); |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame] | 5295 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5296 | |
| 5297 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5298 | } else { |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5299 | struct hci_conn_params *p, *tmp; |
| 5300 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5301 | if (cp->addr.type) { |
| 5302 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5303 | MGMT_STATUS_INVALID_PARAMS, |
| 5304 | &cp->addr, sizeof(cp->addr)); |
| 5305 | goto unlock; |
| 5306 | } |
| 5307 | |
Johan Hedberg | 19de082 | 2014-07-06 13:06:51 +0300 | [diff] [blame] | 5308 | list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) { |
| 5309 | if (p->auto_connect == HCI_AUTO_CONN_DISABLED) |
| 5310 | continue; |
| 5311 | device_removed(sk, hdev, &p->addr, p->addr_type); |
| 5312 | list_del(&p->action); |
| 5313 | list_del(&p->list); |
| 5314 | kfree(p); |
| 5315 | } |
| 5316 | |
| 5317 | BT_DBG("All LE connection parameters were removed"); |
| 5318 | |
| 5319 | hci_update_background_scan(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5323 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5324 | |
| 5325 | unlock: |
| 5326 | hci_dev_unlock(hdev); |
| 5327 | return err; |
| 5328 | } |
| 5329 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5330 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5331 | u16 len) |
| 5332 | { |
| 5333 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5334 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5335 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5336 | u16 param_count, expected_len; |
| 5337 | int i; |
| 5338 | |
| 5339 | if (!lmp_le_capable(hdev)) |
| 5340 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5341 | MGMT_STATUS_NOT_SUPPORTED); |
| 5342 | |
| 5343 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5344 | if (param_count > max_param_count) { |
| 5345 | BT_ERR("load_conn_param: too big param_count value %u", |
| 5346 | param_count); |
| 5347 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5348 | MGMT_STATUS_INVALID_PARAMS); |
| 5349 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5350 | |
| 5351 | expected_len = sizeof(*cp) + param_count * |
| 5352 | sizeof(struct mgmt_conn_param); |
| 5353 | if (expected_len != len) { |
| 5354 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5355 | expected_len, len); |
| 5356 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5357 | MGMT_STATUS_INVALID_PARAMS); |
| 5358 | } |
| 5359 | |
| 5360 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5361 | |
| 5362 | hci_dev_lock(hdev); |
| 5363 | |
| 5364 | hci_conn_params_clear_disabled(hdev); |
| 5365 | |
| 5366 | for (i = 0; i < param_count; i++) { |
| 5367 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5368 | struct hci_conn_params *hci_param; |
| 5369 | u16 min, max, latency, timeout; |
| 5370 | u8 addr_type; |
| 5371 | |
| 5372 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5373 | param->addr.type); |
| 5374 | |
| 5375 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5376 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5377 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5378 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5379 | } else { |
| 5380 | BT_ERR("Ignoring invalid connection parameters"); |
| 5381 | continue; |
| 5382 | } |
| 5383 | |
| 5384 | min = le16_to_cpu(param->min_interval); |
| 5385 | max = le16_to_cpu(param->max_interval); |
| 5386 | latency = le16_to_cpu(param->latency); |
| 5387 | timeout = le16_to_cpu(param->timeout); |
| 5388 | |
| 5389 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5390 | min, max, latency, timeout); |
| 5391 | |
| 5392 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5393 | BT_ERR("Ignoring invalid connection parameters"); |
| 5394 | continue; |
| 5395 | } |
| 5396 | |
| 5397 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5398 | addr_type); |
| 5399 | if (!hci_param) { |
| 5400 | BT_ERR("Failed to add connection parameters"); |
| 5401 | continue; |
| 5402 | } |
| 5403 | |
| 5404 | hci_param->conn_min_interval = min; |
| 5405 | hci_param->conn_max_interval = max; |
| 5406 | hci_param->conn_latency = latency; |
| 5407 | hci_param->supervision_timeout = timeout; |
| 5408 | } |
| 5409 | |
| 5410 | hci_dev_unlock(hdev); |
| 5411 | |
| 5412 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5413 | } |
| 5414 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5415 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5416 | void *data, u16 len) |
| 5417 | { |
| 5418 | struct mgmt_cp_set_external_config *cp = data; |
| 5419 | bool changed; |
| 5420 | int err; |
| 5421 | |
| 5422 | BT_DBG("%s", hdev->name); |
| 5423 | |
| 5424 | if (hdev_is_powered(hdev)) |
| 5425 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5426 | MGMT_STATUS_REJECTED); |
| 5427 | |
| 5428 | if (cp->config != 0x00 && cp->config != 0x01) |
| 5429 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5430 | MGMT_STATUS_INVALID_PARAMS); |
| 5431 | |
| 5432 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 5433 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5434 | MGMT_STATUS_NOT_SUPPORTED); |
| 5435 | |
| 5436 | hci_dev_lock(hdev); |
| 5437 | |
| 5438 | if (cp->config) |
| 5439 | changed = !test_and_set_bit(HCI_EXT_CONFIGURED, |
| 5440 | &hdev->dev_flags); |
| 5441 | else |
| 5442 | changed = test_and_clear_bit(HCI_EXT_CONFIGURED, |
| 5443 | &hdev->dev_flags); |
| 5444 | |
| 5445 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5446 | if (err < 0) |
| 5447 | goto unlock; |
| 5448 | |
| 5449 | if (!changed) |
| 5450 | goto unlock; |
| 5451 | |
Marcel Holtmann | f4537c0 | 2014-07-04 19:06:23 +0200 | [diff] [blame] | 5452 | err = new_options(hdev, sk); |
| 5453 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5454 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) { |
| 5455 | mgmt_index_removed(hdev); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5456 | |
| 5457 | if (test_and_change_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { |
| 5458 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5459 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5460 | |
| 5461 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5462 | } else { |
Marcel Holtmann | 5ea234d | 2014-07-06 12:11:16 +0200 | [diff] [blame] | 5463 | set_bit(HCI_RAW, &hdev->flags); |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5464 | mgmt_index_added(hdev); |
| 5465 | } |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | unlock: |
| 5469 | hci_dev_unlock(hdev); |
| 5470 | return err; |
| 5471 | } |
| 5472 | |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5473 | static int set_public_address(struct sock *sk, struct hci_dev *hdev, |
| 5474 | void *data, u16 len) |
| 5475 | { |
| 5476 | struct mgmt_cp_set_public_address *cp = data; |
| 5477 | bool changed; |
| 5478 | int err; |
| 5479 | |
| 5480 | BT_DBG("%s", hdev->name); |
| 5481 | |
| 5482 | if (hdev_is_powered(hdev)) |
| 5483 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5484 | MGMT_STATUS_REJECTED); |
| 5485 | |
| 5486 | if (!bacmp(&cp->bdaddr, BDADDR_ANY)) |
| 5487 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5488 | MGMT_STATUS_INVALID_PARAMS); |
| 5489 | |
| 5490 | if (!hdev->set_bdaddr) |
| 5491 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, |
| 5492 | MGMT_STATUS_NOT_SUPPORTED); |
| 5493 | |
| 5494 | hci_dev_lock(hdev); |
| 5495 | |
| 5496 | changed = !!bacmp(&hdev->public_addr, &cp->bdaddr); |
| 5497 | bacpy(&hdev->public_addr, &cp->bdaddr); |
| 5498 | |
| 5499 | err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev); |
| 5500 | if (err < 0) |
| 5501 | goto unlock; |
| 5502 | |
| 5503 | if (!changed) |
| 5504 | goto unlock; |
| 5505 | |
| 5506 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5507 | err = new_options(hdev, sk); |
| 5508 | |
| 5509 | if (is_configured(hdev)) { |
| 5510 | mgmt_index_removed(hdev); |
| 5511 | |
| 5512 | clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags); |
| 5513 | |
| 5514 | set_bit(HCI_CONFIG, &hdev->dev_flags); |
| 5515 | set_bit(HCI_AUTO_OFF, &hdev->dev_flags); |
| 5516 | |
| 5517 | queue_work(hdev->req_workqueue, &hdev->power_on); |
| 5518 | } |
| 5519 | |
| 5520 | unlock: |
| 5521 | hci_dev_unlock(hdev); |
| 5522 | return err; |
| 5523 | } |
| 5524 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5525 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5526 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5527 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5528 | bool var_len; |
| 5529 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5530 | } mgmt_handlers[] = { |
| 5531 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5532 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5533 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5534 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5535 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5536 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5537 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5538 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5539 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5540 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5541 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5542 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5543 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5544 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5545 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5546 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5547 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5548 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5549 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5550 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5551 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5552 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5553 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5554 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5555 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5556 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5557 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5558 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5559 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5560 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5561 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5562 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5563 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5564 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5565 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5566 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5567 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5568 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5569 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5570 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5571 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5572 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5573 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5574 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5575 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5576 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5577 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5578 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5579 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5580 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5581 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5582 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5583 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5584 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
| 5585 | { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE }, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 5586 | { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE }, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5587 | { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE }, |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5588 | { set_public_address, false, MGMT_SET_PUBLIC_ADDRESS_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5589 | }; |
| 5590 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5591 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5592 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5593 | void *buf; |
| 5594 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5595 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5596 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5597 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5598 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5599 | int err; |
| 5600 | |
| 5601 | BT_DBG("got %zu bytes", msglen); |
| 5602 | |
| 5603 | if (msglen < sizeof(*hdr)) |
| 5604 | return -EINVAL; |
| 5605 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5606 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5607 | if (!buf) |
| 5608 | return -ENOMEM; |
| 5609 | |
| 5610 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5611 | err = -EFAULT; |
| 5612 | goto done; |
| 5613 | } |
| 5614 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5615 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5616 | opcode = __le16_to_cpu(hdr->opcode); |
| 5617 | index = __le16_to_cpu(hdr->index); |
| 5618 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5619 | |
| 5620 | if (len != msglen - sizeof(*hdr)) { |
| 5621 | err = -EINVAL; |
| 5622 | goto done; |
| 5623 | } |
| 5624 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5625 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5626 | hdev = hci_dev_get(index); |
| 5627 | if (!hdev) { |
| 5628 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5629 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5630 | goto done; |
| 5631 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5632 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5633 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | d603b76b | 2014-07-06 12:11:14 +0200 | [diff] [blame] | 5634 | test_bit(HCI_CONFIG, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5635 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5636 | err = cmd_status(sk, index, opcode, |
| 5637 | MGMT_STATUS_INVALID_INDEX); |
| 5638 | goto done; |
| 5639 | } |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5640 | |
| 5641 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame] | 5642 | opcode != MGMT_OP_READ_CONFIG_INFO && |
Marcel Holtmann | 9713c17 | 2014-07-06 12:11:15 +0200 | [diff] [blame] | 5643 | opcode != MGMT_OP_SET_EXTERNAL_CONFIG && |
| 5644 | opcode != MGMT_OP_SET_PUBLIC_ADDRESS) { |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5645 | err = cmd_status(sk, index, opcode, |
| 5646 | MGMT_STATUS_INVALID_INDEX); |
| 5647 | goto done; |
| 5648 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5649 | } |
| 5650 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5651 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5652 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5653 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5654 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5655 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5656 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5657 | } |
| 5658 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5659 | if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || |
| 5660 | opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
| 5661 | err = cmd_status(sk, index, opcode, |
| 5662 | MGMT_STATUS_INVALID_INDEX); |
| 5663 | goto done; |
| 5664 | } |
| 5665 | |
| 5666 | if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && |
| 5667 | opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5668 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5669 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5670 | goto done; |
| 5671 | } |
| 5672 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5673 | handler = &mgmt_handlers[opcode]; |
| 5674 | |
| 5675 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5676 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5677 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5678 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5679 | goto done; |
| 5680 | } |
| 5681 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5682 | if (hdev) |
| 5683 | mgmt_init_hdev(sk, hdev); |
| 5684 | |
| 5685 | cp = buf + sizeof(*hdr); |
| 5686 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5687 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5688 | if (err < 0) |
| 5689 | goto done; |
| 5690 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5691 | err = msglen; |
| 5692 | |
| 5693 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5694 | if (hdev) |
| 5695 | hci_dev_put(hdev); |
| 5696 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5697 | kfree(buf); |
| 5698 | return err; |
| 5699 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5700 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5701 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5702 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5703 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5704 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5705 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5706 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5707 | return; |
| 5708 | |
| 5709 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5710 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5711 | else |
| 5712 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5713 | } |
| 5714 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5715 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5716 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5717 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5718 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5719 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5720 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5721 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5722 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5723 | return; |
| 5724 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5725 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5726 | |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 5727 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5728 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5729 | else |
| 5730 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5731 | } |
| 5732 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5733 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5734 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5735 | { |
| 5736 | struct hci_conn_params *p; |
| 5737 | |
| 5738 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5739 | /* Needed for AUTO_OFF case where might not "really" |
| 5740 | * have been powered off. |
| 5741 | */ |
| 5742 | list_del_init(&p->action); |
| 5743 | |
| 5744 | switch (p->auto_connect) { |
| 5745 | case HCI_AUTO_CONN_ALWAYS: |
| 5746 | list_add(&p->action, &hdev->pend_le_conns); |
| 5747 | break; |
| 5748 | case HCI_AUTO_CONN_REPORT: |
| 5749 | list_add(&p->action, &hdev->pend_le_reports); |
| 5750 | break; |
| 5751 | default: |
| 5752 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5753 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5754 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5755 | |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5756 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5757 | } |
| 5758 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5759 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5760 | { |
| 5761 | struct cmd_lookup match = { NULL, hdev }; |
| 5762 | |
| 5763 | BT_DBG("status 0x%02x", status); |
| 5764 | |
| 5765 | hci_dev_lock(hdev); |
| 5766 | |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5767 | restart_le_actions(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5768 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5769 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5770 | |
| 5771 | new_settings(hdev, match.sk); |
| 5772 | |
| 5773 | hci_dev_unlock(hdev); |
| 5774 | |
| 5775 | if (match.sk) |
| 5776 | sock_put(match.sk); |
| 5777 | } |
| 5778 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5779 | static int powered_update_hci(struct hci_dev *hdev) |
| 5780 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5781 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5782 | u8 link_sec; |
| 5783 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5784 | hci_req_init(&req, hdev); |
| 5785 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5786 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5787 | !lmp_host_ssp_capable(hdev)) { |
| 5788 | u8 ssp = 1; |
| 5789 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5790 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5791 | } |
| 5792 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5793 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5794 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5795 | struct hci_cp_write_le_host_supported cp; |
| 5796 | |
| 5797 | cp.le = 1; |
| 5798 | cp.simul = lmp_le_br_capable(hdev); |
| 5799 | |
| 5800 | /* Check first if we already have the right |
| 5801 | * host state (host features set) |
| 5802 | */ |
| 5803 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5804 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5805 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5806 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5807 | } |
| 5808 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5809 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5810 | /* Make sure the controller has a good default for |
| 5811 | * advertising data. This also applies to the case |
| 5812 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5813 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5814 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5815 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5816 | update_scan_rsp_data(&req); |
| 5817 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5818 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5819 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5820 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5821 | } |
| 5822 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5823 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5824 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5825 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5826 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5827 | |
| 5828 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5829 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5830 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5831 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5832 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5833 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5834 | } |
| 5835 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5836 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5837 | } |
| 5838 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5839 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5840 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5841 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5842 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5843 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5844 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5845 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5846 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5847 | return 0; |
| 5848 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5849 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5850 | if (powered_update_hci(hdev) == 0) |
| 5851 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5852 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5853 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5854 | &match); |
| 5855 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5856 | } |
| 5857 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5858 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5859 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5860 | |
| 5861 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5862 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5863 | zero_cod, sizeof(zero_cod), NULL); |
| 5864 | |
| 5865 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5866 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5867 | |
| 5868 | if (match.sk) |
| 5869 | sock_put(match.sk); |
| 5870 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5871 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5872 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5873 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5874 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5875 | { |
| 5876 | struct pending_cmd *cmd; |
| 5877 | u8 status; |
| 5878 | |
| 5879 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5880 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5881 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5882 | |
| 5883 | if (err == -ERFKILL) |
| 5884 | status = MGMT_STATUS_RFKILLED; |
| 5885 | else |
| 5886 | status = MGMT_STATUS_FAILED; |
| 5887 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5888 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5889 | |
| 5890 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5891 | } |
| 5892 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5893 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5894 | { |
| 5895 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5896 | |
| 5897 | hci_dev_lock(hdev); |
| 5898 | |
| 5899 | /* When discoverable timeout triggers, then just make sure |
| 5900 | * the limited discoverable flag is cleared. Even in the case |
| 5901 | * of a timeout triggered from general discoverable, it is |
| 5902 | * safe to unconditionally clear the flag. |
| 5903 | */ |
| 5904 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5905 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5906 | |
| 5907 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5908 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5909 | u8 scan = SCAN_PAGE; |
| 5910 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5911 | sizeof(scan), &scan); |
| 5912 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5913 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5914 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5915 | hci_req_run(&req, NULL); |
| 5916 | |
| 5917 | hdev->discov_timeout = 0; |
| 5918 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5919 | new_settings(hdev, NULL); |
| 5920 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5921 | hci_dev_unlock(hdev); |
| 5922 | } |
| 5923 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5924 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5925 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5926 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5927 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5928 | /* Nothing needed here if there's a pending command since that |
| 5929 | * commands request completion callback takes care of everything |
| 5930 | * necessary. |
| 5931 | */ |
| 5932 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5933 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5934 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5935 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5936 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5937 | return; |
| 5938 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5939 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5940 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5941 | } else { |
| 5942 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5943 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5944 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5945 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5946 | if (changed) { |
| 5947 | struct hci_request req; |
| 5948 | |
| 5949 | /* In case this change in discoverable was triggered by |
| 5950 | * a disabling of connectable there could be a need to |
| 5951 | * update the advertising flags. |
| 5952 | */ |
| 5953 | hci_req_init(&req, hdev); |
| 5954 | update_adv_data(&req); |
| 5955 | hci_req_run(&req, NULL); |
| 5956 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5957 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5958 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5959 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5960 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5961 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5962 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5963 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5964 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5965 | /* Nothing needed here if there's a pending command since that |
| 5966 | * commands request completion callback takes care of everything |
| 5967 | * necessary. |
| 5968 | */ |
| 5969 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5970 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5971 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5972 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5973 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5974 | return; |
| 5975 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5976 | if (connectable) |
| 5977 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5978 | else |
| 5979 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5980 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5981 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5982 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5983 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5984 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5985 | 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] | 5986 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5987 | u8 mgmt_err = mgmt_status(status); |
| 5988 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5989 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5990 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5991 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5992 | |
| 5993 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5994 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5995 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5996 | } |
| 5997 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5998 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5999 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6000 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 6001 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6002 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6003 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6004 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6005 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 6006 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6007 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6008 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 6009 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 6010 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6011 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 6012 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 6013 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6014 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6015 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 6016 | { |
| 6017 | if (ltk->authenticated) |
| 6018 | return MGMT_LTK_AUTHENTICATED; |
| 6019 | |
| 6020 | return MGMT_LTK_UNAUTHENTICATED; |
| 6021 | } |
| 6022 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6023 | 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] | 6024 | { |
| 6025 | struct mgmt_ev_new_long_term_key ev; |
| 6026 | |
| 6027 | memset(&ev, 0, sizeof(ev)); |
| 6028 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 6029 | /* Devices using resolvable or non-resolvable random addresses |
| 6030 | * without providing an indentity resolving key don't require |
| 6031 | * to store long term keys. Their addresses will change the |
| 6032 | * next time around. |
| 6033 | * |
| 6034 | * Only when a remote device provides an identity address |
| 6035 | * make sure the long term key is stored. If the remote |
| 6036 | * identity is known, the long term keys are internally |
| 6037 | * mapped to the identity address. So allow static random |
| 6038 | * and public addresses here. |
| 6039 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6040 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6041 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6042 | ev.store_hint = 0x00; |
| 6043 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6044 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 6045 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6046 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6047 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 6048 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6049 | ev.key.enc_size = key->enc_size; |
| 6050 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 6051 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6052 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 6053 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6054 | ev.key.master = 1; |
| 6055 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 6056 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 6057 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 6058 | 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] | 6059 | } |
| 6060 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6061 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 6062 | { |
| 6063 | struct mgmt_ev_new_irk ev; |
| 6064 | |
| 6065 | memset(&ev, 0, sizeof(ev)); |
| 6066 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 6067 | /* For identity resolving keys from devices that are already |
| 6068 | * using a public address or static random address, do not |
| 6069 | * ask for storing this key. The identity resolving key really |
| 6070 | * is only mandatory for devices using resovlable random |
| 6071 | * addresses. |
| 6072 | * |
| 6073 | * Storing all identity resolving keys has the downside that |
| 6074 | * they will be also loaded on next boot of they system. More |
| 6075 | * identity resolving keys, means more time during scanning is |
| 6076 | * needed to actually resolve these addresses. |
| 6077 | */ |
| 6078 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 6079 | ev.store_hint = 0x01; |
| 6080 | else |
| 6081 | ev.store_hint = 0x00; |
| 6082 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 6083 | bacpy(&ev.rpa, &irk->rpa); |
| 6084 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 6085 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 6086 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6087 | |
| 6088 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6089 | } |
| 6090 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6091 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6092 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6093 | { |
| 6094 | struct mgmt_ev_new_csrk ev; |
| 6095 | |
| 6096 | memset(&ev, 0, sizeof(ev)); |
| 6097 | |
| 6098 | /* Devices using resolvable or non-resolvable random addresses |
| 6099 | * without providing an indentity resolving key don't require |
| 6100 | * to store signature resolving keys. Their addresses will change |
| 6101 | * the next time around. |
| 6102 | * |
| 6103 | * Only when a remote device provides an identity address |
| 6104 | * make sure the signature resolving key is stored. So allow |
| 6105 | * static random and public addresses here. |
| 6106 | */ |
| 6107 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6108 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6109 | ev.store_hint = 0x00; |
| 6110 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6111 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6112 | |
| 6113 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6114 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 6115 | ev.key.master = csrk->master; |
| 6116 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6117 | |
| 6118 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6119 | } |
| 6120 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6121 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6122 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6123 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6124 | { |
| 6125 | struct mgmt_ev_new_conn_param ev; |
| 6126 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6127 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6128 | return; |
| 6129 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6130 | memset(&ev, 0, sizeof(ev)); |
| 6131 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6132 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6133 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6134 | ev.min_interval = cpu_to_le16(min_interval); |
| 6135 | ev.max_interval = cpu_to_le16(max_interval); |
| 6136 | ev.latency = cpu_to_le16(latency); |
| 6137 | ev.timeout = cpu_to_le16(timeout); |
| 6138 | |
| 6139 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6140 | } |
| 6141 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 6142 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 6143 | u8 data_len) |
| 6144 | { |
| 6145 | eir[eir_len++] = sizeof(type) + data_len; |
| 6146 | eir[eir_len++] = type; |
| 6147 | memcpy(&eir[eir_len], data, data_len); |
| 6148 | eir_len += data_len; |
| 6149 | |
| 6150 | return eir_len; |
| 6151 | } |
| 6152 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6153 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6154 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 6155 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6156 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6157 | char buf[512]; |
| 6158 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6159 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6160 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6161 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6162 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6163 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6164 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6165 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6166 | if (name_len > 0) |
| 6167 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6168 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6169 | |
| 6170 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 6171 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6172 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6173 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6174 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6175 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6176 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6177 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6178 | } |
| 6179 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6180 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 6181 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 6182 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6183 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 6184 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6185 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6186 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6187 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6188 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6189 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6190 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6191 | |
| 6192 | *sk = cmd->sk; |
| 6193 | sock_hold(*sk); |
| 6194 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6195 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6196 | } |
| 6197 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6198 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6199 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6200 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6201 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 6202 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6203 | |
| 6204 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6205 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6206 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6207 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6208 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6209 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6210 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6211 | |
| 6212 | mgmt_pending_remove(cmd); |
| 6213 | } |
| 6214 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6215 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6216 | u8 link_type, u8 addr_type, u8 reason, |
| 6217 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6218 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6219 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6220 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6221 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6222 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6223 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6224 | if (power_off) { |
| 6225 | struct mgmt_mode *cp = power_off->param; |
| 6226 | |
| 6227 | /* The connection is still in hci_conn_hash so test for 1 |
| 6228 | * instead of 0 to know if this is the last one. |
| 6229 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6230 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6231 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6232 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6233 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6234 | } |
| 6235 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6236 | if (!mgmt_connected) |
| 6237 | return; |
| 6238 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6239 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6240 | return; |
| 6241 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6242 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6243 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6244 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6245 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6246 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6247 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6248 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6249 | |
| 6250 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6251 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6252 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6253 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6254 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6255 | } |
| 6256 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6257 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6258 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6259 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6260 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6261 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6262 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6263 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6264 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6265 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6266 | hdev); |
| 6267 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6268 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6269 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6270 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6271 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6272 | cp = cmd->param; |
| 6273 | |
| 6274 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6275 | return; |
| 6276 | |
| 6277 | if (cp->addr.type != bdaddr_type) |
| 6278 | return; |
| 6279 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6280 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6281 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 6282 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6283 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 6284 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6285 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6286 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6287 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6288 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6289 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6290 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6291 | { |
| 6292 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6293 | struct pending_cmd *power_off; |
| 6294 | |
| 6295 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6296 | if (power_off) { |
| 6297 | struct mgmt_mode *cp = power_off->param; |
| 6298 | |
| 6299 | /* The connection is still in hci_conn_hash so test for 1 |
| 6300 | * instead of 0 to know if this is the last one. |
| 6301 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6302 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6303 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6304 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6305 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6306 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6307 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6308 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6309 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6310 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6311 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6312 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6313 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6314 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6315 | 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] | 6316 | { |
| 6317 | struct mgmt_ev_pin_code_request ev; |
| 6318 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6319 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6320 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6321 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6322 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6323 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6324 | } |
| 6325 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6326 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6327 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6328 | { |
| 6329 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6330 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6331 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6332 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6333 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6334 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6335 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6336 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6337 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6338 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6339 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6340 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6341 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6342 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6343 | } |
| 6344 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6345 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6346 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6347 | { |
| 6348 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6349 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6350 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6351 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6352 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6353 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6354 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6355 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6356 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6357 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6358 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6359 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6360 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6361 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6362 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6363 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6364 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6365 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6366 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6367 | { |
| 6368 | struct mgmt_ev_user_confirm_request ev; |
| 6369 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6370 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6371 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6372 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6373 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6374 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6375 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6376 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6377 | 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] | 6378 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6379 | } |
| 6380 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6381 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6382 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6383 | { |
| 6384 | struct mgmt_ev_user_passkey_request ev; |
| 6385 | |
| 6386 | BT_DBG("%s", hdev->name); |
| 6387 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6388 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6389 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6390 | |
| 6391 | 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] | 6392 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6393 | } |
| 6394 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6395 | 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] | 6396 | u8 link_type, u8 addr_type, u8 status, |
| 6397 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6398 | { |
| 6399 | struct pending_cmd *cmd; |
| 6400 | struct mgmt_rp_user_confirm_reply rp; |
| 6401 | int err; |
| 6402 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6403 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6404 | if (!cmd) |
| 6405 | return -ENOENT; |
| 6406 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6407 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6408 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6409 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6410 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6411 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6412 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6413 | |
| 6414 | return err; |
| 6415 | } |
| 6416 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6417 | 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] | 6418 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6419 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6420 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6421 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6422 | } |
| 6423 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6424 | 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] | 6425 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6426 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6427 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6428 | status, |
| 6429 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6430 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6431 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6432 | 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] | 6433 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6434 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6435 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6436 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6437 | } |
| 6438 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6439 | 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] | 6440 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6441 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6442 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6443 | status, |
| 6444 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6445 | } |
| 6446 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6447 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6448 | u8 link_type, u8 addr_type, u32 passkey, |
| 6449 | u8 entered) |
| 6450 | { |
| 6451 | struct mgmt_ev_passkey_notify ev; |
| 6452 | |
| 6453 | BT_DBG("%s", hdev->name); |
| 6454 | |
| 6455 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6456 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6457 | ev.passkey = __cpu_to_le32(passkey); |
| 6458 | ev.entered = entered; |
| 6459 | |
| 6460 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6461 | } |
| 6462 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6463 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6464 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6465 | { |
| 6466 | struct mgmt_ev_auth_failed ev; |
| 6467 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6468 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6469 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6470 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6471 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6472 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6473 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6474 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6475 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6476 | { |
| 6477 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6478 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6479 | |
| 6480 | if (status) { |
| 6481 | u8 mgmt_err = mgmt_status(status); |
| 6482 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6483 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6484 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6485 | } |
| 6486 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6487 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6488 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6489 | &hdev->dev_flags); |
| 6490 | else |
| 6491 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6492 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6493 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6494 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6495 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6496 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6497 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6498 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6499 | |
| 6500 | if (match.sk) |
| 6501 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6502 | } |
| 6503 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6504 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6505 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6506 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6507 | struct hci_cp_write_eir cp; |
| 6508 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6509 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6510 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6511 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6512 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6513 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6514 | memset(&cp, 0, sizeof(cp)); |
| 6515 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6516 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6517 | } |
| 6518 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6519 | 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] | 6520 | { |
| 6521 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6522 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6523 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6524 | |
| 6525 | if (status) { |
| 6526 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6527 | |
| 6528 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6529 | &hdev->dev_flags)) { |
| 6530 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6531 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6532 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6533 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6534 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6535 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6536 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6537 | } |
| 6538 | |
| 6539 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6540 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6541 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6542 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6543 | if (!changed) |
| 6544 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6545 | &hdev->dev_flags); |
| 6546 | else |
| 6547 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6548 | } |
| 6549 | |
| 6550 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6551 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6552 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6553 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6554 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6555 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6556 | sock_put(match.sk); |
| 6557 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6558 | hci_req_init(&req, hdev); |
| 6559 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6560 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6561 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6562 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6563 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6564 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6565 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6566 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6567 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6568 | |
| 6569 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6570 | } |
| 6571 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6572 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6573 | { |
| 6574 | struct cmd_lookup match = { NULL, hdev }; |
| 6575 | bool changed = false; |
| 6576 | |
| 6577 | if (status) { |
| 6578 | u8 mgmt_err = mgmt_status(status); |
| 6579 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6580 | if (enable) { |
| 6581 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6582 | &hdev->dev_flags)) |
| 6583 | new_settings(hdev, NULL); |
| 6584 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6585 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6586 | |
| 6587 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6588 | cmd_status_rsp, &mgmt_err); |
| 6589 | return; |
| 6590 | } |
| 6591 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6592 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6593 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6594 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6595 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6596 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6597 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6598 | |
| 6599 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6600 | settings_rsp, &match); |
| 6601 | |
| 6602 | if (changed) |
| 6603 | new_settings(hdev, match.sk); |
| 6604 | |
| 6605 | if (match.sk) |
| 6606 | sock_put(match.sk); |
| 6607 | } |
| 6608 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6609 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6610 | { |
| 6611 | struct cmd_lookup *match = data; |
| 6612 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6613 | if (match->sk == NULL) { |
| 6614 | match->sk = cmd->sk; |
| 6615 | sock_hold(match->sk); |
| 6616 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6617 | } |
| 6618 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6619 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6620 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6621 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6622 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6623 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6624 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6625 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6626 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6627 | |
| 6628 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6629 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6630 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6631 | |
| 6632 | if (match.sk) |
| 6633 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6634 | } |
| 6635 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6636 | 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] | 6637 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6638 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6639 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6640 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6641 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6642 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6643 | |
| 6644 | memset(&ev, 0, sizeof(ev)); |
| 6645 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6646 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6647 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6648 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6649 | if (!cmd) { |
| 6650 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6651 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6652 | /* If this is a HCI command related to powering on the |
| 6653 | * HCI dev don't send any mgmt signals. |
| 6654 | */ |
| 6655 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6656 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6657 | } |
| 6658 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6659 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6660 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6661 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6662 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6663 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6664 | u8 *randomizer192, u8 *hash256, |
| 6665 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6666 | { |
| 6667 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6668 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6669 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6670 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6671 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6672 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6673 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6674 | |
| 6675 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6676 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6677 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6678 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6679 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6680 | hash256 && randomizer256) { |
| 6681 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6682 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6683 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6684 | memcpy(rp.randomizer192, randomizer192, |
| 6685 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6686 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6687 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6688 | memcpy(rp.randomizer256, randomizer256, |
| 6689 | sizeof(rp.randomizer256)); |
| 6690 | |
| 6691 | cmd_complete(cmd->sk, hdev->id, |
| 6692 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6693 | &rp, sizeof(rp)); |
| 6694 | } else { |
| 6695 | struct mgmt_rp_read_local_oob_data rp; |
| 6696 | |
| 6697 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6698 | memcpy(rp.randomizer, randomizer192, |
| 6699 | sizeof(rp.randomizer)); |
| 6700 | |
| 6701 | cmd_complete(cmd->sk, hdev->id, |
| 6702 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6703 | &rp, sizeof(rp)); |
| 6704 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6705 | } |
| 6706 | |
| 6707 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6708 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6709 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6710 | 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] | 6711 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6712 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6713 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6714 | char buf[512]; |
| 6715 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6716 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6717 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6718 | /* Don't send events for a non-kernel initiated discovery. With |
| 6719 | * LE one exception is if we have pend_le_reports > 0 in which |
| 6720 | * case we're doing passive scanning and want these events. |
| 6721 | */ |
| 6722 | if (!hci_discovery_active(hdev)) { |
| 6723 | if (link_type == ACL_LINK) |
| 6724 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 6725 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6726 | return; |
| 6727 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6728 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6729 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6730 | * are for the potential CoD field. |
| 6731 | */ |
| 6732 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6733 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6734 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6735 | memset(buf, 0, sizeof(buf)); |
| 6736 | |
Johan Hedberg | 841c564 | 2014-07-07 12:45:54 +0300 | [diff] [blame] | 6737 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6738 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6739 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6740 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6741 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6742 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6743 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6744 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6745 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6746 | 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] | 6747 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6748 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6749 | if (scan_rsp_len > 0) |
| 6750 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6751 | |
| 6752 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6753 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6754 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6755 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6756 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6757 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6758 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6759 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6760 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6761 | struct mgmt_ev_device_found *ev; |
| 6762 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6763 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6764 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6765 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6766 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6767 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6768 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6769 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6770 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6771 | ev->rssi = rssi; |
| 6772 | |
| 6773 | 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] | 6774 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6775 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6776 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6777 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6778 | 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] | 6779 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6780 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6781 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6782 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6783 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6784 | struct pending_cmd *cmd; |
| 6785 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6786 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6787 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6788 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6789 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6790 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6791 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6792 | |
| 6793 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6794 | u8 type = hdev->discovery.type; |
| 6795 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6796 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6797 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6798 | mgmt_pending_remove(cmd); |
| 6799 | } |
| 6800 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6801 | memset(&ev, 0, sizeof(ev)); |
| 6802 | ev.type = hdev->discovery.type; |
| 6803 | ev.discovering = discovering; |
| 6804 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6805 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6806 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6807 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6808 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6809 | { |
| 6810 | BT_DBG("%s status %u", hdev->name, status); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6811 | } |
| 6812 | |
| 6813 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6814 | { |
| 6815 | struct hci_request req; |
| 6816 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6817 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6818 | return; |
| 6819 | |
| 6820 | hci_req_init(&req, hdev); |
| 6821 | enable_advertising(&req); |
Johan Hedberg | 0ec5ae8 | 2014-07-08 15:07:50 +0300 | [diff] [blame^] | 6822 | hci_req_run(&req, adv_enable_complete); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6823 | } |