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, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static const u16 mgmt_events[] = { |
| 98 | MGMT_EV_CONTROLLER_ERROR, |
| 99 | MGMT_EV_INDEX_ADDED, |
| 100 | MGMT_EV_INDEX_REMOVED, |
| 101 | MGMT_EV_NEW_SETTINGS, |
| 102 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 103 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 104 | MGMT_EV_NEW_LINK_KEY, |
| 105 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 106 | MGMT_EV_DEVICE_CONNECTED, |
| 107 | MGMT_EV_DEVICE_DISCONNECTED, |
| 108 | MGMT_EV_CONNECT_FAILED, |
| 109 | MGMT_EV_PIN_CODE_REQUEST, |
| 110 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 111 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 112 | MGMT_EV_AUTH_FAILED, |
| 113 | MGMT_EV_DEVICE_FOUND, |
| 114 | MGMT_EV_DISCOVERING, |
| 115 | MGMT_EV_DEVICE_BLOCKED, |
| 116 | MGMT_EV_DEVICE_UNBLOCKED, |
| 117 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 118 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 119 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 120 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 121 | MGMT_EV_DEVICE_ADDED, |
| 122 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 123 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 124 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 125 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 128 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 129 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 130 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 131 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 132 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 133 | struct pending_cmd { |
| 134 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 135 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 136 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 137 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 138 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 139 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 140 | }; |
| 141 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 142 | /* HCI to MGMT error code conversion table */ |
| 143 | static u8 mgmt_status_table[] = { |
| 144 | MGMT_STATUS_SUCCESS, |
| 145 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 146 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 147 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 148 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 149 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 150 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 151 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 152 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 153 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 154 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 155 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 156 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 157 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 158 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 159 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 160 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 161 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 162 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 163 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 164 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 165 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 166 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 167 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 168 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 169 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 170 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 171 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 172 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 173 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 174 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 175 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 176 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 177 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 178 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 179 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 180 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 181 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 182 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 183 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 184 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 185 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 186 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 187 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 188 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 189 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 190 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 191 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 192 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 193 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 194 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 195 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 196 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 197 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 198 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 199 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 200 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 201 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 202 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 203 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 204 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 205 | }; |
| 206 | |
| 207 | static u8 mgmt_status(u8 hci_status) |
| 208 | { |
| 209 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 210 | return mgmt_status_table[hci_status]; |
| 211 | |
| 212 | return MGMT_STATUS_FAILED; |
| 213 | } |
| 214 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 215 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 216 | { |
| 217 | struct sk_buff *skb; |
| 218 | struct mgmt_hdr *hdr; |
| 219 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 220 | int err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 221 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 222 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 223 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 224 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 225 | if (!skb) |
| 226 | return -ENOMEM; |
| 227 | |
| 228 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 229 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 230 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 231 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 232 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 233 | |
| 234 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 235 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 236 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 237 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 238 | err = sock_queue_rcv_skb(sk, skb); |
| 239 | if (err < 0) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 240 | kfree_skb(skb); |
| 241 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 242 | return err; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 245 | 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] | 246 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 247 | { |
| 248 | struct sk_buff *skb; |
| 249 | struct mgmt_hdr *hdr; |
| 250 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 251 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 252 | |
| 253 | BT_DBG("sock %p", sk); |
| 254 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 255 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 256 | if (!skb) |
| 257 | return -ENOMEM; |
| 258 | |
| 259 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 260 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 261 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 262 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 263 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 264 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 265 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 266 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 267 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 268 | |
| 269 | if (rp) |
| 270 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 271 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 272 | err = sock_queue_rcv_skb(sk, skb); |
| 273 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 274 | kfree_skb(skb); |
| 275 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 276 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 279 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 280 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 281 | { |
| 282 | struct mgmt_rp_read_version rp; |
| 283 | |
| 284 | BT_DBG("sock %p", sk); |
| 285 | |
| 286 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 287 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 288 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 289 | 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] | 290 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 291 | } |
| 292 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 293 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 294 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 295 | { |
| 296 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 297 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 298 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 299 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 300 | size_t rp_size; |
| 301 | int i, err; |
| 302 | |
| 303 | BT_DBG("sock %p", sk); |
| 304 | |
| 305 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 306 | |
| 307 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 308 | if (!rp) |
| 309 | return -ENOMEM; |
| 310 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 311 | rp->num_commands = cpu_to_le16(num_commands); |
| 312 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 313 | |
| 314 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 315 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 316 | |
| 317 | for (i = 0; i < num_events; i++, opcode++) |
| 318 | put_unaligned_le16(mgmt_events[i], opcode); |
| 319 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 320 | 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] | 321 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 322 | kfree(rp); |
| 323 | |
| 324 | return err; |
| 325 | } |
| 326 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 327 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 328 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 329 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 330 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 331 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 332 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 333 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 334 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 335 | |
| 336 | BT_DBG("sock %p", sk); |
| 337 | |
| 338 | read_lock(&hci_dev_list_lock); |
| 339 | |
| 340 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 341 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 342 | if (d->dev_type == HCI_BREDR && |
| 343 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 344 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 347 | rp_len = sizeof(*rp) + (2 * count); |
| 348 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 349 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 350 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 351 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 352 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 353 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 354 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 355 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 356 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
| 357 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 358 | continue; |
| 359 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 360 | /* Devices marked as raw-only are neither configured |
| 361 | * nor unconfigured controllers. |
| 362 | */ |
| 363 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 364 | continue; |
| 365 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 366 | if (d->dev_type == HCI_BREDR && |
| 367 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 368 | rp->index[count++] = cpu_to_le16(d->id); |
| 369 | BT_DBG("Added hci%u", d->id); |
| 370 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 373 | rp->num_controllers = cpu_to_le16(count); |
| 374 | rp_len = sizeof(*rp) + (2 * count); |
| 375 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 376 | read_unlock(&hci_dev_list_lock); |
| 377 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 378 | 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] | 379 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 380 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 381 | kfree(rp); |
| 382 | |
| 383 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 386 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 387 | void *data, u16 data_len) |
| 388 | { |
| 389 | struct mgmt_rp_read_unconf_index_list *rp; |
| 390 | struct hci_dev *d; |
| 391 | size_t rp_len; |
| 392 | u16 count; |
| 393 | int err; |
| 394 | |
| 395 | BT_DBG("sock %p", sk); |
| 396 | |
| 397 | read_lock(&hci_dev_list_lock); |
| 398 | |
| 399 | count = 0; |
| 400 | list_for_each_entry(d, &hci_dev_list, list) { |
| 401 | if (d->dev_type == HCI_BREDR && |
| 402 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
| 403 | count++; |
| 404 | } |
| 405 | |
| 406 | rp_len = sizeof(*rp) + (2 * count); |
| 407 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 408 | if (!rp) { |
| 409 | read_unlock(&hci_dev_list_lock); |
| 410 | return -ENOMEM; |
| 411 | } |
| 412 | |
| 413 | count = 0; |
| 414 | list_for_each_entry(d, &hci_dev_list, list) { |
| 415 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
| 416 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 417 | continue; |
| 418 | |
| 419 | /* Devices marked as raw-only are neither configured |
| 420 | * nor unconfigured controllers. |
| 421 | */ |
| 422 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 423 | continue; |
| 424 | |
| 425 | if (d->dev_type == HCI_BREDR && |
| 426 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
| 427 | rp->index[count++] = cpu_to_le16(d->id); |
| 428 | BT_DBG("Added hci%u", d->id); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | rp->num_controllers = cpu_to_le16(count); |
| 433 | rp_len = sizeof(*rp) + (2 * count); |
| 434 | |
| 435 | read_unlock(&hci_dev_list_lock); |
| 436 | |
| 437 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 438 | 0, rp, rp_len); |
| 439 | |
| 440 | kfree(rp); |
| 441 | |
| 442 | return err; |
| 443 | } |
| 444 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame^] | 445 | static bool is_configured(struct hci_dev *hdev) |
| 446 | { |
| 447 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 448 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
| 449 | return false; |
| 450 | |
| 451 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 452 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 453 | return false; |
| 454 | |
| 455 | return true; |
| 456 | } |
| 457 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 458 | static __le32 get_missing_options(struct hci_dev *hdev) |
| 459 | { |
| 460 | u32 options = 0; |
| 461 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame^] | 462 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && |
| 463 | !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 464 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 465 | |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 466 | if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && |
| 467 | !bacmp(&hdev->public_addr, BDADDR_ANY)) |
| 468 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 469 | |
| 470 | return cpu_to_le32(options); |
| 471 | } |
| 472 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame^] | 473 | static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) |
| 474 | { |
| 475 | __le32 options = get_missing_options(hdev); |
| 476 | |
| 477 | return cmd_complete(sk, hdev->id, opcode, 0, &options, |
| 478 | sizeof(options)); |
| 479 | } |
| 480 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 481 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 482 | void *data, u16 data_len) |
| 483 | { |
| 484 | struct mgmt_rp_read_config_info rp; |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 485 | u32 options = 0; |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 486 | |
| 487 | BT_DBG("sock %p %s", sk, hdev->name); |
| 488 | |
| 489 | hci_dev_lock(hdev); |
| 490 | |
| 491 | memset(&rp, 0, sizeof(rp)); |
| 492 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 493 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 494 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 495 | options |= MGMT_OPTION_EXTERNAL_CONFIG; |
| 496 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 497 | if (hdev->set_bdaddr) |
Marcel Holtmann | 89bc22d | 2014-07-04 16:54:37 +0200 | [diff] [blame] | 498 | options |= MGMT_OPTION_PUBLIC_ADDRESS; |
| 499 | |
| 500 | rp.supported_options = cpu_to_le32(options); |
| 501 | rp.missing_options = get_missing_options(hdev); |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 502 | |
| 503 | hci_dev_unlock(hdev); |
| 504 | |
| 505 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp, |
| 506 | sizeof(rp)); |
| 507 | } |
| 508 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 509 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 510 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 511 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 512 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 513 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 514 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 515 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 516 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 517 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 518 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 519 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 520 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 521 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 522 | settings |= MGMT_SETTING_BREDR; |
| 523 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 524 | |
| 525 | if (lmp_ssp_capable(hdev)) { |
| 526 | settings |= MGMT_SETTING_SSP; |
| 527 | settings |= MGMT_SETTING_HS; |
| 528 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 529 | |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 530 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 531 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 532 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 533 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 534 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 535 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 536 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 537 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 538 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 539 | } |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 540 | |
Marcel Holtmann | eb1904f | 2014-07-04 17:23:33 +0200 | [diff] [blame] | 541 | if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || |
| 542 | hdev->set_bdaddr) |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 543 | settings |= MGMT_SETTING_CONFIGURATION; |
| 544 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 545 | return settings; |
| 546 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 547 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 548 | static u32 get_current_settings(struct hci_dev *hdev) |
| 549 | { |
| 550 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 551 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 552 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 553 | settings |= MGMT_SETTING_POWERED; |
| 554 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 555 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 556 | settings |= MGMT_SETTING_CONNECTABLE; |
| 557 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 558 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 559 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 560 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 561 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 562 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 563 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 564 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 565 | settings |= MGMT_SETTING_PAIRABLE; |
| 566 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 567 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 568 | settings |= MGMT_SETTING_BREDR; |
| 569 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 570 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 571 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 572 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 573 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 574 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 575 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 576 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 577 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 578 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 579 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 580 | settings |= MGMT_SETTING_HS; |
| 581 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 582 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 583 | settings |= MGMT_SETTING_ADVERTISING; |
| 584 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 585 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 586 | settings |= MGMT_SETTING_SECURE_CONN; |
| 587 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 588 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 589 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 590 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 591 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 592 | settings |= MGMT_SETTING_PRIVACY; |
| 593 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 594 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 595 | } |
| 596 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 597 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 598 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 599 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 600 | { |
| 601 | u8 *ptr = data, *uuids_start = NULL; |
| 602 | struct bt_uuid *uuid; |
| 603 | |
| 604 | if (len < 4) |
| 605 | return ptr; |
| 606 | |
| 607 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 608 | u16 uuid16; |
| 609 | |
| 610 | if (uuid->size != 16) |
| 611 | continue; |
| 612 | |
| 613 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 614 | if (uuid16 < 0x1100) |
| 615 | continue; |
| 616 | |
| 617 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 618 | continue; |
| 619 | |
| 620 | if (!uuids_start) { |
| 621 | uuids_start = ptr; |
| 622 | uuids_start[0] = 1; |
| 623 | uuids_start[1] = EIR_UUID16_ALL; |
| 624 | ptr += 2; |
| 625 | } |
| 626 | |
| 627 | /* Stop if not enough space to put next UUID */ |
| 628 | if ((ptr - data) + sizeof(u16) > len) { |
| 629 | uuids_start[1] = EIR_UUID16_SOME; |
| 630 | break; |
| 631 | } |
| 632 | |
| 633 | *ptr++ = (uuid16 & 0x00ff); |
| 634 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 635 | uuids_start[0] += sizeof(uuid16); |
| 636 | } |
| 637 | |
| 638 | return ptr; |
| 639 | } |
| 640 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 641 | static u8 *create_uuid32_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 < 6) |
| 647 | return ptr; |
| 648 | |
| 649 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 650 | if (uuid->size != 32) |
| 651 | continue; |
| 652 | |
| 653 | if (!uuids_start) { |
| 654 | uuids_start = ptr; |
| 655 | uuids_start[0] = 1; |
| 656 | uuids_start[1] = EIR_UUID32_ALL; |
| 657 | ptr += 2; |
| 658 | } |
| 659 | |
| 660 | /* Stop if not enough space to put next UUID */ |
| 661 | if ((ptr - data) + sizeof(u32) > len) { |
| 662 | uuids_start[1] = EIR_UUID32_SOME; |
| 663 | break; |
| 664 | } |
| 665 | |
| 666 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 667 | ptr += sizeof(u32); |
| 668 | uuids_start[0] += sizeof(u32); |
| 669 | } |
| 670 | |
| 671 | return ptr; |
| 672 | } |
| 673 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 674 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 675 | { |
| 676 | u8 *ptr = data, *uuids_start = NULL; |
| 677 | struct bt_uuid *uuid; |
| 678 | |
| 679 | if (len < 18) |
| 680 | return ptr; |
| 681 | |
| 682 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 683 | if (uuid->size != 128) |
| 684 | continue; |
| 685 | |
| 686 | if (!uuids_start) { |
| 687 | uuids_start = ptr; |
| 688 | uuids_start[0] = 1; |
| 689 | uuids_start[1] = EIR_UUID128_ALL; |
| 690 | ptr += 2; |
| 691 | } |
| 692 | |
| 693 | /* Stop if not enough space to put next UUID */ |
| 694 | if ((ptr - data) + 16 > len) { |
| 695 | uuids_start[1] = EIR_UUID128_SOME; |
| 696 | break; |
| 697 | } |
| 698 | |
| 699 | memcpy(ptr, uuid->uuid, 16); |
| 700 | ptr += 16; |
| 701 | uuids_start[0] += 16; |
| 702 | } |
| 703 | |
| 704 | return ptr; |
| 705 | } |
| 706 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 707 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 708 | { |
| 709 | struct pending_cmd *cmd; |
| 710 | |
| 711 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 712 | if (cmd->opcode == opcode) |
| 713 | return cmd; |
| 714 | } |
| 715 | |
| 716 | return NULL; |
| 717 | } |
| 718 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 719 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 720 | struct hci_dev *hdev, |
| 721 | const void *data) |
| 722 | { |
| 723 | struct pending_cmd *cmd; |
| 724 | |
| 725 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 726 | if (cmd->user_data != data) |
| 727 | continue; |
| 728 | if (cmd->opcode == opcode) |
| 729 | return cmd; |
| 730 | } |
| 731 | |
| 732 | return NULL; |
| 733 | } |
| 734 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 735 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 736 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 737 | u8 ad_len = 0; |
| 738 | size_t name_len; |
| 739 | |
| 740 | name_len = strlen(hdev->dev_name); |
| 741 | if (name_len > 0) { |
| 742 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 743 | |
| 744 | if (name_len > max_len) { |
| 745 | name_len = max_len; |
| 746 | ptr[1] = EIR_NAME_SHORT; |
| 747 | } else |
| 748 | ptr[1] = EIR_NAME_COMPLETE; |
| 749 | |
| 750 | ptr[0] = name_len + 1; |
| 751 | |
| 752 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 753 | |
| 754 | ad_len += (name_len + 2); |
| 755 | ptr += (name_len + 2); |
| 756 | } |
| 757 | |
| 758 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | static void update_scan_rsp_data(struct hci_request *req) |
| 762 | { |
| 763 | struct hci_dev *hdev = req->hdev; |
| 764 | struct hci_cp_le_set_scan_rsp_data cp; |
| 765 | u8 len; |
| 766 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 767 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 768 | return; |
| 769 | |
| 770 | memset(&cp, 0, sizeof(cp)); |
| 771 | |
| 772 | len = create_scan_rsp_data(hdev, cp.data); |
| 773 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 774 | if (hdev->scan_rsp_data_len == len && |
| 775 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 776 | return; |
| 777 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 778 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 779 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 780 | |
| 781 | cp.length = len; |
| 782 | |
| 783 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 784 | } |
| 785 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 786 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 787 | { |
| 788 | struct pending_cmd *cmd; |
| 789 | |
| 790 | /* If there's a pending mgmt command the flags will not yet have |
| 791 | * their final values, so check for this first. |
| 792 | */ |
| 793 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 794 | if (cmd) { |
| 795 | struct mgmt_mode *cp = cmd->param; |
| 796 | if (cp->val == 0x01) |
| 797 | return LE_AD_GENERAL; |
| 798 | else if (cp->val == 0x02) |
| 799 | return LE_AD_LIMITED; |
| 800 | } else { |
| 801 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 802 | return LE_AD_LIMITED; |
| 803 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 804 | return LE_AD_GENERAL; |
| 805 | } |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 810 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 811 | { |
| 812 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 813 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 814 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 815 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 816 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 817 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 818 | |
| 819 | if (flags) { |
| 820 | BT_DBG("adv flags 0x%02x", flags); |
| 821 | |
| 822 | ptr[0] = 2; |
| 823 | ptr[1] = EIR_FLAGS; |
| 824 | ptr[2] = flags; |
| 825 | |
| 826 | ad_len += 3; |
| 827 | ptr += 3; |
| 828 | } |
| 829 | |
| 830 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 831 | ptr[0] = 2; |
| 832 | ptr[1] = EIR_TX_POWER; |
| 833 | ptr[2] = (u8) hdev->adv_tx_power; |
| 834 | |
| 835 | ad_len += 3; |
| 836 | ptr += 3; |
| 837 | } |
| 838 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 839 | return ad_len; |
| 840 | } |
| 841 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 842 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 843 | { |
| 844 | struct hci_dev *hdev = req->hdev; |
| 845 | struct hci_cp_le_set_adv_data cp; |
| 846 | u8 len; |
| 847 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 848 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 849 | return; |
| 850 | |
| 851 | memset(&cp, 0, sizeof(cp)); |
| 852 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 853 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 854 | |
| 855 | if (hdev->adv_data_len == len && |
| 856 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 857 | return; |
| 858 | |
| 859 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 860 | hdev->adv_data_len = len; |
| 861 | |
| 862 | cp.length = len; |
| 863 | |
| 864 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 865 | } |
| 866 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 867 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 868 | { |
| 869 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 870 | size_t name_len; |
| 871 | |
| 872 | name_len = strlen(hdev->dev_name); |
| 873 | |
| 874 | if (name_len > 0) { |
| 875 | /* EIR Data type */ |
| 876 | if (name_len > 48) { |
| 877 | name_len = 48; |
| 878 | ptr[1] = EIR_NAME_SHORT; |
| 879 | } else |
| 880 | ptr[1] = EIR_NAME_COMPLETE; |
| 881 | |
| 882 | /* EIR Data length */ |
| 883 | ptr[0] = name_len + 1; |
| 884 | |
| 885 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 886 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 887 | ptr += (name_len + 2); |
| 888 | } |
| 889 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 890 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 891 | ptr[0] = 2; |
| 892 | ptr[1] = EIR_TX_POWER; |
| 893 | ptr[2] = (u8) hdev->inq_tx_power; |
| 894 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 895 | ptr += 3; |
| 896 | } |
| 897 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 898 | if (hdev->devid_source > 0) { |
| 899 | ptr[0] = 9; |
| 900 | ptr[1] = EIR_DEVICE_ID; |
| 901 | |
| 902 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 903 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 904 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 905 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 906 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 907 | ptr += 10; |
| 908 | } |
| 909 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 910 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 911 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 912 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 913 | } |
| 914 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 915 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 916 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 917 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 918 | struct hci_cp_write_eir cp; |
| 919 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 920 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 921 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 922 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 923 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 924 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 925 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 926 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 927 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 928 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 929 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 930 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 931 | |
| 932 | memset(&cp, 0, sizeof(cp)); |
| 933 | |
| 934 | create_eir(hdev, cp.data); |
| 935 | |
| 936 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 937 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 938 | |
| 939 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 940 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 941 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 942 | } |
| 943 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 944 | static u8 get_service_classes(struct hci_dev *hdev) |
| 945 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 946 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 947 | u8 val = 0; |
| 948 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 949 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 950 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 951 | |
| 952 | return val; |
| 953 | } |
| 954 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 955 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 956 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 957 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 958 | u8 cod[3]; |
| 959 | |
| 960 | BT_DBG("%s", hdev->name); |
| 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 | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 965 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 966 | return; |
| 967 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 968 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 969 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 970 | |
| 971 | cod[0] = hdev->minor_class; |
| 972 | cod[1] = hdev->major_class; |
| 973 | cod[2] = get_service_classes(hdev); |
| 974 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 975 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 976 | cod[1] |= 0x20; |
| 977 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 978 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 979 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 980 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 981 | 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] | 982 | } |
| 983 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 984 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 985 | { |
| 986 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 987 | |
| 988 | /* If there's a pending mgmt command the flag will not yet have |
| 989 | * it's final value, so check for this first. |
| 990 | */ |
| 991 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 992 | if (cmd) { |
| 993 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 994 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 997 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | static void enable_advertising(struct hci_request *req) |
| 1001 | { |
| 1002 | struct hci_dev *hdev = req->hdev; |
| 1003 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1004 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1005 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1006 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 1007 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 1008 | * hci_update_random_address knows that it's safe to go ahead |
| 1009 | * and write a new random address. The flag will be set back on |
| 1010 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 1011 | */ |
| 1012 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 1013 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1014 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1015 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1016 | /* Set require_privacy to true only when non-connectable |
| 1017 | * advertising is used. In that case it is fine to use a |
| 1018 | * non-resolvable private address. |
| 1019 | */ |
| 1020 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1021 | return; |
| 1022 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 1023 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1024 | cp.min_interval = cpu_to_le16(0x0800); |
| 1025 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 1026 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 1027 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 1028 | cp.channel_map = hdev->le_adv_channel_map; |
| 1029 | |
| 1030 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1031 | |
| 1032 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1033 | } |
| 1034 | |
| 1035 | static void disable_advertising(struct hci_request *req) |
| 1036 | { |
| 1037 | u8 enable = 0x00; |
| 1038 | |
| 1039 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1040 | } |
| 1041 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1042 | static void service_cache_off(struct work_struct *work) |
| 1043 | { |
| 1044 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1045 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1046 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1047 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1048 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1049 | return; |
| 1050 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1051 | hci_req_init(&req, hdev); |
| 1052 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1053 | hci_dev_lock(hdev); |
| 1054 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1055 | update_eir(&req); |
| 1056 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1057 | |
| 1058 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1059 | |
| 1060 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1061 | } |
| 1062 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1063 | static void rpa_expired(struct work_struct *work) |
| 1064 | { |
| 1065 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1066 | rpa_expired.work); |
| 1067 | struct hci_request req; |
| 1068 | |
| 1069 | BT_DBG(""); |
| 1070 | |
| 1071 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 1072 | |
| 1073 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 1074 | hci_conn_num(hdev, LE_LINK) > 0) |
| 1075 | return; |
| 1076 | |
| 1077 | /* The generation of a new RPA and programming it into the |
| 1078 | * controller happens in the enable_advertising() function. |
| 1079 | */ |
| 1080 | |
| 1081 | hci_req_init(&req, hdev); |
| 1082 | |
| 1083 | disable_advertising(&req); |
| 1084 | enable_advertising(&req); |
| 1085 | |
| 1086 | hci_req_run(&req, NULL); |
| 1087 | } |
| 1088 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1089 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1090 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1091 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1092 | return; |
| 1093 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1094 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1095 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1096 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1097 | /* Non-mgmt controlled devices get this bit set |
| 1098 | * implicitly so that pairing works for them, however |
| 1099 | * for mgmt we require user-space to explicitly enable |
| 1100 | * it |
| 1101 | */ |
| 1102 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1103 | } |
| 1104 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1105 | 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] | 1106 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1107 | { |
| 1108 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1109 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1110 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1111 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1112 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1113 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1114 | memset(&rp, 0, sizeof(rp)); |
| 1115 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1116 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1117 | |
| 1118 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1119 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1120 | |
| 1121 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1122 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1123 | |
| 1124 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1125 | |
| 1126 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1127 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1128 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1129 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1130 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1131 | 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] | 1132 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1136 | { |
| 1137 | sock_put(cmd->sk); |
| 1138 | kfree(cmd->param); |
| 1139 | kfree(cmd); |
| 1140 | } |
| 1141 | |
| 1142 | 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] | 1143 | struct hci_dev *hdev, void *data, |
| 1144 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1145 | { |
| 1146 | struct pending_cmd *cmd; |
| 1147 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1148 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1149 | if (!cmd) |
| 1150 | return NULL; |
| 1151 | |
| 1152 | cmd->opcode = opcode; |
| 1153 | cmd->index = hdev->id; |
| 1154 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1155 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1156 | if (!cmd->param) { |
| 1157 | kfree(cmd); |
| 1158 | return NULL; |
| 1159 | } |
| 1160 | |
| 1161 | if (data) |
| 1162 | memcpy(cmd->param, data, len); |
| 1163 | |
| 1164 | cmd->sk = sk; |
| 1165 | sock_hold(sk); |
| 1166 | |
| 1167 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1168 | |
| 1169 | return cmd; |
| 1170 | } |
| 1171 | |
| 1172 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1173 | void (*cb)(struct pending_cmd *cmd, |
| 1174 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1175 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1176 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1177 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1178 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1179 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1180 | if (opcode > 0 && cmd->opcode != opcode) |
| 1181 | continue; |
| 1182 | |
| 1183 | cb(cmd, data); |
| 1184 | } |
| 1185 | } |
| 1186 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1187 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1188 | { |
| 1189 | list_del(&cmd->list); |
| 1190 | mgmt_pending_free(cmd); |
| 1191 | } |
| 1192 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1193 | 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] | 1194 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1195 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1196 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1197 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1198 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1201 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1202 | { |
| 1203 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1204 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1205 | if (hci_conn_count(hdev) == 0) { |
| 1206 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1207 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1208 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1209 | } |
| 1210 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1211 | static void hci_stop_discovery(struct hci_request *req) |
| 1212 | { |
| 1213 | struct hci_dev *hdev = req->hdev; |
| 1214 | struct hci_cp_remote_name_req_cancel cp; |
| 1215 | struct inquiry_entry *e; |
| 1216 | |
| 1217 | switch (hdev->discovery.state) { |
| 1218 | case DISCOVERY_FINDING: |
| 1219 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1220 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1221 | } else { |
| 1222 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1223 | hci_req_add_le_scan_disable(req); |
| 1224 | } |
| 1225 | |
| 1226 | break; |
| 1227 | |
| 1228 | case DISCOVERY_RESOLVING: |
| 1229 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1230 | NAME_PENDING); |
| 1231 | if (!e) |
| 1232 | return; |
| 1233 | |
| 1234 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1235 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1236 | &cp); |
| 1237 | |
| 1238 | break; |
| 1239 | |
| 1240 | default: |
| 1241 | /* Passive scanning */ |
| 1242 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1243 | hci_req_add_le_scan_disable(req); |
| 1244 | break; |
| 1245 | } |
| 1246 | } |
| 1247 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1248 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1249 | { |
| 1250 | struct hci_request req; |
| 1251 | struct hci_conn *conn; |
| 1252 | |
| 1253 | hci_req_init(&req, hdev); |
| 1254 | |
| 1255 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1256 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1257 | u8 scan = 0x00; |
| 1258 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1259 | } |
| 1260 | |
| 1261 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1262 | disable_advertising(&req); |
| 1263 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1264 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1265 | |
| 1266 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1267 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1268 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1269 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1270 | switch (conn->state) { |
| 1271 | case BT_CONNECTED: |
| 1272 | case BT_CONFIG: |
| 1273 | dc.handle = cpu_to_le16(conn->handle); |
| 1274 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1275 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1276 | break; |
| 1277 | case BT_CONNECT: |
| 1278 | if (conn->type == LE_LINK) |
| 1279 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1280 | 0, NULL); |
| 1281 | else if (conn->type == ACL_LINK) |
| 1282 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1283 | 6, &conn->dst); |
| 1284 | break; |
| 1285 | case BT_CONNECT2: |
| 1286 | bacpy(&rej.bdaddr, &conn->dst); |
| 1287 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1288 | if (conn->type == ACL_LINK) |
| 1289 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1290 | sizeof(rej), &rej); |
| 1291 | else if (conn->type == SCO_LINK) |
| 1292 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1293 | sizeof(rej), &rej); |
| 1294 | break; |
| 1295 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | return hci_req_run(&req, clean_up_hci_complete); |
| 1299 | } |
| 1300 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1301 | 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] | 1302 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1303 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1304 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1305 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1306 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1307 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1308 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1309 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1310 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1311 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1312 | MGMT_STATUS_INVALID_PARAMS); |
| 1313 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1314 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1315 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1316 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1317 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1318 | MGMT_STATUS_BUSY); |
| 1319 | goto failed; |
| 1320 | } |
| 1321 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1322 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1323 | cancel_delayed_work(&hdev->power_off); |
| 1324 | |
| 1325 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1326 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1327 | data, len); |
| 1328 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1329 | goto failed; |
| 1330 | } |
| 1331 | } |
| 1332 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1333 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1334 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1335 | goto failed; |
| 1336 | } |
| 1337 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1338 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1339 | if (!cmd) { |
| 1340 | err = -ENOMEM; |
| 1341 | goto failed; |
| 1342 | } |
| 1343 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1344 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1345 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1346 | err = 0; |
| 1347 | } else { |
| 1348 | /* Disconnect connections, stop scans, etc */ |
| 1349 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1350 | if (!err) |
| 1351 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1352 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1353 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1354 | /* ENODATA means there were no HCI commands queued */ |
| 1355 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1356 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1357 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1358 | err = 0; |
| 1359 | } |
| 1360 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1361 | |
| 1362 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1363 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1364 | return err; |
| 1365 | } |
| 1366 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1367 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1368 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1369 | { |
| 1370 | struct sk_buff *skb; |
| 1371 | struct mgmt_hdr *hdr; |
| 1372 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1373 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1374 | if (!skb) |
| 1375 | return -ENOMEM; |
| 1376 | |
| 1377 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1378 | hdr->opcode = cpu_to_le16(event); |
| 1379 | if (hdev) |
| 1380 | hdr->index = cpu_to_le16(hdev->id); |
| 1381 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1382 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1383 | hdr->len = cpu_to_le16(data_len); |
| 1384 | |
| 1385 | if (data) |
| 1386 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1387 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1388 | /* Time stamp */ |
| 1389 | __net_timestamp(skb); |
| 1390 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1391 | hci_send_to_control(skb, skip_sk); |
| 1392 | kfree_skb(skb); |
| 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1398 | { |
| 1399 | __le32 ev; |
| 1400 | |
| 1401 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1402 | |
| 1403 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1404 | } |
| 1405 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1406 | struct cmd_lookup { |
| 1407 | struct sock *sk; |
| 1408 | struct hci_dev *hdev; |
| 1409 | u8 mgmt_status; |
| 1410 | }; |
| 1411 | |
| 1412 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1413 | { |
| 1414 | struct cmd_lookup *match = data; |
| 1415 | |
| 1416 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1417 | |
| 1418 | list_del(&cmd->list); |
| 1419 | |
| 1420 | if (match->sk == NULL) { |
| 1421 | match->sk = cmd->sk; |
| 1422 | sock_hold(match->sk); |
| 1423 | } |
| 1424 | |
| 1425 | mgmt_pending_free(cmd); |
| 1426 | } |
| 1427 | |
| 1428 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1429 | { |
| 1430 | u8 *status = data; |
| 1431 | |
| 1432 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1433 | mgmt_pending_remove(cmd); |
| 1434 | } |
| 1435 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1436 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1437 | { |
| 1438 | if (!lmp_bredr_capable(hdev)) |
| 1439 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1440 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1441 | return MGMT_STATUS_REJECTED; |
| 1442 | else |
| 1443 | return MGMT_STATUS_SUCCESS; |
| 1444 | } |
| 1445 | |
| 1446 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1447 | { |
| 1448 | if (!lmp_le_capable(hdev)) |
| 1449 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1450 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1451 | return MGMT_STATUS_REJECTED; |
| 1452 | else |
| 1453 | return MGMT_STATUS_SUCCESS; |
| 1454 | } |
| 1455 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1456 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1457 | { |
| 1458 | struct pending_cmd *cmd; |
| 1459 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1460 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1461 | bool changed; |
| 1462 | |
| 1463 | BT_DBG("status 0x%02x", status); |
| 1464 | |
| 1465 | hci_dev_lock(hdev); |
| 1466 | |
| 1467 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1468 | if (!cmd) |
| 1469 | goto unlock; |
| 1470 | |
| 1471 | if (status) { |
| 1472 | u8 mgmt_err = mgmt_status(status); |
| 1473 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1474 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1475 | goto remove_cmd; |
| 1476 | } |
| 1477 | |
| 1478 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1479 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1480 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1481 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1482 | |
| 1483 | if (hdev->discov_timeout > 0) { |
| 1484 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1485 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1486 | to); |
| 1487 | } |
| 1488 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1489 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1490 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1491 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1492 | |
| 1493 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1494 | |
| 1495 | if (changed) |
| 1496 | new_settings(hdev, cmd->sk); |
| 1497 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1498 | /* When the discoverable mode gets changed, make sure |
| 1499 | * that class of device has the limited discoverable |
| 1500 | * bit correctly set. |
| 1501 | */ |
| 1502 | hci_req_init(&req, hdev); |
| 1503 | update_class(&req); |
| 1504 | hci_req_run(&req, NULL); |
| 1505 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1506 | remove_cmd: |
| 1507 | mgmt_pending_remove(cmd); |
| 1508 | |
| 1509 | unlock: |
| 1510 | hci_dev_unlock(hdev); |
| 1511 | } |
| 1512 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1513 | 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] | 1514 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1515 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1516 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1517 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1518 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1519 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1520 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1521 | int err; |
| 1522 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1523 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1524 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1525 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1526 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1527 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1528 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1529 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1530 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1531 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1532 | MGMT_STATUS_INVALID_PARAMS); |
| 1533 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1534 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1535 | |
| 1536 | /* Disabling discoverable requires that no timeout is set, |
| 1537 | * and enabling limited discoverable requires a timeout. |
| 1538 | */ |
| 1539 | if ((cp->val == 0x00 && timeout > 0) || |
| 1540 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1541 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1542 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1543 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1544 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1545 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1546 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1547 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1548 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1549 | goto failed; |
| 1550 | } |
| 1551 | |
| 1552 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1553 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1554 | err = 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_BUSY); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1556 | goto failed; |
| 1557 | } |
| 1558 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1559 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
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_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1562 | goto failed; |
| 1563 | } |
| 1564 | |
| 1565 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1566 | bool changed = false; |
| 1567 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1568 | /* Setting limited discoverable when powered off is |
| 1569 | * not a valid operation since it requires a timeout |
| 1570 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1571 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1572 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1573 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1574 | changed = true; |
| 1575 | } |
| 1576 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1577 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1578 | if (err < 0) |
| 1579 | goto failed; |
| 1580 | |
| 1581 | if (changed) |
| 1582 | err = new_settings(hdev, sk); |
| 1583 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1584 | goto failed; |
| 1585 | } |
| 1586 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1587 | /* If the current mode is the same, then just update the timeout |
| 1588 | * value with the new value. And if only the timeout gets updated, |
| 1589 | * then no need for any HCI transactions. |
| 1590 | */ |
| 1591 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1592 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1593 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1594 | cancel_delayed_work(&hdev->discov_off); |
| 1595 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1596 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1597 | if (cp->val && hdev->discov_timeout > 0) { |
| 1598 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1599 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1600 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1601 | } |
| 1602 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1603 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1604 | goto failed; |
| 1605 | } |
| 1606 | |
| 1607 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1608 | if (!cmd) { |
| 1609 | err = -ENOMEM; |
| 1610 | goto failed; |
| 1611 | } |
| 1612 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1613 | /* Cancel any potential discoverable timeout that might be |
| 1614 | * still active and store new timeout value. The arming of |
| 1615 | * the timeout happens in the complete handler. |
| 1616 | */ |
| 1617 | cancel_delayed_work(&hdev->discov_off); |
| 1618 | hdev->discov_timeout = timeout; |
| 1619 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1620 | /* Limited discoverable mode */ |
| 1621 | if (cp->val == 0x02) |
| 1622 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1623 | else |
| 1624 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1625 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1626 | hci_req_init(&req, hdev); |
| 1627 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1628 | /* The procedure for LE-only controllers is much simpler - just |
| 1629 | * update the advertising data. |
| 1630 | */ |
| 1631 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1632 | goto update_ad; |
| 1633 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1634 | scan = SCAN_PAGE; |
| 1635 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1636 | if (cp->val) { |
| 1637 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1638 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1639 | if (cp->val == 0x02) { |
| 1640 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1641 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1642 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1643 | hci_cp.iac_lap[1] = 0x8b; |
| 1644 | hci_cp.iac_lap[2] = 0x9e; |
| 1645 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1646 | hci_cp.iac_lap[4] = 0x8b; |
| 1647 | hci_cp.iac_lap[5] = 0x9e; |
| 1648 | } else { |
| 1649 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1650 | hci_cp.num_iac = 1; |
| 1651 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1652 | hci_cp.iac_lap[1] = 0x8b; |
| 1653 | hci_cp.iac_lap[2] = 0x9e; |
| 1654 | } |
| 1655 | |
| 1656 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1657 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1658 | |
| 1659 | scan |= SCAN_INQUIRY; |
| 1660 | } else { |
| 1661 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1662 | } |
| 1663 | |
| 1664 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1665 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1666 | update_ad: |
| 1667 | update_adv_data(&req); |
| 1668 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1669 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1670 | if (err < 0) |
| 1671 | mgmt_pending_remove(cmd); |
| 1672 | |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1673 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1674 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1675 | return err; |
| 1676 | } |
| 1677 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1678 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1679 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1680 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1681 | struct hci_cp_write_page_scan_activity acp; |
| 1682 | u8 type; |
| 1683 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1684 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1685 | return; |
| 1686 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1687 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1688 | return; |
| 1689 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1690 | if (enable) { |
| 1691 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1692 | |
| 1693 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1694 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1695 | } else { |
| 1696 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1697 | |
| 1698 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1699 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1700 | } |
| 1701 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1702 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1703 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1704 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1705 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1706 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1707 | sizeof(acp), &acp); |
| 1708 | |
| 1709 | if (hdev->page_scan_type != type) |
| 1710 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1711 | } |
| 1712 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1713 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1714 | { |
| 1715 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1716 | struct mgmt_mode *cp; |
| 1717 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1718 | |
| 1719 | BT_DBG("status 0x%02x", status); |
| 1720 | |
| 1721 | hci_dev_lock(hdev); |
| 1722 | |
| 1723 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1724 | if (!cmd) |
| 1725 | goto unlock; |
| 1726 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1727 | if (status) { |
| 1728 | u8 mgmt_err = mgmt_status(status); |
| 1729 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1730 | goto remove_cmd; |
| 1731 | } |
| 1732 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1733 | cp = cmd->param; |
| 1734 | if (cp->val) |
| 1735 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1736 | else |
| 1737 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1738 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1739 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1740 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1741 | if (changed) |
| 1742 | new_settings(hdev, cmd->sk); |
| 1743 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1744 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1745 | mgmt_pending_remove(cmd); |
| 1746 | |
| 1747 | unlock: |
| 1748 | hci_dev_unlock(hdev); |
| 1749 | } |
| 1750 | |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1751 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1752 | struct sock *sk, u8 val) |
| 1753 | { |
| 1754 | bool changed = false; |
| 1755 | int err; |
| 1756 | |
| 1757 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1758 | changed = true; |
| 1759 | |
| 1760 | if (val) { |
| 1761 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1762 | } else { |
| 1763 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1764 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1765 | } |
| 1766 | |
| 1767 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1768 | if (err < 0) |
| 1769 | return err; |
| 1770 | |
| 1771 | if (changed) |
| 1772 | return new_settings(hdev, sk); |
| 1773 | |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1777 | 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] | 1778 | u16 len) |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1779 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1780 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1781 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1782 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1783 | u8 scan; |
Johan Hedberg | f7b64e6 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1784 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1785 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1786 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1787 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1788 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1789 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1790 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1791 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1792 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1793 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1794 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1795 | MGMT_STATUS_INVALID_PARAMS); |
| 1796 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1797 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1798 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1799 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1 | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1800 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1801 | goto failed; |
| 1802 | } |
| 1803 | |
| 1804 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1805 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1806 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1807 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1808 | goto failed; |
| 1809 | } |
| 1810 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1811 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1812 | if (!cmd) { |
| 1813 | err = -ENOMEM; |
| 1814 | goto failed; |
| 1815 | } |
| 1816 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1817 | hci_req_init(&req, hdev); |
| 1818 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1819 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1820 | * by-product of disabling connectable, we need to update the |
| 1821 | * advertising flags. |
| 1822 | */ |
| 1823 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1824 | if (!cp->val) { |
| 1825 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1826 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1827 | } |
| 1828 | update_adv_data(&req); |
| 1829 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1830 | if (cp->val) { |
| 1831 | scan = SCAN_PAGE; |
| 1832 | } else { |
| 1833 | scan = 0; |
| 1834 | |
| 1835 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1836 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1837 | cancel_delayed_work(&hdev->discov_off); |
| 1838 | } |
| 1839 | |
| 1840 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1841 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1842 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1843 | /* If we're going from non-connectable to connectable or |
| 1844 | * vice-versa when fast connectable is enabled ensure that fast |
| 1845 | * connectable gets disabled. write_fast_connectable won't do |
| 1846 | * anything if the page scan parameters are already what they |
| 1847 | * should be. |
| 1848 | */ |
| 1849 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1850 | write_fast_connectable(&req, false); |
| 1851 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1852 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1853 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1854 | disable_advertising(&req); |
| 1855 | enable_advertising(&req); |
| 1856 | } |
| 1857 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1858 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1859 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1860 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1861 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1862 | err = set_connectable_update_settings(hdev, sk, |
| 1863 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1864 | goto failed; |
| 1865 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1866 | |
| 1867 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1868 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1869 | return err; |
| 1870 | } |
| 1871 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1872 | 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] | 1873 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1874 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1875 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1876 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1877 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1878 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1879 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1880 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1881 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1882 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1883 | MGMT_STATUS_INVALID_PARAMS); |
| 1884 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1885 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1886 | |
| 1887 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1888 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1889 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1890 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1891 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1892 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1893 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1894 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1895 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1896 | if (changed) |
| 1897 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1898 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1899 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1900 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1901 | return err; |
| 1902 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1903 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1904 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1905 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1906 | { |
| 1907 | struct mgmt_mode *cp = data; |
| 1908 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1909 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1910 | int err; |
| 1911 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1912 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1913 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1914 | status = mgmt_bredr_support(hdev); |
| 1915 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1916 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1917 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1918 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1919 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1920 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1921 | MGMT_STATUS_INVALID_PARAMS); |
| 1922 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1923 | hci_dev_lock(hdev); |
| 1924 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1925 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1926 | bool changed = false; |
| 1927 | |
| 1928 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1929 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1930 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1931 | changed = true; |
| 1932 | } |
| 1933 | |
| 1934 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1935 | if (err < 0) |
| 1936 | goto failed; |
| 1937 | |
| 1938 | if (changed) |
| 1939 | err = new_settings(hdev, sk); |
| 1940 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1941 | goto failed; |
| 1942 | } |
| 1943 | |
| 1944 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1945 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1946 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1947 | goto failed; |
| 1948 | } |
| 1949 | |
| 1950 | val = !!cp->val; |
| 1951 | |
| 1952 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1953 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1954 | goto failed; |
| 1955 | } |
| 1956 | |
| 1957 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1958 | if (!cmd) { |
| 1959 | err = -ENOMEM; |
| 1960 | goto failed; |
| 1961 | } |
| 1962 | |
| 1963 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1964 | if (err < 0) { |
| 1965 | mgmt_pending_remove(cmd); |
| 1966 | goto failed; |
| 1967 | } |
| 1968 | |
| 1969 | failed: |
| 1970 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1971 | return err; |
| 1972 | } |
| 1973 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1974 | 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] | 1975 | { |
| 1976 | struct mgmt_mode *cp = data; |
| 1977 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1978 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1979 | int err; |
| 1980 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1981 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1982 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1983 | status = mgmt_bredr_support(hdev); |
| 1984 | if (status) |
| 1985 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1986 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1987 | if (!lmp_ssp_capable(hdev)) |
| 1988 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1989 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1990 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1991 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1992 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1993 | MGMT_STATUS_INVALID_PARAMS); |
| 1994 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1995 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1996 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1997 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1998 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1999 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2000 | if (cp->val) { |
| 2001 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 2002 | &hdev->dev_flags); |
| 2003 | } else { |
| 2004 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 2005 | &hdev->dev_flags); |
| 2006 | if (!changed) |
| 2007 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 2008 | &hdev->dev_flags); |
| 2009 | else |
| 2010 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2014 | if (err < 0) |
| 2015 | goto failed; |
| 2016 | |
| 2017 | if (changed) |
| 2018 | err = new_settings(hdev, sk); |
| 2019 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2020 | goto failed; |
| 2021 | } |
| 2022 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2023 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 2024 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 2025 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 2026 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2027 | goto failed; |
| 2028 | } |
| 2029 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2030 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2031 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 2032 | goto failed; |
| 2033 | } |
| 2034 | |
| 2035 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 2036 | if (!cmd) { |
| 2037 | err = -ENOMEM; |
| 2038 | goto failed; |
| 2039 | } |
| 2040 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 2041 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 2042 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2043 | sizeof(cp->val), &cp->val); |
| 2044 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2045 | 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] | 2046 | if (err < 0) { |
| 2047 | mgmt_pending_remove(cmd); |
| 2048 | goto failed; |
| 2049 | } |
| 2050 | |
| 2051 | failed: |
| 2052 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2053 | return err; |
| 2054 | } |
| 2055 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2056 | 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] | 2057 | { |
| 2058 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2059 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2060 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2061 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2062 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2063 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2064 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2065 | status = mgmt_bredr_support(hdev); |
| 2066 | if (status) |
| 2067 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2068 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2069 | if (!lmp_ssp_capable(hdev)) |
| 2070 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2071 | MGMT_STATUS_NOT_SUPPORTED); |
| 2072 | |
| 2073 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 2074 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2075 | MGMT_STATUS_REJECTED); |
| 2076 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2077 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2078 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2079 | MGMT_STATUS_INVALID_PARAMS); |
| 2080 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2081 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2082 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2083 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2084 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2085 | } else { |
| 2086 | if (hdev_is_powered(hdev)) { |
| 2087 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2088 | MGMT_STATUS_REJECTED); |
| 2089 | goto unlock; |
| 2090 | } |
| 2091 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2092 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2093 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2094 | |
| 2095 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2096 | if (err < 0) |
| 2097 | goto unlock; |
| 2098 | |
| 2099 | if (changed) |
| 2100 | err = new_settings(hdev, sk); |
| 2101 | |
| 2102 | unlock: |
| 2103 | hci_dev_unlock(hdev); |
| 2104 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2105 | } |
| 2106 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2107 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 2108 | { |
| 2109 | struct cmd_lookup match = { NULL, hdev }; |
| 2110 | |
| 2111 | if (status) { |
| 2112 | u8 mgmt_err = mgmt_status(status); |
| 2113 | |
| 2114 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2115 | &mgmt_err); |
| 2116 | return; |
| 2117 | } |
| 2118 | |
| 2119 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2120 | |
| 2121 | new_settings(hdev, match.sk); |
| 2122 | |
| 2123 | if (match.sk) |
| 2124 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2125 | |
| 2126 | /* Make sure the controller has a good default for |
| 2127 | * advertising data. Restrict the update to when LE |
| 2128 | * has actually been enabled. During power on, the |
| 2129 | * update in powered_update_hci will take care of it. |
| 2130 | */ |
| 2131 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2132 | struct hci_request req; |
| 2133 | |
| 2134 | hci_dev_lock(hdev); |
| 2135 | |
| 2136 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2137 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2138 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2139 | hci_req_run(&req, NULL); |
| 2140 | |
| 2141 | hci_dev_unlock(hdev); |
| 2142 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2143 | } |
| 2144 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2145 | 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] | 2146 | { |
| 2147 | struct mgmt_mode *cp = data; |
| 2148 | struct hci_cp_write_le_host_supported hci_cp; |
| 2149 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2150 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2151 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2152 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2153 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2154 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2155 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2156 | if (!lmp_le_capable(hdev)) |
| 2157 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2158 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2159 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2160 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2161 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2162 | MGMT_STATUS_INVALID_PARAMS); |
| 2163 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2164 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2165 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2166 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2167 | MGMT_STATUS_REJECTED); |
| 2168 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2169 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2170 | |
| 2171 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2172 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2173 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2174 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2175 | bool changed = false; |
| 2176 | |
| 2177 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2178 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2179 | changed = true; |
| 2180 | } |
| 2181 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2182 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2183 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2184 | changed = true; |
| 2185 | } |
| 2186 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2187 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2188 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2189 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2190 | |
| 2191 | if (changed) |
| 2192 | err = new_settings(hdev, sk); |
| 2193 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2194 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2195 | } |
| 2196 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2197 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2198 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2199 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2200 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2201 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2205 | if (!cmd) { |
| 2206 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2207 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2208 | } |
| 2209 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2210 | hci_req_init(&req, hdev); |
| 2211 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2212 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2213 | |
| 2214 | if (val) { |
| 2215 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2216 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2217 | } else { |
| 2218 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2219 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2220 | } |
| 2221 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2222 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2223 | &hci_cp); |
| 2224 | |
| 2225 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2226 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2227 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2228 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2229 | unlock: |
| 2230 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2231 | return err; |
| 2232 | } |
| 2233 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2234 | /* This is a helper function to test for pending mgmt commands that can |
| 2235 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2236 | * mgmt command at a time since otherwise we cannot easily track what |
| 2237 | * the current values are, will be, and based on that calculate if a new |
| 2238 | * HCI command needs to be sent and if yes with what value. |
| 2239 | */ |
| 2240 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2241 | { |
| 2242 | struct pending_cmd *cmd; |
| 2243 | |
| 2244 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2245 | switch (cmd->opcode) { |
| 2246 | case MGMT_OP_ADD_UUID: |
| 2247 | case MGMT_OP_REMOVE_UUID: |
| 2248 | case MGMT_OP_SET_DEV_CLASS: |
| 2249 | case MGMT_OP_SET_POWERED: |
| 2250 | return true; |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | return false; |
| 2255 | } |
| 2256 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2257 | static const u8 bluetooth_base_uuid[] = { |
| 2258 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2259 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2260 | }; |
| 2261 | |
| 2262 | static u8 get_uuid_size(const u8 *uuid) |
| 2263 | { |
| 2264 | u32 val; |
| 2265 | |
| 2266 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2267 | return 128; |
| 2268 | |
| 2269 | val = get_unaligned_le32(&uuid[12]); |
| 2270 | if (val > 0xffff) |
| 2271 | return 32; |
| 2272 | |
| 2273 | return 16; |
| 2274 | } |
| 2275 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2276 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2277 | { |
| 2278 | struct pending_cmd *cmd; |
| 2279 | |
| 2280 | hci_dev_lock(hdev); |
| 2281 | |
| 2282 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2283 | if (!cmd) |
| 2284 | goto unlock; |
| 2285 | |
| 2286 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2287 | hdev->dev_class, 3); |
| 2288 | |
| 2289 | mgmt_pending_remove(cmd); |
| 2290 | |
| 2291 | unlock: |
| 2292 | hci_dev_unlock(hdev); |
| 2293 | } |
| 2294 | |
| 2295 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2296 | { |
| 2297 | BT_DBG("status 0x%02x", status); |
| 2298 | |
| 2299 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2300 | } |
| 2301 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2302 | 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] | 2303 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2304 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2305 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2306 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2307 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2308 | int err; |
| 2309 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2310 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2311 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2312 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2313 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2314 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2315 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2316 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2317 | goto failed; |
| 2318 | } |
| 2319 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2320 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2321 | if (!uuid) { |
| 2322 | err = -ENOMEM; |
| 2323 | goto failed; |
| 2324 | } |
| 2325 | |
| 2326 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2327 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2328 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2329 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2330 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2331 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2332 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2333 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2334 | update_class(&req); |
| 2335 | update_eir(&req); |
| 2336 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2337 | err = hci_req_run(&req, add_uuid_complete); |
| 2338 | if (err < 0) { |
| 2339 | if (err != -ENODATA) |
| 2340 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2341 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2342 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2343 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2344 | goto failed; |
| 2345 | } |
| 2346 | |
| 2347 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2348 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2349 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2350 | goto failed; |
| 2351 | } |
| 2352 | |
| 2353 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2354 | |
| 2355 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2356 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2357 | return err; |
| 2358 | } |
| 2359 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2360 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2361 | { |
| 2362 | if (!hdev_is_powered(hdev)) |
| 2363 | return false; |
| 2364 | |
| 2365 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2366 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2367 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2368 | return true; |
| 2369 | } |
| 2370 | |
| 2371 | return false; |
| 2372 | } |
| 2373 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2374 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2375 | { |
| 2376 | BT_DBG("status 0x%02x", status); |
| 2377 | |
| 2378 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2379 | } |
| 2380 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2381 | 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] | 2382 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2383 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2384 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2385 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2386 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2387 | 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] | 2388 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2389 | int err, found; |
| 2390 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2391 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2392 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2393 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2394 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2395 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2396 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2397 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2398 | goto unlock; |
| 2399 | } |
| 2400 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2401 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2402 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2403 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2404 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2405 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2406 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2407 | goto unlock; |
| 2408 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2409 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2410 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | found = 0; |
| 2414 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2415 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2416 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2417 | continue; |
| 2418 | |
| 2419 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2420 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2421 | found++; |
| 2422 | } |
| 2423 | |
| 2424 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2425 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2426 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2427 | goto unlock; |
| 2428 | } |
| 2429 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2430 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2431 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2432 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2433 | update_class(&req); |
| 2434 | update_eir(&req); |
| 2435 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2436 | err = hci_req_run(&req, remove_uuid_complete); |
| 2437 | if (err < 0) { |
| 2438 | if (err != -ENODATA) |
| 2439 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2440 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2441 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2442 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2443 | goto unlock; |
| 2444 | } |
| 2445 | |
| 2446 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2447 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2448 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2449 | goto unlock; |
| 2450 | } |
| 2451 | |
| 2452 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2453 | |
| 2454 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2455 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2456 | return err; |
| 2457 | } |
| 2458 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2459 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2460 | { |
| 2461 | BT_DBG("status 0x%02x", status); |
| 2462 | |
| 2463 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2464 | } |
| 2465 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2466 | 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] | 2467 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2468 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2469 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2470 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2471 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2472 | int err; |
| 2473 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2474 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2475 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2476 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2477 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2478 | MGMT_STATUS_NOT_SUPPORTED); |
| 2479 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2480 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2481 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2482 | if (pending_eir_or_class(hdev)) { |
| 2483 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2484 | MGMT_STATUS_BUSY); |
| 2485 | goto unlock; |
| 2486 | } |
| 2487 | |
| 2488 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2489 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2490 | MGMT_STATUS_INVALID_PARAMS); |
| 2491 | goto unlock; |
| 2492 | } |
| 2493 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2494 | hdev->major_class = cp->major; |
| 2495 | hdev->minor_class = cp->minor; |
| 2496 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2497 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2498 | 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] | 2499 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2500 | goto unlock; |
| 2501 | } |
| 2502 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2503 | hci_req_init(&req, hdev); |
| 2504 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2505 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2506 | hci_dev_unlock(hdev); |
| 2507 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2508 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2509 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2510 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2511 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2512 | update_class(&req); |
| 2513 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2514 | err = hci_req_run(&req, set_class_complete); |
| 2515 | if (err < 0) { |
| 2516 | if (err != -ENODATA) |
| 2517 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2518 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2519 | 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] | 2520 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2521 | goto unlock; |
| 2522 | } |
| 2523 | |
| 2524 | 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] | 2525 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2526 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2527 | goto unlock; |
| 2528 | } |
| 2529 | |
| 2530 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2531 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2532 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2533 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2534 | return err; |
| 2535 | } |
| 2536 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2537 | 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] | 2538 | u16 len) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2539 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2540 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2541 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2542 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2543 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2544 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2545 | int i; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2546 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2547 | BT_DBG("request for %s", hdev->name); |
| 2548 | |
| 2549 | if (!lmp_bredr_capable(hdev)) |
| 2550 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2551 | MGMT_STATUS_NOT_SUPPORTED); |
| 2552 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2553 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2554 | if (key_count > max_key_count) { |
| 2555 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2556 | key_count); |
| 2557 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2558 | MGMT_STATUS_INVALID_PARAMS); |
| 2559 | } |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2560 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2561 | expected_len = sizeof(*cp) + key_count * |
| 2562 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2563 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2564 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2565 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2566 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2567 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2568 | } |
| 2569 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2570 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2571 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2572 | MGMT_STATUS_INVALID_PARAMS); |
| 2573 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2574 | 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] | 2575 | key_count); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2576 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2577 | for (i = 0; i < key_count; i++) { |
| 2578 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2579 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2580 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2581 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2582 | MGMT_STATUS_INVALID_PARAMS); |
| 2583 | } |
| 2584 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2585 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2586 | |
| 2587 | hci_link_keys_clear(hdev); |
| 2588 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2589 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2590 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2591 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2592 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2593 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2594 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2595 | |
| 2596 | if (changed) |
| 2597 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2598 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2599 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2600 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2601 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2602 | /* Always ignore debug keys and require a new pairing if |
| 2603 | * the user wants to use them. |
| 2604 | */ |
| 2605 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2606 | continue; |
| 2607 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2608 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2609 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2610 | } |
| 2611 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2612 | 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] | 2613 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2614 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2615 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2616 | return 0; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2617 | } |
| 2618 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2619 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2620 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2621 | { |
| 2622 | struct mgmt_ev_device_unpaired ev; |
| 2623 | |
| 2624 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2625 | ev.addr.type = addr_type; |
| 2626 | |
| 2627 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2628 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2629 | } |
| 2630 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2631 | 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] | 2632 | u16 len) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2633 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2634 | struct mgmt_cp_unpair_device *cp = data; |
| 2635 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2636 | struct hci_cp_disconnect dc; |
| 2637 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2638 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2639 | int err; |
| 2640 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2641 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2642 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2643 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2644 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2645 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2646 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2647 | MGMT_STATUS_INVALID_PARAMS, |
| 2648 | &rp, sizeof(rp)); |
| 2649 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2650 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2651 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2652 | MGMT_STATUS_INVALID_PARAMS, |
| 2653 | &rp, sizeof(rp)); |
| 2654 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2655 | hci_dev_lock(hdev); |
| 2656 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2657 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2658 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2659 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2660 | goto unlock; |
| 2661 | } |
| 2662 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2663 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2664 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2665 | } else { |
| 2666 | u8 addr_type; |
| 2667 | |
| 2668 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2669 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2670 | else |
| 2671 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2672 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2673 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2674 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2675 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2676 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2677 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2678 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2679 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2680 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2681 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2682 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2683 | goto unlock; |
| 2684 | } |
| 2685 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2686 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2687 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2688 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2689 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2690 | else |
| 2691 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2692 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2693 | } else { |
| 2694 | conn = NULL; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2695 | } |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2696 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2697 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2698 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2699 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2700 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2701 | goto unlock; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2702 | } |
| 2703 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2704 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2705 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2706 | if (!cmd) { |
| 2707 | err = -ENOMEM; |
| 2708 | goto unlock; |
| 2709 | } |
| 2710 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2711 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2712 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2713 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2714 | if (err < 0) |
| 2715 | mgmt_pending_remove(cmd); |
| 2716 | |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2717 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2718 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2719 | return err; |
| 2720 | } |
| 2721 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2722 | 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] | 2723 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2724 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2725 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2726 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2727 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2728 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2729 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2730 | int err; |
| 2731 | |
| 2732 | BT_DBG(""); |
| 2733 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2734 | memset(&rp, 0, sizeof(rp)); |
| 2735 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2736 | rp.addr.type = cp->addr.type; |
| 2737 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2738 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2739 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2740 | MGMT_STATUS_INVALID_PARAMS, |
| 2741 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2742 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2743 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2744 | |
| 2745 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2746 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2747 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2748 | goto failed; |
| 2749 | } |
| 2750 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2751 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2752 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2753 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2754 | goto failed; |
| 2755 | } |
| 2756 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2757 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2758 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2759 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2760 | else |
| 2761 | 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] | 2762 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2763 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2764 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2765 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2766 | goto failed; |
| 2767 | } |
| 2768 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2769 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2770 | if (!cmd) { |
| 2771 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2772 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2773 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2774 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2775 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2776 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2777 | |
| 2778 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2779 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2780 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2781 | |
| 2782 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2783 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2784 | return err; |
| 2785 | } |
| 2786 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2787 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2788 | { |
| 2789 | switch (link_type) { |
| 2790 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2791 | switch (addr_type) { |
| 2792 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2793 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2794 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2795 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2796 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2797 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2798 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2799 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2800 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2801 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2802 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2803 | } |
| 2804 | } |
| 2805 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2806 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2807 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2808 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2809 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2810 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2811 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2812 | int err; |
| 2813 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2814 | |
| 2815 | BT_DBG(""); |
| 2816 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2817 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2818 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2819 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2820 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2821 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2822 | goto unlock; |
| 2823 | } |
| 2824 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2825 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2826 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2827 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2828 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2829 | } |
| 2830 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2831 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2832 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2833 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2834 | err = -ENOMEM; |
| 2835 | goto unlock; |
| 2836 | } |
| 2837 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2838 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2839 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2840 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2841 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2842 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2843 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2844 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2845 | continue; |
| 2846 | i++; |
| 2847 | } |
| 2848 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2849 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2850 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2851 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2852 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2853 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2854 | 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] | 2855 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2856 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2857 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2858 | |
| 2859 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2860 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2861 | return err; |
| 2862 | } |
| 2863 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2864 | 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] | 2865 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2866 | { |
| 2867 | struct pending_cmd *cmd; |
| 2868 | int err; |
| 2869 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2870 | 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] | 2871 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2872 | if (!cmd) |
| 2873 | return -ENOMEM; |
| 2874 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2875 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2876 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2877 | if (err < 0) |
| 2878 | mgmt_pending_remove(cmd); |
| 2879 | |
| 2880 | return err; |
| 2881 | } |
| 2882 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2883 | 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] | 2884 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2885 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2886 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2887 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2888 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2889 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2890 | int err; |
| 2891 | |
| 2892 | BT_DBG(""); |
| 2893 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2894 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2895 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2896 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2897 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2898 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2899 | goto failed; |
| 2900 | } |
| 2901 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2902 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2903 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2904 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2905 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2906 | goto failed; |
| 2907 | } |
| 2908 | |
| 2909 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2910 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2911 | |
| 2912 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2913 | |
| 2914 | BT_ERR("PIN code is not 16 bytes long"); |
| 2915 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2916 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2917 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2918 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2919 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2920 | |
| 2921 | goto failed; |
| 2922 | } |
| 2923 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2924 | 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] | 2925 | if (!cmd) { |
| 2926 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2927 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2928 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2929 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2930 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2931 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2932 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2933 | |
| 2934 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2935 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2936 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2937 | |
| 2938 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2939 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2940 | return err; |
| 2941 | } |
| 2942 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2943 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2944 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2945 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2946 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2947 | |
| 2948 | BT_DBG(""); |
| 2949 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2950 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2951 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2952 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2953 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2954 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2955 | |
| 2956 | hdev->io_capability = cp->io_capability; |
| 2957 | |
| 2958 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2959 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2960 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2961 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2962 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2963 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2964 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2965 | } |
| 2966 | |
Gustavo Padovan | 6039aa73 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2967 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2968 | { |
| 2969 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2970 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2971 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2972 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2973 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2974 | continue; |
| 2975 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2976 | if (cmd->user_data != conn) |
| 2977 | continue; |
| 2978 | |
| 2979 | return cmd; |
| 2980 | } |
| 2981 | |
| 2982 | return NULL; |
| 2983 | } |
| 2984 | |
| 2985 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2986 | { |
| 2987 | struct mgmt_rp_pair_device rp; |
| 2988 | struct hci_conn *conn = cmd->user_data; |
| 2989 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2990 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2991 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2992 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2993 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2994 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2995 | |
| 2996 | /* So we don't get further callbacks for this connection */ |
| 2997 | conn->connect_cfm_cb = NULL; |
| 2998 | conn->security_cfm_cb = NULL; |
| 2999 | conn->disconn_cfm_cb = NULL; |
| 3000 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3001 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3002 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3003 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3004 | } |
| 3005 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3006 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 3007 | { |
| 3008 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 3009 | struct pending_cmd *cmd; |
| 3010 | |
| 3011 | cmd = find_pairing(conn); |
| 3012 | if (cmd) |
| 3013 | pairing_complete(cmd, status); |
| 3014 | } |
| 3015 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3016 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 3017 | { |
| 3018 | struct pending_cmd *cmd; |
| 3019 | |
| 3020 | BT_DBG("status %u", status); |
| 3021 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 3022 | cmd = find_pairing(conn); |
| 3023 | if (!cmd) |
| 3024 | BT_DBG("Unable to find a pending command"); |
| 3025 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 3026 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3027 | } |
| 3028 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3029 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 3030 | { |
| 3031 | struct pending_cmd *cmd; |
| 3032 | |
| 3033 | BT_DBG("status %u", status); |
| 3034 | |
| 3035 | if (!status) |
| 3036 | return; |
| 3037 | |
| 3038 | cmd = find_pairing(conn); |
| 3039 | if (!cmd) |
| 3040 | BT_DBG("Unable to find a pending command"); |
| 3041 | else |
| 3042 | pairing_complete(cmd, mgmt_status(status)); |
| 3043 | } |
| 3044 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3045 | 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] | 3046 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3047 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3048 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3049 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3050 | struct pending_cmd *cmd; |
| 3051 | u8 sec_level, auth_type; |
| 3052 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3053 | int err; |
| 3054 | |
| 3055 | BT_DBG(""); |
| 3056 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3057 | memset(&rp, 0, sizeof(rp)); |
| 3058 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3059 | rp.addr.type = cp->addr.type; |
| 3060 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3061 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 3062 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3063 | MGMT_STATUS_INVALID_PARAMS, |
| 3064 | &rp, sizeof(rp)); |
| 3065 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3066 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 3067 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3068 | MGMT_STATUS_INVALID_PARAMS, |
| 3069 | &rp, sizeof(rp)); |
| 3070 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3071 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3072 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3073 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3074 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3075 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3076 | goto unlock; |
| 3077 | } |
| 3078 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3079 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3080 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3081 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3082 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3083 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3084 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3085 | } else { |
| 3086 | u8 addr_type; |
| 3087 | |
| 3088 | /* Convert from L2CAP channel address type to HCI address type |
| 3089 | */ |
| 3090 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3091 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3092 | else |
| 3093 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3094 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3095 | /* When pairing a new device, it is expected to remember |
| 3096 | * this device for future connections. Adding the connection |
| 3097 | * parameter information ahead of time allows tracking |
| 3098 | * of the slave preferred values and will speed up any |
| 3099 | * further connection establishment. |
| 3100 | * |
| 3101 | * If connection parameters already exist, then they |
| 3102 | * will be kept and this function does nothing. |
| 3103 | */ |
| 3104 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3105 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3106 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3107 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3108 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3109 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3110 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3111 | int status; |
| 3112 | |
| 3113 | if (PTR_ERR(conn) == -EBUSY) |
| 3114 | status = MGMT_STATUS_BUSY; |
| 3115 | else |
| 3116 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3117 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3118 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3119 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3120 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3121 | goto unlock; |
| 3122 | } |
| 3123 | |
| 3124 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3125 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3126 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3127 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3128 | goto unlock; |
| 3129 | } |
| 3130 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3131 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3132 | if (!cmd) { |
| 3133 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3134 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3135 | goto unlock; |
| 3136 | } |
| 3137 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3138 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3139 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3140 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3141 | conn->security_cfm_cb = pairing_complete_cb; |
| 3142 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3143 | } else { |
| 3144 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3145 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3146 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3147 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3148 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3149 | conn->io_capability = cp->io_cap; |
| 3150 | cmd->user_data = conn; |
| 3151 | |
| 3152 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3153 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3154 | pairing_complete(cmd, 0); |
| 3155 | |
| 3156 | err = 0; |
| 3157 | |
| 3158 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3159 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3160 | return err; |
| 3161 | } |
| 3162 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3163 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3164 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3165 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3166 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3167 | struct pending_cmd *cmd; |
| 3168 | struct hci_conn *conn; |
| 3169 | int err; |
| 3170 | |
| 3171 | BT_DBG(""); |
| 3172 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3173 | hci_dev_lock(hdev); |
| 3174 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3175 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3176 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3177 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3178 | goto unlock; |
| 3179 | } |
| 3180 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3181 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3182 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3183 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3184 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3185 | goto unlock; |
| 3186 | } |
| 3187 | |
| 3188 | conn = cmd->user_data; |
| 3189 | |
| 3190 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3191 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3192 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3193 | goto unlock; |
| 3194 | } |
| 3195 | |
| 3196 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3197 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3198 | 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] | 3199 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3200 | unlock: |
| 3201 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3202 | return err; |
| 3203 | } |
| 3204 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3205 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3206 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3207 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3208 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3209 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3210 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3211 | int err; |
| 3212 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3213 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3214 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3215 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3216 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3217 | MGMT_STATUS_NOT_POWERED, addr, |
| 3218 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3219 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3220 | } |
| 3221 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3222 | if (addr->type == BDADDR_BREDR) |
| 3223 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3224 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3225 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3226 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3227 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3228 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3229 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3230 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3231 | goto done; |
| 3232 | } |
| 3233 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3234 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3235 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3236 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3237 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3238 | MGMT_STATUS_SUCCESS, addr, |
| 3239 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3240 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3241 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3242 | MGMT_STATUS_FAILED, addr, |
| 3243 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3244 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3245 | goto done; |
| 3246 | } |
| 3247 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3248 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3249 | if (!cmd) { |
| 3250 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3251 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3252 | } |
| 3253 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3254 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3255 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3256 | struct hci_cp_user_passkey_reply cp; |
| 3257 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3258 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3259 | cp.passkey = passkey; |
| 3260 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3261 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3262 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3263 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3264 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3265 | if (err < 0) |
| 3266 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3267 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3268 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3269 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3270 | return err; |
| 3271 | } |
| 3272 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3273 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3274 | void *data, u16 len) |
| 3275 | { |
| 3276 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3277 | |
| 3278 | BT_DBG(""); |
| 3279 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3280 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3281 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3282 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3283 | } |
| 3284 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3285 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3286 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3287 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3288 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3289 | |
| 3290 | BT_DBG(""); |
| 3291 | |
| 3292 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3293 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3294 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3295 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3296 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3297 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3298 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3299 | } |
| 3300 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3301 | 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] | 3302 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3303 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3304 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3305 | |
| 3306 | BT_DBG(""); |
| 3307 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3308 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3309 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3310 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3311 | } |
| 3312 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3313 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3314 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3315 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3316 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3317 | |
| 3318 | BT_DBG(""); |
| 3319 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3320 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3321 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3322 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3323 | } |
| 3324 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3325 | 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] | 3326 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3327 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3328 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3329 | |
| 3330 | BT_DBG(""); |
| 3331 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3332 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3333 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3334 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3335 | } |
| 3336 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3337 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3338 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3339 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3340 | struct hci_cp_write_local_name cp; |
| 3341 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3342 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3343 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3344 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3345 | } |
| 3346 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3347 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3348 | { |
| 3349 | struct mgmt_cp_set_local_name *cp; |
| 3350 | struct pending_cmd *cmd; |
| 3351 | |
| 3352 | BT_DBG("status 0x%02x", status); |
| 3353 | |
| 3354 | hci_dev_lock(hdev); |
| 3355 | |
| 3356 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3357 | if (!cmd) |
| 3358 | goto unlock; |
| 3359 | |
| 3360 | cp = cmd->param; |
| 3361 | |
| 3362 | if (status) |
| 3363 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3364 | mgmt_status(status)); |
| 3365 | else |
| 3366 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3367 | cp, sizeof(*cp)); |
| 3368 | |
| 3369 | mgmt_pending_remove(cmd); |
| 3370 | |
| 3371 | unlock: |
| 3372 | hci_dev_unlock(hdev); |
| 3373 | } |
| 3374 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3375 | 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] | 3376 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3377 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3378 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3379 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3380 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3381 | int err; |
| 3382 | |
| 3383 | BT_DBG(""); |
| 3384 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3385 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3386 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3387 | /* If the old values are the same as the new ones just return a |
| 3388 | * direct command complete event. |
| 3389 | */ |
| 3390 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3391 | !memcmp(hdev->short_name, cp->short_name, |
| 3392 | sizeof(hdev->short_name))) { |
| 3393 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3394 | data, len); |
| 3395 | goto failed; |
| 3396 | } |
| 3397 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3398 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3399 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3400 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3401 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3402 | |
| 3403 | 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] | 3404 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3405 | if (err < 0) |
| 3406 | goto failed; |
| 3407 | |
| 3408 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3409 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3410 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3411 | goto failed; |
| 3412 | } |
| 3413 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3414 | 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] | 3415 | if (!cmd) { |
| 3416 | err = -ENOMEM; |
| 3417 | goto failed; |
| 3418 | } |
| 3419 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3420 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3421 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3422 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3423 | |
| 3424 | if (lmp_bredr_capable(hdev)) { |
| 3425 | update_name(&req); |
| 3426 | update_eir(&req); |
| 3427 | } |
| 3428 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3429 | /* The name is stored in the scan response data and so |
| 3430 | * no need to udpate the advertising data here. |
| 3431 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3432 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3433 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3434 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3435 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3436 | if (err < 0) |
| 3437 | mgmt_pending_remove(cmd); |
| 3438 | |
| 3439 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3440 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3441 | return err; |
| 3442 | } |
| 3443 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3444 | 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] | 3445 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3446 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3447 | struct pending_cmd *cmd; |
| 3448 | int err; |
| 3449 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3450 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3451 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3452 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3453 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3454 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3455 | 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] | 3456 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3457 | goto unlock; |
| 3458 | } |
| 3459 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3460 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3461 | 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] | 3462 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3463 | goto unlock; |
| 3464 | } |
| 3465 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3466 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3467 | 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] | 3468 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3469 | goto unlock; |
| 3470 | } |
| 3471 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3472 | 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] | 3473 | if (!cmd) { |
| 3474 | err = -ENOMEM; |
| 3475 | goto unlock; |
| 3476 | } |
| 3477 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3478 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3479 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3480 | 0, NULL); |
| 3481 | else |
| 3482 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3483 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3484 | if (err < 0) |
| 3485 | mgmt_pending_remove(cmd); |
| 3486 | |
| 3487 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3488 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3489 | return err; |
| 3490 | } |
| 3491 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3492 | 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] | 3493 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3494 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3495 | int err; |
| 3496 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3497 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3498 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3499 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3500 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3501 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3502 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3503 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3504 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3505 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3506 | cp->hash, cp->randomizer); |
| 3507 | if (err < 0) |
| 3508 | status = MGMT_STATUS_FAILED; |
| 3509 | else |
| 3510 | status = MGMT_STATUS_SUCCESS; |
| 3511 | |
| 3512 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3513 | status, &cp->addr, sizeof(cp->addr)); |
| 3514 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3515 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3516 | u8 status; |
| 3517 | |
| 3518 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3519 | cp->hash192, |
| 3520 | cp->randomizer192, |
| 3521 | cp->hash256, |
| 3522 | cp->randomizer256); |
| 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 { |
| 3531 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3532 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3533 | MGMT_STATUS_INVALID_PARAMS); |
| 3534 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3535 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3536 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3537 | return err; |
| 3538 | } |
| 3539 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3540 | 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] | 3541 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3542 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3543 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3544 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3545 | int err; |
| 3546 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3547 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3548 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3549 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3550 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3551 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3552 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3553 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3554 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3555 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3556 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3557 | 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] | 3558 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3559 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3560 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3561 | return err; |
| 3562 | } |
| 3563 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3564 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3565 | { |
| 3566 | struct pending_cmd *cmd; |
| 3567 | u8 type; |
| 3568 | int err; |
| 3569 | |
| 3570 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3571 | |
| 3572 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3573 | if (!cmd) |
| 3574 | return -ENOENT; |
| 3575 | |
| 3576 | type = hdev->discovery.type; |
| 3577 | |
| 3578 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3579 | &type, sizeof(type)); |
| 3580 | mgmt_pending_remove(cmd); |
| 3581 | |
| 3582 | return err; |
| 3583 | } |
| 3584 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3585 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3586 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3587 | unsigned long timeout = 0; |
| 3588 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3589 | BT_DBG("status %d", status); |
| 3590 | |
| 3591 | if (status) { |
| 3592 | hci_dev_lock(hdev); |
| 3593 | mgmt_start_discovery_failed(hdev, status); |
| 3594 | hci_dev_unlock(hdev); |
| 3595 | return; |
| 3596 | } |
| 3597 | |
| 3598 | hci_dev_lock(hdev); |
| 3599 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3600 | hci_dev_unlock(hdev); |
| 3601 | |
| 3602 | switch (hdev->discovery.type) { |
| 3603 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3604 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3605 | break; |
| 3606 | |
| 3607 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3608 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3609 | break; |
| 3610 | |
| 3611 | case DISCOV_TYPE_BREDR: |
| 3612 | break; |
| 3613 | |
| 3614 | default: |
| 3615 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3616 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3617 | |
| 3618 | if (!timeout) |
| 3619 | return; |
| 3620 | |
| 3621 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3622 | } |
| 3623 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3624 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3625 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3626 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3627 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3628 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3629 | struct hci_cp_le_set_scan_param param_cp; |
| 3630 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3631 | struct hci_cp_inquiry inq_cp; |
| 3632 | struct hci_request req; |
| 3633 | /* General inquiry access code (GIAC) */ |
| 3634 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3635 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3636 | int err; |
| 3637 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3638 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3639 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3640 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3641 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3642 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3643 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3644 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3645 | goto failed; |
| 3646 | } |
| 3647 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3648 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3649 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3650 | MGMT_STATUS_BUSY); |
| 3651 | goto failed; |
| 3652 | } |
| 3653 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3654 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3655 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3656 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3657 | goto failed; |
| 3658 | } |
| 3659 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3660 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3661 | if (!cmd) { |
| 3662 | err = -ENOMEM; |
| 3663 | goto failed; |
| 3664 | } |
| 3665 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3666 | hdev->discovery.type = cp->type; |
| 3667 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3668 | hci_req_init(&req, hdev); |
| 3669 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3670 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3671 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3672 | status = mgmt_bredr_support(hdev); |
| 3673 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3674 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3675 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3676 | mgmt_pending_remove(cmd); |
| 3677 | goto failed; |
| 3678 | } |
| 3679 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3680 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3681 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3682 | MGMT_STATUS_BUSY); |
| 3683 | mgmt_pending_remove(cmd); |
| 3684 | goto failed; |
| 3685 | } |
| 3686 | |
| 3687 | hci_inquiry_cache_flush(hdev); |
| 3688 | |
| 3689 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3690 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3691 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3692 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3693 | break; |
| 3694 | |
| 3695 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3696 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3697 | status = mgmt_le_support(hdev); |
| 3698 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3699 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3700 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3701 | mgmt_pending_remove(cmd); |
| 3702 | goto failed; |
| 3703 | } |
| 3704 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3705 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3706 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3707 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3708 | MGMT_STATUS_NOT_SUPPORTED); |
| 3709 | mgmt_pending_remove(cmd); |
| 3710 | goto failed; |
| 3711 | } |
| 3712 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3713 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3714 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3715 | MGMT_STATUS_REJECTED); |
| 3716 | mgmt_pending_remove(cmd); |
| 3717 | goto failed; |
| 3718 | } |
| 3719 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3720 | /* If controller is scanning, it means the background scanning |
| 3721 | * is running. Thus, we should temporarily stop it in order to |
| 3722 | * set the discovery scanning parameters. |
| 3723 | */ |
| 3724 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3725 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3726 | |
| 3727 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3728 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3729 | /* All active scans will be done with either a resolvable |
| 3730 | * private address (when privacy feature has been enabled) |
| 3731 | * or unresolvable private address. |
| 3732 | */ |
| 3733 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3734 | if (err < 0) { |
| 3735 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3736 | MGMT_STATUS_FAILED); |
| 3737 | mgmt_pending_remove(cmd); |
| 3738 | goto failed; |
| 3739 | } |
| 3740 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3741 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3742 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3743 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3744 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3745 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3746 | ¶m_cp); |
| 3747 | |
| 3748 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3749 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3750 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3751 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3752 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3753 | break; |
| 3754 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3755 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3756 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3757 | MGMT_STATUS_INVALID_PARAMS); |
| 3758 | mgmt_pending_remove(cmd); |
| 3759 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3760 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3761 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3762 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3763 | if (err < 0) |
| 3764 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3765 | else |
| 3766 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3767 | |
| 3768 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3769 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3770 | return err; |
| 3771 | } |
| 3772 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3773 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3774 | { |
| 3775 | struct pending_cmd *cmd; |
| 3776 | int err; |
| 3777 | |
| 3778 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3779 | if (!cmd) |
| 3780 | return -ENOENT; |
| 3781 | |
| 3782 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3783 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3784 | mgmt_pending_remove(cmd); |
| 3785 | |
| 3786 | return err; |
| 3787 | } |
| 3788 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3789 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3790 | { |
| 3791 | BT_DBG("status %d", status); |
| 3792 | |
| 3793 | hci_dev_lock(hdev); |
| 3794 | |
| 3795 | if (status) { |
| 3796 | mgmt_stop_discovery_failed(hdev, status); |
| 3797 | goto unlock; |
| 3798 | } |
| 3799 | |
| 3800 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3801 | |
| 3802 | unlock: |
| 3803 | hci_dev_unlock(hdev); |
| 3804 | } |
| 3805 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3806 | 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] | 3807 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3808 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3809 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3810 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3811 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3812 | int err; |
| 3813 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3814 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3815 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3816 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3817 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3818 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3819 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3820 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3821 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3822 | goto unlock; |
| 3823 | } |
| 3824 | |
| 3825 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3826 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3827 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3828 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3829 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3830 | } |
| 3831 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3832 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3833 | if (!cmd) { |
| 3834 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3835 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3836 | } |
| 3837 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3838 | hci_req_init(&req, hdev); |
| 3839 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3840 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3841 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3842 | err = hci_req_run(&req, stop_discovery_complete); |
| 3843 | if (!err) { |
| 3844 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3845 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3846 | } |
| 3847 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3848 | mgmt_pending_remove(cmd); |
| 3849 | |
| 3850 | /* If no HCI commands were sent we're done */ |
| 3851 | if (err == -ENODATA) { |
| 3852 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3853 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3854 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3855 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3856 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3857 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3858 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3859 | return err; |
| 3860 | } |
| 3861 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3862 | 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] | 3863 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3864 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3865 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3866 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3867 | int err; |
| 3868 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3869 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3870 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3871 | hci_dev_lock(hdev); |
| 3872 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3873 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3874 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3875 | MGMT_STATUS_FAILED, &cp->addr, |
| 3876 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3877 | goto failed; |
| 3878 | } |
| 3879 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3880 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3881 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3882 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3883 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3884 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3885 | goto failed; |
| 3886 | } |
| 3887 | |
| 3888 | if (cp->name_known) { |
| 3889 | e->name_state = NAME_KNOWN; |
| 3890 | list_del(&e->list); |
| 3891 | } else { |
| 3892 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3893 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3894 | } |
| 3895 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3896 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3897 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3898 | |
| 3899 | failed: |
| 3900 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3901 | return err; |
| 3902 | } |
| 3903 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3904 | 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] | 3905 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3906 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3907 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3908 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3909 | int err; |
| 3910 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3911 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3912 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3913 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3914 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3915 | MGMT_STATUS_INVALID_PARAMS, |
| 3916 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3917 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3918 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3919 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3920 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3921 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3922 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3923 | goto done; |
| 3924 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3925 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3926 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3927 | sk); |
| 3928 | status = MGMT_STATUS_SUCCESS; |
| 3929 | |
| 3930 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3931 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3932 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3933 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3934 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3935 | |
| 3936 | return err; |
| 3937 | } |
| 3938 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3939 | 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] | 3940 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3941 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3942 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3943 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3944 | int err; |
| 3945 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3946 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3947 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3948 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3949 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3950 | MGMT_STATUS_INVALID_PARAMS, |
| 3951 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3952 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3953 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3954 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3955 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3956 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3957 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3958 | goto done; |
| 3959 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3960 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3961 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3962 | sk); |
| 3963 | status = MGMT_STATUS_SUCCESS; |
| 3964 | |
| 3965 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3966 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3967 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3968 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3969 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3970 | |
| 3971 | return err; |
| 3972 | } |
| 3973 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3974 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3975 | u16 len) |
| 3976 | { |
| 3977 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3978 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3979 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3980 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3981 | |
| 3982 | BT_DBG("%s", hdev->name); |
| 3983 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3984 | source = __le16_to_cpu(cp->source); |
| 3985 | |
| 3986 | if (source > 0x0002) |
| 3987 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3988 | MGMT_STATUS_INVALID_PARAMS); |
| 3989 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3990 | hci_dev_lock(hdev); |
| 3991 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3992 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3993 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3994 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3995 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3996 | |
| 3997 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3998 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3999 | hci_req_init(&req, hdev); |
| 4000 | update_eir(&req); |
| 4001 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4002 | |
| 4003 | hci_dev_unlock(hdev); |
| 4004 | |
| 4005 | return err; |
| 4006 | } |
| 4007 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4008 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 4009 | { |
| 4010 | struct cmd_lookup match = { NULL, hdev }; |
| 4011 | |
| 4012 | if (status) { |
| 4013 | u8 mgmt_err = mgmt_status(status); |
| 4014 | |
| 4015 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 4016 | cmd_status_rsp, &mgmt_err); |
| 4017 | return; |
| 4018 | } |
| 4019 | |
| 4020 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 4021 | &match); |
| 4022 | |
| 4023 | new_settings(hdev, match.sk); |
| 4024 | |
| 4025 | if (match.sk) |
| 4026 | sock_put(match.sk); |
| 4027 | } |
| 4028 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 4029 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4030 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4031 | { |
| 4032 | struct mgmt_mode *cp = data; |
| 4033 | struct pending_cmd *cmd; |
| 4034 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4035 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4036 | int err; |
| 4037 | |
| 4038 | BT_DBG("request for %s", hdev->name); |
| 4039 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4040 | status = mgmt_le_support(hdev); |
| 4041 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4042 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4043 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4044 | |
| 4045 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4046 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4047 | MGMT_STATUS_INVALID_PARAMS); |
| 4048 | |
| 4049 | hci_dev_lock(hdev); |
| 4050 | |
| 4051 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4052 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4053 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4054 | /* The following conditions are ones which mean that we should |
| 4055 | * not do any HCI communication but directly send a mgmt |
| 4056 | * response to user space (after toggling the flag if |
| 4057 | * necessary). |
| 4058 | */ |
| 4059 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 4060 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4061 | bool changed = false; |
| 4062 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4063 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 4064 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4065 | changed = true; |
| 4066 | } |
| 4067 | |
| 4068 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4069 | if (err < 0) |
| 4070 | goto unlock; |
| 4071 | |
| 4072 | if (changed) |
| 4073 | err = new_settings(hdev, sk); |
| 4074 | |
| 4075 | goto unlock; |
| 4076 | } |
| 4077 | |
| 4078 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4079 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 4080 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4081 | MGMT_STATUS_BUSY); |
| 4082 | goto unlock; |
| 4083 | } |
| 4084 | |
| 4085 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4086 | if (!cmd) { |
| 4087 | err = -ENOMEM; |
| 4088 | goto unlock; |
| 4089 | } |
| 4090 | |
| 4091 | hci_req_init(&req, hdev); |
| 4092 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4093 | if (val) |
| 4094 | enable_advertising(&req); |
| 4095 | else |
| 4096 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4097 | |
| 4098 | err = hci_req_run(&req, set_advertising_complete); |
| 4099 | if (err < 0) |
| 4100 | mgmt_pending_remove(cmd); |
| 4101 | |
| 4102 | unlock: |
| 4103 | hci_dev_unlock(hdev); |
| 4104 | return err; |
| 4105 | } |
| 4106 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4107 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4108 | void *data, u16 len) |
| 4109 | { |
| 4110 | struct mgmt_cp_set_static_address *cp = data; |
| 4111 | int err; |
| 4112 | |
| 4113 | BT_DBG("%s", hdev->name); |
| 4114 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4115 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4116 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4117 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4118 | |
| 4119 | if (hdev_is_powered(hdev)) |
| 4120 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4121 | MGMT_STATUS_REJECTED); |
| 4122 | |
| 4123 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4124 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 4125 | return cmd_status(sk, hdev->id, |
| 4126 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4127 | MGMT_STATUS_INVALID_PARAMS); |
| 4128 | |
| 4129 | /* Two most significant bits shall be set */ |
| 4130 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4131 | return cmd_status(sk, hdev->id, |
| 4132 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4133 | MGMT_STATUS_INVALID_PARAMS); |
| 4134 | } |
| 4135 | |
| 4136 | hci_dev_lock(hdev); |
| 4137 | |
| 4138 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4139 | |
| 4140 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4141 | |
| 4142 | hci_dev_unlock(hdev); |
| 4143 | |
| 4144 | return err; |
| 4145 | } |
| 4146 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4147 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4148 | void *data, u16 len) |
| 4149 | { |
| 4150 | struct mgmt_cp_set_scan_params *cp = data; |
| 4151 | __u16 interval, window; |
| 4152 | int err; |
| 4153 | |
| 4154 | BT_DBG("%s", hdev->name); |
| 4155 | |
| 4156 | if (!lmp_le_capable(hdev)) |
| 4157 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4158 | MGMT_STATUS_NOT_SUPPORTED); |
| 4159 | |
| 4160 | interval = __le16_to_cpu(cp->interval); |
| 4161 | |
| 4162 | if (interval < 0x0004 || interval > 0x4000) |
| 4163 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4164 | MGMT_STATUS_INVALID_PARAMS); |
| 4165 | |
| 4166 | window = __le16_to_cpu(cp->window); |
| 4167 | |
| 4168 | if (window < 0x0004 || window > 0x4000) |
| 4169 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4170 | MGMT_STATUS_INVALID_PARAMS); |
| 4171 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4172 | if (window > interval) |
| 4173 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4174 | MGMT_STATUS_INVALID_PARAMS); |
| 4175 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4176 | hci_dev_lock(hdev); |
| 4177 | |
| 4178 | hdev->le_scan_interval = interval; |
| 4179 | hdev->le_scan_window = window; |
| 4180 | |
| 4181 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4182 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4183 | /* If background scan is running, restart it so new parameters are |
| 4184 | * loaded. |
| 4185 | */ |
| 4186 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4187 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4188 | struct hci_request req; |
| 4189 | |
| 4190 | hci_req_init(&req, hdev); |
| 4191 | |
| 4192 | hci_req_add_le_scan_disable(&req); |
| 4193 | hci_req_add_le_passive_scan(&req); |
| 4194 | |
| 4195 | hci_req_run(&req, NULL); |
| 4196 | } |
| 4197 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4198 | hci_dev_unlock(hdev); |
| 4199 | |
| 4200 | return err; |
| 4201 | } |
| 4202 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4203 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4204 | { |
| 4205 | struct pending_cmd *cmd; |
| 4206 | |
| 4207 | BT_DBG("status 0x%02x", status); |
| 4208 | |
| 4209 | hci_dev_lock(hdev); |
| 4210 | |
| 4211 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4212 | if (!cmd) |
| 4213 | goto unlock; |
| 4214 | |
| 4215 | if (status) { |
| 4216 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4217 | mgmt_status(status)); |
| 4218 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4219 | struct mgmt_mode *cp = cmd->param; |
| 4220 | |
| 4221 | if (cp->val) |
| 4222 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4223 | else |
| 4224 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4225 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4226 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4227 | new_settings(hdev, cmd->sk); |
| 4228 | } |
| 4229 | |
| 4230 | mgmt_pending_remove(cmd); |
| 4231 | |
| 4232 | unlock: |
| 4233 | hci_dev_unlock(hdev); |
| 4234 | } |
| 4235 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4236 | 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] | 4237 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4238 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4239 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4240 | struct pending_cmd *cmd; |
| 4241 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4242 | int err; |
| 4243 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4244 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4245 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4246 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4247 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4248 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4249 | MGMT_STATUS_NOT_SUPPORTED); |
| 4250 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4251 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4252 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4253 | MGMT_STATUS_INVALID_PARAMS); |
| 4254 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4255 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4256 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4257 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4258 | |
| 4259 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4260 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4261 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4262 | |
| 4263 | hci_dev_lock(hdev); |
| 4264 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4265 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4266 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4267 | MGMT_STATUS_BUSY); |
| 4268 | goto unlock; |
| 4269 | } |
| 4270 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4271 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4272 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4273 | hdev); |
| 4274 | goto unlock; |
| 4275 | } |
| 4276 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4277 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4278 | data, len); |
| 4279 | if (!cmd) { |
| 4280 | err = -ENOMEM; |
| 4281 | goto unlock; |
| 4282 | } |
| 4283 | |
| 4284 | hci_req_init(&req, hdev); |
| 4285 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4286 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4287 | |
| 4288 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4289 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4290 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4291 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4292 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4293 | } |
| 4294 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4295 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4296 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4297 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4298 | return err; |
| 4299 | } |
| 4300 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4301 | static void set_bredr_scan(struct hci_request *req) |
| 4302 | { |
| 4303 | struct hci_dev *hdev = req->hdev; |
| 4304 | u8 scan = 0; |
| 4305 | |
| 4306 | /* Ensure that fast connectable is disabled. This function will |
| 4307 | * not do anything if the page scan parameters are already what |
| 4308 | * they should be. |
| 4309 | */ |
| 4310 | write_fast_connectable(req, false); |
| 4311 | |
| 4312 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4313 | scan |= SCAN_PAGE; |
| 4314 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4315 | scan |= SCAN_INQUIRY; |
| 4316 | |
| 4317 | if (scan) |
| 4318 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4319 | } |
| 4320 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4321 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4322 | { |
| 4323 | struct pending_cmd *cmd; |
| 4324 | |
| 4325 | BT_DBG("status 0x%02x", status); |
| 4326 | |
| 4327 | hci_dev_lock(hdev); |
| 4328 | |
| 4329 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4330 | if (!cmd) |
| 4331 | goto unlock; |
| 4332 | |
| 4333 | if (status) { |
| 4334 | u8 mgmt_err = mgmt_status(status); |
| 4335 | |
| 4336 | /* We need to restore the flag if related HCI commands |
| 4337 | * failed. |
| 4338 | */ |
| 4339 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4340 | |
| 4341 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4342 | } else { |
| 4343 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4344 | new_settings(hdev, cmd->sk); |
| 4345 | } |
| 4346 | |
| 4347 | mgmt_pending_remove(cmd); |
| 4348 | |
| 4349 | unlock: |
| 4350 | hci_dev_unlock(hdev); |
| 4351 | } |
| 4352 | |
| 4353 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4354 | { |
| 4355 | struct mgmt_mode *cp = data; |
| 4356 | struct pending_cmd *cmd; |
| 4357 | struct hci_request req; |
| 4358 | int err; |
| 4359 | |
| 4360 | BT_DBG("request for %s", hdev->name); |
| 4361 | |
| 4362 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4363 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4364 | MGMT_STATUS_NOT_SUPPORTED); |
| 4365 | |
| 4366 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4367 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4368 | MGMT_STATUS_REJECTED); |
| 4369 | |
| 4370 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4371 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4372 | MGMT_STATUS_INVALID_PARAMS); |
| 4373 | |
| 4374 | hci_dev_lock(hdev); |
| 4375 | |
| 4376 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4377 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4378 | goto unlock; |
| 4379 | } |
| 4380 | |
| 4381 | if (!hdev_is_powered(hdev)) { |
| 4382 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4383 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4384 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4385 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4386 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4387 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4388 | } |
| 4389 | |
| 4390 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4391 | |
| 4392 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4393 | if (err < 0) |
| 4394 | goto unlock; |
| 4395 | |
| 4396 | err = new_settings(hdev, sk); |
| 4397 | goto unlock; |
| 4398 | } |
| 4399 | |
| 4400 | /* Reject disabling when powered on */ |
| 4401 | if (!cp->val) { |
| 4402 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4403 | MGMT_STATUS_REJECTED); |
| 4404 | goto unlock; |
| 4405 | } |
| 4406 | |
| 4407 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4408 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4409 | MGMT_STATUS_BUSY); |
| 4410 | goto unlock; |
| 4411 | } |
| 4412 | |
| 4413 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4414 | if (!cmd) { |
| 4415 | err = -ENOMEM; |
| 4416 | goto unlock; |
| 4417 | } |
| 4418 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4419 | /* 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] | 4420 | * generates the correct flags. |
| 4421 | */ |
| 4422 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4423 | |
| 4424 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4425 | |
| 4426 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4427 | set_bredr_scan(&req); |
| 4428 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4429 | /* Since only the advertising data flags will change, there |
| 4430 | * is no need to update the scan response data. |
| 4431 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4432 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4433 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4434 | err = hci_req_run(&req, set_bredr_complete); |
| 4435 | if (err < 0) |
| 4436 | mgmt_pending_remove(cmd); |
| 4437 | |
| 4438 | unlock: |
| 4439 | hci_dev_unlock(hdev); |
| 4440 | return err; |
| 4441 | } |
| 4442 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4443 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4444 | void *data, u16 len) |
| 4445 | { |
| 4446 | struct mgmt_mode *cp = data; |
| 4447 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4448 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4449 | int err; |
| 4450 | |
| 4451 | BT_DBG("request for %s", hdev->name); |
| 4452 | |
| 4453 | status = mgmt_bredr_support(hdev); |
| 4454 | if (status) |
| 4455 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4456 | status); |
| 4457 | |
Marcel Holtmann | 5afeac14 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4458 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4459 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4460 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4461 | MGMT_STATUS_NOT_SUPPORTED); |
| 4462 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4463 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4464 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4465 | MGMT_STATUS_INVALID_PARAMS); |
| 4466 | |
| 4467 | hci_dev_lock(hdev); |
| 4468 | |
| 4469 | if (!hdev_is_powered(hdev)) { |
| 4470 | bool changed; |
| 4471 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4472 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4473 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4474 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4475 | if (cp->val == 0x02) |
| 4476 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4477 | else |
| 4478 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4479 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4480 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4481 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4482 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4483 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4484 | |
| 4485 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4486 | if (err < 0) |
| 4487 | goto failed; |
| 4488 | |
| 4489 | if (changed) |
| 4490 | err = new_settings(hdev, sk); |
| 4491 | |
| 4492 | goto failed; |
| 4493 | } |
| 4494 | |
| 4495 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4496 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4497 | MGMT_STATUS_BUSY); |
| 4498 | goto failed; |
| 4499 | } |
| 4500 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4501 | val = !!cp->val; |
| 4502 | |
| 4503 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4504 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4505 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4506 | goto failed; |
| 4507 | } |
| 4508 | |
| 4509 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4510 | if (!cmd) { |
| 4511 | err = -ENOMEM; |
| 4512 | goto failed; |
| 4513 | } |
| 4514 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4515 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4516 | if (err < 0) { |
| 4517 | mgmt_pending_remove(cmd); |
| 4518 | goto failed; |
| 4519 | } |
| 4520 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4521 | if (cp->val == 0x02) |
| 4522 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4523 | else |
| 4524 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4525 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4526 | failed: |
| 4527 | hci_dev_unlock(hdev); |
| 4528 | return err; |
| 4529 | } |
| 4530 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4531 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4532 | void *data, u16 len) |
| 4533 | { |
| 4534 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4535 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4536 | int err; |
| 4537 | |
| 4538 | BT_DBG("request for %s", hdev->name); |
| 4539 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4540 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4541 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4542 | MGMT_STATUS_INVALID_PARAMS); |
| 4543 | |
| 4544 | hci_dev_lock(hdev); |
| 4545 | |
| 4546 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4547 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4548 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4549 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4550 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4551 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4552 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4553 | if (cp->val == 0x02) |
| 4554 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4555 | &hdev->dev_flags); |
| 4556 | else |
| 4557 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4558 | &hdev->dev_flags); |
| 4559 | |
| 4560 | if (hdev_is_powered(hdev) && use_changed && |
| 4561 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4562 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4563 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4564 | sizeof(mode), &mode); |
| 4565 | } |
| 4566 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4567 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4568 | if (err < 0) |
| 4569 | goto unlock; |
| 4570 | |
| 4571 | if (changed) |
| 4572 | err = new_settings(hdev, sk); |
| 4573 | |
| 4574 | unlock: |
| 4575 | hci_dev_unlock(hdev); |
| 4576 | return err; |
| 4577 | } |
| 4578 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4579 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4580 | u16 len) |
| 4581 | { |
| 4582 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4583 | bool changed; |
| 4584 | int err; |
| 4585 | |
| 4586 | BT_DBG("request for %s", hdev->name); |
| 4587 | |
| 4588 | if (!lmp_le_capable(hdev)) |
| 4589 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4590 | MGMT_STATUS_NOT_SUPPORTED); |
| 4591 | |
| 4592 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4593 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4594 | MGMT_STATUS_INVALID_PARAMS); |
| 4595 | |
| 4596 | if (hdev_is_powered(hdev)) |
| 4597 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4598 | MGMT_STATUS_REJECTED); |
| 4599 | |
| 4600 | hci_dev_lock(hdev); |
| 4601 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4602 | /* If user space supports this command it is also expected to |
| 4603 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4604 | */ |
| 4605 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4606 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4607 | if (cp->privacy) { |
| 4608 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4609 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4610 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4611 | } else { |
| 4612 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4613 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4614 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4615 | } |
| 4616 | |
| 4617 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4618 | if (err < 0) |
| 4619 | goto unlock; |
| 4620 | |
| 4621 | if (changed) |
| 4622 | err = new_settings(hdev, sk); |
| 4623 | |
| 4624 | unlock: |
| 4625 | hci_dev_unlock(hdev); |
| 4626 | return err; |
| 4627 | } |
| 4628 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4629 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4630 | { |
| 4631 | switch (irk->addr.type) { |
| 4632 | case BDADDR_LE_PUBLIC: |
| 4633 | return true; |
| 4634 | |
| 4635 | case BDADDR_LE_RANDOM: |
| 4636 | /* Two most significant bits shall be set */ |
| 4637 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4638 | return false; |
| 4639 | return true; |
| 4640 | } |
| 4641 | |
| 4642 | return false; |
| 4643 | } |
| 4644 | |
| 4645 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4646 | u16 len) |
| 4647 | { |
| 4648 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4649 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4650 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4651 | u16 irk_count, expected_len; |
| 4652 | int i, err; |
| 4653 | |
| 4654 | BT_DBG("request for %s", hdev->name); |
| 4655 | |
| 4656 | if (!lmp_le_capable(hdev)) |
| 4657 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4658 | MGMT_STATUS_NOT_SUPPORTED); |
| 4659 | |
| 4660 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4661 | if (irk_count > max_irk_count) { |
| 4662 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
| 4663 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4664 | MGMT_STATUS_INVALID_PARAMS); |
| 4665 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4666 | |
| 4667 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4668 | if (expected_len != len) { |
| 4669 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4670 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4671 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4672 | MGMT_STATUS_INVALID_PARAMS); |
| 4673 | } |
| 4674 | |
| 4675 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4676 | |
| 4677 | for (i = 0; i < irk_count; i++) { |
| 4678 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4679 | |
| 4680 | if (!irk_is_valid(key)) |
| 4681 | return cmd_status(sk, hdev->id, |
| 4682 | MGMT_OP_LOAD_IRKS, |
| 4683 | MGMT_STATUS_INVALID_PARAMS); |
| 4684 | } |
| 4685 | |
| 4686 | hci_dev_lock(hdev); |
| 4687 | |
| 4688 | hci_smp_irks_clear(hdev); |
| 4689 | |
| 4690 | for (i = 0; i < irk_count; i++) { |
| 4691 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4692 | u8 addr_type; |
| 4693 | |
| 4694 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4695 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4696 | else |
| 4697 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4698 | |
| 4699 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4700 | BDADDR_ANY); |
| 4701 | } |
| 4702 | |
| 4703 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4704 | |
| 4705 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4706 | |
| 4707 | hci_dev_unlock(hdev); |
| 4708 | |
| 4709 | return err; |
| 4710 | } |
| 4711 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4712 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4713 | { |
| 4714 | if (key->master != 0x00 && key->master != 0x01) |
| 4715 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4716 | |
| 4717 | switch (key->addr.type) { |
| 4718 | case BDADDR_LE_PUBLIC: |
| 4719 | return true; |
| 4720 | |
| 4721 | case BDADDR_LE_RANDOM: |
| 4722 | /* Two most significant bits shall be set */ |
| 4723 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4724 | return false; |
| 4725 | return true; |
| 4726 | } |
| 4727 | |
| 4728 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4729 | } |
| 4730 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4731 | 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] | 4732 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4733 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4734 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4735 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4736 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4737 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4738 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4739 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4740 | BT_DBG("request for %s", hdev->name); |
| 4741 | |
| 4742 | if (!lmp_le_capable(hdev)) |
| 4743 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4744 | MGMT_STATUS_NOT_SUPPORTED); |
| 4745 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4746 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4747 | if (key_count > max_key_count) { |
| 4748 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
| 4749 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4750 | MGMT_STATUS_INVALID_PARAMS); |
| 4751 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4752 | |
| 4753 | expected_len = sizeof(*cp) + key_count * |
| 4754 | sizeof(struct mgmt_ltk_info); |
| 4755 | if (expected_len != len) { |
| 4756 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4757 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4758 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4759 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4760 | } |
| 4761 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4762 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4763 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4764 | for (i = 0; i < key_count; i++) { |
| 4765 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4766 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4767 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4768 | return cmd_status(sk, hdev->id, |
| 4769 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4770 | MGMT_STATUS_INVALID_PARAMS); |
| 4771 | } |
| 4772 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4773 | hci_dev_lock(hdev); |
| 4774 | |
| 4775 | hci_smp_ltks_clear(hdev); |
| 4776 | |
| 4777 | for (i = 0; i < key_count; i++) { |
| 4778 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4779 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4780 | |
| 4781 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4782 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4783 | else |
| 4784 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4785 | |
| 4786 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4787 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4788 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4789 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4790 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4791 | switch (key->type) { |
| 4792 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4793 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4794 | break; |
| 4795 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4796 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4797 | break; |
| 4798 | default: |
| 4799 | continue; |
| 4800 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4801 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4802 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4803 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4804 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4805 | } |
| 4806 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4807 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4808 | NULL, 0); |
| 4809 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4810 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4811 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4812 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4813 | } |
| 4814 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4815 | struct cmd_conn_lookup { |
| 4816 | struct hci_conn *conn; |
| 4817 | bool valid_tx_power; |
| 4818 | u8 mgmt_status; |
| 4819 | }; |
| 4820 | |
| 4821 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4822 | { |
| 4823 | struct cmd_conn_lookup *match = data; |
| 4824 | struct mgmt_cp_get_conn_info *cp; |
| 4825 | struct mgmt_rp_get_conn_info rp; |
| 4826 | struct hci_conn *conn = cmd->user_data; |
| 4827 | |
| 4828 | if (conn != match->conn) |
| 4829 | return; |
| 4830 | |
| 4831 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4832 | |
| 4833 | memset(&rp, 0, sizeof(rp)); |
| 4834 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4835 | rp.addr.type = cp->addr.type; |
| 4836 | |
| 4837 | if (!match->mgmt_status) { |
| 4838 | rp.rssi = conn->rssi; |
| 4839 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4840 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4841 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4842 | rp.max_tx_power = conn->max_tx_power; |
| 4843 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4844 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4845 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4846 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4847 | } |
| 4848 | |
| 4849 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4850 | match->mgmt_status, &rp, sizeof(rp)); |
| 4851 | |
| 4852 | hci_conn_drop(conn); |
| 4853 | |
| 4854 | mgmt_pending_remove(cmd); |
| 4855 | } |
| 4856 | |
| 4857 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4858 | { |
| 4859 | struct hci_cp_read_rssi *cp; |
| 4860 | struct hci_conn *conn; |
| 4861 | struct cmd_conn_lookup match; |
| 4862 | u16 handle; |
| 4863 | |
| 4864 | BT_DBG("status 0x%02x", status); |
| 4865 | |
| 4866 | hci_dev_lock(hdev); |
| 4867 | |
| 4868 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4869 | * otherwise we assume it's not valid. At the moment we assume that |
| 4870 | * either both or none of current and max values are valid to keep code |
| 4871 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4872 | */ |
| 4873 | match.valid_tx_power = !status; |
| 4874 | |
| 4875 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4876 | * Level so we check which one was last sent to retrieve connection |
| 4877 | * handle. Both commands have handle as first parameter so it's safe to |
| 4878 | * cast data on the same command struct. |
| 4879 | * |
| 4880 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4881 | * In other case we simply override error to indicate success as we |
| 4882 | * already remembered if TX power value is actually valid. |
| 4883 | */ |
| 4884 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4885 | if (!cp) { |
| 4886 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4887 | status = 0; |
| 4888 | } |
| 4889 | |
| 4890 | if (!cp) { |
| 4891 | BT_ERR("invalid sent_cmd in response"); |
| 4892 | goto unlock; |
| 4893 | } |
| 4894 | |
| 4895 | handle = __le16_to_cpu(cp->handle); |
| 4896 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4897 | if (!conn) { |
| 4898 | BT_ERR("unknown handle (%d) in response", handle); |
| 4899 | goto unlock; |
| 4900 | } |
| 4901 | |
| 4902 | match.conn = conn; |
| 4903 | match.mgmt_status = mgmt_status(status); |
| 4904 | |
| 4905 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4906 | * connection only. |
| 4907 | */ |
| 4908 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4909 | get_conn_info_complete, &match); |
| 4910 | |
| 4911 | unlock: |
| 4912 | hci_dev_unlock(hdev); |
| 4913 | } |
| 4914 | |
| 4915 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4916 | u16 len) |
| 4917 | { |
| 4918 | struct mgmt_cp_get_conn_info *cp = data; |
| 4919 | struct mgmt_rp_get_conn_info rp; |
| 4920 | struct hci_conn *conn; |
| 4921 | unsigned long conn_info_age; |
| 4922 | int err = 0; |
| 4923 | |
| 4924 | BT_DBG("%s", hdev->name); |
| 4925 | |
| 4926 | memset(&rp, 0, sizeof(rp)); |
| 4927 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4928 | rp.addr.type = cp->addr.type; |
| 4929 | |
| 4930 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4931 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4932 | MGMT_STATUS_INVALID_PARAMS, |
| 4933 | &rp, sizeof(rp)); |
| 4934 | |
| 4935 | hci_dev_lock(hdev); |
| 4936 | |
| 4937 | if (!hdev_is_powered(hdev)) { |
| 4938 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4939 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4940 | goto unlock; |
| 4941 | } |
| 4942 | |
| 4943 | if (cp->addr.type == BDADDR_BREDR) |
| 4944 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4945 | &cp->addr.bdaddr); |
| 4946 | else |
| 4947 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4948 | |
| 4949 | if (!conn || conn->state != BT_CONNECTED) { |
| 4950 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4951 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4952 | goto unlock; |
| 4953 | } |
| 4954 | |
| 4955 | /* To avoid client trying to guess when to poll again for information we |
| 4956 | * calculate conn info age as random value between min/max set in hdev. |
| 4957 | */ |
| 4958 | conn_info_age = hdev->conn_info_min_age + |
| 4959 | prandom_u32_max(hdev->conn_info_max_age - |
| 4960 | hdev->conn_info_min_age); |
| 4961 | |
| 4962 | /* Query controller to refresh cached values if they are too old or were |
| 4963 | * never read. |
| 4964 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4965 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4966 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4967 | !conn->conn_info_timestamp) { |
| 4968 | struct hci_request req; |
| 4969 | struct hci_cp_read_tx_power req_txp_cp; |
| 4970 | struct hci_cp_read_rssi req_rssi_cp; |
| 4971 | struct pending_cmd *cmd; |
| 4972 | |
| 4973 | hci_req_init(&req, hdev); |
| 4974 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4975 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4976 | &req_rssi_cp); |
| 4977 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4978 | /* For LE links TX power does not change thus we don't need to |
| 4979 | * query for it once value is known. |
| 4980 | */ |
| 4981 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4982 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4983 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4984 | req_txp_cp.type = 0x00; |
| 4985 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4986 | sizeof(req_txp_cp), &req_txp_cp); |
| 4987 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4988 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4989 | /* Max TX power needs to be read only once per connection */ |
| 4990 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4991 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4992 | req_txp_cp.type = 0x01; |
| 4993 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4994 | sizeof(req_txp_cp), &req_txp_cp); |
| 4995 | } |
| 4996 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4997 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4998 | if (err < 0) |
| 4999 | goto unlock; |
| 5000 | |
| 5001 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 5002 | data, len); |
| 5003 | if (!cmd) { |
| 5004 | err = -ENOMEM; |
| 5005 | goto unlock; |
| 5006 | } |
| 5007 | |
| 5008 | hci_conn_hold(conn); |
| 5009 | cmd->user_data = conn; |
| 5010 | |
| 5011 | conn->conn_info_timestamp = jiffies; |
| 5012 | } else { |
| 5013 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 5014 | rp.rssi = conn->rssi; |
| 5015 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 5016 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5017 | |
| 5018 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 5019 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 5020 | } |
| 5021 | |
| 5022 | unlock: |
| 5023 | hci_dev_unlock(hdev); |
| 5024 | return err; |
| 5025 | } |
| 5026 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5027 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 5028 | { |
| 5029 | struct mgmt_cp_get_clock_info *cp; |
| 5030 | struct mgmt_rp_get_clock_info rp; |
| 5031 | struct hci_cp_read_clock *hci_cp; |
| 5032 | struct pending_cmd *cmd; |
| 5033 | struct hci_conn *conn; |
| 5034 | |
| 5035 | BT_DBG("%s status %u", hdev->name, status); |
| 5036 | |
| 5037 | hci_dev_lock(hdev); |
| 5038 | |
| 5039 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 5040 | if (!hci_cp) |
| 5041 | goto unlock; |
| 5042 | |
| 5043 | if (hci_cp->which) { |
| 5044 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5045 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5046 | } else { |
| 5047 | conn = NULL; |
| 5048 | } |
| 5049 | |
| 5050 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5051 | if (!cmd) |
| 5052 | goto unlock; |
| 5053 | |
| 5054 | cp = cmd->param; |
| 5055 | |
| 5056 | memset(&rp, 0, sizeof(rp)); |
| 5057 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 5058 | |
| 5059 | if (status) |
| 5060 | goto send_rsp; |
| 5061 | |
| 5062 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5063 | |
| 5064 | if (conn) { |
| 5065 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5066 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5067 | } |
| 5068 | |
| 5069 | send_rsp: |
| 5070 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 5071 | &rp, sizeof(rp)); |
| 5072 | mgmt_pending_remove(cmd); |
| 5073 | if (conn) |
| 5074 | hci_conn_drop(conn); |
| 5075 | |
| 5076 | unlock: |
| 5077 | hci_dev_unlock(hdev); |
| 5078 | } |
| 5079 | |
| 5080 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5081 | u16 len) |
| 5082 | { |
| 5083 | struct mgmt_cp_get_clock_info *cp = data; |
| 5084 | struct mgmt_rp_get_clock_info rp; |
| 5085 | struct hci_cp_read_clock hci_cp; |
| 5086 | struct pending_cmd *cmd; |
| 5087 | struct hci_request req; |
| 5088 | struct hci_conn *conn; |
| 5089 | int err; |
| 5090 | |
| 5091 | BT_DBG("%s", hdev->name); |
| 5092 | |
| 5093 | memset(&rp, 0, sizeof(rp)); |
| 5094 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5095 | rp.addr.type = cp->addr.type; |
| 5096 | |
| 5097 | if (cp->addr.type != BDADDR_BREDR) |
| 5098 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5099 | MGMT_STATUS_INVALID_PARAMS, |
| 5100 | &rp, sizeof(rp)); |
| 5101 | |
| 5102 | hci_dev_lock(hdev); |
| 5103 | |
| 5104 | if (!hdev_is_powered(hdev)) { |
| 5105 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5106 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5107 | goto unlock; |
| 5108 | } |
| 5109 | |
| 5110 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5111 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5112 | &cp->addr.bdaddr); |
| 5113 | if (!conn || conn->state != BT_CONNECTED) { |
| 5114 | err = cmd_complete(sk, hdev->id, |
| 5115 | MGMT_OP_GET_CLOCK_INFO, |
| 5116 | MGMT_STATUS_NOT_CONNECTED, |
| 5117 | &rp, sizeof(rp)); |
| 5118 | goto unlock; |
| 5119 | } |
| 5120 | } else { |
| 5121 | conn = NULL; |
| 5122 | } |
| 5123 | |
| 5124 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5125 | if (!cmd) { |
| 5126 | err = -ENOMEM; |
| 5127 | goto unlock; |
| 5128 | } |
| 5129 | |
| 5130 | hci_req_init(&req, hdev); |
| 5131 | |
| 5132 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5133 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5134 | |
| 5135 | if (conn) { |
| 5136 | hci_conn_hold(conn); |
| 5137 | cmd->user_data = conn; |
| 5138 | |
| 5139 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5140 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5141 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5142 | } |
| 5143 | |
| 5144 | err = hci_req_run(&req, get_clock_info_complete); |
| 5145 | if (err < 0) |
| 5146 | mgmt_pending_remove(cmd); |
| 5147 | |
| 5148 | unlock: |
| 5149 | hci_dev_unlock(hdev); |
| 5150 | return err; |
| 5151 | } |
| 5152 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5153 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5154 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5155 | { |
| 5156 | struct mgmt_ev_device_added ev; |
| 5157 | |
| 5158 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5159 | ev.addr.type = type; |
| 5160 | ev.action = action; |
| 5161 | |
| 5162 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5163 | } |
| 5164 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5165 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5166 | void *data, u16 len) |
| 5167 | { |
| 5168 | struct mgmt_cp_add_device *cp = data; |
| 5169 | u8 auto_conn, addr_type; |
| 5170 | int err; |
| 5171 | |
| 5172 | BT_DBG("%s", hdev->name); |
| 5173 | |
| 5174 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5175 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5176 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5177 | MGMT_STATUS_INVALID_PARAMS, |
| 5178 | &cp->addr, sizeof(cp->addr)); |
| 5179 | |
| 5180 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5181 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5182 | MGMT_STATUS_INVALID_PARAMS, |
| 5183 | &cp->addr, sizeof(cp->addr)); |
| 5184 | |
| 5185 | hci_dev_lock(hdev); |
| 5186 | |
| 5187 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5188 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5189 | else |
| 5190 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5191 | |
| 5192 | if (cp->action) |
| 5193 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5194 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5195 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5196 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5197 | /* If the connection parameters don't exist for this device, |
| 5198 | * they will be created and configured with defaults. |
| 5199 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5200 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5201 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5202 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5203 | MGMT_STATUS_FAILED, |
| 5204 | &cp->addr, sizeof(cp->addr)); |
| 5205 | goto unlock; |
| 5206 | } |
| 5207 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5208 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5209 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5210 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5211 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5212 | |
| 5213 | unlock: |
| 5214 | hci_dev_unlock(hdev); |
| 5215 | return err; |
| 5216 | } |
| 5217 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5218 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5219 | bdaddr_t *bdaddr, u8 type) |
| 5220 | { |
| 5221 | struct mgmt_ev_device_removed ev; |
| 5222 | |
| 5223 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5224 | ev.addr.type = type; |
| 5225 | |
| 5226 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5227 | } |
| 5228 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5229 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5230 | void *data, u16 len) |
| 5231 | { |
| 5232 | struct mgmt_cp_remove_device *cp = data; |
| 5233 | int err; |
| 5234 | |
| 5235 | BT_DBG("%s", hdev->name); |
| 5236 | |
| 5237 | hci_dev_lock(hdev); |
| 5238 | |
| 5239 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5240 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5241 | u8 addr_type; |
| 5242 | |
| 5243 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5244 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5245 | MGMT_STATUS_INVALID_PARAMS, |
| 5246 | &cp->addr, sizeof(cp->addr)); |
| 5247 | goto unlock; |
| 5248 | } |
| 5249 | |
| 5250 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5251 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5252 | else |
| 5253 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5254 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5255 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5256 | addr_type); |
| 5257 | if (!params) { |
| 5258 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5259 | MGMT_STATUS_INVALID_PARAMS, |
| 5260 | &cp->addr, sizeof(cp->addr)); |
| 5261 | goto unlock; |
| 5262 | } |
| 5263 | |
| 5264 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 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 | |
Johan Hedberg | d1dbf12 | 2014-07-04 16:17:23 +0300 | [diff] [blame] | 5271 | list_del(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5272 | list_del(¶ms->list); |
| 5273 | kfree(params); |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame] | 5274 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5275 | |
| 5276 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5277 | } else { |
| 5278 | if (cp->addr.type) { |
| 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 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5285 | hci_conn_params_clear_enabled(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5289 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5290 | |
| 5291 | unlock: |
| 5292 | hci_dev_unlock(hdev); |
| 5293 | return err; |
| 5294 | } |
| 5295 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5296 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5297 | u16 len) |
| 5298 | { |
| 5299 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5300 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5301 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5302 | u16 param_count, expected_len; |
| 5303 | int i; |
| 5304 | |
| 5305 | if (!lmp_le_capable(hdev)) |
| 5306 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5307 | MGMT_STATUS_NOT_SUPPORTED); |
| 5308 | |
| 5309 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5310 | if (param_count > max_param_count) { |
| 5311 | BT_ERR("load_conn_param: too big param_count value %u", |
| 5312 | param_count); |
| 5313 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5314 | MGMT_STATUS_INVALID_PARAMS); |
| 5315 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5316 | |
| 5317 | expected_len = sizeof(*cp) + param_count * |
| 5318 | sizeof(struct mgmt_conn_param); |
| 5319 | if (expected_len != len) { |
| 5320 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5321 | expected_len, len); |
| 5322 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5323 | MGMT_STATUS_INVALID_PARAMS); |
| 5324 | } |
| 5325 | |
| 5326 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5327 | |
| 5328 | hci_dev_lock(hdev); |
| 5329 | |
| 5330 | hci_conn_params_clear_disabled(hdev); |
| 5331 | |
| 5332 | for (i = 0; i < param_count; i++) { |
| 5333 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5334 | struct hci_conn_params *hci_param; |
| 5335 | u16 min, max, latency, timeout; |
| 5336 | u8 addr_type; |
| 5337 | |
| 5338 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5339 | param->addr.type); |
| 5340 | |
| 5341 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5342 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5343 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5344 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5345 | } else { |
| 5346 | BT_ERR("Ignoring invalid connection parameters"); |
| 5347 | continue; |
| 5348 | } |
| 5349 | |
| 5350 | min = le16_to_cpu(param->min_interval); |
| 5351 | max = le16_to_cpu(param->max_interval); |
| 5352 | latency = le16_to_cpu(param->latency); |
| 5353 | timeout = le16_to_cpu(param->timeout); |
| 5354 | |
| 5355 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5356 | min, max, latency, timeout); |
| 5357 | |
| 5358 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5359 | BT_ERR("Ignoring invalid connection parameters"); |
| 5360 | continue; |
| 5361 | } |
| 5362 | |
| 5363 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5364 | addr_type); |
| 5365 | if (!hci_param) { |
| 5366 | BT_ERR("Failed to add connection parameters"); |
| 5367 | continue; |
| 5368 | } |
| 5369 | |
| 5370 | hci_param->conn_min_interval = min; |
| 5371 | hci_param->conn_max_interval = max; |
| 5372 | hci_param->conn_latency = latency; |
| 5373 | hci_param->supervision_timeout = timeout; |
| 5374 | } |
| 5375 | |
| 5376 | hci_dev_unlock(hdev); |
| 5377 | |
| 5378 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5379 | } |
| 5380 | |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame^] | 5381 | static int set_external_config(struct sock *sk, struct hci_dev *hdev, |
| 5382 | void *data, u16 len) |
| 5383 | { |
| 5384 | struct mgmt_cp_set_external_config *cp = data; |
| 5385 | bool changed; |
| 5386 | int err; |
| 5387 | |
| 5388 | BT_DBG("%s", hdev->name); |
| 5389 | |
| 5390 | if (hdev_is_powered(hdev)) |
| 5391 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5392 | MGMT_STATUS_REJECTED); |
| 5393 | |
| 5394 | if (cp->config != 0x00 && cp->config != 0x01) |
| 5395 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5396 | MGMT_STATUS_INVALID_PARAMS); |
| 5397 | |
| 5398 | if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) |
| 5399 | return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, |
| 5400 | MGMT_STATUS_NOT_SUPPORTED); |
| 5401 | |
| 5402 | hci_dev_lock(hdev); |
| 5403 | |
| 5404 | if (cp->config) |
| 5405 | changed = !test_and_set_bit(HCI_EXT_CONFIGURED, |
| 5406 | &hdev->dev_flags); |
| 5407 | else |
| 5408 | changed = test_and_clear_bit(HCI_EXT_CONFIGURED, |
| 5409 | &hdev->dev_flags); |
| 5410 | |
| 5411 | err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); |
| 5412 | if (err < 0) |
| 5413 | goto unlock; |
| 5414 | |
| 5415 | if (!changed) |
| 5416 | goto unlock; |
| 5417 | |
| 5418 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) { |
| 5419 | mgmt_index_removed(hdev); |
| 5420 | change_bit(HCI_UNCONFIGURED, &hdev->dev_flags); |
| 5421 | mgmt_index_added(hdev); |
| 5422 | } |
| 5423 | |
| 5424 | unlock: |
| 5425 | hci_dev_unlock(hdev); |
| 5426 | return err; |
| 5427 | } |
| 5428 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5429 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5430 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5431 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5432 | bool var_len; |
| 5433 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5434 | } mgmt_handlers[] = { |
| 5435 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5436 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5437 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5438 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5439 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5440 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5441 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5442 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5443 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5444 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5445 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5446 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5447 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5448 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5449 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5450 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5451 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5452 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5453 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5454 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5455 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5456 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5457 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5458 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5459 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5460 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5461 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5462 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5463 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5464 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5465 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5466 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5467 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5468 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5469 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5470 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5471 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5472 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5473 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5474 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5475 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5476 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5477 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5478 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5479 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5480 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5481 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5482 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5483 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5484 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5485 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5486 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5487 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5488 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
| 5489 | { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE }, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 5490 | { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE }, |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame^] | 5491 | { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5492 | }; |
| 5493 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5494 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5495 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5496 | void *buf; |
| 5497 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5498 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5499 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5500 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5501 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5502 | int err; |
| 5503 | |
| 5504 | BT_DBG("got %zu bytes", msglen); |
| 5505 | |
| 5506 | if (msglen < sizeof(*hdr)) |
| 5507 | return -EINVAL; |
| 5508 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5509 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5510 | if (!buf) |
| 5511 | return -ENOMEM; |
| 5512 | |
| 5513 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5514 | err = -EFAULT; |
| 5515 | goto done; |
| 5516 | } |
| 5517 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5518 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5519 | opcode = __le16_to_cpu(hdr->opcode); |
| 5520 | index = __le16_to_cpu(hdr->index); |
| 5521 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5522 | |
| 5523 | if (len != msglen - sizeof(*hdr)) { |
| 5524 | err = -EINVAL; |
| 5525 | goto done; |
| 5526 | } |
| 5527 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5528 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5529 | hdev = hci_dev_get(index); |
| 5530 | if (!hdev) { |
| 5531 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5532 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5533 | goto done; |
| 5534 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5535 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5536 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5537 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5538 | err = cmd_status(sk, index, opcode, |
| 5539 | MGMT_STATUS_INVALID_INDEX); |
| 5540 | goto done; |
| 5541 | } |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5542 | |
| 5543 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && |
Marcel Holtmann | dbece37 | 2014-07-04 18:11:55 +0200 | [diff] [blame^] | 5544 | opcode != MGMT_OP_READ_CONFIG_INFO && |
| 5545 | opcode != MGMT_OP_SET_EXTERNAL_CONFIG) { |
Marcel Holtmann | 42a9bc14 | 2014-07-04 16:54:40 +0200 | [diff] [blame] | 5546 | err = cmd_status(sk, index, opcode, |
| 5547 | MGMT_STATUS_INVALID_INDEX); |
| 5548 | goto done; |
| 5549 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5550 | } |
| 5551 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5552 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5553 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5554 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5555 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5556 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5557 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5558 | } |
| 5559 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5560 | if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || |
| 5561 | opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
| 5562 | err = cmd_status(sk, index, opcode, |
| 5563 | MGMT_STATUS_INVALID_INDEX); |
| 5564 | goto done; |
| 5565 | } |
| 5566 | |
| 5567 | if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && |
| 5568 | opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5569 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5570 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5571 | goto done; |
| 5572 | } |
| 5573 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5574 | handler = &mgmt_handlers[opcode]; |
| 5575 | |
| 5576 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5577 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5578 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5579 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5580 | goto done; |
| 5581 | } |
| 5582 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5583 | if (hdev) |
| 5584 | mgmt_init_hdev(sk, hdev); |
| 5585 | |
| 5586 | cp = buf + sizeof(*hdr); |
| 5587 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5588 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5589 | if (err < 0) |
| 5590 | goto done; |
| 5591 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5592 | err = msglen; |
| 5593 | |
| 5594 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5595 | if (hdev) |
| 5596 | hci_dev_put(hdev); |
| 5597 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5598 | kfree(buf); |
| 5599 | return err; |
| 5600 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5601 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5602 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5603 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5604 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5605 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5606 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5607 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5608 | return; |
| 5609 | |
| 5610 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5611 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5612 | else |
| 5613 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5614 | } |
| 5615 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5616 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5617 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5618 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5619 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5620 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5621 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5622 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5623 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5624 | return; |
| 5625 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5626 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5627 | |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 5628 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5629 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5630 | else |
| 5631 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5632 | } |
| 5633 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5634 | /* This function requires the caller holds hdev->lock */ |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5635 | static void restart_le_actions(struct hci_dev *hdev) |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5636 | { |
| 5637 | struct hci_conn_params *p; |
| 5638 | |
| 5639 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5640 | /* Needed for AUTO_OFF case where might not "really" |
| 5641 | * have been powered off. |
| 5642 | */ |
| 5643 | list_del_init(&p->action); |
| 5644 | |
| 5645 | switch (p->auto_connect) { |
| 5646 | case HCI_AUTO_CONN_ALWAYS: |
| 5647 | list_add(&p->action, &hdev->pend_le_conns); |
| 5648 | break; |
| 5649 | case HCI_AUTO_CONN_REPORT: |
| 5650 | list_add(&p->action, &hdev->pend_le_reports); |
| 5651 | break; |
| 5652 | default: |
| 5653 | break; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5654 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5655 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5656 | |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5657 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5658 | } |
| 5659 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5660 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5661 | { |
| 5662 | struct cmd_lookup match = { NULL, hdev }; |
| 5663 | |
| 5664 | BT_DBG("status 0x%02x", status); |
| 5665 | |
| 5666 | hci_dev_lock(hdev); |
| 5667 | |
Johan Hedberg | d7347f3 | 2014-07-04 12:37:23 +0300 | [diff] [blame] | 5668 | restart_le_actions(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5669 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5670 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5671 | |
| 5672 | new_settings(hdev, match.sk); |
| 5673 | |
| 5674 | hci_dev_unlock(hdev); |
| 5675 | |
| 5676 | if (match.sk) |
| 5677 | sock_put(match.sk); |
| 5678 | } |
| 5679 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5680 | static int powered_update_hci(struct hci_dev *hdev) |
| 5681 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5682 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5683 | u8 link_sec; |
| 5684 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5685 | hci_req_init(&req, hdev); |
| 5686 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5687 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5688 | !lmp_host_ssp_capable(hdev)) { |
| 5689 | u8 ssp = 1; |
| 5690 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5691 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5692 | } |
| 5693 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5694 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5695 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5696 | struct hci_cp_write_le_host_supported cp; |
| 5697 | |
| 5698 | cp.le = 1; |
| 5699 | cp.simul = lmp_le_br_capable(hdev); |
| 5700 | |
| 5701 | /* Check first if we already have the right |
| 5702 | * host state (host features set) |
| 5703 | */ |
| 5704 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5705 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5706 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5707 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5708 | } |
| 5709 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5710 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5711 | /* Make sure the controller has a good default for |
| 5712 | * advertising data. This also applies to the case |
| 5713 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5714 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5715 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5716 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5717 | update_scan_rsp_data(&req); |
| 5718 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5719 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5720 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5721 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5722 | } |
| 5723 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5724 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5725 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5726 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5727 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5728 | |
| 5729 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5730 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5731 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5732 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5733 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5734 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5735 | } |
| 5736 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5737 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5738 | } |
| 5739 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5740 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5741 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5742 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5743 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5744 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5745 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5746 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5747 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5748 | return 0; |
| 5749 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5750 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5751 | if (powered_update_hci(hdev) == 0) |
| 5752 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5753 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5754 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5755 | &match); |
| 5756 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5757 | } |
| 5758 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5759 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5760 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5761 | |
| 5762 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5763 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5764 | zero_cod, sizeof(zero_cod), NULL); |
| 5765 | |
| 5766 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5767 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5768 | |
| 5769 | if (match.sk) |
| 5770 | sock_put(match.sk); |
| 5771 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5772 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5773 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5774 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5775 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5776 | { |
| 5777 | struct pending_cmd *cmd; |
| 5778 | u8 status; |
| 5779 | |
| 5780 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5781 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5782 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5783 | |
| 5784 | if (err == -ERFKILL) |
| 5785 | status = MGMT_STATUS_RFKILLED; |
| 5786 | else |
| 5787 | status = MGMT_STATUS_FAILED; |
| 5788 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5789 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5790 | |
| 5791 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5792 | } |
| 5793 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5794 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5795 | { |
| 5796 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5797 | |
| 5798 | hci_dev_lock(hdev); |
| 5799 | |
| 5800 | /* When discoverable timeout triggers, then just make sure |
| 5801 | * the limited discoverable flag is cleared. Even in the case |
| 5802 | * of a timeout triggered from general discoverable, it is |
| 5803 | * safe to unconditionally clear the flag. |
| 5804 | */ |
| 5805 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5806 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5807 | |
| 5808 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5809 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5810 | u8 scan = SCAN_PAGE; |
| 5811 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5812 | sizeof(scan), &scan); |
| 5813 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5814 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5815 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5816 | hci_req_run(&req, NULL); |
| 5817 | |
| 5818 | hdev->discov_timeout = 0; |
| 5819 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5820 | new_settings(hdev, NULL); |
| 5821 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5822 | hci_dev_unlock(hdev); |
| 5823 | } |
| 5824 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5825 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5826 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5827 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5828 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5829 | /* Nothing needed here if there's a pending command since that |
| 5830 | * commands request completion callback takes care of everything |
| 5831 | * necessary. |
| 5832 | */ |
| 5833 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5834 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5835 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5836 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5837 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5838 | return; |
| 5839 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5840 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5841 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5842 | } else { |
| 5843 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5844 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5845 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5846 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5847 | if (changed) { |
| 5848 | struct hci_request req; |
| 5849 | |
| 5850 | /* In case this change in discoverable was triggered by |
| 5851 | * a disabling of connectable there could be a need to |
| 5852 | * update the advertising flags. |
| 5853 | */ |
| 5854 | hci_req_init(&req, hdev); |
| 5855 | update_adv_data(&req); |
| 5856 | hci_req_run(&req, NULL); |
| 5857 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5858 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5859 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5860 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5861 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5862 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5863 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5864 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5865 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5866 | /* Nothing needed here if there's a pending command since that |
| 5867 | * commands request completion callback takes care of everything |
| 5868 | * necessary. |
| 5869 | */ |
| 5870 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5871 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5872 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5873 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5874 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5875 | return; |
| 5876 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5877 | if (connectable) |
| 5878 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5879 | else |
| 5880 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5881 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5882 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5883 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5884 | } |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5885 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5886 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5887 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5888 | /* Powering off may stop advertising - don't let that interfere */ |
| 5889 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5890 | return; |
| 5891 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5892 | if (advertising) |
| 5893 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5894 | else |
| 5895 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5896 | } |
| 5897 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5898 | 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] | 5899 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5900 | u8 mgmt_err = mgmt_status(status); |
| 5901 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5902 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5903 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5904 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5905 | |
| 5906 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5907 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5908 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5909 | } |
| 5910 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5911 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5912 | bool persistent) |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5913 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5914 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5915 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5916 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5917 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5918 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5919 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5920 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5921 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5922 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5923 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5924 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5925 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5926 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5927 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5928 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5929 | { |
| 5930 | if (ltk->authenticated) |
| 5931 | return MGMT_LTK_AUTHENTICATED; |
| 5932 | |
| 5933 | return MGMT_LTK_UNAUTHENTICATED; |
| 5934 | } |
| 5935 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5936 | 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] | 5937 | { |
| 5938 | struct mgmt_ev_new_long_term_key ev; |
| 5939 | |
| 5940 | memset(&ev, 0, sizeof(ev)); |
| 5941 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5942 | /* Devices using resolvable or non-resolvable random addresses |
| 5943 | * without providing an indentity resolving key don't require |
| 5944 | * to store long term keys. Their addresses will change the |
| 5945 | * next time around. |
| 5946 | * |
| 5947 | * Only when a remote device provides an identity address |
| 5948 | * make sure the long term key is stored. If the remote |
| 5949 | * identity is known, the long term keys are internally |
| 5950 | * mapped to the identity address. So allow static random |
| 5951 | * and public addresses here. |
| 5952 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5953 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5954 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5955 | ev.store_hint = 0x00; |
| 5956 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5957 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5958 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5959 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5960 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5961 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5962 | ev.key.enc_size = key->enc_size; |
| 5963 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5964 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5965 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5966 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5967 | ev.key.master = 1; |
| 5968 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5969 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5970 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5971 | 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] | 5972 | } |
| 5973 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5974 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5975 | { |
| 5976 | struct mgmt_ev_new_irk ev; |
| 5977 | |
| 5978 | memset(&ev, 0, sizeof(ev)); |
| 5979 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5980 | /* For identity resolving keys from devices that are already |
| 5981 | * using a public address or static random address, do not |
| 5982 | * ask for storing this key. The identity resolving key really |
| 5983 | * is only mandatory for devices using resovlable random |
| 5984 | * addresses. |
| 5985 | * |
| 5986 | * Storing all identity resolving keys has the downside that |
| 5987 | * they will be also loaded on next boot of they system. More |
| 5988 | * identity resolving keys, means more time during scanning is |
| 5989 | * needed to actually resolve these addresses. |
| 5990 | */ |
| 5991 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5992 | ev.store_hint = 0x01; |
| 5993 | else |
| 5994 | ev.store_hint = 0x00; |
| 5995 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5996 | bacpy(&ev.rpa, &irk->rpa); |
| 5997 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5998 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5999 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 6000 | |
| 6001 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 6002 | } |
| 6003 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6004 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 6005 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6006 | { |
| 6007 | struct mgmt_ev_new_csrk ev; |
| 6008 | |
| 6009 | memset(&ev, 0, sizeof(ev)); |
| 6010 | |
| 6011 | /* Devices using resolvable or non-resolvable random addresses |
| 6012 | * without providing an indentity resolving key don't require |
| 6013 | * to store signature resolving keys. Their addresses will change |
| 6014 | * the next time around. |
| 6015 | * |
| 6016 | * Only when a remote device provides an identity address |
| 6017 | * make sure the signature resolving key is stored. So allow |
| 6018 | * static random and public addresses here. |
| 6019 | */ |
| 6020 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 6021 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 6022 | ev.store_hint = 0x00; |
| 6023 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 6024 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 6025 | |
| 6026 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 6027 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 6028 | ev.key.master = csrk->master; |
| 6029 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 6030 | |
| 6031 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 6032 | } |
| 6033 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6034 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6035 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 6036 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6037 | { |
| 6038 | struct mgmt_ev_new_conn_param ev; |
| 6039 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 6040 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 6041 | return; |
| 6042 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6043 | memset(&ev, 0, sizeof(ev)); |
| 6044 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6045 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 6046 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 6047 | ev.min_interval = cpu_to_le16(min_interval); |
| 6048 | ev.max_interval = cpu_to_le16(max_interval); |
| 6049 | ev.latency = cpu_to_le16(latency); |
| 6050 | ev.timeout = cpu_to_le16(timeout); |
| 6051 | |
| 6052 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 6053 | } |
| 6054 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 6055 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 6056 | u8 data_len) |
| 6057 | { |
| 6058 | eir[eir_len++] = sizeof(type) + data_len; |
| 6059 | eir[eir_len++] = type; |
| 6060 | memcpy(&eir[eir_len], data, data_len); |
| 6061 | eir_len += data_len; |
| 6062 | |
| 6063 | return eir_len; |
| 6064 | } |
| 6065 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6066 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6067 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 6068 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6069 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6070 | char buf[512]; |
| 6071 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 6072 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6073 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6074 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6075 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6076 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 6077 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 6078 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6079 | if (name_len > 0) |
| 6080 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6081 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6082 | |
| 6083 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 6084 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6085 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6086 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6087 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6088 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 6089 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 6090 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6091 | } |
| 6092 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6093 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 6094 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 6095 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6096 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 6097 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6098 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6099 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6100 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6101 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6102 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6103 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6104 | |
| 6105 | *sk = cmd->sk; |
| 6106 | sock_hold(*sk); |
| 6107 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6108 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6109 | } |
| 6110 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6111 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6112 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6113 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6114 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 6115 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6116 | |
| 6117 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6118 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6119 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6120 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6121 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6122 | |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6123 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6124 | |
| 6125 | mgmt_pending_remove(cmd); |
| 6126 | } |
| 6127 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6128 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6129 | u8 link_type, u8 addr_type, u8 reason, |
| 6130 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6131 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6132 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6133 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6134 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6135 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6136 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6137 | if (power_off) { |
| 6138 | struct mgmt_mode *cp = power_off->param; |
| 6139 | |
| 6140 | /* The connection is still in hci_conn_hash so test for 1 |
| 6141 | * instead of 0 to know if this is the last one. |
| 6142 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6143 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6144 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6145 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6146 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6147 | } |
| 6148 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6149 | if (!mgmt_connected) |
| 6150 | return; |
| 6151 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6152 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6153 | return; |
| 6154 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6155 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6156 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6157 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6158 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6159 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6160 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6161 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6162 | |
| 6163 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6164 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6165 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6166 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6167 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6168 | } |
| 6169 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6170 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6171 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6172 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6173 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6174 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6175 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6176 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6177 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6178 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6179 | hdev); |
| 6180 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6181 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6182 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6183 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6184 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6185 | cp = cmd->param; |
| 6186 | |
| 6187 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6188 | return; |
| 6189 | |
| 6190 | if (cp->addr.type != bdaddr_type) |
| 6191 | return; |
| 6192 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6193 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6194 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 6195 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6196 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 6197 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6198 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6199 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6200 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6201 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6202 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6203 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6204 | { |
| 6205 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6206 | struct pending_cmd *power_off; |
| 6207 | |
| 6208 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6209 | if (power_off) { |
| 6210 | struct mgmt_mode *cp = power_off->param; |
| 6211 | |
| 6212 | /* The connection is still in hci_conn_hash so test for 1 |
| 6213 | * instead of 0 to know if this is the last one. |
| 6214 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6215 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6216 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6217 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6218 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6219 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6220 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6221 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6222 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6223 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6224 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6225 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6226 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6227 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6228 | 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] | 6229 | { |
| 6230 | struct mgmt_ev_pin_code_request ev; |
| 6231 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6232 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6233 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6234 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6235 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6236 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6237 | } |
| 6238 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6239 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6240 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6241 | { |
| 6242 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6243 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6244 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6245 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6246 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6247 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6248 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6249 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6250 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6251 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6252 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6253 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6254 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6255 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6256 | } |
| 6257 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6258 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6259 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6260 | { |
| 6261 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6262 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6263 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6264 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6265 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6266 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6267 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6268 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6269 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6270 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6271 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6272 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6273 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6274 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6275 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6276 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6277 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6278 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6279 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6280 | { |
| 6281 | struct mgmt_ev_user_confirm_request ev; |
| 6282 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6283 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6284 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6285 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6286 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6287 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6288 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6289 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6290 | 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] | 6291 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6292 | } |
| 6293 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6294 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6295 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6296 | { |
| 6297 | struct mgmt_ev_user_passkey_request ev; |
| 6298 | |
| 6299 | BT_DBG("%s", hdev->name); |
| 6300 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6301 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6302 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6303 | |
| 6304 | 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] | 6305 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6306 | } |
| 6307 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6308 | 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] | 6309 | u8 link_type, u8 addr_type, u8 status, |
| 6310 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6311 | { |
| 6312 | struct pending_cmd *cmd; |
| 6313 | struct mgmt_rp_user_confirm_reply rp; |
| 6314 | int err; |
| 6315 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6316 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6317 | if (!cmd) |
| 6318 | return -ENOENT; |
| 6319 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6320 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6321 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b218 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6322 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6323 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6324 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6325 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6326 | |
| 6327 | return err; |
| 6328 | } |
| 6329 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6330 | 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] | 6331 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6332 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6333 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6334 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6335 | } |
| 6336 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6337 | 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] | 6338 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6339 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6340 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6341 | status, |
| 6342 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6343 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6344 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6345 | 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] | 6346 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6347 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6348 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6349 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6350 | } |
| 6351 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6352 | 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] | 6353 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6354 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6355 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6356 | status, |
| 6357 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6358 | } |
| 6359 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6360 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6361 | u8 link_type, u8 addr_type, u32 passkey, |
| 6362 | u8 entered) |
| 6363 | { |
| 6364 | struct mgmt_ev_passkey_notify ev; |
| 6365 | |
| 6366 | BT_DBG("%s", hdev->name); |
| 6367 | |
| 6368 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6369 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6370 | ev.passkey = __cpu_to_le32(passkey); |
| 6371 | ev.entered = entered; |
| 6372 | |
| 6373 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6374 | } |
| 6375 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6376 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6377 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6378 | { |
| 6379 | struct mgmt_ev_auth_failed ev; |
| 6380 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6381 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6382 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6383 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6384 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6385 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6386 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6387 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6388 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6389 | { |
| 6390 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6391 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6392 | |
| 6393 | if (status) { |
| 6394 | u8 mgmt_err = mgmt_status(status); |
| 6395 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6396 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6397 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6398 | } |
| 6399 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6400 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6401 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6402 | &hdev->dev_flags); |
| 6403 | else |
| 6404 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6405 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6406 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6407 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6408 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6409 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6410 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6411 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6412 | |
| 6413 | if (match.sk) |
| 6414 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6415 | } |
| 6416 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6417 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6418 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6419 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6420 | struct hci_cp_write_eir cp; |
| 6421 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6422 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6423 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6424 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6425 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6426 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6427 | memset(&cp, 0, sizeof(cp)); |
| 6428 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6429 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6430 | } |
| 6431 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6432 | 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] | 6433 | { |
| 6434 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6435 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6436 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6437 | |
| 6438 | if (status) { |
| 6439 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6440 | |
| 6441 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6442 | &hdev->dev_flags)) { |
| 6443 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6444 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6445 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6446 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6447 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6448 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6449 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6450 | } |
| 6451 | |
| 6452 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6453 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6454 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6455 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6456 | if (!changed) |
| 6457 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6458 | &hdev->dev_flags); |
| 6459 | else |
| 6460 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6464 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6465 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6466 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6467 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6468 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6469 | sock_put(match.sk); |
| 6470 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6471 | hci_req_init(&req, hdev); |
| 6472 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6473 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6474 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6475 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6476 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6477 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6478 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6479 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6480 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6481 | |
| 6482 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6483 | } |
| 6484 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6485 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6486 | { |
| 6487 | struct cmd_lookup match = { NULL, hdev }; |
| 6488 | bool changed = false; |
| 6489 | |
| 6490 | if (status) { |
| 6491 | u8 mgmt_err = mgmt_status(status); |
| 6492 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6493 | if (enable) { |
| 6494 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6495 | &hdev->dev_flags)) |
| 6496 | new_settings(hdev, NULL); |
| 6497 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6498 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6499 | |
| 6500 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6501 | cmd_status_rsp, &mgmt_err); |
| 6502 | return; |
| 6503 | } |
| 6504 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6505 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6506 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6507 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6508 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6509 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6510 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6511 | |
| 6512 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6513 | settings_rsp, &match); |
| 6514 | |
| 6515 | if (changed) |
| 6516 | new_settings(hdev, match.sk); |
| 6517 | |
| 6518 | if (match.sk) |
| 6519 | sock_put(match.sk); |
| 6520 | } |
| 6521 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6522 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6523 | { |
| 6524 | struct cmd_lookup *match = data; |
| 6525 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6526 | if (match->sk == NULL) { |
| 6527 | match->sk = cmd->sk; |
| 6528 | sock_hold(match->sk); |
| 6529 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6530 | } |
| 6531 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6532 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6533 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6534 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6535 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6536 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6537 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6538 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6539 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6540 | |
| 6541 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6542 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6543 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6544 | |
| 6545 | if (match.sk) |
| 6546 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6547 | } |
| 6548 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6549 | 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] | 6550 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6551 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6552 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6553 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6554 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6555 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6556 | |
| 6557 | memset(&ev, 0, sizeof(ev)); |
| 6558 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6559 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6560 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6561 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6562 | if (!cmd) { |
| 6563 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6564 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6565 | /* If this is a HCI command related to powering on the |
| 6566 | * HCI dev don't send any mgmt signals. |
| 6567 | */ |
| 6568 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6569 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6570 | } |
| 6571 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6572 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6573 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6574 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6575 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6576 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6577 | u8 *randomizer192, u8 *hash256, |
| 6578 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6579 | { |
| 6580 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6581 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6582 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6583 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6584 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6585 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6586 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6587 | |
| 6588 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6589 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6590 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6591 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6592 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6593 | hash256 && randomizer256) { |
| 6594 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6595 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6596 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6597 | memcpy(rp.randomizer192, randomizer192, |
| 6598 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6599 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6600 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6601 | memcpy(rp.randomizer256, randomizer256, |
| 6602 | sizeof(rp.randomizer256)); |
| 6603 | |
| 6604 | cmd_complete(cmd->sk, hdev->id, |
| 6605 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6606 | &rp, sizeof(rp)); |
| 6607 | } else { |
| 6608 | struct mgmt_rp_read_local_oob_data rp; |
| 6609 | |
| 6610 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6611 | memcpy(rp.randomizer, randomizer192, |
| 6612 | sizeof(rp.randomizer)); |
| 6613 | |
| 6614 | cmd_complete(cmd->sk, hdev->id, |
| 6615 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6616 | &rp, sizeof(rp)); |
| 6617 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6618 | } |
| 6619 | |
| 6620 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6621 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6622 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6623 | 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] | 6624 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6625 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6626 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6627 | char buf[512]; |
| 6628 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6629 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6630 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6631 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6632 | /* Don't send events for a non-kernel initiated discovery. With |
| 6633 | * LE one exception is if we have pend_le_reports > 0 in which |
| 6634 | * case we're doing passive scanning and want these events. |
| 6635 | */ |
| 6636 | if (!hci_discovery_active(hdev)) { |
| 6637 | if (link_type == ACL_LINK) |
| 6638 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 6639 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6640 | return; |
| 6641 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6642 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6643 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6644 | * are for the potential CoD field. |
| 6645 | */ |
| 6646 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6647 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6648 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6649 | memset(buf, 0, sizeof(buf)); |
| 6650 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6651 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6652 | if (irk) { |
| 6653 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6654 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6655 | } else { |
| 6656 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6657 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6658 | } |
| 6659 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6660 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6661 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6662 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6663 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6664 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6665 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6666 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6667 | 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] | 6668 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6669 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6670 | if (scan_rsp_len > 0) |
| 6671 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6672 | |
| 6673 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6674 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6675 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6676 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6677 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6678 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6679 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6680 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6681 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6682 | struct mgmt_ev_device_found *ev; |
| 6683 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6684 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6685 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6686 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6687 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6688 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6689 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6690 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6691 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6692 | ev->rssi = rssi; |
| 6693 | |
| 6694 | 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] | 6695 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6696 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6697 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6698 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6699 | 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] | 6700 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6701 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6702 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6703 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6704 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6705 | struct pending_cmd *cmd; |
| 6706 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6707 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6708 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6709 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6710 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6711 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6712 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6713 | |
| 6714 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6715 | u8 type = hdev->discovery.type; |
| 6716 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6717 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6718 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6719 | mgmt_pending_remove(cmd); |
| 6720 | } |
| 6721 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6722 | memset(&ev, 0, sizeof(ev)); |
| 6723 | ev.type = hdev->discovery.type; |
| 6724 | ev.discovering = discovering; |
| 6725 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6726 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6727 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6728 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6729 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6730 | { |
| 6731 | BT_DBG("%s status %u", hdev->name, status); |
| 6732 | |
| 6733 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6734 | if (status) { |
| 6735 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6736 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6737 | } |
| 6738 | } |
| 6739 | |
| 6740 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6741 | { |
| 6742 | struct hci_request req; |
| 6743 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6744 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6745 | return; |
| 6746 | |
| 6747 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6748 | return; |
| 6749 | |
| 6750 | hci_req_init(&req, hdev); |
| 6751 | enable_advertising(&req); |
| 6752 | |
| 6753 | /* If this fails we have no option but to let user space know |
| 6754 | * that we've disabled advertising. |
| 6755 | */ |
| 6756 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6757 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6758 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6759 | } |
| 6760 | } |