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, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | static const u16 mgmt_events[] = { |
| 97 | MGMT_EV_CONTROLLER_ERROR, |
| 98 | MGMT_EV_INDEX_ADDED, |
| 99 | MGMT_EV_INDEX_REMOVED, |
| 100 | MGMT_EV_NEW_SETTINGS, |
| 101 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 102 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 103 | MGMT_EV_NEW_LINK_KEY, |
| 104 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 105 | MGMT_EV_DEVICE_CONNECTED, |
| 106 | MGMT_EV_DEVICE_DISCONNECTED, |
| 107 | MGMT_EV_CONNECT_FAILED, |
| 108 | MGMT_EV_PIN_CODE_REQUEST, |
| 109 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 110 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 111 | MGMT_EV_AUTH_FAILED, |
| 112 | MGMT_EV_DEVICE_FOUND, |
| 113 | MGMT_EV_DISCOVERING, |
| 114 | MGMT_EV_DEVICE_BLOCKED, |
| 115 | MGMT_EV_DEVICE_UNBLOCKED, |
| 116 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 117 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 118 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 119 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 120 | MGMT_EV_DEVICE_ADDED, |
| 121 | MGMT_EV_DEVICE_REMOVED, |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 122 | MGMT_EV_NEW_CONN_PARAM, |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 123 | MGMT_EV_UNCONF_INDEX_ADDED, |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 124 | MGMT_EV_UNCONF_INDEX_REMOVED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 127 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 128 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 129 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 130 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 131 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 132 | struct pending_cmd { |
| 133 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 134 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 135 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 136 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 137 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 138 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 139 | }; |
| 140 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 141 | /* HCI to MGMT error code conversion table */ |
| 142 | static u8 mgmt_status_table[] = { |
| 143 | MGMT_STATUS_SUCCESS, |
| 144 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 145 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 146 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 147 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 148 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 149 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 150 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 151 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 152 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 153 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 154 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 155 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 156 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 157 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 158 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 159 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 160 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 161 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 162 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 163 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 164 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 165 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 166 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 167 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 168 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 169 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 170 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 171 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 172 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 173 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 174 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 175 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 176 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 177 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 178 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 179 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 180 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 181 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 182 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 183 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 184 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 185 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 186 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 187 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 188 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 189 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 190 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 191 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 192 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 193 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 194 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 195 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 196 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 197 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 198 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 199 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 200 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 201 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 202 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 203 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 204 | }; |
| 205 | |
| 206 | static u8 mgmt_status(u8 hci_status) |
| 207 | { |
| 208 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 209 | return mgmt_status_table[hci_status]; |
| 210 | |
| 211 | return MGMT_STATUS_FAILED; |
| 212 | } |
| 213 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 214 | static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 215 | { |
| 216 | struct sk_buff *skb; |
| 217 | struct mgmt_hdr *hdr; |
| 218 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 219 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 220 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 221 | BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 222 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 223 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 224 | if (!skb) |
| 225 | return -ENOMEM; |
| 226 | |
| 227 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 228 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 229 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 230 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 231 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 232 | |
| 233 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 234 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 235 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 236 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 237 | err = sock_queue_rcv_skb(sk, skb); |
| 238 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 239 | kfree_skb(skb); |
| 240 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 241 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 244 | 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] | 245 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 246 | { |
| 247 | struct sk_buff *skb; |
| 248 | struct mgmt_hdr *hdr; |
| 249 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 250 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 251 | |
| 252 | BT_DBG("sock %p", sk); |
| 253 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 254 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 255 | if (!skb) |
| 256 | return -ENOMEM; |
| 257 | |
| 258 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 259 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 260 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 261 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 262 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 263 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 264 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 265 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 266 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 267 | |
| 268 | if (rp) |
| 269 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 270 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 271 | err = sock_queue_rcv_skb(sk, skb); |
| 272 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 273 | kfree_skb(skb); |
| 274 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 275 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 276 | } |
| 277 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 278 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 279 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 280 | { |
| 281 | struct mgmt_rp_read_version rp; |
| 282 | |
| 283 | BT_DBG("sock %p", sk); |
| 284 | |
| 285 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 286 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 287 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 288 | 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] | 289 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 292 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 293 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 294 | { |
| 295 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 296 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 297 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 298 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 299 | size_t rp_size; |
| 300 | int i, err; |
| 301 | |
| 302 | BT_DBG("sock %p", sk); |
| 303 | |
| 304 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 305 | |
| 306 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 307 | if (!rp) |
| 308 | return -ENOMEM; |
| 309 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 310 | rp->num_commands = cpu_to_le16(num_commands); |
| 311 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 312 | |
| 313 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 314 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 315 | |
| 316 | for (i = 0; i < num_events; i++, opcode++) |
| 317 | put_unaligned_le16(mgmt_events[i], opcode); |
| 318 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 319 | 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] | 320 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 321 | kfree(rp); |
| 322 | |
| 323 | return err; |
| 324 | } |
| 325 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 326 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 327 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 328 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 329 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 330 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 331 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 332 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 333 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 334 | |
| 335 | BT_DBG("sock %p", sk); |
| 336 | |
| 337 | read_lock(&hci_dev_list_lock); |
| 338 | |
| 339 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 340 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 341 | if (d->dev_type == HCI_BREDR && |
| 342 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 343 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 344 | } |
| 345 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 346 | rp_len = sizeof(*rp) + (2 * count); |
| 347 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 348 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 349 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 350 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 351 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 352 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 353 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 354 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 355 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
| 356 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 357 | continue; |
| 358 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 359 | /* Devices marked as raw-only are neither configured |
| 360 | * nor unconfigured controllers. |
| 361 | */ |
| 362 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 363 | continue; |
| 364 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 365 | if (d->dev_type == HCI_BREDR && |
| 366 | !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 367 | rp->index[count++] = cpu_to_le16(d->id); |
| 368 | BT_DBG("Added hci%u", d->id); |
| 369 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 370 | } |
| 371 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 372 | rp->num_controllers = cpu_to_le16(count); |
| 373 | rp_len = sizeof(*rp) + (2 * count); |
| 374 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 375 | read_unlock(&hci_dev_list_lock); |
| 376 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 377 | 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] | 378 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 379 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 380 | kfree(rp); |
| 381 | |
| 382 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 383 | } |
| 384 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 385 | static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev, |
| 386 | void *data, u16 data_len) |
| 387 | { |
| 388 | struct mgmt_rp_read_unconf_index_list *rp; |
| 389 | struct hci_dev *d; |
| 390 | size_t rp_len; |
| 391 | u16 count; |
| 392 | int err; |
| 393 | |
| 394 | BT_DBG("sock %p", sk); |
| 395 | |
| 396 | read_lock(&hci_dev_list_lock); |
| 397 | |
| 398 | count = 0; |
| 399 | list_for_each_entry(d, &hci_dev_list, list) { |
| 400 | if (d->dev_type == HCI_BREDR && |
| 401 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) |
| 402 | count++; |
| 403 | } |
| 404 | |
| 405 | rp_len = sizeof(*rp) + (2 * count); |
| 406 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 407 | if (!rp) { |
| 408 | read_unlock(&hci_dev_list_lock); |
| 409 | return -ENOMEM; |
| 410 | } |
| 411 | |
| 412 | count = 0; |
| 413 | list_for_each_entry(d, &hci_dev_list, list) { |
| 414 | if (test_bit(HCI_SETUP, &d->dev_flags) || |
| 415 | test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 416 | continue; |
| 417 | |
| 418 | /* Devices marked as raw-only are neither configured |
| 419 | * nor unconfigured controllers. |
| 420 | */ |
| 421 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 422 | continue; |
| 423 | |
| 424 | if (d->dev_type == HCI_BREDR && |
| 425 | test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { |
| 426 | rp->index[count++] = cpu_to_le16(d->id); |
| 427 | BT_DBG("Added hci%u", d->id); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | rp->num_controllers = cpu_to_le16(count); |
| 432 | rp_len = sizeof(*rp) + (2 * count); |
| 433 | |
| 434 | read_unlock(&hci_dev_list_lock); |
| 435 | |
| 436 | err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST, |
| 437 | 0, rp, rp_len); |
| 438 | |
| 439 | kfree(rp); |
| 440 | |
| 441 | return err; |
| 442 | } |
| 443 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 444 | static int read_config_info(struct sock *sk, struct hci_dev *hdev, |
| 445 | void *data, u16 data_len) |
| 446 | { |
| 447 | struct mgmt_rp_read_config_info rp; |
| 448 | |
| 449 | BT_DBG("sock %p %s", sk, hdev->name); |
| 450 | |
| 451 | hci_dev_lock(hdev); |
| 452 | |
| 453 | memset(&rp, 0, sizeof(rp)); |
| 454 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
| 455 | if (hdev->set_bdaddr) |
| 456 | rp.supported_options = cpu_to_le32(MGMT_OPTION_PUBLIC_ADDRESS); |
| 457 | else |
| 458 | rp.supported_options = cpu_to_le32(0); |
| 459 | rp.missing_options = cpu_to_le32(0); |
| 460 | |
| 461 | hci_dev_unlock(hdev); |
| 462 | |
| 463 | return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp, |
| 464 | sizeof(rp)); |
| 465 | } |
| 466 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 467 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 468 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 469 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 470 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 471 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 472 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 473 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 474 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 475 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 476 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 477 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 478 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 479 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 480 | settings |= MGMT_SETTING_BREDR; |
| 481 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 482 | |
| 483 | if (lmp_ssp_capable(hdev)) { |
| 484 | settings |= MGMT_SETTING_SSP; |
| 485 | settings |= MGMT_SETTING_HS; |
| 486 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 487 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 488 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 489 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 490 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 491 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 492 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 493 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 494 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 495 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 496 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 497 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 498 | |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 499 | if (hdev->set_bdaddr) |
| 500 | settings |= MGMT_SETTING_CONFIGURATION; |
| 501 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 502 | return settings; |
| 503 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 504 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 505 | static u32 get_current_settings(struct hci_dev *hdev) |
| 506 | { |
| 507 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 508 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 509 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 510 | settings |= MGMT_SETTING_POWERED; |
| 511 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 512 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 513 | settings |= MGMT_SETTING_CONNECTABLE; |
| 514 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 515 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 516 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 517 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 518 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 519 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 520 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 521 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 522 | settings |= MGMT_SETTING_PAIRABLE; |
| 523 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 524 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 525 | settings |= MGMT_SETTING_BREDR; |
| 526 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 527 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 528 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 529 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 530 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 531 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 532 | |
Johan Hedberg | 84bde9d6c | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 533 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 534 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 535 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 536 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 537 | settings |= MGMT_SETTING_HS; |
| 538 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 539 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 540 | settings |= MGMT_SETTING_ADVERTISING; |
| 541 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 542 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 543 | settings |= MGMT_SETTING_SECURE_CONN; |
| 544 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 545 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 546 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 547 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 548 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 549 | settings |= MGMT_SETTING_PRIVACY; |
| 550 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 551 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 552 | } |
| 553 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 554 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 555 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 556 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 557 | { |
| 558 | u8 *ptr = data, *uuids_start = NULL; |
| 559 | struct bt_uuid *uuid; |
| 560 | |
| 561 | if (len < 4) |
| 562 | return ptr; |
| 563 | |
| 564 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 565 | u16 uuid16; |
| 566 | |
| 567 | if (uuid->size != 16) |
| 568 | continue; |
| 569 | |
| 570 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 571 | if (uuid16 < 0x1100) |
| 572 | continue; |
| 573 | |
| 574 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 575 | continue; |
| 576 | |
| 577 | if (!uuids_start) { |
| 578 | uuids_start = ptr; |
| 579 | uuids_start[0] = 1; |
| 580 | uuids_start[1] = EIR_UUID16_ALL; |
| 581 | ptr += 2; |
| 582 | } |
| 583 | |
| 584 | /* Stop if not enough space to put next UUID */ |
| 585 | if ((ptr - data) + sizeof(u16) > len) { |
| 586 | uuids_start[1] = EIR_UUID16_SOME; |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | *ptr++ = (uuid16 & 0x00ff); |
| 591 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 592 | uuids_start[0] += sizeof(uuid16); |
| 593 | } |
| 594 | |
| 595 | return ptr; |
| 596 | } |
| 597 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 598 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 599 | { |
| 600 | u8 *ptr = data, *uuids_start = NULL; |
| 601 | struct bt_uuid *uuid; |
| 602 | |
| 603 | if (len < 6) |
| 604 | return ptr; |
| 605 | |
| 606 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 607 | if (uuid->size != 32) |
| 608 | continue; |
| 609 | |
| 610 | if (!uuids_start) { |
| 611 | uuids_start = ptr; |
| 612 | uuids_start[0] = 1; |
| 613 | uuids_start[1] = EIR_UUID32_ALL; |
| 614 | ptr += 2; |
| 615 | } |
| 616 | |
| 617 | /* Stop if not enough space to put next UUID */ |
| 618 | if ((ptr - data) + sizeof(u32) > len) { |
| 619 | uuids_start[1] = EIR_UUID32_SOME; |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 624 | ptr += sizeof(u32); |
| 625 | uuids_start[0] += sizeof(u32); |
| 626 | } |
| 627 | |
| 628 | return ptr; |
| 629 | } |
| 630 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 631 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 632 | { |
| 633 | u8 *ptr = data, *uuids_start = NULL; |
| 634 | struct bt_uuid *uuid; |
| 635 | |
| 636 | if (len < 18) |
| 637 | return ptr; |
| 638 | |
| 639 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 640 | if (uuid->size != 128) |
| 641 | continue; |
| 642 | |
| 643 | if (!uuids_start) { |
| 644 | uuids_start = ptr; |
| 645 | uuids_start[0] = 1; |
| 646 | uuids_start[1] = EIR_UUID128_ALL; |
| 647 | ptr += 2; |
| 648 | } |
| 649 | |
| 650 | /* Stop if not enough space to put next UUID */ |
| 651 | if ((ptr - data) + 16 > len) { |
| 652 | uuids_start[1] = EIR_UUID128_SOME; |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | memcpy(ptr, uuid->uuid, 16); |
| 657 | ptr += 16; |
| 658 | uuids_start[0] += 16; |
| 659 | } |
| 660 | |
| 661 | return ptr; |
| 662 | } |
| 663 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 664 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 665 | { |
| 666 | struct pending_cmd *cmd; |
| 667 | |
| 668 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 669 | if (cmd->opcode == opcode) |
| 670 | return cmd; |
| 671 | } |
| 672 | |
| 673 | return NULL; |
| 674 | } |
| 675 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 676 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 677 | struct hci_dev *hdev, |
| 678 | const void *data) |
| 679 | { |
| 680 | struct pending_cmd *cmd; |
| 681 | |
| 682 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 683 | if (cmd->user_data != data) |
| 684 | continue; |
| 685 | if (cmd->opcode == opcode) |
| 686 | return cmd; |
| 687 | } |
| 688 | |
| 689 | return NULL; |
| 690 | } |
| 691 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 692 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 693 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 694 | u8 ad_len = 0; |
| 695 | size_t name_len; |
| 696 | |
| 697 | name_len = strlen(hdev->dev_name); |
| 698 | if (name_len > 0) { |
| 699 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 700 | |
| 701 | if (name_len > max_len) { |
| 702 | name_len = max_len; |
| 703 | ptr[1] = EIR_NAME_SHORT; |
| 704 | } else |
| 705 | ptr[1] = EIR_NAME_COMPLETE; |
| 706 | |
| 707 | ptr[0] = name_len + 1; |
| 708 | |
| 709 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 710 | |
| 711 | ad_len += (name_len + 2); |
| 712 | ptr += (name_len + 2); |
| 713 | } |
| 714 | |
| 715 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | static void update_scan_rsp_data(struct hci_request *req) |
| 719 | { |
| 720 | struct hci_dev *hdev = req->hdev; |
| 721 | struct hci_cp_le_set_scan_rsp_data cp; |
| 722 | u8 len; |
| 723 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 724 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 725 | return; |
| 726 | |
| 727 | memset(&cp, 0, sizeof(cp)); |
| 728 | |
| 729 | len = create_scan_rsp_data(hdev, cp.data); |
| 730 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 731 | if (hdev->scan_rsp_data_len == len && |
| 732 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 733 | return; |
| 734 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 735 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 736 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 737 | |
| 738 | cp.length = len; |
| 739 | |
| 740 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 741 | } |
| 742 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 743 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 744 | { |
| 745 | struct pending_cmd *cmd; |
| 746 | |
| 747 | /* If there's a pending mgmt command the flags will not yet have |
| 748 | * their final values, so check for this first. |
| 749 | */ |
| 750 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 751 | if (cmd) { |
| 752 | struct mgmt_mode *cp = cmd->param; |
| 753 | if (cp->val == 0x01) |
| 754 | return LE_AD_GENERAL; |
| 755 | else if (cp->val == 0x02) |
| 756 | return LE_AD_LIMITED; |
| 757 | } else { |
| 758 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 759 | return LE_AD_LIMITED; |
| 760 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 761 | return LE_AD_GENERAL; |
| 762 | } |
| 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 767 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 768 | { |
| 769 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 770 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 771 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 772 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 773 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 774 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 775 | |
| 776 | if (flags) { |
| 777 | BT_DBG("adv flags 0x%02x", flags); |
| 778 | |
| 779 | ptr[0] = 2; |
| 780 | ptr[1] = EIR_FLAGS; |
| 781 | ptr[2] = flags; |
| 782 | |
| 783 | ad_len += 3; |
| 784 | ptr += 3; |
| 785 | } |
| 786 | |
| 787 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 788 | ptr[0] = 2; |
| 789 | ptr[1] = EIR_TX_POWER; |
| 790 | ptr[2] = (u8) hdev->adv_tx_power; |
| 791 | |
| 792 | ad_len += 3; |
| 793 | ptr += 3; |
| 794 | } |
| 795 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 796 | return ad_len; |
| 797 | } |
| 798 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 799 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 800 | { |
| 801 | struct hci_dev *hdev = req->hdev; |
| 802 | struct hci_cp_le_set_adv_data cp; |
| 803 | u8 len; |
| 804 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 805 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 806 | return; |
| 807 | |
| 808 | memset(&cp, 0, sizeof(cp)); |
| 809 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 810 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 811 | |
| 812 | if (hdev->adv_data_len == len && |
| 813 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 814 | return; |
| 815 | |
| 816 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 817 | hdev->adv_data_len = len; |
| 818 | |
| 819 | cp.length = len; |
| 820 | |
| 821 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 822 | } |
| 823 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 824 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 825 | { |
| 826 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 827 | size_t name_len; |
| 828 | |
| 829 | name_len = strlen(hdev->dev_name); |
| 830 | |
| 831 | if (name_len > 0) { |
| 832 | /* EIR Data type */ |
| 833 | if (name_len > 48) { |
| 834 | name_len = 48; |
| 835 | ptr[1] = EIR_NAME_SHORT; |
| 836 | } else |
| 837 | ptr[1] = EIR_NAME_COMPLETE; |
| 838 | |
| 839 | /* EIR Data length */ |
| 840 | ptr[0] = name_len + 1; |
| 841 | |
| 842 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 843 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 844 | ptr += (name_len + 2); |
| 845 | } |
| 846 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 847 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 848 | ptr[0] = 2; |
| 849 | ptr[1] = EIR_TX_POWER; |
| 850 | ptr[2] = (u8) hdev->inq_tx_power; |
| 851 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 852 | ptr += 3; |
| 853 | } |
| 854 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 855 | if (hdev->devid_source > 0) { |
| 856 | ptr[0] = 9; |
| 857 | ptr[1] = EIR_DEVICE_ID; |
| 858 | |
| 859 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 860 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 861 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 862 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 863 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 864 | ptr += 10; |
| 865 | } |
| 866 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 867 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 868 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 869 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 870 | } |
| 871 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 872 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 873 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 874 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 875 | struct hci_cp_write_eir cp; |
| 876 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 877 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 878 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 879 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 880 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 881 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 882 | |
Johan Hedberg | 84bde9d6c | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 883 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 884 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 885 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 886 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 887 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 888 | |
| 889 | memset(&cp, 0, sizeof(cp)); |
| 890 | |
| 891 | create_eir(hdev, cp.data); |
| 892 | |
| 893 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 894 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 895 | |
| 896 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 897 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 898 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 899 | } |
| 900 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 901 | static u8 get_service_classes(struct hci_dev *hdev) |
| 902 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 903 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 904 | u8 val = 0; |
| 905 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 906 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 907 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 908 | |
| 909 | return val; |
| 910 | } |
| 911 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 912 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 913 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 914 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 915 | u8 cod[3]; |
| 916 | |
| 917 | BT_DBG("%s", hdev->name); |
| 918 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 919 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 920 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 921 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 922 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 923 | return; |
| 924 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 925 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 926 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 927 | |
| 928 | cod[0] = hdev->minor_class; |
| 929 | cod[1] = hdev->major_class; |
| 930 | cod[2] = get_service_classes(hdev); |
| 931 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 932 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 933 | cod[1] |= 0x20; |
| 934 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 935 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 936 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 937 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 938 | 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] | 939 | } |
| 940 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 941 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 942 | { |
| 943 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 944 | |
| 945 | /* If there's a pending mgmt command the flag will not yet have |
| 946 | * it's final value, so check for this first. |
| 947 | */ |
| 948 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 949 | if (cmd) { |
| 950 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 951 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 952 | } |
| 953 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 954 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | static void enable_advertising(struct hci_request *req) |
| 958 | { |
| 959 | struct hci_dev *hdev = req->hdev; |
| 960 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 961 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 962 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 963 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 964 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 965 | * hci_update_random_address knows that it's safe to go ahead |
| 966 | * and write a new random address. The flag will be set back on |
| 967 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 968 | */ |
| 969 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 970 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 971 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 972 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 973 | /* Set require_privacy to true only when non-connectable |
| 974 | * advertising is used. In that case it is fine to use a |
| 975 | * non-resolvable private address. |
| 976 | */ |
| 977 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 978 | return; |
| 979 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 980 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 981 | cp.min_interval = cpu_to_le16(0x0800); |
| 982 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 983 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 984 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 985 | cp.channel_map = hdev->le_adv_channel_map; |
| 986 | |
| 987 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 988 | |
| 989 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 990 | } |
| 991 | |
| 992 | static void disable_advertising(struct hci_request *req) |
| 993 | { |
| 994 | u8 enable = 0x00; |
| 995 | |
| 996 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 997 | } |
| 998 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 999 | static void service_cache_off(struct work_struct *work) |
| 1000 | { |
| 1001 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1002 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1003 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1004 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 1005 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1006 | return; |
| 1007 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1008 | hci_req_init(&req, hdev); |
| 1009 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1010 | hci_dev_lock(hdev); |
| 1011 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1012 | update_eir(&req); |
| 1013 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1014 | |
| 1015 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1016 | |
| 1017 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1020 | static void rpa_expired(struct work_struct *work) |
| 1021 | { |
| 1022 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 1023 | rpa_expired.work); |
| 1024 | struct hci_request req; |
| 1025 | |
| 1026 | BT_DBG(""); |
| 1027 | |
| 1028 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 1029 | |
| 1030 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 1031 | hci_conn_num(hdev, LE_LINK) > 0) |
| 1032 | return; |
| 1033 | |
| 1034 | /* The generation of a new RPA and programming it into the |
| 1035 | * controller happens in the enable_advertising() function. |
| 1036 | */ |
| 1037 | |
| 1038 | hci_req_init(&req, hdev); |
| 1039 | |
| 1040 | disable_advertising(&req); |
| 1041 | enable_advertising(&req); |
| 1042 | |
| 1043 | hci_req_run(&req, NULL); |
| 1044 | } |
| 1045 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1046 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1047 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1048 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 1049 | return; |
| 1050 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1051 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 1052 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1053 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 1054 | /* Non-mgmt controlled devices get this bit set |
| 1055 | * implicitly so that pairing works for them, however |
| 1056 | * for mgmt we require user-space to explicitly enable |
| 1057 | * it |
| 1058 | */ |
| 1059 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 1060 | } |
| 1061 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 1062 | 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] | 1063 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1064 | { |
| 1065 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1066 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1067 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1068 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1069 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1070 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1071 | memset(&rp, 0, sizeof(rp)); |
| 1072 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1073 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1074 | |
| 1075 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 1076 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1077 | |
| 1078 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 1079 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 1080 | |
| 1081 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1082 | |
| 1083 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 1084 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1085 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1086 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1087 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1088 | 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] | 1089 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 1093 | { |
| 1094 | sock_put(cmd->sk); |
| 1095 | kfree(cmd->param); |
| 1096 | kfree(cmd); |
| 1097 | } |
| 1098 | |
| 1099 | 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] | 1100 | struct hci_dev *hdev, void *data, |
| 1101 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1102 | { |
| 1103 | struct pending_cmd *cmd; |
| 1104 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1105 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1106 | if (!cmd) |
| 1107 | return NULL; |
| 1108 | |
| 1109 | cmd->opcode = opcode; |
| 1110 | cmd->index = hdev->id; |
| 1111 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1112 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1113 | if (!cmd->param) { |
| 1114 | kfree(cmd); |
| 1115 | return NULL; |
| 1116 | } |
| 1117 | |
| 1118 | if (data) |
| 1119 | memcpy(cmd->param, data, len); |
| 1120 | |
| 1121 | cmd->sk = sk; |
| 1122 | sock_hold(sk); |
| 1123 | |
| 1124 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1125 | |
| 1126 | return cmd; |
| 1127 | } |
| 1128 | |
| 1129 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1130 | void (*cb)(struct pending_cmd *cmd, |
| 1131 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1132 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1133 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1134 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1135 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1136 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1137 | if (opcode > 0 && cmd->opcode != opcode) |
| 1138 | continue; |
| 1139 | |
| 1140 | cb(cmd, data); |
| 1141 | } |
| 1142 | } |
| 1143 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1144 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1145 | { |
| 1146 | list_del(&cmd->list); |
| 1147 | mgmt_pending_free(cmd); |
| 1148 | } |
| 1149 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1150 | 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] | 1151 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1152 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1153 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1154 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1155 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1156 | } |
| 1157 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1158 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1159 | { |
| 1160 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1161 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1162 | if (hci_conn_count(hdev) == 0) { |
| 1163 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1164 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1165 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1166 | } |
| 1167 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1168 | static void hci_stop_discovery(struct hci_request *req) |
| 1169 | { |
| 1170 | struct hci_dev *hdev = req->hdev; |
| 1171 | struct hci_cp_remote_name_req_cancel cp; |
| 1172 | struct inquiry_entry *e; |
| 1173 | |
| 1174 | switch (hdev->discovery.state) { |
| 1175 | case DISCOVERY_FINDING: |
| 1176 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1177 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1178 | } else { |
| 1179 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1180 | hci_req_add_le_scan_disable(req); |
| 1181 | } |
| 1182 | |
| 1183 | break; |
| 1184 | |
| 1185 | case DISCOVERY_RESOLVING: |
| 1186 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1187 | NAME_PENDING); |
| 1188 | if (!e) |
| 1189 | return; |
| 1190 | |
| 1191 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1192 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1193 | &cp); |
| 1194 | |
| 1195 | break; |
| 1196 | |
| 1197 | default: |
| 1198 | /* Passive scanning */ |
| 1199 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1200 | hci_req_add_le_scan_disable(req); |
| 1201 | break; |
| 1202 | } |
| 1203 | } |
| 1204 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1205 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1206 | { |
| 1207 | struct hci_request req; |
| 1208 | struct hci_conn *conn; |
| 1209 | |
| 1210 | hci_req_init(&req, hdev); |
| 1211 | |
| 1212 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1213 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1214 | u8 scan = 0x00; |
| 1215 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1216 | } |
| 1217 | |
| 1218 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1219 | disable_advertising(&req); |
| 1220 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1221 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1222 | |
| 1223 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1224 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1225 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1226 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1227 | switch (conn->state) { |
| 1228 | case BT_CONNECTED: |
| 1229 | case BT_CONFIG: |
| 1230 | dc.handle = cpu_to_le16(conn->handle); |
| 1231 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1232 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1233 | break; |
| 1234 | case BT_CONNECT: |
| 1235 | if (conn->type == LE_LINK) |
| 1236 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1237 | 0, NULL); |
| 1238 | else if (conn->type == ACL_LINK) |
| 1239 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1240 | 6, &conn->dst); |
| 1241 | break; |
| 1242 | case BT_CONNECT2: |
| 1243 | bacpy(&rej.bdaddr, &conn->dst); |
| 1244 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1245 | if (conn->type == ACL_LINK) |
| 1246 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1247 | sizeof(rej), &rej); |
| 1248 | else if (conn->type == SCO_LINK) |
| 1249 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1250 | sizeof(rej), &rej); |
| 1251 | break; |
| 1252 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | return hci_req_run(&req, clean_up_hci_complete); |
| 1256 | } |
| 1257 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1258 | 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] | 1259 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1260 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1261 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1262 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1263 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1264 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1265 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1266 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1267 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1268 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1269 | MGMT_STATUS_INVALID_PARAMS); |
| 1270 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1271 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1272 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1273 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1274 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1275 | MGMT_STATUS_BUSY); |
| 1276 | goto failed; |
| 1277 | } |
| 1278 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1279 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1280 | cancel_delayed_work(&hdev->power_off); |
| 1281 | |
| 1282 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1283 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1284 | data, len); |
| 1285 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1286 | goto failed; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1290 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1291 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1292 | goto failed; |
| 1293 | } |
| 1294 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1295 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1296 | if (!cmd) { |
| 1297 | err = -ENOMEM; |
| 1298 | goto failed; |
| 1299 | } |
| 1300 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1301 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1302 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1303 | err = 0; |
| 1304 | } else { |
| 1305 | /* Disconnect connections, stop scans, etc */ |
| 1306 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1307 | if (!err) |
| 1308 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1309 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1310 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1311 | /* ENODATA means there were no HCI commands queued */ |
| 1312 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1313 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1314 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1315 | err = 0; |
| 1316 | } |
| 1317 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1318 | |
| 1319 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1320 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1321 | return err; |
| 1322 | } |
| 1323 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1324 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1325 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1326 | { |
| 1327 | struct sk_buff *skb; |
| 1328 | struct mgmt_hdr *hdr; |
| 1329 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1330 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1331 | if (!skb) |
| 1332 | return -ENOMEM; |
| 1333 | |
| 1334 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1335 | hdr->opcode = cpu_to_le16(event); |
| 1336 | if (hdev) |
| 1337 | hdr->index = cpu_to_le16(hdev->id); |
| 1338 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1339 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1340 | hdr->len = cpu_to_le16(data_len); |
| 1341 | |
| 1342 | if (data) |
| 1343 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1344 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1345 | /* Time stamp */ |
| 1346 | __net_timestamp(skb); |
| 1347 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1348 | hci_send_to_control(skb, skip_sk); |
| 1349 | kfree_skb(skb); |
| 1350 | |
| 1351 | return 0; |
| 1352 | } |
| 1353 | |
| 1354 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1355 | { |
| 1356 | __le32 ev; |
| 1357 | |
| 1358 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1359 | |
| 1360 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1361 | } |
| 1362 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1363 | struct cmd_lookup { |
| 1364 | struct sock *sk; |
| 1365 | struct hci_dev *hdev; |
| 1366 | u8 mgmt_status; |
| 1367 | }; |
| 1368 | |
| 1369 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1370 | { |
| 1371 | struct cmd_lookup *match = data; |
| 1372 | |
| 1373 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1374 | |
| 1375 | list_del(&cmd->list); |
| 1376 | |
| 1377 | if (match->sk == NULL) { |
| 1378 | match->sk = cmd->sk; |
| 1379 | sock_hold(match->sk); |
| 1380 | } |
| 1381 | |
| 1382 | mgmt_pending_free(cmd); |
| 1383 | } |
| 1384 | |
| 1385 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1386 | { |
| 1387 | u8 *status = data; |
| 1388 | |
| 1389 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1390 | mgmt_pending_remove(cmd); |
| 1391 | } |
| 1392 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1393 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1394 | { |
| 1395 | if (!lmp_bredr_capable(hdev)) |
| 1396 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1397 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1398 | return MGMT_STATUS_REJECTED; |
| 1399 | else |
| 1400 | return MGMT_STATUS_SUCCESS; |
| 1401 | } |
| 1402 | |
| 1403 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1404 | { |
| 1405 | if (!lmp_le_capable(hdev)) |
| 1406 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1407 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1408 | return MGMT_STATUS_REJECTED; |
| 1409 | else |
| 1410 | return MGMT_STATUS_SUCCESS; |
| 1411 | } |
| 1412 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1413 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1414 | { |
| 1415 | struct pending_cmd *cmd; |
| 1416 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1417 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1418 | bool changed; |
| 1419 | |
| 1420 | BT_DBG("status 0x%02x", status); |
| 1421 | |
| 1422 | hci_dev_lock(hdev); |
| 1423 | |
| 1424 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1425 | if (!cmd) |
| 1426 | goto unlock; |
| 1427 | |
| 1428 | if (status) { |
| 1429 | u8 mgmt_err = mgmt_status(status); |
| 1430 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1431 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1432 | goto remove_cmd; |
| 1433 | } |
| 1434 | |
| 1435 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1436 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1437 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1438 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1439 | |
| 1440 | if (hdev->discov_timeout > 0) { |
| 1441 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1442 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1443 | to); |
| 1444 | } |
| 1445 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1446 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1447 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1448 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1449 | |
| 1450 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1451 | |
| 1452 | if (changed) |
| 1453 | new_settings(hdev, cmd->sk); |
| 1454 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1455 | /* When the discoverable mode gets changed, make sure |
| 1456 | * that class of device has the limited discoverable |
| 1457 | * bit correctly set. |
| 1458 | */ |
| 1459 | hci_req_init(&req, hdev); |
| 1460 | update_class(&req); |
| 1461 | hci_req_run(&req, NULL); |
| 1462 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1463 | remove_cmd: |
| 1464 | mgmt_pending_remove(cmd); |
| 1465 | |
| 1466 | unlock: |
| 1467 | hci_dev_unlock(hdev); |
| 1468 | } |
| 1469 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1470 | 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] | 1471 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1472 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1473 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1474 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1475 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1476 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1477 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1478 | int err; |
| 1479 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1480 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1481 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1482 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1483 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1484 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1485 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1486 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1487 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1488 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1489 | MGMT_STATUS_INVALID_PARAMS); |
| 1490 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1491 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1492 | |
| 1493 | /* Disabling discoverable requires that no timeout is set, |
| 1494 | * and enabling limited discoverable requires a timeout. |
| 1495 | */ |
| 1496 | if ((cp->val == 0x00 && timeout > 0) || |
| 1497 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1498 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1499 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1500 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1501 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1502 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1503 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1504 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1505 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1506 | goto failed; |
| 1507 | } |
| 1508 | |
| 1509 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1510 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1511 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1512 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1513 | goto failed; |
| 1514 | } |
| 1515 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1516 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1517 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1518 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1519 | goto failed; |
| 1520 | } |
| 1521 | |
| 1522 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1523 | bool changed = false; |
| 1524 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1525 | /* Setting limited discoverable when powered off is |
| 1526 | * not a valid operation since it requires a timeout |
| 1527 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1528 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1529 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1530 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1531 | changed = true; |
| 1532 | } |
| 1533 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1534 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1535 | if (err < 0) |
| 1536 | goto failed; |
| 1537 | |
| 1538 | if (changed) |
| 1539 | err = new_settings(hdev, sk); |
| 1540 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1541 | goto failed; |
| 1542 | } |
| 1543 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1544 | /* If the current mode is the same, then just update the timeout |
| 1545 | * value with the new value. And if only the timeout gets updated, |
| 1546 | * then no need for any HCI transactions. |
| 1547 | */ |
| 1548 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1549 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1550 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1551 | cancel_delayed_work(&hdev->discov_off); |
| 1552 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1553 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1554 | if (cp->val && hdev->discov_timeout > 0) { |
| 1555 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1556 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1557 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1558 | } |
| 1559 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1560 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1561 | goto failed; |
| 1562 | } |
| 1563 | |
| 1564 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1565 | if (!cmd) { |
| 1566 | err = -ENOMEM; |
| 1567 | goto failed; |
| 1568 | } |
| 1569 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1570 | /* Cancel any potential discoverable timeout that might be |
| 1571 | * still active and store new timeout value. The arming of |
| 1572 | * the timeout happens in the complete handler. |
| 1573 | */ |
| 1574 | cancel_delayed_work(&hdev->discov_off); |
| 1575 | hdev->discov_timeout = timeout; |
| 1576 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1577 | /* Limited discoverable mode */ |
| 1578 | if (cp->val == 0x02) |
| 1579 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1580 | else |
| 1581 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1582 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1583 | hci_req_init(&req, hdev); |
| 1584 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1585 | /* The procedure for LE-only controllers is much simpler - just |
| 1586 | * update the advertising data. |
| 1587 | */ |
| 1588 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1589 | goto update_ad; |
| 1590 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1591 | scan = SCAN_PAGE; |
| 1592 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1593 | if (cp->val) { |
| 1594 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1595 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1596 | if (cp->val == 0x02) { |
| 1597 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1598 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1599 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1600 | hci_cp.iac_lap[1] = 0x8b; |
| 1601 | hci_cp.iac_lap[2] = 0x9e; |
| 1602 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1603 | hci_cp.iac_lap[4] = 0x8b; |
| 1604 | hci_cp.iac_lap[5] = 0x9e; |
| 1605 | } else { |
| 1606 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1607 | hci_cp.num_iac = 1; |
| 1608 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1609 | hci_cp.iac_lap[1] = 0x8b; |
| 1610 | hci_cp.iac_lap[2] = 0x9e; |
| 1611 | } |
| 1612 | |
| 1613 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1614 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1615 | |
| 1616 | scan |= SCAN_INQUIRY; |
| 1617 | } else { |
| 1618 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1619 | } |
| 1620 | |
| 1621 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1622 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1623 | update_ad: |
| 1624 | update_adv_data(&req); |
| 1625 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1626 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1627 | if (err < 0) |
| 1628 | mgmt_pending_remove(cmd); |
| 1629 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1630 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1631 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1632 | return err; |
| 1633 | } |
| 1634 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1635 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1636 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1637 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1638 | struct hci_cp_write_page_scan_activity acp; |
| 1639 | u8 type; |
| 1640 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1641 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1642 | return; |
| 1643 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1644 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1645 | return; |
| 1646 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1647 | if (enable) { |
| 1648 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1649 | |
| 1650 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1651 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1652 | } else { |
| 1653 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1654 | |
| 1655 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1656 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1657 | } |
| 1658 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1659 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1660 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1661 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1662 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1663 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1664 | sizeof(acp), &acp); |
| 1665 | |
| 1666 | if (hdev->page_scan_type != type) |
| 1667 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1668 | } |
| 1669 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1670 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1671 | { |
| 1672 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1673 | struct mgmt_mode *cp; |
| 1674 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1675 | |
| 1676 | BT_DBG("status 0x%02x", status); |
| 1677 | |
| 1678 | hci_dev_lock(hdev); |
| 1679 | |
| 1680 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1681 | if (!cmd) |
| 1682 | goto unlock; |
| 1683 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1684 | if (status) { |
| 1685 | u8 mgmt_err = mgmt_status(status); |
| 1686 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1687 | goto remove_cmd; |
| 1688 | } |
| 1689 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1690 | cp = cmd->param; |
| 1691 | if (cp->val) |
| 1692 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1693 | else |
| 1694 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1695 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1696 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1697 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1698 | if (changed) |
| 1699 | new_settings(hdev, cmd->sk); |
| 1700 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1701 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1702 | mgmt_pending_remove(cmd); |
| 1703 | |
| 1704 | unlock: |
| 1705 | hci_dev_unlock(hdev); |
| 1706 | } |
| 1707 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1708 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1709 | struct sock *sk, u8 val) |
| 1710 | { |
| 1711 | bool changed = false; |
| 1712 | int err; |
| 1713 | |
| 1714 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1715 | changed = true; |
| 1716 | |
| 1717 | if (val) { |
| 1718 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1719 | } else { |
| 1720 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1721 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1722 | } |
| 1723 | |
| 1724 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1725 | if (err < 0) |
| 1726 | return err; |
| 1727 | |
| 1728 | if (changed) |
| 1729 | return new_settings(hdev, sk); |
| 1730 | |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1734 | 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] | 1735 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1736 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1737 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1738 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1739 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1740 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1741 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1742 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1743 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1744 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1745 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1746 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1747 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1748 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1749 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1750 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1751 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1752 | MGMT_STATUS_INVALID_PARAMS); |
| 1753 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1754 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1755 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1756 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1757 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1758 | goto failed; |
| 1759 | } |
| 1760 | |
| 1761 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1762 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1763 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1764 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1765 | goto failed; |
| 1766 | } |
| 1767 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1768 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1769 | if (!cmd) { |
| 1770 | err = -ENOMEM; |
| 1771 | goto failed; |
| 1772 | } |
| 1773 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1774 | hci_req_init(&req, hdev); |
| 1775 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1776 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1777 | * by-product of disabling connectable, we need to update the |
| 1778 | * advertising flags. |
| 1779 | */ |
| 1780 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1781 | if (!cp->val) { |
| 1782 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1783 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1784 | } |
| 1785 | update_adv_data(&req); |
| 1786 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1787 | if (cp->val) { |
| 1788 | scan = SCAN_PAGE; |
| 1789 | } else { |
| 1790 | scan = 0; |
| 1791 | |
| 1792 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1793 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1794 | cancel_delayed_work(&hdev->discov_off); |
| 1795 | } |
| 1796 | |
| 1797 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1798 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1799 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1800 | /* If we're going from non-connectable to connectable or |
| 1801 | * vice-versa when fast connectable is enabled ensure that fast |
| 1802 | * connectable gets disabled. write_fast_connectable won't do |
| 1803 | * anything if the page scan parameters are already what they |
| 1804 | * should be. |
| 1805 | */ |
| 1806 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1807 | write_fast_connectable(&req, false); |
| 1808 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1809 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1810 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1811 | disable_advertising(&req); |
| 1812 | enable_advertising(&req); |
| 1813 | } |
| 1814 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1815 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1816 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1817 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1818 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1819 | err = set_connectable_update_settings(hdev, sk, |
| 1820 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1821 | goto failed; |
| 1822 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1823 | |
| 1824 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1825 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1826 | return err; |
| 1827 | } |
| 1828 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1829 | 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] | 1830 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1831 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1832 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1833 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1834 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1835 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1836 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1837 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1838 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1839 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1840 | MGMT_STATUS_INVALID_PARAMS); |
| 1841 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1842 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1843 | |
| 1844 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1845 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1846 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1847 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1848 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1849 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1850 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1851 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1852 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1853 | if (changed) |
| 1854 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1855 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1856 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1857 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1858 | return err; |
| 1859 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1860 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1861 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1862 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1863 | { |
| 1864 | struct mgmt_mode *cp = data; |
| 1865 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1866 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1867 | int err; |
| 1868 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1869 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1870 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1871 | status = mgmt_bredr_support(hdev); |
| 1872 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1873 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1874 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1875 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1876 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1877 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1878 | MGMT_STATUS_INVALID_PARAMS); |
| 1879 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1880 | hci_dev_lock(hdev); |
| 1881 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1882 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1883 | bool changed = false; |
| 1884 | |
| 1885 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1886 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1887 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1888 | changed = true; |
| 1889 | } |
| 1890 | |
| 1891 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1892 | if (err < 0) |
| 1893 | goto failed; |
| 1894 | |
| 1895 | if (changed) |
| 1896 | err = new_settings(hdev, sk); |
| 1897 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1898 | goto failed; |
| 1899 | } |
| 1900 | |
| 1901 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1902 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1903 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1904 | goto failed; |
| 1905 | } |
| 1906 | |
| 1907 | val = !!cp->val; |
| 1908 | |
| 1909 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1910 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1911 | goto failed; |
| 1912 | } |
| 1913 | |
| 1914 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1915 | if (!cmd) { |
| 1916 | err = -ENOMEM; |
| 1917 | goto failed; |
| 1918 | } |
| 1919 | |
| 1920 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1921 | if (err < 0) { |
| 1922 | mgmt_pending_remove(cmd); |
| 1923 | goto failed; |
| 1924 | } |
| 1925 | |
| 1926 | failed: |
| 1927 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1928 | return err; |
| 1929 | } |
| 1930 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1931 | 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] | 1932 | { |
| 1933 | struct mgmt_mode *cp = data; |
| 1934 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1935 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1936 | int err; |
| 1937 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1938 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1939 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1940 | status = mgmt_bredr_support(hdev); |
| 1941 | if (status) |
| 1942 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1943 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1944 | if (!lmp_ssp_capable(hdev)) |
| 1945 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1946 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1947 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1948 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1949 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1950 | MGMT_STATUS_INVALID_PARAMS); |
| 1951 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1952 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1953 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1954 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1955 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1956 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1957 | if (cp->val) { |
| 1958 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1959 | &hdev->dev_flags); |
| 1960 | } else { |
| 1961 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1962 | &hdev->dev_flags); |
| 1963 | if (!changed) |
| 1964 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1965 | &hdev->dev_flags); |
| 1966 | else |
| 1967 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1971 | if (err < 0) |
| 1972 | goto failed; |
| 1973 | |
| 1974 | if (changed) |
| 1975 | err = new_settings(hdev, sk); |
| 1976 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1977 | goto failed; |
| 1978 | } |
| 1979 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1980 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1981 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1982 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1983 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1984 | goto failed; |
| 1985 | } |
| 1986 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1987 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1988 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1989 | goto failed; |
| 1990 | } |
| 1991 | |
| 1992 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1993 | if (!cmd) { |
| 1994 | err = -ENOMEM; |
| 1995 | goto failed; |
| 1996 | } |
| 1997 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1998 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 1999 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 2000 | sizeof(cp->val), &cp->val); |
| 2001 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 2002 | 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] | 2003 | if (err < 0) { |
| 2004 | mgmt_pending_remove(cmd); |
| 2005 | goto failed; |
| 2006 | } |
| 2007 | |
| 2008 | failed: |
| 2009 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 2010 | return err; |
| 2011 | } |
| 2012 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2013 | 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] | 2014 | { |
| 2015 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2016 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2017 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2018 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2019 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2020 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2021 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 2022 | status = mgmt_bredr_support(hdev); |
| 2023 | if (status) |
| 2024 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2025 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 2026 | if (!lmp_ssp_capable(hdev)) |
| 2027 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2028 | MGMT_STATUS_NOT_SUPPORTED); |
| 2029 | |
| 2030 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 2031 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2032 | MGMT_STATUS_REJECTED); |
| 2033 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2034 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2035 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2036 | MGMT_STATUS_INVALID_PARAMS); |
| 2037 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2038 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2039 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2040 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2041 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2042 | } else { |
| 2043 | if (hdev_is_powered(hdev)) { |
| 2044 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 2045 | MGMT_STATUS_REJECTED); |
| 2046 | goto unlock; |
| 2047 | } |
| 2048 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2049 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 2050 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 2051 | |
| 2052 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 2053 | if (err < 0) |
| 2054 | goto unlock; |
| 2055 | |
| 2056 | if (changed) |
| 2057 | err = new_settings(hdev, sk); |
| 2058 | |
| 2059 | unlock: |
| 2060 | hci_dev_unlock(hdev); |
| 2061 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 2062 | } |
| 2063 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2064 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 2065 | { |
| 2066 | struct cmd_lookup match = { NULL, hdev }; |
| 2067 | |
| 2068 | if (status) { |
| 2069 | u8 mgmt_err = mgmt_status(status); |
| 2070 | |
| 2071 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 2072 | &mgmt_err); |
| 2073 | return; |
| 2074 | } |
| 2075 | |
| 2076 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 2077 | |
| 2078 | new_settings(hdev, match.sk); |
| 2079 | |
| 2080 | if (match.sk) |
| 2081 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2082 | |
| 2083 | /* Make sure the controller has a good default for |
| 2084 | * advertising data. Restrict the update to when LE |
| 2085 | * has actually been enabled. During power on, the |
| 2086 | * update in powered_update_hci will take care of it. |
| 2087 | */ |
| 2088 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2089 | struct hci_request req; |
| 2090 | |
| 2091 | hci_dev_lock(hdev); |
| 2092 | |
| 2093 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2094 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2095 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2096 | hci_req_run(&req, NULL); |
| 2097 | |
| 2098 | hci_dev_unlock(hdev); |
| 2099 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2100 | } |
| 2101 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2102 | 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] | 2103 | { |
| 2104 | struct mgmt_mode *cp = data; |
| 2105 | struct hci_cp_write_le_host_supported hci_cp; |
| 2106 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2107 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2108 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2109 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2110 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2111 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2112 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2113 | if (!lmp_le_capable(hdev)) |
| 2114 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2115 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2116 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2117 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2118 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2119 | MGMT_STATUS_INVALID_PARAMS); |
| 2120 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2121 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2122 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2123 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2124 | MGMT_STATUS_REJECTED); |
| 2125 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2126 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2127 | |
| 2128 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2129 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2130 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2131 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2132 | bool changed = false; |
| 2133 | |
| 2134 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2135 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2136 | changed = true; |
| 2137 | } |
| 2138 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2139 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2140 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2141 | changed = true; |
| 2142 | } |
| 2143 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2144 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2145 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2146 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2147 | |
| 2148 | if (changed) |
| 2149 | err = new_settings(hdev, sk); |
| 2150 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2151 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2152 | } |
| 2153 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2154 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2155 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2156 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2157 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2158 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2162 | if (!cmd) { |
| 2163 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2164 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2165 | } |
| 2166 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2167 | hci_req_init(&req, hdev); |
| 2168 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2169 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2170 | |
| 2171 | if (val) { |
| 2172 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2173 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2174 | } else { |
| 2175 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2176 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2177 | } |
| 2178 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2179 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2180 | &hci_cp); |
| 2181 | |
| 2182 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2183 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2184 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2185 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2186 | unlock: |
| 2187 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2188 | return err; |
| 2189 | } |
| 2190 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2191 | /* This is a helper function to test for pending mgmt commands that can |
| 2192 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2193 | * mgmt command at a time since otherwise we cannot easily track what |
| 2194 | * the current values are, will be, and based on that calculate if a new |
| 2195 | * HCI command needs to be sent and if yes with what value. |
| 2196 | */ |
| 2197 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2198 | { |
| 2199 | struct pending_cmd *cmd; |
| 2200 | |
| 2201 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2202 | switch (cmd->opcode) { |
| 2203 | case MGMT_OP_ADD_UUID: |
| 2204 | case MGMT_OP_REMOVE_UUID: |
| 2205 | case MGMT_OP_SET_DEV_CLASS: |
| 2206 | case MGMT_OP_SET_POWERED: |
| 2207 | return true; |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | return false; |
| 2212 | } |
| 2213 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2214 | static const u8 bluetooth_base_uuid[] = { |
| 2215 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2216 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2217 | }; |
| 2218 | |
| 2219 | static u8 get_uuid_size(const u8 *uuid) |
| 2220 | { |
| 2221 | u32 val; |
| 2222 | |
| 2223 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2224 | return 128; |
| 2225 | |
| 2226 | val = get_unaligned_le32(&uuid[12]); |
| 2227 | if (val > 0xffff) |
| 2228 | return 32; |
| 2229 | |
| 2230 | return 16; |
| 2231 | } |
| 2232 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2233 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2234 | { |
| 2235 | struct pending_cmd *cmd; |
| 2236 | |
| 2237 | hci_dev_lock(hdev); |
| 2238 | |
| 2239 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2240 | if (!cmd) |
| 2241 | goto unlock; |
| 2242 | |
| 2243 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2244 | hdev->dev_class, 3); |
| 2245 | |
| 2246 | mgmt_pending_remove(cmd); |
| 2247 | |
| 2248 | unlock: |
| 2249 | hci_dev_unlock(hdev); |
| 2250 | } |
| 2251 | |
| 2252 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2253 | { |
| 2254 | BT_DBG("status 0x%02x", status); |
| 2255 | |
| 2256 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2257 | } |
| 2258 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2259 | 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] | 2260 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2261 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2262 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2263 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2264 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2265 | int err; |
| 2266 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2267 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2268 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2269 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2270 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2271 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2272 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2273 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2274 | goto failed; |
| 2275 | } |
| 2276 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2277 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2278 | if (!uuid) { |
| 2279 | err = -ENOMEM; |
| 2280 | goto failed; |
| 2281 | } |
| 2282 | |
| 2283 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2284 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2285 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2286 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2287 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2288 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2289 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2290 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2291 | update_class(&req); |
| 2292 | update_eir(&req); |
| 2293 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2294 | err = hci_req_run(&req, add_uuid_complete); |
| 2295 | if (err < 0) { |
| 2296 | if (err != -ENODATA) |
| 2297 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2298 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2299 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2300 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2301 | goto failed; |
| 2302 | } |
| 2303 | |
| 2304 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2305 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2306 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2307 | goto failed; |
| 2308 | } |
| 2309 | |
| 2310 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2311 | |
| 2312 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2313 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2314 | return err; |
| 2315 | } |
| 2316 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2317 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2318 | { |
| 2319 | if (!hdev_is_powered(hdev)) |
| 2320 | return false; |
| 2321 | |
| 2322 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2323 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2324 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2325 | return true; |
| 2326 | } |
| 2327 | |
| 2328 | return false; |
| 2329 | } |
| 2330 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2331 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2332 | { |
| 2333 | BT_DBG("status 0x%02x", status); |
| 2334 | |
| 2335 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2336 | } |
| 2337 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2338 | 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] | 2339 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2340 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2341 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2342 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2343 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2344 | 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] | 2345 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2346 | int err, found; |
| 2347 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2348 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2349 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2350 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2351 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2352 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2353 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2354 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2355 | goto unlock; |
| 2356 | } |
| 2357 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2358 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2359 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2360 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2361 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2362 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2363 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2364 | goto unlock; |
| 2365 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2366 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2367 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | found = 0; |
| 2371 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2372 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2373 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2374 | continue; |
| 2375 | |
| 2376 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2377 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2378 | found++; |
| 2379 | } |
| 2380 | |
| 2381 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2382 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2383 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2384 | goto unlock; |
| 2385 | } |
| 2386 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2387 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2388 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2389 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2390 | update_class(&req); |
| 2391 | update_eir(&req); |
| 2392 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2393 | err = hci_req_run(&req, remove_uuid_complete); |
| 2394 | if (err < 0) { |
| 2395 | if (err != -ENODATA) |
| 2396 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2397 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2398 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2399 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2400 | goto unlock; |
| 2401 | } |
| 2402 | |
| 2403 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2404 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2405 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2406 | goto unlock; |
| 2407 | } |
| 2408 | |
| 2409 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2410 | |
| 2411 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2412 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2413 | return err; |
| 2414 | } |
| 2415 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2416 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2417 | { |
| 2418 | BT_DBG("status 0x%02x", status); |
| 2419 | |
| 2420 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2421 | } |
| 2422 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2423 | 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] | 2424 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2425 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2426 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2427 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2428 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2429 | int err; |
| 2430 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2431 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2432 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2433 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2434 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2435 | MGMT_STATUS_NOT_SUPPORTED); |
| 2436 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2437 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2438 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2439 | if (pending_eir_or_class(hdev)) { |
| 2440 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2441 | MGMT_STATUS_BUSY); |
| 2442 | goto unlock; |
| 2443 | } |
| 2444 | |
| 2445 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2446 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2447 | MGMT_STATUS_INVALID_PARAMS); |
| 2448 | goto unlock; |
| 2449 | } |
| 2450 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2451 | hdev->major_class = cp->major; |
| 2452 | hdev->minor_class = cp->minor; |
| 2453 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2454 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2455 | 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] | 2456 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2457 | goto unlock; |
| 2458 | } |
| 2459 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2460 | hci_req_init(&req, hdev); |
| 2461 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2462 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2463 | hci_dev_unlock(hdev); |
| 2464 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2465 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2466 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2467 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2468 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2469 | update_class(&req); |
| 2470 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2471 | err = hci_req_run(&req, set_class_complete); |
| 2472 | if (err < 0) { |
| 2473 | if (err != -ENODATA) |
| 2474 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2475 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2476 | 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] | 2477 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2478 | goto unlock; |
| 2479 | } |
| 2480 | |
| 2481 | 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] | 2482 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2483 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2484 | goto unlock; |
| 2485 | } |
| 2486 | |
| 2487 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2488 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2489 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2490 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2491 | return err; |
| 2492 | } |
| 2493 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2494 | 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] | 2495 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2496 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2497 | struct mgmt_cp_load_link_keys *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2498 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 2499 | sizeof(struct mgmt_link_key_info)); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2500 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2501 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2502 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2503 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2504 | BT_DBG("request for %s", hdev->name); |
| 2505 | |
| 2506 | if (!lmp_bredr_capable(hdev)) |
| 2507 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2508 | MGMT_STATUS_NOT_SUPPORTED); |
| 2509 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2510 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 2511 | if (key_count > max_key_count) { |
| 2512 | BT_ERR("load_link_keys: too big key_count value %u", |
| 2513 | key_count); |
| 2514 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2515 | MGMT_STATUS_INVALID_PARAMS); |
| 2516 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2517 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2518 | expected_len = sizeof(*cp) + key_count * |
| 2519 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2520 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2521 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2522 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2523 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2524 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2525 | } |
| 2526 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2527 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2528 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2529 | MGMT_STATUS_INVALID_PARAMS); |
| 2530 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2531 | 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] | 2532 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2533 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2534 | for (i = 0; i < key_count; i++) { |
| 2535 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2536 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2537 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2538 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2539 | MGMT_STATUS_INVALID_PARAMS); |
| 2540 | } |
| 2541 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2542 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2543 | |
| 2544 | hci_link_keys_clear(hdev); |
| 2545 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2546 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2547 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2548 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2549 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2550 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2551 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2552 | |
| 2553 | if (changed) |
| 2554 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2555 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2556 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2557 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2558 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2559 | /* Always ignore debug keys and require a new pairing if |
| 2560 | * the user wants to use them. |
| 2561 | */ |
| 2562 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2563 | continue; |
| 2564 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2565 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2566 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2567 | } |
| 2568 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2569 | 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] | 2570 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2571 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2572 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2573 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2574 | } |
| 2575 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2576 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2577 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2578 | { |
| 2579 | struct mgmt_ev_device_unpaired ev; |
| 2580 | |
| 2581 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2582 | ev.addr.type = addr_type; |
| 2583 | |
| 2584 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2585 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2586 | } |
| 2587 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2588 | 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] | 2589 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2590 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2591 | struct mgmt_cp_unpair_device *cp = data; |
| 2592 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2593 | struct hci_cp_disconnect dc; |
| 2594 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2595 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2596 | int err; |
| 2597 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2598 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2599 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2600 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2601 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2602 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2603 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2604 | MGMT_STATUS_INVALID_PARAMS, |
| 2605 | &rp, sizeof(rp)); |
| 2606 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2607 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2608 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2609 | MGMT_STATUS_INVALID_PARAMS, |
| 2610 | &rp, sizeof(rp)); |
| 2611 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2612 | hci_dev_lock(hdev); |
| 2613 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2614 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2615 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2616 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2617 | goto unlock; |
| 2618 | } |
| 2619 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2620 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2621 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2622 | } else { |
| 2623 | u8 addr_type; |
| 2624 | |
| 2625 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2626 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2627 | else |
| 2628 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2629 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2630 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2631 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2632 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2633 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2634 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2635 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2636 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2637 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2638 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2639 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2640 | goto unlock; |
| 2641 | } |
| 2642 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2643 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2644 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2645 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2646 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2647 | else |
| 2648 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2649 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2650 | } else { |
| 2651 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2652 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2653 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2654 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2655 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2656 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2657 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2658 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2659 | } |
| 2660 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2661 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2662 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2663 | if (!cmd) { |
| 2664 | err = -ENOMEM; |
| 2665 | goto unlock; |
| 2666 | } |
| 2667 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2668 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2669 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2670 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2671 | if (err < 0) |
| 2672 | mgmt_pending_remove(cmd); |
| 2673 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2674 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2675 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2676 | return err; |
| 2677 | } |
| 2678 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2679 | 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] | 2680 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2681 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2682 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2683 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2684 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2685 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2686 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2687 | int err; |
| 2688 | |
| 2689 | BT_DBG(""); |
| 2690 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2691 | memset(&rp, 0, sizeof(rp)); |
| 2692 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2693 | rp.addr.type = cp->addr.type; |
| 2694 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2695 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2696 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2697 | MGMT_STATUS_INVALID_PARAMS, |
| 2698 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2699 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2700 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2701 | |
| 2702 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2703 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2704 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2705 | goto failed; |
| 2706 | } |
| 2707 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2708 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2709 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2710 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2711 | goto failed; |
| 2712 | } |
| 2713 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2714 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2715 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2716 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2717 | else |
| 2718 | 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] | 2719 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2720 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2721 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2722 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2723 | goto failed; |
| 2724 | } |
| 2725 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2726 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2727 | if (!cmd) { |
| 2728 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2729 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2730 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2731 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2732 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2733 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2734 | |
| 2735 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2736 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2737 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2738 | |
| 2739 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2740 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2741 | return err; |
| 2742 | } |
| 2743 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2744 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2745 | { |
| 2746 | switch (link_type) { |
| 2747 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2748 | switch (addr_type) { |
| 2749 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2750 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2751 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2752 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2753 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2754 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2755 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2756 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2757 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2758 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2759 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2760 | } |
| 2761 | } |
| 2762 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2763 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2764 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2765 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2766 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2767 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2768 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2769 | int err; |
| 2770 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2771 | |
| 2772 | BT_DBG(""); |
| 2773 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2774 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2775 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2776 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2777 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2778 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2779 | goto unlock; |
| 2780 | } |
| 2781 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2782 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2783 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2784 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2785 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2786 | } |
| 2787 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2788 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2789 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2790 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2791 | err = -ENOMEM; |
| 2792 | goto unlock; |
| 2793 | } |
| 2794 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2795 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2796 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2797 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2798 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2799 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2800 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2801 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2802 | continue; |
| 2803 | i++; |
| 2804 | } |
| 2805 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2806 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2807 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2808 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2809 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2810 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2811 | 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] | 2812 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2813 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2814 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2815 | |
| 2816 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2817 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2818 | return err; |
| 2819 | } |
| 2820 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2821 | 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] | 2822 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2823 | { |
| 2824 | struct pending_cmd *cmd; |
| 2825 | int err; |
| 2826 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2827 | 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] | 2828 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2829 | if (!cmd) |
| 2830 | return -ENOMEM; |
| 2831 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2832 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2833 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2834 | if (err < 0) |
| 2835 | mgmt_pending_remove(cmd); |
| 2836 | |
| 2837 | return err; |
| 2838 | } |
| 2839 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2840 | 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] | 2841 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2842 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2843 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2844 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2845 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2846 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2847 | int err; |
| 2848 | |
| 2849 | BT_DBG(""); |
| 2850 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2851 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2852 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2853 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2854 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2855 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2856 | goto failed; |
| 2857 | } |
| 2858 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2859 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2860 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2861 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2862 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2863 | goto failed; |
| 2864 | } |
| 2865 | |
| 2866 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2867 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2868 | |
| 2869 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2870 | |
| 2871 | BT_ERR("PIN code is not 16 bytes long"); |
| 2872 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2873 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2874 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2875 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2876 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2877 | |
| 2878 | goto failed; |
| 2879 | } |
| 2880 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2881 | 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] | 2882 | if (!cmd) { |
| 2883 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2884 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2885 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2886 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2887 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2888 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2889 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2890 | |
| 2891 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2892 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2893 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2894 | |
| 2895 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2896 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2897 | return err; |
| 2898 | } |
| 2899 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2900 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2901 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2902 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2903 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2904 | |
| 2905 | BT_DBG(""); |
| 2906 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2907 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2908 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2909 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2910 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2911 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2912 | |
| 2913 | hdev->io_capability = cp->io_capability; |
| 2914 | |
| 2915 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2916 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2917 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2918 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2919 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2920 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2921 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2924 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2925 | { |
| 2926 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2927 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2928 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2929 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2930 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2931 | continue; |
| 2932 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2933 | if (cmd->user_data != conn) |
| 2934 | continue; |
| 2935 | |
| 2936 | return cmd; |
| 2937 | } |
| 2938 | |
| 2939 | return NULL; |
| 2940 | } |
| 2941 | |
| 2942 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2943 | { |
| 2944 | struct mgmt_rp_pair_device rp; |
| 2945 | struct hci_conn *conn = cmd->user_data; |
| 2946 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2947 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2948 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2949 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2950 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2951 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2952 | |
| 2953 | /* So we don't get further callbacks for this connection */ |
| 2954 | conn->connect_cfm_cb = NULL; |
| 2955 | conn->security_cfm_cb = NULL; |
| 2956 | conn->disconn_cfm_cb = NULL; |
| 2957 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2958 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2959 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2960 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2961 | } |
| 2962 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2963 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2964 | { |
| 2965 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2966 | struct pending_cmd *cmd; |
| 2967 | |
| 2968 | cmd = find_pairing(conn); |
| 2969 | if (cmd) |
| 2970 | pairing_complete(cmd, status); |
| 2971 | } |
| 2972 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2973 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2974 | { |
| 2975 | struct pending_cmd *cmd; |
| 2976 | |
| 2977 | BT_DBG("status %u", status); |
| 2978 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2979 | cmd = find_pairing(conn); |
| 2980 | if (!cmd) |
| 2981 | BT_DBG("Unable to find a pending command"); |
| 2982 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2983 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2984 | } |
| 2985 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2986 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2987 | { |
| 2988 | struct pending_cmd *cmd; |
| 2989 | |
| 2990 | BT_DBG("status %u", status); |
| 2991 | |
| 2992 | if (!status) |
| 2993 | return; |
| 2994 | |
| 2995 | cmd = find_pairing(conn); |
| 2996 | if (!cmd) |
| 2997 | BT_DBG("Unable to find a pending command"); |
| 2998 | else |
| 2999 | pairing_complete(cmd, mgmt_status(status)); |
| 3000 | } |
| 3001 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3002 | 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] | 3003 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3004 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3005 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 3006 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3007 | struct pending_cmd *cmd; |
| 3008 | u8 sec_level, auth_type; |
| 3009 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3010 | int err; |
| 3011 | |
| 3012 | BT_DBG(""); |
| 3013 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3014 | memset(&rp, 0, sizeof(rp)); |
| 3015 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 3016 | rp.addr.type = cp->addr.type; |
| 3017 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3018 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 3019 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3020 | MGMT_STATUS_INVALID_PARAMS, |
| 3021 | &rp, sizeof(rp)); |
| 3022 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 3023 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 3024 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3025 | MGMT_STATUS_INVALID_PARAMS, |
| 3026 | &rp, sizeof(rp)); |
| 3027 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3028 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3029 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3030 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 3031 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 3032 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3033 | goto unlock; |
| 3034 | } |
| 3035 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 3036 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 3037 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3038 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3039 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3040 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 3041 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3042 | } else { |
| 3043 | u8 addr_type; |
| 3044 | |
| 3045 | /* Convert from L2CAP channel address type to HCI address type |
| 3046 | */ |
| 3047 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 3048 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 3049 | else |
| 3050 | addr_type = ADDR_LE_DEV_RANDOM; |
| 3051 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 3052 | /* When pairing a new device, it is expected to remember |
| 3053 | * this device for future connections. Adding the connection |
| 3054 | * parameter information ahead of time allows tracking |
| 3055 | * of the slave preferred values and will speed up any |
| 3056 | * further connection establishment. |
| 3057 | * |
| 3058 | * If connection parameters already exist, then they |
| 3059 | * will be kept and this function does nothing. |
| 3060 | */ |
| 3061 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 3062 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3063 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 3064 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 3065 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3066 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 3067 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3068 | int status; |
| 3069 | |
| 3070 | if (PTR_ERR(conn) == -EBUSY) |
| 3071 | status = MGMT_STATUS_BUSY; |
| 3072 | else |
| 3073 | status = MGMT_STATUS_CONNECT_FAILED; |
| 3074 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3075 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 3076 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3077 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3078 | goto unlock; |
| 3079 | } |
| 3080 | |
| 3081 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3082 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3083 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3084 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3085 | goto unlock; |
| 3086 | } |
| 3087 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3088 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3089 | if (!cmd) { |
| 3090 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 3091 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3092 | goto unlock; |
| 3093 | } |
| 3094 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3095 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3096 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3097 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 3098 | conn->security_cfm_cb = pairing_complete_cb; |
| 3099 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 3100 | } else { |
| 3101 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3102 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3103 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3104 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3105 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3106 | conn->io_capability = cp->io_cap; |
| 3107 | cmd->user_data = conn; |
| 3108 | |
| 3109 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3110 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3111 | pairing_complete(cmd, 0); |
| 3112 | |
| 3113 | err = 0; |
| 3114 | |
| 3115 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3116 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3117 | return err; |
| 3118 | } |
| 3119 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3120 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3121 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3122 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3123 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3124 | struct pending_cmd *cmd; |
| 3125 | struct hci_conn *conn; |
| 3126 | int err; |
| 3127 | |
| 3128 | BT_DBG(""); |
| 3129 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3130 | hci_dev_lock(hdev); |
| 3131 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3132 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3133 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3134 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3135 | goto unlock; |
| 3136 | } |
| 3137 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3138 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3139 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3140 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3141 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3142 | goto unlock; |
| 3143 | } |
| 3144 | |
| 3145 | conn = cmd->user_data; |
| 3146 | |
| 3147 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3148 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3149 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3150 | goto unlock; |
| 3151 | } |
| 3152 | |
| 3153 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3154 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3155 | 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] | 3156 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3157 | unlock: |
| 3158 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3159 | return err; |
| 3160 | } |
| 3161 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3162 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3163 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3164 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3165 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3166 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3167 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3168 | int err; |
| 3169 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3170 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3171 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3172 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3173 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3174 | MGMT_STATUS_NOT_POWERED, addr, |
| 3175 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3176 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3177 | } |
| 3178 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3179 | if (addr->type == BDADDR_BREDR) |
| 3180 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3181 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3182 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3183 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3184 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3185 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3186 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3187 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3188 | goto done; |
| 3189 | } |
| 3190 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3191 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3192 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3193 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3194 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3195 | MGMT_STATUS_SUCCESS, addr, |
| 3196 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3197 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3198 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3199 | MGMT_STATUS_FAILED, addr, |
| 3200 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3201 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3202 | goto done; |
| 3203 | } |
| 3204 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3205 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3206 | if (!cmd) { |
| 3207 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3208 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3209 | } |
| 3210 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3211 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3212 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3213 | struct hci_cp_user_passkey_reply cp; |
| 3214 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3215 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3216 | cp.passkey = passkey; |
| 3217 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3218 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3219 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3220 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3221 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3222 | if (err < 0) |
| 3223 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3224 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3225 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3226 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3227 | return err; |
| 3228 | } |
| 3229 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3230 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3231 | void *data, u16 len) |
| 3232 | { |
| 3233 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3234 | |
| 3235 | BT_DBG(""); |
| 3236 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3237 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3238 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3239 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3240 | } |
| 3241 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3242 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3243 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3244 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3245 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3246 | |
| 3247 | BT_DBG(""); |
| 3248 | |
| 3249 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3250 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3251 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3252 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3253 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3254 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3255 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3256 | } |
| 3257 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3258 | 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] | 3259 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3260 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3261 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3262 | |
| 3263 | BT_DBG(""); |
| 3264 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3265 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3266 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3267 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3268 | } |
| 3269 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3270 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3271 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3272 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3273 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3274 | |
| 3275 | BT_DBG(""); |
| 3276 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3277 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3278 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3279 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3280 | } |
| 3281 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3282 | 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] | 3283 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3284 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3285 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3286 | |
| 3287 | BT_DBG(""); |
| 3288 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3289 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3290 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3291 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3292 | } |
| 3293 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3294 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3295 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3296 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3297 | struct hci_cp_write_local_name cp; |
| 3298 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3299 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3300 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3301 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3302 | } |
| 3303 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3304 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3305 | { |
| 3306 | struct mgmt_cp_set_local_name *cp; |
| 3307 | struct pending_cmd *cmd; |
| 3308 | |
| 3309 | BT_DBG("status 0x%02x", status); |
| 3310 | |
| 3311 | hci_dev_lock(hdev); |
| 3312 | |
| 3313 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3314 | if (!cmd) |
| 3315 | goto unlock; |
| 3316 | |
| 3317 | cp = cmd->param; |
| 3318 | |
| 3319 | if (status) |
| 3320 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3321 | mgmt_status(status)); |
| 3322 | else |
| 3323 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3324 | cp, sizeof(*cp)); |
| 3325 | |
| 3326 | mgmt_pending_remove(cmd); |
| 3327 | |
| 3328 | unlock: |
| 3329 | hci_dev_unlock(hdev); |
| 3330 | } |
| 3331 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3332 | 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] | 3333 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3334 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3335 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3336 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3337 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3338 | int err; |
| 3339 | |
| 3340 | BT_DBG(""); |
| 3341 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3342 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3343 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3344 | /* If the old values are the same as the new ones just return a |
| 3345 | * direct command complete event. |
| 3346 | */ |
| 3347 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3348 | !memcmp(hdev->short_name, cp->short_name, |
| 3349 | sizeof(hdev->short_name))) { |
| 3350 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3351 | data, len); |
| 3352 | goto failed; |
| 3353 | } |
| 3354 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3355 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3356 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3357 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3358 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3359 | |
| 3360 | 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] | 3361 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3362 | if (err < 0) |
| 3363 | goto failed; |
| 3364 | |
| 3365 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3366 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3367 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3368 | goto failed; |
| 3369 | } |
| 3370 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3371 | 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] | 3372 | if (!cmd) { |
| 3373 | err = -ENOMEM; |
| 3374 | goto failed; |
| 3375 | } |
| 3376 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3377 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3378 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3379 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3380 | |
| 3381 | if (lmp_bredr_capable(hdev)) { |
| 3382 | update_name(&req); |
| 3383 | update_eir(&req); |
| 3384 | } |
| 3385 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3386 | /* The name is stored in the scan response data and so |
| 3387 | * no need to udpate the advertising data here. |
| 3388 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3389 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3390 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3391 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3392 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3393 | if (err < 0) |
| 3394 | mgmt_pending_remove(cmd); |
| 3395 | |
| 3396 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3397 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3398 | return err; |
| 3399 | } |
| 3400 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3401 | 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] | 3402 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3403 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3404 | struct pending_cmd *cmd; |
| 3405 | int err; |
| 3406 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3407 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3408 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3409 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3410 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3411 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3412 | 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] | 3413 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3414 | goto unlock; |
| 3415 | } |
| 3416 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3417 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3418 | 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] | 3419 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3420 | goto unlock; |
| 3421 | } |
| 3422 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3423 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3424 | 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] | 3425 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3426 | goto unlock; |
| 3427 | } |
| 3428 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3429 | 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] | 3430 | if (!cmd) { |
| 3431 | err = -ENOMEM; |
| 3432 | goto unlock; |
| 3433 | } |
| 3434 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3435 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3436 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3437 | 0, NULL); |
| 3438 | else |
| 3439 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3440 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3441 | if (err < 0) |
| 3442 | mgmt_pending_remove(cmd); |
| 3443 | |
| 3444 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3445 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3446 | return err; |
| 3447 | } |
| 3448 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3449 | 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] | 3450 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3451 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3452 | int err; |
| 3453 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3454 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3455 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3456 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3457 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3458 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3459 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3460 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3461 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3462 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3463 | cp->hash, cp->randomizer); |
| 3464 | if (err < 0) |
| 3465 | status = MGMT_STATUS_FAILED; |
| 3466 | else |
| 3467 | status = MGMT_STATUS_SUCCESS; |
| 3468 | |
| 3469 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3470 | status, &cp->addr, sizeof(cp->addr)); |
| 3471 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3472 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3473 | u8 status; |
| 3474 | |
| 3475 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3476 | cp->hash192, |
| 3477 | cp->randomizer192, |
| 3478 | cp->hash256, |
| 3479 | cp->randomizer256); |
| 3480 | if (err < 0) |
| 3481 | status = MGMT_STATUS_FAILED; |
| 3482 | else |
| 3483 | status = MGMT_STATUS_SUCCESS; |
| 3484 | |
| 3485 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3486 | status, &cp->addr, sizeof(cp->addr)); |
| 3487 | } else { |
| 3488 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3489 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3490 | MGMT_STATUS_INVALID_PARAMS); |
| 3491 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3492 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3493 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3494 | return err; |
| 3495 | } |
| 3496 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3497 | 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] | 3498 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3499 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3500 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3501 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3502 | int err; |
| 3503 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3504 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3505 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3506 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3507 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3508 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3509 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3510 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3511 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3512 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3513 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3514 | 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] | 3515 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3516 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3517 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3518 | return err; |
| 3519 | } |
| 3520 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3521 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3522 | { |
| 3523 | struct pending_cmd *cmd; |
| 3524 | u8 type; |
| 3525 | int err; |
| 3526 | |
| 3527 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3528 | |
| 3529 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3530 | if (!cmd) |
| 3531 | return -ENOENT; |
| 3532 | |
| 3533 | type = hdev->discovery.type; |
| 3534 | |
| 3535 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3536 | &type, sizeof(type)); |
| 3537 | mgmt_pending_remove(cmd); |
| 3538 | |
| 3539 | return err; |
| 3540 | } |
| 3541 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3542 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3543 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3544 | unsigned long timeout = 0; |
| 3545 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3546 | BT_DBG("status %d", status); |
| 3547 | |
| 3548 | if (status) { |
| 3549 | hci_dev_lock(hdev); |
| 3550 | mgmt_start_discovery_failed(hdev, status); |
| 3551 | hci_dev_unlock(hdev); |
| 3552 | return; |
| 3553 | } |
| 3554 | |
| 3555 | hci_dev_lock(hdev); |
| 3556 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3557 | hci_dev_unlock(hdev); |
| 3558 | |
| 3559 | switch (hdev->discovery.type) { |
| 3560 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3561 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3562 | break; |
| 3563 | |
| 3564 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3565 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3566 | break; |
| 3567 | |
| 3568 | case DISCOV_TYPE_BREDR: |
| 3569 | break; |
| 3570 | |
| 3571 | default: |
| 3572 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3573 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3574 | |
| 3575 | if (!timeout) |
| 3576 | return; |
| 3577 | |
| 3578 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3579 | } |
| 3580 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3581 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3582 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3583 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3584 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3585 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3586 | struct hci_cp_le_set_scan_param param_cp; |
| 3587 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3588 | struct hci_cp_inquiry inq_cp; |
| 3589 | struct hci_request req; |
| 3590 | /* General inquiry access code (GIAC) */ |
| 3591 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3592 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3593 | int err; |
| 3594 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3595 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3596 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3597 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3598 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3599 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3600 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3601 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3602 | goto failed; |
| 3603 | } |
| 3604 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3605 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3606 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3607 | MGMT_STATUS_BUSY); |
| 3608 | goto failed; |
| 3609 | } |
| 3610 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3611 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3612 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3613 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3614 | goto failed; |
| 3615 | } |
| 3616 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3617 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3618 | if (!cmd) { |
| 3619 | err = -ENOMEM; |
| 3620 | goto failed; |
| 3621 | } |
| 3622 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3623 | hdev->discovery.type = cp->type; |
| 3624 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3625 | hci_req_init(&req, hdev); |
| 3626 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3627 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3628 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3629 | status = mgmt_bredr_support(hdev); |
| 3630 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3631 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3632 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3633 | mgmt_pending_remove(cmd); |
| 3634 | goto failed; |
| 3635 | } |
| 3636 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3637 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3638 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3639 | MGMT_STATUS_BUSY); |
| 3640 | mgmt_pending_remove(cmd); |
| 3641 | goto failed; |
| 3642 | } |
| 3643 | |
| 3644 | hci_inquiry_cache_flush(hdev); |
| 3645 | |
| 3646 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3647 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3648 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3649 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3650 | break; |
| 3651 | |
| 3652 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3653 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3654 | status = mgmt_le_support(hdev); |
| 3655 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3656 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3657 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3658 | mgmt_pending_remove(cmd); |
| 3659 | goto failed; |
| 3660 | } |
| 3661 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3662 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3663 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3664 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3665 | MGMT_STATUS_NOT_SUPPORTED); |
| 3666 | mgmt_pending_remove(cmd); |
| 3667 | goto failed; |
| 3668 | } |
| 3669 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3670 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3671 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3672 | MGMT_STATUS_REJECTED); |
| 3673 | mgmt_pending_remove(cmd); |
| 3674 | goto failed; |
| 3675 | } |
| 3676 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3677 | /* If controller is scanning, it means the background scanning |
| 3678 | * is running. Thus, we should temporarily stop it in order to |
| 3679 | * set the discovery scanning parameters. |
| 3680 | */ |
| 3681 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3682 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3683 | |
| 3684 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3685 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3686 | /* All active scans will be done with either a resolvable |
| 3687 | * private address (when privacy feature has been enabled) |
| 3688 | * or unresolvable private address. |
| 3689 | */ |
| 3690 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3691 | if (err < 0) { |
| 3692 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3693 | MGMT_STATUS_FAILED); |
| 3694 | mgmt_pending_remove(cmd); |
| 3695 | goto failed; |
| 3696 | } |
| 3697 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3698 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3699 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3700 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3701 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3702 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3703 | ¶m_cp); |
| 3704 | |
| 3705 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3706 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3707 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3708 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3709 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3710 | break; |
| 3711 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3712 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3713 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3714 | MGMT_STATUS_INVALID_PARAMS); |
| 3715 | mgmt_pending_remove(cmd); |
| 3716 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3717 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3718 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3719 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3720 | if (err < 0) |
| 3721 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3722 | else |
| 3723 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3724 | |
| 3725 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3726 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3727 | return err; |
| 3728 | } |
| 3729 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3730 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3731 | { |
| 3732 | struct pending_cmd *cmd; |
| 3733 | int err; |
| 3734 | |
| 3735 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3736 | if (!cmd) |
| 3737 | return -ENOENT; |
| 3738 | |
| 3739 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3740 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3741 | mgmt_pending_remove(cmd); |
| 3742 | |
| 3743 | return err; |
| 3744 | } |
| 3745 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3746 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3747 | { |
| 3748 | BT_DBG("status %d", status); |
| 3749 | |
| 3750 | hci_dev_lock(hdev); |
| 3751 | |
| 3752 | if (status) { |
| 3753 | mgmt_stop_discovery_failed(hdev, status); |
| 3754 | goto unlock; |
| 3755 | } |
| 3756 | |
| 3757 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3758 | |
| 3759 | unlock: |
| 3760 | hci_dev_unlock(hdev); |
| 3761 | } |
| 3762 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3763 | 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] | 3764 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3765 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3766 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3767 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3768 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3769 | int err; |
| 3770 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3771 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3772 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3773 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3774 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3775 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3776 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3777 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3778 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3779 | goto unlock; |
| 3780 | } |
| 3781 | |
| 3782 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3783 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3784 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3785 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3786 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3787 | } |
| 3788 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3789 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3790 | if (!cmd) { |
| 3791 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3792 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3793 | } |
| 3794 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3795 | hci_req_init(&req, hdev); |
| 3796 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3797 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3798 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3799 | err = hci_req_run(&req, stop_discovery_complete); |
| 3800 | if (!err) { |
| 3801 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3802 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3803 | } |
| 3804 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3805 | mgmt_pending_remove(cmd); |
| 3806 | |
| 3807 | /* If no HCI commands were sent we're done */ |
| 3808 | if (err == -ENODATA) { |
| 3809 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3810 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3811 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3812 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3813 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3814 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3815 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3816 | return err; |
| 3817 | } |
| 3818 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3819 | 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] | 3820 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3821 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3822 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3823 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3824 | int err; |
| 3825 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3826 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3827 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3828 | hci_dev_lock(hdev); |
| 3829 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3830 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3831 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3832 | MGMT_STATUS_FAILED, &cp->addr, |
| 3833 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3834 | goto failed; |
| 3835 | } |
| 3836 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3837 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3838 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3839 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3840 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3841 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3842 | goto failed; |
| 3843 | } |
| 3844 | |
| 3845 | if (cp->name_known) { |
| 3846 | e->name_state = NAME_KNOWN; |
| 3847 | list_del(&e->list); |
| 3848 | } else { |
| 3849 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3850 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3851 | } |
| 3852 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3853 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3854 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3855 | |
| 3856 | failed: |
| 3857 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3858 | return err; |
| 3859 | } |
| 3860 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3861 | 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] | 3862 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3863 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3864 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3865 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3866 | int err; |
| 3867 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3868 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3869 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3870 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3871 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3872 | MGMT_STATUS_INVALID_PARAMS, |
| 3873 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3874 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3875 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3876 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3877 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3878 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3879 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3880 | goto done; |
| 3881 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3882 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3883 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3884 | sk); |
| 3885 | status = MGMT_STATUS_SUCCESS; |
| 3886 | |
| 3887 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3888 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3889 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3890 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3891 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3892 | |
| 3893 | return err; |
| 3894 | } |
| 3895 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3896 | 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] | 3897 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3898 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3899 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3900 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3901 | int err; |
| 3902 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3903 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3904 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3905 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3906 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3907 | MGMT_STATUS_INVALID_PARAMS, |
| 3908 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3909 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3910 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3911 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3912 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3913 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3914 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3915 | goto done; |
| 3916 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3917 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame] | 3918 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3919 | sk); |
| 3920 | status = MGMT_STATUS_SUCCESS; |
| 3921 | |
| 3922 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3923 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3924 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3925 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3926 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3927 | |
| 3928 | return err; |
| 3929 | } |
| 3930 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3931 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3932 | u16 len) |
| 3933 | { |
| 3934 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3935 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3936 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3937 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3938 | |
| 3939 | BT_DBG("%s", hdev->name); |
| 3940 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3941 | source = __le16_to_cpu(cp->source); |
| 3942 | |
| 3943 | if (source > 0x0002) |
| 3944 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3945 | MGMT_STATUS_INVALID_PARAMS); |
| 3946 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3947 | hci_dev_lock(hdev); |
| 3948 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3949 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3950 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3951 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3952 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3953 | |
| 3954 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3955 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3956 | hci_req_init(&req, hdev); |
| 3957 | update_eir(&req); |
| 3958 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3959 | |
| 3960 | hci_dev_unlock(hdev); |
| 3961 | |
| 3962 | return err; |
| 3963 | } |
| 3964 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3965 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3966 | { |
| 3967 | struct cmd_lookup match = { NULL, hdev }; |
| 3968 | |
| 3969 | if (status) { |
| 3970 | u8 mgmt_err = mgmt_status(status); |
| 3971 | |
| 3972 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3973 | cmd_status_rsp, &mgmt_err); |
| 3974 | return; |
| 3975 | } |
| 3976 | |
| 3977 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3978 | &match); |
| 3979 | |
| 3980 | new_settings(hdev, match.sk); |
| 3981 | |
| 3982 | if (match.sk) |
| 3983 | sock_put(match.sk); |
| 3984 | } |
| 3985 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3986 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3987 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3988 | { |
| 3989 | struct mgmt_mode *cp = data; |
| 3990 | struct pending_cmd *cmd; |
| 3991 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3992 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3993 | int err; |
| 3994 | |
| 3995 | BT_DBG("request for %s", hdev->name); |
| 3996 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3997 | status = mgmt_le_support(hdev); |
| 3998 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3999 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 4000 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4001 | |
| 4002 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4003 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4004 | MGMT_STATUS_INVALID_PARAMS); |
| 4005 | |
| 4006 | hci_dev_lock(hdev); |
| 4007 | |
| 4008 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4009 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4010 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 4011 | /* The following conditions are ones which mean that we should |
| 4012 | * not do any HCI communication but directly send a mgmt |
| 4013 | * response to user space (after toggling the flag if |
| 4014 | * necessary). |
| 4015 | */ |
| 4016 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 4017 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4018 | bool changed = false; |
| 4019 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 4020 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 4021 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4022 | changed = true; |
| 4023 | } |
| 4024 | |
| 4025 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 4026 | if (err < 0) |
| 4027 | goto unlock; |
| 4028 | |
| 4029 | if (changed) |
| 4030 | err = new_settings(hdev, sk); |
| 4031 | |
| 4032 | goto unlock; |
| 4033 | } |
| 4034 | |
| 4035 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 4036 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 4037 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 4038 | MGMT_STATUS_BUSY); |
| 4039 | goto unlock; |
| 4040 | } |
| 4041 | |
| 4042 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 4043 | if (!cmd) { |
| 4044 | err = -ENOMEM; |
| 4045 | goto unlock; |
| 4046 | } |
| 4047 | |
| 4048 | hci_req_init(&req, hdev); |
| 4049 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4050 | if (val) |
| 4051 | enable_advertising(&req); |
| 4052 | else |
| 4053 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4054 | |
| 4055 | err = hci_req_run(&req, set_advertising_complete); |
| 4056 | if (err < 0) |
| 4057 | mgmt_pending_remove(cmd); |
| 4058 | |
| 4059 | unlock: |
| 4060 | hci_dev_unlock(hdev); |
| 4061 | return err; |
| 4062 | } |
| 4063 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4064 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 4065 | void *data, u16 len) |
| 4066 | { |
| 4067 | struct mgmt_cp_set_static_address *cp = data; |
| 4068 | int err; |
| 4069 | |
| 4070 | BT_DBG("%s", hdev->name); |
| 4071 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4072 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4073 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 4074 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4075 | |
| 4076 | if (hdev_is_powered(hdev)) |
| 4077 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 4078 | MGMT_STATUS_REJECTED); |
| 4079 | |
| 4080 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 4081 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 4082 | return cmd_status(sk, hdev->id, |
| 4083 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4084 | MGMT_STATUS_INVALID_PARAMS); |
| 4085 | |
| 4086 | /* Two most significant bits shall be set */ |
| 4087 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4088 | return cmd_status(sk, hdev->id, |
| 4089 | MGMT_OP_SET_STATIC_ADDRESS, |
| 4090 | MGMT_STATUS_INVALID_PARAMS); |
| 4091 | } |
| 4092 | |
| 4093 | hci_dev_lock(hdev); |
| 4094 | |
| 4095 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 4096 | |
| 4097 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 4098 | |
| 4099 | hci_dev_unlock(hdev); |
| 4100 | |
| 4101 | return err; |
| 4102 | } |
| 4103 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4104 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4105 | void *data, u16 len) |
| 4106 | { |
| 4107 | struct mgmt_cp_set_scan_params *cp = data; |
| 4108 | __u16 interval, window; |
| 4109 | int err; |
| 4110 | |
| 4111 | BT_DBG("%s", hdev->name); |
| 4112 | |
| 4113 | if (!lmp_le_capable(hdev)) |
| 4114 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4115 | MGMT_STATUS_NOT_SUPPORTED); |
| 4116 | |
| 4117 | interval = __le16_to_cpu(cp->interval); |
| 4118 | |
| 4119 | if (interval < 0x0004 || interval > 0x4000) |
| 4120 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4121 | MGMT_STATUS_INVALID_PARAMS); |
| 4122 | |
| 4123 | window = __le16_to_cpu(cp->window); |
| 4124 | |
| 4125 | if (window < 0x0004 || window > 0x4000) |
| 4126 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4127 | MGMT_STATUS_INVALID_PARAMS); |
| 4128 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4129 | if (window > interval) |
| 4130 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4131 | MGMT_STATUS_INVALID_PARAMS); |
| 4132 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4133 | hci_dev_lock(hdev); |
| 4134 | |
| 4135 | hdev->le_scan_interval = interval; |
| 4136 | hdev->le_scan_window = window; |
| 4137 | |
| 4138 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4139 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4140 | /* If background scan is running, restart it so new parameters are |
| 4141 | * loaded. |
| 4142 | */ |
| 4143 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4144 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4145 | struct hci_request req; |
| 4146 | |
| 4147 | hci_req_init(&req, hdev); |
| 4148 | |
| 4149 | hci_req_add_le_scan_disable(&req); |
| 4150 | hci_req_add_le_passive_scan(&req); |
| 4151 | |
| 4152 | hci_req_run(&req, NULL); |
| 4153 | } |
| 4154 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4155 | hci_dev_unlock(hdev); |
| 4156 | |
| 4157 | return err; |
| 4158 | } |
| 4159 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4160 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4161 | { |
| 4162 | struct pending_cmd *cmd; |
| 4163 | |
| 4164 | BT_DBG("status 0x%02x", status); |
| 4165 | |
| 4166 | hci_dev_lock(hdev); |
| 4167 | |
| 4168 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4169 | if (!cmd) |
| 4170 | goto unlock; |
| 4171 | |
| 4172 | if (status) { |
| 4173 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4174 | mgmt_status(status)); |
| 4175 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4176 | struct mgmt_mode *cp = cmd->param; |
| 4177 | |
| 4178 | if (cp->val) |
| 4179 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4180 | else |
| 4181 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4182 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4183 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4184 | new_settings(hdev, cmd->sk); |
| 4185 | } |
| 4186 | |
| 4187 | mgmt_pending_remove(cmd); |
| 4188 | |
| 4189 | unlock: |
| 4190 | hci_dev_unlock(hdev); |
| 4191 | } |
| 4192 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4193 | 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] | 4194 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4195 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4196 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4197 | struct pending_cmd *cmd; |
| 4198 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4199 | int err; |
| 4200 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4201 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4202 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4203 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4204 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4205 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4206 | MGMT_STATUS_NOT_SUPPORTED); |
| 4207 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4208 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4209 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4210 | MGMT_STATUS_INVALID_PARAMS); |
| 4211 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4212 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4213 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4214 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4215 | |
| 4216 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4217 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4218 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4219 | |
| 4220 | hci_dev_lock(hdev); |
| 4221 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4222 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4223 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4224 | MGMT_STATUS_BUSY); |
| 4225 | goto unlock; |
| 4226 | } |
| 4227 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4228 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4229 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4230 | hdev); |
| 4231 | goto unlock; |
| 4232 | } |
| 4233 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4234 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4235 | data, len); |
| 4236 | if (!cmd) { |
| 4237 | err = -ENOMEM; |
| 4238 | goto unlock; |
| 4239 | } |
| 4240 | |
| 4241 | hci_req_init(&req, hdev); |
| 4242 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4243 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4244 | |
| 4245 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4246 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4247 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4248 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4249 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4250 | } |
| 4251 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4252 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4253 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4254 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4255 | return err; |
| 4256 | } |
| 4257 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4258 | static void set_bredr_scan(struct hci_request *req) |
| 4259 | { |
| 4260 | struct hci_dev *hdev = req->hdev; |
| 4261 | u8 scan = 0; |
| 4262 | |
| 4263 | /* Ensure that fast connectable is disabled. This function will |
| 4264 | * not do anything if the page scan parameters are already what |
| 4265 | * they should be. |
| 4266 | */ |
| 4267 | write_fast_connectable(req, false); |
| 4268 | |
| 4269 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4270 | scan |= SCAN_PAGE; |
| 4271 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4272 | scan |= SCAN_INQUIRY; |
| 4273 | |
| 4274 | if (scan) |
| 4275 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4276 | } |
| 4277 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4278 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4279 | { |
| 4280 | struct pending_cmd *cmd; |
| 4281 | |
| 4282 | BT_DBG("status 0x%02x", status); |
| 4283 | |
| 4284 | hci_dev_lock(hdev); |
| 4285 | |
| 4286 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4287 | if (!cmd) |
| 4288 | goto unlock; |
| 4289 | |
| 4290 | if (status) { |
| 4291 | u8 mgmt_err = mgmt_status(status); |
| 4292 | |
| 4293 | /* We need to restore the flag if related HCI commands |
| 4294 | * failed. |
| 4295 | */ |
| 4296 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4297 | |
| 4298 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4299 | } else { |
| 4300 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4301 | new_settings(hdev, cmd->sk); |
| 4302 | } |
| 4303 | |
| 4304 | mgmt_pending_remove(cmd); |
| 4305 | |
| 4306 | unlock: |
| 4307 | hci_dev_unlock(hdev); |
| 4308 | } |
| 4309 | |
| 4310 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4311 | { |
| 4312 | struct mgmt_mode *cp = data; |
| 4313 | struct pending_cmd *cmd; |
| 4314 | struct hci_request req; |
| 4315 | int err; |
| 4316 | |
| 4317 | BT_DBG("request for %s", hdev->name); |
| 4318 | |
| 4319 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4320 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4321 | MGMT_STATUS_NOT_SUPPORTED); |
| 4322 | |
| 4323 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4324 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4325 | MGMT_STATUS_REJECTED); |
| 4326 | |
| 4327 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4328 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4329 | MGMT_STATUS_INVALID_PARAMS); |
| 4330 | |
| 4331 | hci_dev_lock(hdev); |
| 4332 | |
| 4333 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4334 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4335 | goto unlock; |
| 4336 | } |
| 4337 | |
| 4338 | if (!hdev_is_powered(hdev)) { |
| 4339 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4340 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4341 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4342 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4343 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4344 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4345 | } |
| 4346 | |
| 4347 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4348 | |
| 4349 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4350 | if (err < 0) |
| 4351 | goto unlock; |
| 4352 | |
| 4353 | err = new_settings(hdev, sk); |
| 4354 | goto unlock; |
| 4355 | } |
| 4356 | |
| 4357 | /* Reject disabling when powered on */ |
| 4358 | if (!cp->val) { |
| 4359 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4360 | MGMT_STATUS_REJECTED); |
| 4361 | goto unlock; |
| 4362 | } |
| 4363 | |
| 4364 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4365 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4366 | MGMT_STATUS_BUSY); |
| 4367 | goto unlock; |
| 4368 | } |
| 4369 | |
| 4370 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4371 | if (!cmd) { |
| 4372 | err = -ENOMEM; |
| 4373 | goto unlock; |
| 4374 | } |
| 4375 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4376 | /* 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] | 4377 | * generates the correct flags. |
| 4378 | */ |
| 4379 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4380 | |
| 4381 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4382 | |
| 4383 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4384 | set_bredr_scan(&req); |
| 4385 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4386 | /* Since only the advertising data flags will change, there |
| 4387 | * is no need to update the scan response data. |
| 4388 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4389 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4390 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4391 | err = hci_req_run(&req, set_bredr_complete); |
| 4392 | if (err < 0) |
| 4393 | mgmt_pending_remove(cmd); |
| 4394 | |
| 4395 | unlock: |
| 4396 | hci_dev_unlock(hdev); |
| 4397 | return err; |
| 4398 | } |
| 4399 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4400 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4401 | void *data, u16 len) |
| 4402 | { |
| 4403 | struct mgmt_mode *cp = data; |
| 4404 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4405 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4406 | int err; |
| 4407 | |
| 4408 | BT_DBG("request for %s", hdev->name); |
| 4409 | |
| 4410 | status = mgmt_bredr_support(hdev); |
| 4411 | if (status) |
| 4412 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4413 | status); |
| 4414 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4415 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4416 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4417 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4418 | MGMT_STATUS_NOT_SUPPORTED); |
| 4419 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4420 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4421 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4422 | MGMT_STATUS_INVALID_PARAMS); |
| 4423 | |
| 4424 | hci_dev_lock(hdev); |
| 4425 | |
| 4426 | if (!hdev_is_powered(hdev)) { |
| 4427 | bool changed; |
| 4428 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4429 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4430 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4431 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4432 | if (cp->val == 0x02) |
| 4433 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4434 | else |
| 4435 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4436 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4437 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4438 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4439 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4440 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4441 | |
| 4442 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4443 | if (err < 0) |
| 4444 | goto failed; |
| 4445 | |
| 4446 | if (changed) |
| 4447 | err = new_settings(hdev, sk); |
| 4448 | |
| 4449 | goto failed; |
| 4450 | } |
| 4451 | |
| 4452 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4453 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4454 | MGMT_STATUS_BUSY); |
| 4455 | goto failed; |
| 4456 | } |
| 4457 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4458 | val = !!cp->val; |
| 4459 | |
| 4460 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4461 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4462 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4463 | goto failed; |
| 4464 | } |
| 4465 | |
| 4466 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4467 | if (!cmd) { |
| 4468 | err = -ENOMEM; |
| 4469 | goto failed; |
| 4470 | } |
| 4471 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4472 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4473 | if (err < 0) { |
| 4474 | mgmt_pending_remove(cmd); |
| 4475 | goto failed; |
| 4476 | } |
| 4477 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4478 | if (cp->val == 0x02) |
| 4479 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4480 | else |
| 4481 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4482 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4483 | failed: |
| 4484 | hci_dev_unlock(hdev); |
| 4485 | return err; |
| 4486 | } |
| 4487 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4488 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4489 | void *data, u16 len) |
| 4490 | { |
| 4491 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4492 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4493 | int err; |
| 4494 | |
| 4495 | BT_DBG("request for %s", hdev->name); |
| 4496 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4497 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4498 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4499 | MGMT_STATUS_INVALID_PARAMS); |
| 4500 | |
| 4501 | hci_dev_lock(hdev); |
| 4502 | |
| 4503 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4504 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4505 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4506 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4507 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4508 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4509 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4510 | if (cp->val == 0x02) |
| 4511 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4512 | &hdev->dev_flags); |
| 4513 | else |
| 4514 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4515 | &hdev->dev_flags); |
| 4516 | |
| 4517 | if (hdev_is_powered(hdev) && use_changed && |
| 4518 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4519 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4520 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4521 | sizeof(mode), &mode); |
| 4522 | } |
| 4523 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4524 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4525 | if (err < 0) |
| 4526 | goto unlock; |
| 4527 | |
| 4528 | if (changed) |
| 4529 | err = new_settings(hdev, sk); |
| 4530 | |
| 4531 | unlock: |
| 4532 | hci_dev_unlock(hdev); |
| 4533 | return err; |
| 4534 | } |
| 4535 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4536 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4537 | u16 len) |
| 4538 | { |
| 4539 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4540 | bool changed; |
| 4541 | int err; |
| 4542 | |
| 4543 | BT_DBG("request for %s", hdev->name); |
| 4544 | |
| 4545 | if (!lmp_le_capable(hdev)) |
| 4546 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4547 | MGMT_STATUS_NOT_SUPPORTED); |
| 4548 | |
| 4549 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4550 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4551 | MGMT_STATUS_INVALID_PARAMS); |
| 4552 | |
| 4553 | if (hdev_is_powered(hdev)) |
| 4554 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4555 | MGMT_STATUS_REJECTED); |
| 4556 | |
| 4557 | hci_dev_lock(hdev); |
| 4558 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4559 | /* If user space supports this command it is also expected to |
| 4560 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4561 | */ |
| 4562 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4563 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4564 | if (cp->privacy) { |
| 4565 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4566 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4567 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4568 | } else { |
| 4569 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4570 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4571 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4572 | } |
| 4573 | |
| 4574 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4575 | if (err < 0) |
| 4576 | goto unlock; |
| 4577 | |
| 4578 | if (changed) |
| 4579 | err = new_settings(hdev, sk); |
| 4580 | |
| 4581 | unlock: |
| 4582 | hci_dev_unlock(hdev); |
| 4583 | return err; |
| 4584 | } |
| 4585 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4586 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4587 | { |
| 4588 | switch (irk->addr.type) { |
| 4589 | case BDADDR_LE_PUBLIC: |
| 4590 | return true; |
| 4591 | |
| 4592 | case BDADDR_LE_RANDOM: |
| 4593 | /* Two most significant bits shall be set */ |
| 4594 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4595 | return false; |
| 4596 | return true; |
| 4597 | } |
| 4598 | |
| 4599 | return false; |
| 4600 | } |
| 4601 | |
| 4602 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4603 | u16 len) |
| 4604 | { |
| 4605 | struct mgmt_cp_load_irks *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4606 | const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) / |
| 4607 | sizeof(struct mgmt_irk_info)); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4608 | u16 irk_count, expected_len; |
| 4609 | int i, err; |
| 4610 | |
| 4611 | BT_DBG("request for %s", hdev->name); |
| 4612 | |
| 4613 | if (!lmp_le_capable(hdev)) |
| 4614 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4615 | MGMT_STATUS_NOT_SUPPORTED); |
| 4616 | |
| 4617 | irk_count = __le16_to_cpu(cp->irk_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4618 | if (irk_count > max_irk_count) { |
| 4619 | BT_ERR("load_irks: too big irk_count value %u", irk_count); |
| 4620 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4621 | MGMT_STATUS_INVALID_PARAMS); |
| 4622 | } |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4623 | |
| 4624 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4625 | if (expected_len != len) { |
| 4626 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4627 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4628 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4629 | MGMT_STATUS_INVALID_PARAMS); |
| 4630 | } |
| 4631 | |
| 4632 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4633 | |
| 4634 | for (i = 0; i < irk_count; i++) { |
| 4635 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4636 | |
| 4637 | if (!irk_is_valid(key)) |
| 4638 | return cmd_status(sk, hdev->id, |
| 4639 | MGMT_OP_LOAD_IRKS, |
| 4640 | MGMT_STATUS_INVALID_PARAMS); |
| 4641 | } |
| 4642 | |
| 4643 | hci_dev_lock(hdev); |
| 4644 | |
| 4645 | hci_smp_irks_clear(hdev); |
| 4646 | |
| 4647 | for (i = 0; i < irk_count; i++) { |
| 4648 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4649 | u8 addr_type; |
| 4650 | |
| 4651 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4652 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4653 | else |
| 4654 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4655 | |
| 4656 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4657 | BDADDR_ANY); |
| 4658 | } |
| 4659 | |
| 4660 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4661 | |
| 4662 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4663 | |
| 4664 | hci_dev_unlock(hdev); |
| 4665 | |
| 4666 | return err; |
| 4667 | } |
| 4668 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4669 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4670 | { |
| 4671 | if (key->master != 0x00 && key->master != 0x01) |
| 4672 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4673 | |
| 4674 | switch (key->addr.type) { |
| 4675 | case BDADDR_LE_PUBLIC: |
| 4676 | return true; |
| 4677 | |
| 4678 | case BDADDR_LE_RANDOM: |
| 4679 | /* Two most significant bits shall be set */ |
| 4680 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4681 | return false; |
| 4682 | return true; |
| 4683 | } |
| 4684 | |
| 4685 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4686 | } |
| 4687 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4688 | 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] | 4689 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4690 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4691 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4692 | const u16 max_key_count = ((U16_MAX - sizeof(*cp)) / |
| 4693 | sizeof(struct mgmt_ltk_info)); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4694 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4695 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4696 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4697 | BT_DBG("request for %s", hdev->name); |
| 4698 | |
| 4699 | if (!lmp_le_capable(hdev)) |
| 4700 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4701 | MGMT_STATUS_NOT_SUPPORTED); |
| 4702 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4703 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 4704 | if (key_count > max_key_count) { |
| 4705 | BT_ERR("load_ltks: too big key_count value %u", key_count); |
| 4706 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4707 | MGMT_STATUS_INVALID_PARAMS); |
| 4708 | } |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4709 | |
| 4710 | expected_len = sizeof(*cp) + key_count * |
| 4711 | sizeof(struct mgmt_ltk_info); |
| 4712 | if (expected_len != len) { |
| 4713 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4714 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4715 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4716 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4717 | } |
| 4718 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4719 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4720 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4721 | for (i = 0; i < key_count; i++) { |
| 4722 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4723 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4724 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4725 | return cmd_status(sk, hdev->id, |
| 4726 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4727 | MGMT_STATUS_INVALID_PARAMS); |
| 4728 | } |
| 4729 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4730 | hci_dev_lock(hdev); |
| 4731 | |
| 4732 | hci_smp_ltks_clear(hdev); |
| 4733 | |
| 4734 | for (i = 0; i < key_count; i++) { |
| 4735 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4736 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4737 | |
| 4738 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4739 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4740 | else |
| 4741 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4742 | |
| 4743 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4744 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4745 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4746 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4747 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4748 | switch (key->type) { |
| 4749 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4750 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4751 | break; |
| 4752 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4753 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4754 | break; |
| 4755 | default: |
| 4756 | continue; |
| 4757 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4758 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4759 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4760 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4761 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4762 | } |
| 4763 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4764 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4765 | NULL, 0); |
| 4766 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4767 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4768 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4769 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4770 | } |
| 4771 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4772 | struct cmd_conn_lookup { |
| 4773 | struct hci_conn *conn; |
| 4774 | bool valid_tx_power; |
| 4775 | u8 mgmt_status; |
| 4776 | }; |
| 4777 | |
| 4778 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4779 | { |
| 4780 | struct cmd_conn_lookup *match = data; |
| 4781 | struct mgmt_cp_get_conn_info *cp; |
| 4782 | struct mgmt_rp_get_conn_info rp; |
| 4783 | struct hci_conn *conn = cmd->user_data; |
| 4784 | |
| 4785 | if (conn != match->conn) |
| 4786 | return; |
| 4787 | |
| 4788 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4789 | |
| 4790 | memset(&rp, 0, sizeof(rp)); |
| 4791 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4792 | rp.addr.type = cp->addr.type; |
| 4793 | |
| 4794 | if (!match->mgmt_status) { |
| 4795 | rp.rssi = conn->rssi; |
| 4796 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4797 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4798 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4799 | rp.max_tx_power = conn->max_tx_power; |
| 4800 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4801 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4802 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4803 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4804 | } |
| 4805 | |
| 4806 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4807 | match->mgmt_status, &rp, sizeof(rp)); |
| 4808 | |
| 4809 | hci_conn_drop(conn); |
| 4810 | |
| 4811 | mgmt_pending_remove(cmd); |
| 4812 | } |
| 4813 | |
| 4814 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4815 | { |
| 4816 | struct hci_cp_read_rssi *cp; |
| 4817 | struct hci_conn *conn; |
| 4818 | struct cmd_conn_lookup match; |
| 4819 | u16 handle; |
| 4820 | |
| 4821 | BT_DBG("status 0x%02x", status); |
| 4822 | |
| 4823 | hci_dev_lock(hdev); |
| 4824 | |
| 4825 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4826 | * otherwise we assume it's not valid. At the moment we assume that |
| 4827 | * either both or none of current and max values are valid to keep code |
| 4828 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4829 | */ |
| 4830 | match.valid_tx_power = !status; |
| 4831 | |
| 4832 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4833 | * Level so we check which one was last sent to retrieve connection |
| 4834 | * handle. Both commands have handle as first parameter so it's safe to |
| 4835 | * cast data on the same command struct. |
| 4836 | * |
| 4837 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4838 | * In other case we simply override error to indicate success as we |
| 4839 | * already remembered if TX power value is actually valid. |
| 4840 | */ |
| 4841 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4842 | if (!cp) { |
| 4843 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4844 | status = 0; |
| 4845 | } |
| 4846 | |
| 4847 | if (!cp) { |
| 4848 | BT_ERR("invalid sent_cmd in response"); |
| 4849 | goto unlock; |
| 4850 | } |
| 4851 | |
| 4852 | handle = __le16_to_cpu(cp->handle); |
| 4853 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4854 | if (!conn) { |
| 4855 | BT_ERR("unknown handle (%d) in response", handle); |
| 4856 | goto unlock; |
| 4857 | } |
| 4858 | |
| 4859 | match.conn = conn; |
| 4860 | match.mgmt_status = mgmt_status(status); |
| 4861 | |
| 4862 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4863 | * connection only. |
| 4864 | */ |
| 4865 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4866 | get_conn_info_complete, &match); |
| 4867 | |
| 4868 | unlock: |
| 4869 | hci_dev_unlock(hdev); |
| 4870 | } |
| 4871 | |
| 4872 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4873 | u16 len) |
| 4874 | { |
| 4875 | struct mgmt_cp_get_conn_info *cp = data; |
| 4876 | struct mgmt_rp_get_conn_info rp; |
| 4877 | struct hci_conn *conn; |
| 4878 | unsigned long conn_info_age; |
| 4879 | int err = 0; |
| 4880 | |
| 4881 | BT_DBG("%s", hdev->name); |
| 4882 | |
| 4883 | memset(&rp, 0, sizeof(rp)); |
| 4884 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4885 | rp.addr.type = cp->addr.type; |
| 4886 | |
| 4887 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4888 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4889 | MGMT_STATUS_INVALID_PARAMS, |
| 4890 | &rp, sizeof(rp)); |
| 4891 | |
| 4892 | hci_dev_lock(hdev); |
| 4893 | |
| 4894 | if (!hdev_is_powered(hdev)) { |
| 4895 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4896 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4897 | goto unlock; |
| 4898 | } |
| 4899 | |
| 4900 | if (cp->addr.type == BDADDR_BREDR) |
| 4901 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4902 | &cp->addr.bdaddr); |
| 4903 | else |
| 4904 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4905 | |
| 4906 | if (!conn || conn->state != BT_CONNECTED) { |
| 4907 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4908 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4909 | goto unlock; |
| 4910 | } |
| 4911 | |
| 4912 | /* To avoid client trying to guess when to poll again for information we |
| 4913 | * calculate conn info age as random value between min/max set in hdev. |
| 4914 | */ |
| 4915 | conn_info_age = hdev->conn_info_min_age + |
| 4916 | prandom_u32_max(hdev->conn_info_max_age - |
| 4917 | hdev->conn_info_min_age); |
| 4918 | |
| 4919 | /* Query controller to refresh cached values if they are too old or were |
| 4920 | * never read. |
| 4921 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4922 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4923 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4924 | !conn->conn_info_timestamp) { |
| 4925 | struct hci_request req; |
| 4926 | struct hci_cp_read_tx_power req_txp_cp; |
| 4927 | struct hci_cp_read_rssi req_rssi_cp; |
| 4928 | struct pending_cmd *cmd; |
| 4929 | |
| 4930 | hci_req_init(&req, hdev); |
| 4931 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4932 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4933 | &req_rssi_cp); |
| 4934 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4935 | /* For LE links TX power does not change thus we don't need to |
| 4936 | * query for it once value is known. |
| 4937 | */ |
| 4938 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4939 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4940 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4941 | req_txp_cp.type = 0x00; |
| 4942 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4943 | sizeof(req_txp_cp), &req_txp_cp); |
| 4944 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4945 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4946 | /* Max TX power needs to be read only once per connection */ |
| 4947 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4948 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4949 | req_txp_cp.type = 0x01; |
| 4950 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4951 | sizeof(req_txp_cp), &req_txp_cp); |
| 4952 | } |
| 4953 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4954 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4955 | if (err < 0) |
| 4956 | goto unlock; |
| 4957 | |
| 4958 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4959 | data, len); |
| 4960 | if (!cmd) { |
| 4961 | err = -ENOMEM; |
| 4962 | goto unlock; |
| 4963 | } |
| 4964 | |
| 4965 | hci_conn_hold(conn); |
| 4966 | cmd->user_data = conn; |
| 4967 | |
| 4968 | conn->conn_info_timestamp = jiffies; |
| 4969 | } else { |
| 4970 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4971 | rp.rssi = conn->rssi; |
| 4972 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4973 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4974 | |
| 4975 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4976 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4977 | } |
| 4978 | |
| 4979 | unlock: |
| 4980 | hci_dev_unlock(hdev); |
| 4981 | return err; |
| 4982 | } |
| 4983 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 4984 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 4985 | { |
| 4986 | struct mgmt_cp_get_clock_info *cp; |
| 4987 | struct mgmt_rp_get_clock_info rp; |
| 4988 | struct hci_cp_read_clock *hci_cp; |
| 4989 | struct pending_cmd *cmd; |
| 4990 | struct hci_conn *conn; |
| 4991 | |
| 4992 | BT_DBG("%s status %u", hdev->name, status); |
| 4993 | |
| 4994 | hci_dev_lock(hdev); |
| 4995 | |
| 4996 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 4997 | if (!hci_cp) |
| 4998 | goto unlock; |
| 4999 | |
| 5000 | if (hci_cp->which) { |
| 5001 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 5002 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 5003 | } else { |
| 5004 | conn = NULL; |
| 5005 | } |
| 5006 | |
| 5007 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 5008 | if (!cmd) |
| 5009 | goto unlock; |
| 5010 | |
| 5011 | cp = cmd->param; |
| 5012 | |
| 5013 | memset(&rp, 0, sizeof(rp)); |
| 5014 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 5015 | |
| 5016 | if (status) |
| 5017 | goto send_rsp; |
| 5018 | |
| 5019 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 5020 | |
| 5021 | if (conn) { |
| 5022 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 5023 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 5024 | } |
| 5025 | |
| 5026 | send_rsp: |
| 5027 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 5028 | &rp, sizeof(rp)); |
| 5029 | mgmt_pending_remove(cmd); |
| 5030 | if (conn) |
| 5031 | hci_conn_drop(conn); |
| 5032 | |
| 5033 | unlock: |
| 5034 | hci_dev_unlock(hdev); |
| 5035 | } |
| 5036 | |
| 5037 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5038 | u16 len) |
| 5039 | { |
| 5040 | struct mgmt_cp_get_clock_info *cp = data; |
| 5041 | struct mgmt_rp_get_clock_info rp; |
| 5042 | struct hci_cp_read_clock hci_cp; |
| 5043 | struct pending_cmd *cmd; |
| 5044 | struct hci_request req; |
| 5045 | struct hci_conn *conn; |
| 5046 | int err; |
| 5047 | |
| 5048 | BT_DBG("%s", hdev->name); |
| 5049 | |
| 5050 | memset(&rp, 0, sizeof(rp)); |
| 5051 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5052 | rp.addr.type = cp->addr.type; |
| 5053 | |
| 5054 | if (cp->addr.type != BDADDR_BREDR) |
| 5055 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5056 | MGMT_STATUS_INVALID_PARAMS, |
| 5057 | &rp, sizeof(rp)); |
| 5058 | |
| 5059 | hci_dev_lock(hdev); |
| 5060 | |
| 5061 | if (!hdev_is_powered(hdev)) { |
| 5062 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 5063 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 5064 | goto unlock; |
| 5065 | } |
| 5066 | |
| 5067 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5068 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 5069 | &cp->addr.bdaddr); |
| 5070 | if (!conn || conn->state != BT_CONNECTED) { |
| 5071 | err = cmd_complete(sk, hdev->id, |
| 5072 | MGMT_OP_GET_CLOCK_INFO, |
| 5073 | MGMT_STATUS_NOT_CONNECTED, |
| 5074 | &rp, sizeof(rp)); |
| 5075 | goto unlock; |
| 5076 | } |
| 5077 | } else { |
| 5078 | conn = NULL; |
| 5079 | } |
| 5080 | |
| 5081 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 5082 | if (!cmd) { |
| 5083 | err = -ENOMEM; |
| 5084 | goto unlock; |
| 5085 | } |
| 5086 | |
| 5087 | hci_req_init(&req, hdev); |
| 5088 | |
| 5089 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 5090 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5091 | |
| 5092 | if (conn) { |
| 5093 | hci_conn_hold(conn); |
| 5094 | cmd->user_data = conn; |
| 5095 | |
| 5096 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 5097 | hci_cp.which = 0x01; /* Piconet clock */ |
| 5098 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 5099 | } |
| 5100 | |
| 5101 | err = hci_req_run(&req, get_clock_info_complete); |
| 5102 | if (err < 0) |
| 5103 | mgmt_pending_remove(cmd); |
| 5104 | |
| 5105 | unlock: |
| 5106 | hci_dev_unlock(hdev); |
| 5107 | return err; |
| 5108 | } |
| 5109 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5110 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 5111 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 5112 | { |
| 5113 | struct mgmt_ev_device_added ev; |
| 5114 | |
| 5115 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5116 | ev.addr.type = type; |
| 5117 | ev.action = action; |
| 5118 | |
| 5119 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5120 | } |
| 5121 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5122 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5123 | void *data, u16 len) |
| 5124 | { |
| 5125 | struct mgmt_cp_add_device *cp = data; |
| 5126 | u8 auto_conn, addr_type; |
| 5127 | int err; |
| 5128 | |
| 5129 | BT_DBG("%s", hdev->name); |
| 5130 | |
| 5131 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5132 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5133 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5134 | MGMT_STATUS_INVALID_PARAMS, |
| 5135 | &cp->addr, sizeof(cp->addr)); |
| 5136 | |
| 5137 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5138 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5139 | MGMT_STATUS_INVALID_PARAMS, |
| 5140 | &cp->addr, sizeof(cp->addr)); |
| 5141 | |
| 5142 | hci_dev_lock(hdev); |
| 5143 | |
| 5144 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5145 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5146 | else |
| 5147 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5148 | |
| 5149 | if (cp->action) |
| 5150 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5151 | else |
Johan Hedberg | a3451d2 | 2014-07-02 17:37:27 +0300 | [diff] [blame] | 5152 | auto_conn = HCI_AUTO_CONN_REPORT; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5153 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5154 | /* If the connection parameters don't exist for this device, |
| 5155 | * they will be created and configured with defaults. |
| 5156 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5157 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5158 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5159 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5160 | MGMT_STATUS_FAILED, |
| 5161 | &cp->addr, sizeof(cp->addr)); |
| 5162 | goto unlock; |
| 5163 | } |
| 5164 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5165 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5166 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5167 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5168 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5169 | |
| 5170 | unlock: |
| 5171 | hci_dev_unlock(hdev); |
| 5172 | return err; |
| 5173 | } |
| 5174 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5175 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5176 | bdaddr_t *bdaddr, u8 type) |
| 5177 | { |
| 5178 | struct mgmt_ev_device_removed ev; |
| 5179 | |
| 5180 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5181 | ev.addr.type = type; |
| 5182 | |
| 5183 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5184 | } |
| 5185 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5186 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5187 | void *data, u16 len) |
| 5188 | { |
| 5189 | struct mgmt_cp_remove_device *cp = data; |
| 5190 | int err; |
| 5191 | |
| 5192 | BT_DBG("%s", hdev->name); |
| 5193 | |
| 5194 | hci_dev_lock(hdev); |
| 5195 | |
| 5196 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5197 | struct hci_conn_params *params; |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5198 | u8 addr_type; |
| 5199 | |
| 5200 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5201 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5202 | MGMT_STATUS_INVALID_PARAMS, |
| 5203 | &cp->addr, sizeof(cp->addr)); |
| 5204 | goto unlock; |
| 5205 | } |
| 5206 | |
| 5207 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5208 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5209 | else |
| 5210 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5211 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5212 | params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, |
| 5213 | addr_type); |
| 5214 | if (!params) { |
| 5215 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5216 | MGMT_STATUS_INVALID_PARAMS, |
| 5217 | &cp->addr, sizeof(cp->addr)); |
| 5218 | goto unlock; |
| 5219 | } |
| 5220 | |
| 5221 | if (params->auto_connect == HCI_AUTO_CONN_DISABLED) { |
| 5222 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5223 | MGMT_STATUS_INVALID_PARAMS, |
| 5224 | &cp->addr, sizeof(cp->addr)); |
| 5225 | goto unlock; |
| 5226 | } |
| 5227 | |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame^] | 5228 | list_del_init(¶ms->action); |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5229 | list_del(¶ms->list); |
| 5230 | kfree(params); |
Johan Hedberg | 95305ba | 2014-07-04 12:37:21 +0300 | [diff] [blame^] | 5231 | hci_update_background_scan(hdev); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5232 | |
| 5233 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5234 | } else { |
| 5235 | if (cp->addr.type) { |
| 5236 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5237 | MGMT_STATUS_INVALID_PARAMS, |
| 5238 | &cp->addr, sizeof(cp->addr)); |
| 5239 | goto unlock; |
| 5240 | } |
| 5241 | |
Johan Hedberg | c71593d | 2014-07-02 17:37:28 +0300 | [diff] [blame] | 5242 | hci_conn_params_clear_enabled(hdev); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5243 | } |
| 5244 | |
| 5245 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5246 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5247 | |
| 5248 | unlock: |
| 5249 | hci_dev_unlock(hdev); |
| 5250 | return err; |
| 5251 | } |
| 5252 | |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5253 | static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data, |
| 5254 | u16 len) |
| 5255 | { |
| 5256 | struct mgmt_cp_load_conn_param *cp = data; |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5257 | const u16 max_param_count = ((U16_MAX - sizeof(*cp)) / |
| 5258 | sizeof(struct mgmt_conn_param)); |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5259 | u16 param_count, expected_len; |
| 5260 | int i; |
| 5261 | |
| 5262 | if (!lmp_le_capable(hdev)) |
| 5263 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5264 | MGMT_STATUS_NOT_SUPPORTED); |
| 5265 | |
| 5266 | param_count = __le16_to_cpu(cp->param_count); |
Johan Hedberg | ba1d693 | 2014-07-03 13:52:27 +0300 | [diff] [blame] | 5267 | if (param_count > max_param_count) { |
| 5268 | BT_ERR("load_conn_param: too big param_count value %u", |
| 5269 | param_count); |
| 5270 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5271 | MGMT_STATUS_INVALID_PARAMS); |
| 5272 | } |
Johan Hedberg | a26f3dc | 2014-07-02 17:37:29 +0300 | [diff] [blame] | 5273 | |
| 5274 | expected_len = sizeof(*cp) + param_count * |
| 5275 | sizeof(struct mgmt_conn_param); |
| 5276 | if (expected_len != len) { |
| 5277 | BT_ERR("load_conn_param: expected %u bytes, got %u bytes", |
| 5278 | expected_len, len); |
| 5279 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, |
| 5280 | MGMT_STATUS_INVALID_PARAMS); |
| 5281 | } |
| 5282 | |
| 5283 | BT_DBG("%s param_count %u", hdev->name, param_count); |
| 5284 | |
| 5285 | hci_dev_lock(hdev); |
| 5286 | |
| 5287 | hci_conn_params_clear_disabled(hdev); |
| 5288 | |
| 5289 | for (i = 0; i < param_count; i++) { |
| 5290 | struct mgmt_conn_param *param = &cp->params[i]; |
| 5291 | struct hci_conn_params *hci_param; |
| 5292 | u16 min, max, latency, timeout; |
| 5293 | u8 addr_type; |
| 5294 | |
| 5295 | BT_DBG("Adding %pMR (type %u)", ¶m->addr.bdaddr, |
| 5296 | param->addr.type); |
| 5297 | |
| 5298 | if (param->addr.type == BDADDR_LE_PUBLIC) { |
| 5299 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5300 | } else if (param->addr.type == BDADDR_LE_RANDOM) { |
| 5301 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5302 | } else { |
| 5303 | BT_ERR("Ignoring invalid connection parameters"); |
| 5304 | continue; |
| 5305 | } |
| 5306 | |
| 5307 | min = le16_to_cpu(param->min_interval); |
| 5308 | max = le16_to_cpu(param->max_interval); |
| 5309 | latency = le16_to_cpu(param->latency); |
| 5310 | timeout = le16_to_cpu(param->timeout); |
| 5311 | |
| 5312 | BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x", |
| 5313 | min, max, latency, timeout); |
| 5314 | |
| 5315 | if (hci_check_conn_params(min, max, latency, timeout) < 0) { |
| 5316 | BT_ERR("Ignoring invalid connection parameters"); |
| 5317 | continue; |
| 5318 | } |
| 5319 | |
| 5320 | hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr, |
| 5321 | addr_type); |
| 5322 | if (!hci_param) { |
| 5323 | BT_ERR("Failed to add connection parameters"); |
| 5324 | continue; |
| 5325 | } |
| 5326 | |
| 5327 | hci_param->conn_min_interval = min; |
| 5328 | hci_param->conn_max_interval = max; |
| 5329 | hci_param->conn_latency = latency; |
| 5330 | hci_param->supervision_timeout = timeout; |
| 5331 | } |
| 5332 | |
| 5333 | hci_dev_unlock(hdev); |
| 5334 | |
| 5335 | return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); |
| 5336 | } |
| 5337 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5338 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5339 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5340 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5341 | bool var_len; |
| 5342 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5343 | } mgmt_handlers[] = { |
| 5344 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5345 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5346 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5347 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5348 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5349 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5350 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5351 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5352 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5353 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5354 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5355 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5356 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5357 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5358 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5359 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5360 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5361 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5362 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5363 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5364 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5365 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5366 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5367 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5368 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5369 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5370 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5371 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5372 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5373 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5374 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5375 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5376 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5377 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5378 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5379 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5380 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5381 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5382 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5383 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5384 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5385 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5386 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5387 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5388 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5389 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5390 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5391 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5392 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5393 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5394 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5395 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5396 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5397 | { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE }, |
| 5398 | { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE }, |
Marcel Holtmann | 9fc3bfb | 2014-07-04 00:46:56 +0200 | [diff] [blame] | 5399 | { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5400 | }; |
| 5401 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5402 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5403 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5404 | void *buf; |
| 5405 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5406 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5407 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5408 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5409 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5410 | int err; |
| 5411 | |
| 5412 | BT_DBG("got %zu bytes", msglen); |
| 5413 | |
| 5414 | if (msglen < sizeof(*hdr)) |
| 5415 | return -EINVAL; |
| 5416 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5417 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5418 | if (!buf) |
| 5419 | return -ENOMEM; |
| 5420 | |
| 5421 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5422 | err = -EFAULT; |
| 5423 | goto done; |
| 5424 | } |
| 5425 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5426 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5427 | opcode = __le16_to_cpu(hdr->opcode); |
| 5428 | index = __le16_to_cpu(hdr->index); |
| 5429 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5430 | |
| 5431 | if (len != msglen - sizeof(*hdr)) { |
| 5432 | err = -EINVAL; |
| 5433 | goto done; |
| 5434 | } |
| 5435 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5436 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5437 | hdev = hci_dev_get(index); |
| 5438 | if (!hdev) { |
| 5439 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5440 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5441 | goto done; |
| 5442 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5443 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5444 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | 4a96440 | 2014-07-02 19:10:33 +0200 | [diff] [blame] | 5445 | test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) || |
| 5446 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5447 | err = cmd_status(sk, index, opcode, |
| 5448 | MGMT_STATUS_INVALID_INDEX); |
| 5449 | goto done; |
| 5450 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5451 | } |
| 5452 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5453 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5454 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5455 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5456 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5457 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5458 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5459 | } |
| 5460 | |
Marcel Holtmann | 73d1df2 | 2014-07-02 22:10:52 +0200 | [diff] [blame] | 5461 | if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || |
| 5462 | opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
| 5463 | err = cmd_status(sk, index, opcode, |
| 5464 | MGMT_STATUS_INVALID_INDEX); |
| 5465 | goto done; |
| 5466 | } |
| 5467 | |
| 5468 | if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && |
| 5469 | opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5470 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5471 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5472 | goto done; |
| 5473 | } |
| 5474 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5475 | handler = &mgmt_handlers[opcode]; |
| 5476 | |
| 5477 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5478 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5479 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5480 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5481 | goto done; |
| 5482 | } |
| 5483 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5484 | if (hdev) |
| 5485 | mgmt_init_hdev(sk, hdev); |
| 5486 | |
| 5487 | cp = buf + sizeof(*hdr); |
| 5488 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5489 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5490 | if (err < 0) |
| 5491 | goto done; |
| 5492 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5493 | err = msglen; |
| 5494 | |
| 5495 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5496 | if (hdev) |
| 5497 | hci_dev_put(hdev); |
| 5498 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5499 | kfree(buf); |
| 5500 | return err; |
| 5501 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5502 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5503 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5504 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5505 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5506 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5507 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5508 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5509 | return; |
| 5510 | |
| 5511 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5512 | mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); |
| 5513 | else |
| 5514 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5515 | } |
| 5516 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5517 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5518 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5519 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5520 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5521 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5522 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5523 | |
Marcel Holtmann | 0602a8a | 2014-07-02 21:30:54 +0200 | [diff] [blame] | 5524 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) |
| 5525 | return; |
| 5526 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5527 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5528 | |
Marcel Holtmann | edd3896 | 2014-07-02 21:30:55 +0200 | [diff] [blame] | 5529 | if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) |
| 5530 | mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); |
| 5531 | else |
| 5532 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5533 | } |
| 5534 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5535 | /* This function requires the caller holds hdev->lock */ |
| 5536 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 5537 | { |
| 5538 | struct hci_conn_params *p; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5539 | bool added = false; |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5540 | |
| 5541 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5542 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) { |
Johan Hedberg | 912b42e | 2014-07-03 19:33:49 +0300 | [diff] [blame] | 5543 | hci_pend_le_conn_add(hdev, p); |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5544 | added = true; |
| 5545 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5546 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5547 | |
| 5548 | /* Calling hci_pend_le_conn_add will actually already trigger |
| 5549 | * background scanning when needed. So no need to trigger it |
| 5550 | * just another time. |
| 5551 | * |
| 5552 | * This check is here to avoid an unneeded restart of the |
| 5553 | * passive scanning. Since this is during the controller |
| 5554 | * power up phase the duplicate filtering is not an issue. |
| 5555 | */ |
| 5556 | if (added) |
| 5557 | return; |
| 5558 | |
| 5559 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5560 | } |
| 5561 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5562 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5563 | { |
| 5564 | struct cmd_lookup match = { NULL, hdev }; |
| 5565 | |
| 5566 | BT_DBG("status 0x%02x", status); |
| 5567 | |
| 5568 | hci_dev_lock(hdev); |
| 5569 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5570 | restart_le_auto_conns(hdev); |
| 5571 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5572 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5573 | |
| 5574 | new_settings(hdev, match.sk); |
| 5575 | |
| 5576 | hci_dev_unlock(hdev); |
| 5577 | |
| 5578 | if (match.sk) |
| 5579 | sock_put(match.sk); |
| 5580 | } |
| 5581 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5582 | static int powered_update_hci(struct hci_dev *hdev) |
| 5583 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5584 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5585 | u8 link_sec; |
| 5586 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5587 | hci_req_init(&req, hdev); |
| 5588 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5589 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5590 | !lmp_host_ssp_capable(hdev)) { |
| 5591 | u8 ssp = 1; |
| 5592 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5593 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5594 | } |
| 5595 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5596 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5597 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5598 | struct hci_cp_write_le_host_supported cp; |
| 5599 | |
| 5600 | cp.le = 1; |
| 5601 | cp.simul = lmp_le_br_capable(hdev); |
| 5602 | |
| 5603 | /* Check first if we already have the right |
| 5604 | * host state (host features set) |
| 5605 | */ |
| 5606 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5607 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5608 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5609 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5610 | } |
| 5611 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5612 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5613 | /* Make sure the controller has a good default for |
| 5614 | * advertising data. This also applies to the case |
| 5615 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5616 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5617 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5618 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5619 | update_scan_rsp_data(&req); |
| 5620 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5621 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5622 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5623 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5624 | } |
| 5625 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5626 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5627 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5628 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5629 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5630 | |
| 5631 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5632 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5633 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5634 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5635 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5636 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5637 | } |
| 5638 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5639 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5640 | } |
| 5641 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5642 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5643 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5644 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5645 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5646 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5647 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5648 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5649 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5650 | return 0; |
| 5651 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5652 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5653 | if (powered_update_hci(hdev) == 0) |
| 5654 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5655 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5656 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5657 | &match); |
| 5658 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5659 | } |
| 5660 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5661 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5662 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5663 | |
| 5664 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5665 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5666 | zero_cod, sizeof(zero_cod), NULL); |
| 5667 | |
| 5668 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5669 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5670 | |
| 5671 | if (match.sk) |
| 5672 | sock_put(match.sk); |
| 5673 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5674 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5675 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5676 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5677 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5678 | { |
| 5679 | struct pending_cmd *cmd; |
| 5680 | u8 status; |
| 5681 | |
| 5682 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5683 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5684 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5685 | |
| 5686 | if (err == -ERFKILL) |
| 5687 | status = MGMT_STATUS_RFKILLED; |
| 5688 | else |
| 5689 | status = MGMT_STATUS_FAILED; |
| 5690 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5691 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5692 | |
| 5693 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5694 | } |
| 5695 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5696 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5697 | { |
| 5698 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5699 | |
| 5700 | hci_dev_lock(hdev); |
| 5701 | |
| 5702 | /* When discoverable timeout triggers, then just make sure |
| 5703 | * the limited discoverable flag is cleared. Even in the case |
| 5704 | * of a timeout triggered from general discoverable, it is |
| 5705 | * safe to unconditionally clear the flag. |
| 5706 | */ |
| 5707 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5708 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5709 | |
| 5710 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5711 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5712 | u8 scan = SCAN_PAGE; |
| 5713 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5714 | sizeof(scan), &scan); |
| 5715 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5716 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5717 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5718 | hci_req_run(&req, NULL); |
| 5719 | |
| 5720 | hdev->discov_timeout = 0; |
| 5721 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5722 | new_settings(hdev, NULL); |
| 5723 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5724 | hci_dev_unlock(hdev); |
| 5725 | } |
| 5726 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5727 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5728 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5729 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5730 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5731 | /* Nothing needed here if there's a pending command since that |
| 5732 | * commands request completion callback takes care of everything |
| 5733 | * necessary. |
| 5734 | */ |
| 5735 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5736 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5737 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5738 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5739 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5740 | return; |
| 5741 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5742 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5743 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5744 | } else { |
| 5745 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5746 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5747 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5748 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5749 | if (changed) { |
| 5750 | struct hci_request req; |
| 5751 | |
| 5752 | /* In case this change in discoverable was triggered by |
| 5753 | * a disabling of connectable there could be a need to |
| 5754 | * update the advertising flags. |
| 5755 | */ |
| 5756 | hci_req_init(&req, hdev); |
| 5757 | update_adv_data(&req); |
| 5758 | hci_req_run(&req, NULL); |
| 5759 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5760 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5761 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5762 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5763 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5764 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5765 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5766 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5767 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5768 | /* Nothing needed here if there's a pending command since that |
| 5769 | * commands request completion callback takes care of everything |
| 5770 | * necessary. |
| 5771 | */ |
| 5772 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5773 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5774 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5775 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5776 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5777 | return; |
| 5778 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5779 | if (connectable) |
| 5780 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5781 | else |
| 5782 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5783 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5784 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5785 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5786 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5787 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5788 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5789 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5790 | /* Powering off may stop advertising - don't let that interfere */ |
| 5791 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5792 | return; |
| 5793 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5794 | if (advertising) |
| 5795 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5796 | else |
| 5797 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5798 | } |
| 5799 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5800 | 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] | 5801 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5802 | u8 mgmt_err = mgmt_status(status); |
| 5803 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5804 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5805 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5806 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5807 | |
| 5808 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5809 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5810 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5811 | } |
| 5812 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5813 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5814 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5815 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5816 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5817 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5818 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5819 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5820 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5821 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5822 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5823 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5824 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5825 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5826 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5827 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5828 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5829 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5830 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5831 | { |
| 5832 | if (ltk->authenticated) |
| 5833 | return MGMT_LTK_AUTHENTICATED; |
| 5834 | |
| 5835 | return MGMT_LTK_UNAUTHENTICATED; |
| 5836 | } |
| 5837 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5838 | 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] | 5839 | { |
| 5840 | struct mgmt_ev_new_long_term_key ev; |
| 5841 | |
| 5842 | memset(&ev, 0, sizeof(ev)); |
| 5843 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5844 | /* Devices using resolvable or non-resolvable random addresses |
| 5845 | * without providing an indentity resolving key don't require |
| 5846 | * to store long term keys. Their addresses will change the |
| 5847 | * next time around. |
| 5848 | * |
| 5849 | * Only when a remote device provides an identity address |
| 5850 | * make sure the long term key is stored. If the remote |
| 5851 | * identity is known, the long term keys are internally |
| 5852 | * mapped to the identity address. So allow static random |
| 5853 | * and public addresses here. |
| 5854 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5855 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5856 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5857 | ev.store_hint = 0x00; |
| 5858 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5859 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5860 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5861 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5862 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5863 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5864 | ev.key.enc_size = key->enc_size; |
| 5865 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5866 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5867 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5868 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5869 | ev.key.master = 1; |
| 5870 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5871 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5872 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5873 | 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] | 5874 | } |
| 5875 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5876 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5877 | { |
| 5878 | struct mgmt_ev_new_irk ev; |
| 5879 | |
| 5880 | memset(&ev, 0, sizeof(ev)); |
| 5881 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5882 | /* For identity resolving keys from devices that are already |
| 5883 | * using a public address or static random address, do not |
| 5884 | * ask for storing this key. The identity resolving key really |
| 5885 | * is only mandatory for devices using resovlable random |
| 5886 | * addresses. |
| 5887 | * |
| 5888 | * Storing all identity resolving keys has the downside that |
| 5889 | * they will be also loaded on next boot of they system. More |
| 5890 | * identity resolving keys, means more time during scanning is |
| 5891 | * needed to actually resolve these addresses. |
| 5892 | */ |
| 5893 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5894 | ev.store_hint = 0x01; |
| 5895 | else |
| 5896 | ev.store_hint = 0x00; |
| 5897 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5898 | bacpy(&ev.rpa, &irk->rpa); |
| 5899 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5900 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5901 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5902 | |
| 5903 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5904 | } |
| 5905 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5906 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5907 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5908 | { |
| 5909 | struct mgmt_ev_new_csrk ev; |
| 5910 | |
| 5911 | memset(&ev, 0, sizeof(ev)); |
| 5912 | |
| 5913 | /* Devices using resolvable or non-resolvable random addresses |
| 5914 | * without providing an indentity resolving key don't require |
| 5915 | * to store signature resolving keys. Their addresses will change |
| 5916 | * the next time around. |
| 5917 | * |
| 5918 | * Only when a remote device provides an identity address |
| 5919 | * make sure the signature resolving key is stored. So allow |
| 5920 | * static random and public addresses here. |
| 5921 | */ |
| 5922 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5923 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5924 | ev.store_hint = 0x00; |
| 5925 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5926 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5927 | |
| 5928 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5929 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5930 | ev.key.master = csrk->master; |
| 5931 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5932 | |
| 5933 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5934 | } |
| 5935 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5936 | void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5937 | u8 bdaddr_type, u8 store_hint, u16 min_interval, |
| 5938 | u16 max_interval, u16 latency, u16 timeout) |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5939 | { |
| 5940 | struct mgmt_ev_new_conn_param ev; |
| 5941 | |
Johan Hedberg | c103aea | 2014-07-02 17:37:34 +0300 | [diff] [blame] | 5942 | if (!hci_is_identity_address(bdaddr, bdaddr_type)) |
| 5943 | return; |
| 5944 | |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5945 | memset(&ev, 0, sizeof(ev)); |
| 5946 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5947 | ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type); |
Johan Hedberg | f4869e2 | 2014-07-02 17:37:32 +0300 | [diff] [blame] | 5948 | ev.store_hint = store_hint; |
Andre Guedes | ffb5a827 | 2014-07-01 18:10:11 -0300 | [diff] [blame] | 5949 | ev.min_interval = cpu_to_le16(min_interval); |
| 5950 | ev.max_interval = cpu_to_le16(max_interval); |
| 5951 | ev.latency = cpu_to_le16(latency); |
| 5952 | ev.timeout = cpu_to_le16(timeout); |
| 5953 | |
| 5954 | mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); |
| 5955 | } |
| 5956 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5957 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5958 | u8 data_len) |
| 5959 | { |
| 5960 | eir[eir_len++] = sizeof(type) + data_len; |
| 5961 | eir[eir_len++] = type; |
| 5962 | memcpy(&eir[eir_len], data, data_len); |
| 5963 | eir_len += data_len; |
| 5964 | |
| 5965 | return eir_len; |
| 5966 | } |
| 5967 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5968 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5969 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5970 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5971 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5972 | char buf[512]; |
| 5973 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5974 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5975 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5976 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5977 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5978 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5979 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5980 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5981 | if (name_len > 0) |
| 5982 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5983 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5984 | |
| 5985 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5986 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5987 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5988 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5989 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5990 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5991 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5992 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5993 | } |
| 5994 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5995 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5996 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5997 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5998 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5999 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6000 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6001 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6002 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6003 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6004 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6005 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6006 | |
| 6007 | *sk = cmd->sk; |
| 6008 | sock_hold(*sk); |
| 6009 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6010 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6011 | } |
| 6012 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6013 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6014 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6015 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6016 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 6017 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6018 | |
| 6019 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6020 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 6021 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6022 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 6023 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 6024 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6025 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 6026 | |
| 6027 | mgmt_pending_remove(cmd); |
| 6028 | } |
| 6029 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6030 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6031 | u8 link_type, u8 addr_type, u8 reason, |
| 6032 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6033 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6034 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6035 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6036 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6037 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6038 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6039 | if (power_off) { |
| 6040 | struct mgmt_mode *cp = power_off->param; |
| 6041 | |
| 6042 | /* The connection is still in hci_conn_hash so test for 1 |
| 6043 | * instead of 0 to know if this is the last one. |
| 6044 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6045 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6046 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6047 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6048 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 6049 | } |
| 6050 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 6051 | if (!mgmt_connected) |
| 6052 | return; |
| 6053 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 6054 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 6055 | return; |
| 6056 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6057 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6058 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 6059 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6060 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6061 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6062 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 6063 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6064 | |
| 6065 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 6066 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6067 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 6068 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6069 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6070 | } |
| 6071 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6072 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6073 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6074 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6075 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 6076 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6077 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6078 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6079 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 6080 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 6081 | hdev); |
| 6082 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6083 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6084 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6085 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6086 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6087 | cp = cmd->param; |
| 6088 | |
| 6089 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 6090 | return; |
| 6091 | |
| 6092 | if (cp->addr.type != bdaddr_type) |
| 6093 | return; |
| 6094 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 6095 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 6096 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 6097 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 6098 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 6099 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 6100 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6101 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 6102 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6103 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6104 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6105 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6106 | { |
| 6107 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6108 | struct pending_cmd *power_off; |
| 6109 | |
| 6110 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 6111 | if (power_off) { |
| 6112 | struct mgmt_mode *cp = power_off->param; |
| 6113 | |
| 6114 | /* The connection is still in hci_conn_hash so test for 1 |
| 6115 | * instead of 0 to know if this is the last one. |
| 6116 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6117 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 6118 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6119 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 6120 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 6121 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6122 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 6123 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6124 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6125 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6126 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 6127 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 6128 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6129 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6130 | 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] | 6131 | { |
| 6132 | struct mgmt_ev_pin_code_request ev; |
| 6133 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6134 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6135 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 6136 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6137 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 6138 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6139 | } |
| 6140 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6141 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6142 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6143 | { |
| 6144 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6145 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6146 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6147 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6148 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6149 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6150 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6151 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6152 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6153 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 6154 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 6155 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6156 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6157 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6158 | } |
| 6159 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6160 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6161 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6162 | { |
| 6163 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6164 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6165 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6166 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6167 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6168 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6169 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 6170 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 6171 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 6172 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 6173 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 6174 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6175 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6176 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 6177 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6178 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6179 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6180 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6181 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6182 | { |
| 6183 | struct mgmt_ev_user_confirm_request ev; |
| 6184 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6185 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6186 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6187 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6188 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 6189 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 6190 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6191 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6192 | 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] | 6193 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6194 | } |
| 6195 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6196 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 6197 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6198 | { |
| 6199 | struct mgmt_ev_user_passkey_request ev; |
| 6200 | |
| 6201 | BT_DBG("%s", hdev->name); |
| 6202 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6203 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6204 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6205 | |
| 6206 | 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] | 6207 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6208 | } |
| 6209 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 6210 | 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] | 6211 | u8 link_type, u8 addr_type, u8 status, |
| 6212 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6213 | { |
| 6214 | struct pending_cmd *cmd; |
| 6215 | struct mgmt_rp_user_confirm_reply rp; |
| 6216 | int err; |
| 6217 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6218 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6219 | if (!cmd) |
| 6220 | return -ENOENT; |
| 6221 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6222 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6223 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 6224 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6225 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6226 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 6227 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6228 | |
| 6229 | return err; |
| 6230 | } |
| 6231 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6232 | 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] | 6233 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6234 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6235 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6236 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6237 | } |
| 6238 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6239 | 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] | 6240 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6241 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6242 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6243 | status, |
| 6244 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 6245 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6246 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6247 | 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] | 6248 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6249 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6250 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6251 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6252 | } |
| 6253 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6254 | 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] | 6255 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6256 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 6257 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 6258 | status, |
| 6259 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 6260 | } |
| 6261 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 6262 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 6263 | u8 link_type, u8 addr_type, u32 passkey, |
| 6264 | u8 entered) |
| 6265 | { |
| 6266 | struct mgmt_ev_passkey_notify ev; |
| 6267 | |
| 6268 | BT_DBG("%s", hdev->name); |
| 6269 | |
| 6270 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6271 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6272 | ev.passkey = __cpu_to_le32(passkey); |
| 6273 | ev.entered = entered; |
| 6274 | |
| 6275 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6276 | } |
| 6277 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6278 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6279 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6280 | { |
| 6281 | struct mgmt_ev_auth_failed ev; |
| 6282 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6283 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6284 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6285 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6286 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6287 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6288 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6289 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6290 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6291 | { |
| 6292 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6293 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6294 | |
| 6295 | if (status) { |
| 6296 | u8 mgmt_err = mgmt_status(status); |
| 6297 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6298 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6299 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6300 | } |
| 6301 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6302 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6303 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6304 | &hdev->dev_flags); |
| 6305 | else |
| 6306 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6307 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6308 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6309 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6310 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6311 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6312 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6313 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6314 | |
| 6315 | if (match.sk) |
| 6316 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6317 | } |
| 6318 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6319 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6320 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6321 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6322 | struct hci_cp_write_eir cp; |
| 6323 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6324 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6325 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6326 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6327 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6328 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6329 | memset(&cp, 0, sizeof(cp)); |
| 6330 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6331 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6332 | } |
| 6333 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6334 | 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] | 6335 | { |
| 6336 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6337 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6338 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6339 | |
| 6340 | if (status) { |
| 6341 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6342 | |
| 6343 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6344 | &hdev->dev_flags)) { |
| 6345 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6346 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6347 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6348 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6349 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6350 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6351 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6352 | } |
| 6353 | |
| 6354 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6355 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6356 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6357 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6358 | if (!changed) |
| 6359 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6360 | &hdev->dev_flags); |
| 6361 | else |
| 6362 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6363 | } |
| 6364 | |
| 6365 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6366 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6367 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6368 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6369 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6370 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6371 | sock_put(match.sk); |
| 6372 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6373 | hci_req_init(&req, hdev); |
| 6374 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6375 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6376 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6377 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6378 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6379 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6380 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6381 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6382 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6383 | |
| 6384 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6385 | } |
| 6386 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6387 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6388 | { |
| 6389 | struct cmd_lookup match = { NULL, hdev }; |
| 6390 | bool changed = false; |
| 6391 | |
| 6392 | if (status) { |
| 6393 | u8 mgmt_err = mgmt_status(status); |
| 6394 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6395 | if (enable) { |
| 6396 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6397 | &hdev->dev_flags)) |
| 6398 | new_settings(hdev, NULL); |
| 6399 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6400 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6401 | |
| 6402 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6403 | cmd_status_rsp, &mgmt_err); |
| 6404 | return; |
| 6405 | } |
| 6406 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6407 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6408 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6409 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6410 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6411 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6412 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6413 | |
| 6414 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6415 | settings_rsp, &match); |
| 6416 | |
| 6417 | if (changed) |
| 6418 | new_settings(hdev, match.sk); |
| 6419 | |
| 6420 | if (match.sk) |
| 6421 | sock_put(match.sk); |
| 6422 | } |
| 6423 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6424 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6425 | { |
| 6426 | struct cmd_lookup *match = data; |
| 6427 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6428 | if (match->sk == NULL) { |
| 6429 | match->sk = cmd->sk; |
| 6430 | sock_hold(match->sk); |
| 6431 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6432 | } |
| 6433 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6434 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6435 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6436 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6437 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6438 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6439 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6440 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6441 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6442 | |
| 6443 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6444 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6445 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6446 | |
| 6447 | if (match.sk) |
| 6448 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6449 | } |
| 6450 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6451 | 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] | 6452 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6453 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6454 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6455 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6456 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6457 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6458 | |
| 6459 | memset(&ev, 0, sizeof(ev)); |
| 6460 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6461 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6462 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6463 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6464 | if (!cmd) { |
| 6465 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6466 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6467 | /* If this is a HCI command related to powering on the |
| 6468 | * HCI dev don't send any mgmt signals. |
| 6469 | */ |
| 6470 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6471 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6472 | } |
| 6473 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6474 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6475 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6476 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6477 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6478 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6479 | u8 *randomizer192, u8 *hash256, |
| 6480 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6481 | { |
| 6482 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6483 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6484 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6485 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6486 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6487 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6488 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6489 | |
| 6490 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6491 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6492 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6493 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6494 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6495 | hash256 && randomizer256) { |
| 6496 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6497 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6498 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6499 | memcpy(rp.randomizer192, randomizer192, |
| 6500 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6501 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6502 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6503 | memcpy(rp.randomizer256, randomizer256, |
| 6504 | sizeof(rp.randomizer256)); |
| 6505 | |
| 6506 | cmd_complete(cmd->sk, hdev->id, |
| 6507 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6508 | &rp, sizeof(rp)); |
| 6509 | } else { |
| 6510 | struct mgmt_rp_read_local_oob_data rp; |
| 6511 | |
| 6512 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6513 | memcpy(rp.randomizer, randomizer192, |
| 6514 | sizeof(rp.randomizer)); |
| 6515 | |
| 6516 | cmd_complete(cmd->sk, hdev->id, |
| 6517 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6518 | &rp, sizeof(rp)); |
| 6519 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6520 | } |
| 6521 | |
| 6522 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6523 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6524 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6525 | 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] | 6526 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6527 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6528 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6529 | char buf[512]; |
| 6530 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6531 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6532 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6533 | |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6534 | /* Don't send events for a non-kernel initiated discovery. With |
| 6535 | * LE one exception is if we have pend_le_reports > 0 in which |
| 6536 | * case we're doing passive scanning and want these events. |
| 6537 | */ |
| 6538 | if (!hci_discovery_active(hdev)) { |
| 6539 | if (link_type == ACL_LINK) |
| 6540 | return; |
Johan Hedberg | 66f8455 | 2014-07-04 12:37:18 +0300 | [diff] [blame] | 6541 | if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports)) |
Johan Hedberg | 75ce208 | 2014-07-02 22:42:01 +0300 | [diff] [blame] | 6542 | return; |
| 6543 | } |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6544 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6545 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6546 | * are for the potential CoD field. |
| 6547 | */ |
| 6548 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6549 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6550 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6551 | memset(buf, 0, sizeof(buf)); |
| 6552 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6553 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6554 | if (irk) { |
| 6555 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6556 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6557 | } else { |
| 6558 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6559 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6560 | } |
| 6561 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6562 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6563 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6564 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6565 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6566 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6567 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6568 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6569 | 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] | 6570 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6571 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6572 | if (scan_rsp_len > 0) |
| 6573 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6574 | |
| 6575 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6576 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6577 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6578 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6579 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6580 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6581 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6582 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6583 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6584 | struct mgmt_ev_device_found *ev; |
| 6585 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6586 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6587 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6588 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6589 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6590 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6591 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6592 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6593 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6594 | ev->rssi = rssi; |
| 6595 | |
| 6596 | 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] | 6597 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6598 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6599 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6600 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6601 | 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] | 6602 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6603 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6604 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6605 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6606 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6607 | struct pending_cmd *cmd; |
| 6608 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6609 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6610 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6611 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6612 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6613 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6614 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6615 | |
| 6616 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6617 | u8 type = hdev->discovery.type; |
| 6618 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6619 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6620 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6621 | mgmt_pending_remove(cmd); |
| 6622 | } |
| 6623 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6624 | memset(&ev, 0, sizeof(ev)); |
| 6625 | ev.type = hdev->discovery.type; |
| 6626 | ev.discovering = discovering; |
| 6627 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6628 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6629 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6630 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6631 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6632 | { |
| 6633 | BT_DBG("%s status %u", hdev->name, status); |
| 6634 | |
| 6635 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6636 | if (status) { |
| 6637 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6638 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6639 | } |
| 6640 | } |
| 6641 | |
| 6642 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6643 | { |
| 6644 | struct hci_request req; |
| 6645 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6646 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6647 | return; |
| 6648 | |
| 6649 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6650 | return; |
| 6651 | |
| 6652 | hci_req_init(&req, hdev); |
| 6653 | enable_advertising(&req); |
| 6654 | |
| 6655 | /* If this fails we have no option but to let user space know |
| 6656 | * that we've disabled advertising. |
| 6657 | */ |
| 6658 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6659 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6660 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6661 | } |
| 6662 | } |