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 | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static const u16 mgmt_events[] = { |
| 94 | MGMT_EV_CONTROLLER_ERROR, |
| 95 | MGMT_EV_INDEX_ADDED, |
| 96 | MGMT_EV_INDEX_REMOVED, |
| 97 | MGMT_EV_NEW_SETTINGS, |
| 98 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 99 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 100 | MGMT_EV_NEW_LINK_KEY, |
| 101 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 102 | MGMT_EV_DEVICE_CONNECTED, |
| 103 | MGMT_EV_DEVICE_DISCONNECTED, |
| 104 | MGMT_EV_CONNECT_FAILED, |
| 105 | MGMT_EV_PIN_CODE_REQUEST, |
| 106 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 107 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 108 | MGMT_EV_AUTH_FAILED, |
| 109 | MGMT_EV_DEVICE_FOUND, |
| 110 | MGMT_EV_DISCOVERING, |
| 111 | MGMT_EV_DEVICE_BLOCKED, |
| 112 | MGMT_EV_DEVICE_UNBLOCKED, |
| 113 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 114 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 115 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 116 | MGMT_EV_NEW_CSRK, |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame^] | 117 | MGMT_EV_DEVICE_ADDED, |
| 118 | MGMT_EV_DEVICE_REMOVED, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 119 | }; |
| 120 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 121 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 122 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 123 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 124 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 125 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 126 | struct pending_cmd { |
| 127 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 128 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 129 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 130 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 131 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 132 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 133 | }; |
| 134 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 135 | /* HCI to MGMT error code conversion table */ |
| 136 | static u8 mgmt_status_table[] = { |
| 137 | MGMT_STATUS_SUCCESS, |
| 138 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 139 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 140 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 141 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 142 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 143 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 144 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 145 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 146 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 147 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 148 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 149 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 150 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 151 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 152 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 153 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 154 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 155 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 156 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 157 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 158 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 159 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 160 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 161 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 162 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 163 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 164 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 165 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 166 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 167 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 168 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 169 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 170 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 171 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 172 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 173 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 174 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 175 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 176 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 177 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 178 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 179 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 180 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 181 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 182 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 183 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 184 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 185 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 186 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 187 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 188 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 189 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 190 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 191 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 192 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 193 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 194 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 195 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 196 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 197 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 198 | }; |
| 199 | |
| 200 | static u8 mgmt_status(u8 hci_status) |
| 201 | { |
| 202 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 203 | return mgmt_status_table[hci_status]; |
| 204 | |
| 205 | return MGMT_STATUS_FAILED; |
| 206 | } |
| 207 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 208 | 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] | 209 | { |
| 210 | struct sk_buff *skb; |
| 211 | struct mgmt_hdr *hdr; |
| 212 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 213 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 214 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 215 | 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] | 216 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 217 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 218 | if (!skb) |
| 219 | return -ENOMEM; |
| 220 | |
| 221 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 222 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 223 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 224 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 225 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 226 | |
| 227 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 228 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 229 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 230 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 231 | err = sock_queue_rcv_skb(sk, skb); |
| 232 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 233 | kfree_skb(skb); |
| 234 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 235 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 238 | 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] | 239 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 240 | { |
| 241 | struct sk_buff *skb; |
| 242 | struct mgmt_hdr *hdr; |
| 243 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 244 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 245 | |
| 246 | BT_DBG("sock %p", sk); |
| 247 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 248 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 249 | if (!skb) |
| 250 | return -ENOMEM; |
| 251 | |
| 252 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 253 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 254 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 255 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 256 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 257 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 258 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 259 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 260 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 261 | |
| 262 | if (rp) |
| 263 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 264 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 265 | err = sock_queue_rcv_skb(sk, skb); |
| 266 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 267 | kfree_skb(skb); |
| 268 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 269 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 272 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 273 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 274 | { |
| 275 | struct mgmt_rp_read_version rp; |
| 276 | |
| 277 | BT_DBG("sock %p", sk); |
| 278 | |
| 279 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 280 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 281 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 282 | 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] | 283 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 284 | } |
| 285 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 286 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 287 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 288 | { |
| 289 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 290 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 291 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 292 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 293 | size_t rp_size; |
| 294 | int i, err; |
| 295 | |
| 296 | BT_DBG("sock %p", sk); |
| 297 | |
| 298 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 299 | |
| 300 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 301 | if (!rp) |
| 302 | return -ENOMEM; |
| 303 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 304 | rp->num_commands = cpu_to_le16(num_commands); |
| 305 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 306 | |
| 307 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 308 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 309 | |
| 310 | for (i = 0; i < num_events; i++, opcode++) |
| 311 | put_unaligned_le16(mgmt_events[i], opcode); |
| 312 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 313 | 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] | 314 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 315 | kfree(rp); |
| 316 | |
| 317 | return err; |
| 318 | } |
| 319 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 320 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 321 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 322 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 323 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 324 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 325 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 326 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 327 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 328 | |
| 329 | BT_DBG("sock %p", sk); |
| 330 | |
| 331 | read_lock(&hci_dev_list_lock); |
| 332 | |
| 333 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 334 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 335 | if (d->dev_type == HCI_BREDR) |
| 336 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 337 | } |
| 338 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 339 | rp_len = sizeof(*rp) + (2 * count); |
| 340 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 341 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 342 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 343 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 344 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 345 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 346 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 347 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 348 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 349 | continue; |
| 350 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 351 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 352 | continue; |
| 353 | |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 354 | if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks)) |
| 355 | continue; |
| 356 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 357 | if (d->dev_type == HCI_BREDR) { |
| 358 | rp->index[count++] = cpu_to_le16(d->id); |
| 359 | BT_DBG("Added hci%u", d->id); |
| 360 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 363 | rp->num_controllers = cpu_to_le16(count); |
| 364 | rp_len = sizeof(*rp) + (2 * count); |
| 365 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 366 | read_unlock(&hci_dev_list_lock); |
| 367 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 368 | 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] | 369 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 370 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 371 | kfree(rp); |
| 372 | |
| 373 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 376 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 377 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 378 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 379 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 380 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 381 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 382 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 383 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 384 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 385 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 386 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 387 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 388 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 389 | settings |= MGMT_SETTING_BREDR; |
| 390 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 391 | |
| 392 | if (lmp_ssp_capable(hdev)) { |
| 393 | settings |= MGMT_SETTING_SSP; |
| 394 | settings |= MGMT_SETTING_HS; |
| 395 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 396 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 397 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 398 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 399 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 400 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 401 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 402 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 403 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 404 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 405 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 406 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 407 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 408 | return settings; |
| 409 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 410 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 411 | static u32 get_current_settings(struct hci_dev *hdev) |
| 412 | { |
| 413 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 414 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 415 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 416 | settings |= MGMT_SETTING_POWERED; |
| 417 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 418 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 419 | settings |= MGMT_SETTING_CONNECTABLE; |
| 420 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 421 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 422 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 423 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 424 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 425 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 426 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 427 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 428 | settings |= MGMT_SETTING_PAIRABLE; |
| 429 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 430 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 431 | settings |= MGMT_SETTING_BREDR; |
| 432 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 433 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 434 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 435 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 436 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 437 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 438 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 439 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 440 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 441 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 442 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 443 | settings |= MGMT_SETTING_HS; |
| 444 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 445 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 446 | settings |= MGMT_SETTING_ADVERTISING; |
| 447 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 448 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 449 | settings |= MGMT_SETTING_SECURE_CONN; |
| 450 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 451 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 452 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 453 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 454 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 455 | settings |= MGMT_SETTING_PRIVACY; |
| 456 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 457 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 460 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 461 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 462 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 463 | { |
| 464 | u8 *ptr = data, *uuids_start = NULL; |
| 465 | struct bt_uuid *uuid; |
| 466 | |
| 467 | if (len < 4) |
| 468 | return ptr; |
| 469 | |
| 470 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 471 | u16 uuid16; |
| 472 | |
| 473 | if (uuid->size != 16) |
| 474 | continue; |
| 475 | |
| 476 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 477 | if (uuid16 < 0x1100) |
| 478 | continue; |
| 479 | |
| 480 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 481 | continue; |
| 482 | |
| 483 | if (!uuids_start) { |
| 484 | uuids_start = ptr; |
| 485 | uuids_start[0] = 1; |
| 486 | uuids_start[1] = EIR_UUID16_ALL; |
| 487 | ptr += 2; |
| 488 | } |
| 489 | |
| 490 | /* Stop if not enough space to put next UUID */ |
| 491 | if ((ptr - data) + sizeof(u16) > len) { |
| 492 | uuids_start[1] = EIR_UUID16_SOME; |
| 493 | break; |
| 494 | } |
| 495 | |
| 496 | *ptr++ = (uuid16 & 0x00ff); |
| 497 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 498 | uuids_start[0] += sizeof(uuid16); |
| 499 | } |
| 500 | |
| 501 | return ptr; |
| 502 | } |
| 503 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 504 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 505 | { |
| 506 | u8 *ptr = data, *uuids_start = NULL; |
| 507 | struct bt_uuid *uuid; |
| 508 | |
| 509 | if (len < 6) |
| 510 | return ptr; |
| 511 | |
| 512 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 513 | if (uuid->size != 32) |
| 514 | continue; |
| 515 | |
| 516 | if (!uuids_start) { |
| 517 | uuids_start = ptr; |
| 518 | uuids_start[0] = 1; |
| 519 | uuids_start[1] = EIR_UUID32_ALL; |
| 520 | ptr += 2; |
| 521 | } |
| 522 | |
| 523 | /* Stop if not enough space to put next UUID */ |
| 524 | if ((ptr - data) + sizeof(u32) > len) { |
| 525 | uuids_start[1] = EIR_UUID32_SOME; |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 530 | ptr += sizeof(u32); |
| 531 | uuids_start[0] += sizeof(u32); |
| 532 | } |
| 533 | |
| 534 | return ptr; |
| 535 | } |
| 536 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 537 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 538 | { |
| 539 | u8 *ptr = data, *uuids_start = NULL; |
| 540 | struct bt_uuid *uuid; |
| 541 | |
| 542 | if (len < 18) |
| 543 | return ptr; |
| 544 | |
| 545 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 546 | if (uuid->size != 128) |
| 547 | continue; |
| 548 | |
| 549 | if (!uuids_start) { |
| 550 | uuids_start = ptr; |
| 551 | uuids_start[0] = 1; |
| 552 | uuids_start[1] = EIR_UUID128_ALL; |
| 553 | ptr += 2; |
| 554 | } |
| 555 | |
| 556 | /* Stop if not enough space to put next UUID */ |
| 557 | if ((ptr - data) + 16 > len) { |
| 558 | uuids_start[1] = EIR_UUID128_SOME; |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | memcpy(ptr, uuid->uuid, 16); |
| 563 | ptr += 16; |
| 564 | uuids_start[0] += 16; |
| 565 | } |
| 566 | |
| 567 | return ptr; |
| 568 | } |
| 569 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 570 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 571 | { |
| 572 | struct pending_cmd *cmd; |
| 573 | |
| 574 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 575 | if (cmd->opcode == opcode) |
| 576 | return cmd; |
| 577 | } |
| 578 | |
| 579 | return NULL; |
| 580 | } |
| 581 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 582 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 583 | struct hci_dev *hdev, |
| 584 | const void *data) |
| 585 | { |
| 586 | struct pending_cmd *cmd; |
| 587 | |
| 588 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 589 | if (cmd->user_data != data) |
| 590 | continue; |
| 591 | if (cmd->opcode == opcode) |
| 592 | return cmd; |
| 593 | } |
| 594 | |
| 595 | return NULL; |
| 596 | } |
| 597 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 598 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 599 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 600 | u8 ad_len = 0; |
| 601 | size_t name_len; |
| 602 | |
| 603 | name_len = strlen(hdev->dev_name); |
| 604 | if (name_len > 0) { |
| 605 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 606 | |
| 607 | if (name_len > max_len) { |
| 608 | name_len = max_len; |
| 609 | ptr[1] = EIR_NAME_SHORT; |
| 610 | } else |
| 611 | ptr[1] = EIR_NAME_COMPLETE; |
| 612 | |
| 613 | ptr[0] = name_len + 1; |
| 614 | |
| 615 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 616 | |
| 617 | ad_len += (name_len + 2); |
| 618 | ptr += (name_len + 2); |
| 619 | } |
| 620 | |
| 621 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static void update_scan_rsp_data(struct hci_request *req) |
| 625 | { |
| 626 | struct hci_dev *hdev = req->hdev; |
| 627 | struct hci_cp_le_set_scan_rsp_data cp; |
| 628 | u8 len; |
| 629 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 630 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 631 | return; |
| 632 | |
| 633 | memset(&cp, 0, sizeof(cp)); |
| 634 | |
| 635 | len = create_scan_rsp_data(hdev, cp.data); |
| 636 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 637 | if (hdev->scan_rsp_data_len == len && |
| 638 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 639 | return; |
| 640 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 641 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 642 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 643 | |
| 644 | cp.length = len; |
| 645 | |
| 646 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 647 | } |
| 648 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 649 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 650 | { |
| 651 | struct pending_cmd *cmd; |
| 652 | |
| 653 | /* If there's a pending mgmt command the flags will not yet have |
| 654 | * their final values, so check for this first. |
| 655 | */ |
| 656 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 657 | if (cmd) { |
| 658 | struct mgmt_mode *cp = cmd->param; |
| 659 | if (cp->val == 0x01) |
| 660 | return LE_AD_GENERAL; |
| 661 | else if (cp->val == 0x02) |
| 662 | return LE_AD_LIMITED; |
| 663 | } else { |
| 664 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 665 | return LE_AD_LIMITED; |
| 666 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 667 | return LE_AD_GENERAL; |
| 668 | } |
| 669 | |
| 670 | return 0; |
| 671 | } |
| 672 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 673 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 674 | { |
| 675 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 676 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 677 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 678 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 679 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 680 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 681 | |
| 682 | if (flags) { |
| 683 | BT_DBG("adv flags 0x%02x", flags); |
| 684 | |
| 685 | ptr[0] = 2; |
| 686 | ptr[1] = EIR_FLAGS; |
| 687 | ptr[2] = flags; |
| 688 | |
| 689 | ad_len += 3; |
| 690 | ptr += 3; |
| 691 | } |
| 692 | |
| 693 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 694 | ptr[0] = 2; |
| 695 | ptr[1] = EIR_TX_POWER; |
| 696 | ptr[2] = (u8) hdev->adv_tx_power; |
| 697 | |
| 698 | ad_len += 3; |
| 699 | ptr += 3; |
| 700 | } |
| 701 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 702 | return ad_len; |
| 703 | } |
| 704 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 705 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 706 | { |
| 707 | struct hci_dev *hdev = req->hdev; |
| 708 | struct hci_cp_le_set_adv_data cp; |
| 709 | u8 len; |
| 710 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 711 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 712 | return; |
| 713 | |
| 714 | memset(&cp, 0, sizeof(cp)); |
| 715 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 716 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 717 | |
| 718 | if (hdev->adv_data_len == len && |
| 719 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 720 | return; |
| 721 | |
| 722 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 723 | hdev->adv_data_len = len; |
| 724 | |
| 725 | cp.length = len; |
| 726 | |
| 727 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 728 | } |
| 729 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 730 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 731 | { |
| 732 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 733 | size_t name_len; |
| 734 | |
| 735 | name_len = strlen(hdev->dev_name); |
| 736 | |
| 737 | if (name_len > 0) { |
| 738 | /* EIR Data type */ |
| 739 | if (name_len > 48) { |
| 740 | name_len = 48; |
| 741 | ptr[1] = EIR_NAME_SHORT; |
| 742 | } else |
| 743 | ptr[1] = EIR_NAME_COMPLETE; |
| 744 | |
| 745 | /* EIR Data length */ |
| 746 | ptr[0] = name_len + 1; |
| 747 | |
| 748 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 749 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 750 | ptr += (name_len + 2); |
| 751 | } |
| 752 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 753 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 754 | ptr[0] = 2; |
| 755 | ptr[1] = EIR_TX_POWER; |
| 756 | ptr[2] = (u8) hdev->inq_tx_power; |
| 757 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 758 | ptr += 3; |
| 759 | } |
| 760 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 761 | if (hdev->devid_source > 0) { |
| 762 | ptr[0] = 9; |
| 763 | ptr[1] = EIR_DEVICE_ID; |
| 764 | |
| 765 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 766 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 767 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 768 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 769 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 770 | ptr += 10; |
| 771 | } |
| 772 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 773 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 774 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 775 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 776 | } |
| 777 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 778 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 779 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 780 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 781 | struct hci_cp_write_eir cp; |
| 782 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 783 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 784 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 785 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 786 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 787 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 788 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 789 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 790 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 791 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 792 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 793 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 794 | |
| 795 | memset(&cp, 0, sizeof(cp)); |
| 796 | |
| 797 | create_eir(hdev, cp.data); |
| 798 | |
| 799 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 800 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 801 | |
| 802 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 803 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 804 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 805 | } |
| 806 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 807 | static u8 get_service_classes(struct hci_dev *hdev) |
| 808 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 809 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 810 | u8 val = 0; |
| 811 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 812 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 813 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 814 | |
| 815 | return val; |
| 816 | } |
| 817 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 818 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 819 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 820 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 821 | u8 cod[3]; |
| 822 | |
| 823 | BT_DBG("%s", hdev->name); |
| 824 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 825 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 826 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 827 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 828 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 829 | return; |
| 830 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 831 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 832 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 833 | |
| 834 | cod[0] = hdev->minor_class; |
| 835 | cod[1] = hdev->major_class; |
| 836 | cod[2] = get_service_classes(hdev); |
| 837 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 838 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 839 | cod[1] |= 0x20; |
| 840 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 841 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 842 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 843 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 844 | 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] | 845 | } |
| 846 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 847 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 848 | { |
| 849 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 850 | |
| 851 | /* If there's a pending mgmt command the flag will not yet have |
| 852 | * it's final value, so check for this first. |
| 853 | */ |
| 854 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 855 | if (cmd) { |
| 856 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 857 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 860 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static void enable_advertising(struct hci_request *req) |
| 864 | { |
| 865 | struct hci_dev *hdev = req->hdev; |
| 866 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 867 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 868 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 869 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 870 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 871 | * hci_update_random_address knows that it's safe to go ahead |
| 872 | * and write a new random address. The flag will be set back on |
| 873 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 874 | */ |
| 875 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 876 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 877 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 878 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 879 | /* Set require_privacy to true only when non-connectable |
| 880 | * advertising is used. In that case it is fine to use a |
| 881 | * non-resolvable private address. |
| 882 | */ |
| 883 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 884 | return; |
| 885 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 886 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 887 | cp.min_interval = cpu_to_le16(0x0800); |
| 888 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 889 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 890 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 891 | cp.channel_map = hdev->le_adv_channel_map; |
| 892 | |
| 893 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 894 | |
| 895 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 896 | } |
| 897 | |
| 898 | static void disable_advertising(struct hci_request *req) |
| 899 | { |
| 900 | u8 enable = 0x00; |
| 901 | |
| 902 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 903 | } |
| 904 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 905 | static void service_cache_off(struct work_struct *work) |
| 906 | { |
| 907 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 908 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 909 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 910 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 911 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 912 | return; |
| 913 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 914 | hci_req_init(&req, hdev); |
| 915 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 916 | hci_dev_lock(hdev); |
| 917 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 918 | update_eir(&req); |
| 919 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 920 | |
| 921 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 922 | |
| 923 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 924 | } |
| 925 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 926 | static void rpa_expired(struct work_struct *work) |
| 927 | { |
| 928 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 929 | rpa_expired.work); |
| 930 | struct hci_request req; |
| 931 | |
| 932 | BT_DBG(""); |
| 933 | |
| 934 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 935 | |
| 936 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 937 | hci_conn_num(hdev, LE_LINK) > 0) |
| 938 | return; |
| 939 | |
| 940 | /* The generation of a new RPA and programming it into the |
| 941 | * controller happens in the enable_advertising() function. |
| 942 | */ |
| 943 | |
| 944 | hci_req_init(&req, hdev); |
| 945 | |
| 946 | disable_advertising(&req); |
| 947 | enable_advertising(&req); |
| 948 | |
| 949 | hci_req_run(&req, NULL); |
| 950 | } |
| 951 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 952 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 953 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 954 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 955 | return; |
| 956 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 957 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 958 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 959 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 960 | /* Non-mgmt controlled devices get this bit set |
| 961 | * implicitly so that pairing works for them, however |
| 962 | * for mgmt we require user-space to explicitly enable |
| 963 | * it |
| 964 | */ |
| 965 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 966 | } |
| 967 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 968 | 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] | 969 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 970 | { |
| 971 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 972 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 973 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 974 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 975 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 976 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 977 | memset(&rp, 0, sizeof(rp)); |
| 978 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 979 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 980 | |
| 981 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 982 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 983 | |
| 984 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 985 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 986 | |
| 987 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 988 | |
| 989 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 990 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 991 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 992 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 993 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 994 | 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] | 995 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 999 | { |
| 1000 | sock_put(cmd->sk); |
| 1001 | kfree(cmd->param); |
| 1002 | kfree(cmd); |
| 1003 | } |
| 1004 | |
| 1005 | 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] | 1006 | struct hci_dev *hdev, void *data, |
| 1007 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1008 | { |
| 1009 | struct pending_cmd *cmd; |
| 1010 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1011 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1012 | if (!cmd) |
| 1013 | return NULL; |
| 1014 | |
| 1015 | cmd->opcode = opcode; |
| 1016 | cmd->index = hdev->id; |
| 1017 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1018 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1019 | if (!cmd->param) { |
| 1020 | kfree(cmd); |
| 1021 | return NULL; |
| 1022 | } |
| 1023 | |
| 1024 | if (data) |
| 1025 | memcpy(cmd->param, data, len); |
| 1026 | |
| 1027 | cmd->sk = sk; |
| 1028 | sock_hold(sk); |
| 1029 | |
| 1030 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1031 | |
| 1032 | return cmd; |
| 1033 | } |
| 1034 | |
| 1035 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1036 | void (*cb)(struct pending_cmd *cmd, |
| 1037 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1038 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1039 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1040 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1041 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1042 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1043 | if (opcode > 0 && cmd->opcode != opcode) |
| 1044 | continue; |
| 1045 | |
| 1046 | cb(cmd, data); |
| 1047 | } |
| 1048 | } |
| 1049 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1050 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1051 | { |
| 1052 | list_del(&cmd->list); |
| 1053 | mgmt_pending_free(cmd); |
| 1054 | } |
| 1055 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1056 | 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] | 1057 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1058 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1059 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1060 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1061 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1062 | } |
| 1063 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1064 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1065 | { |
| 1066 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1067 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1068 | if (hci_conn_count(hdev) == 0) { |
| 1069 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1070 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1071 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1074 | static void hci_stop_discovery(struct hci_request *req) |
| 1075 | { |
| 1076 | struct hci_dev *hdev = req->hdev; |
| 1077 | struct hci_cp_remote_name_req_cancel cp; |
| 1078 | struct inquiry_entry *e; |
| 1079 | |
| 1080 | switch (hdev->discovery.state) { |
| 1081 | case DISCOVERY_FINDING: |
| 1082 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1083 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1084 | } else { |
| 1085 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1086 | hci_req_add_le_scan_disable(req); |
| 1087 | } |
| 1088 | |
| 1089 | break; |
| 1090 | |
| 1091 | case DISCOVERY_RESOLVING: |
| 1092 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1093 | NAME_PENDING); |
| 1094 | if (!e) |
| 1095 | return; |
| 1096 | |
| 1097 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1098 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1099 | &cp); |
| 1100 | |
| 1101 | break; |
| 1102 | |
| 1103 | default: |
| 1104 | /* Passive scanning */ |
| 1105 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1106 | hci_req_add_le_scan_disable(req); |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1111 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1112 | { |
| 1113 | struct hci_request req; |
| 1114 | struct hci_conn *conn; |
| 1115 | |
| 1116 | hci_req_init(&req, hdev); |
| 1117 | |
| 1118 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1119 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1120 | u8 scan = 0x00; |
| 1121 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1122 | } |
| 1123 | |
| 1124 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1125 | disable_advertising(&req); |
| 1126 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1127 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1128 | |
| 1129 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1130 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1131 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1132 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1133 | switch (conn->state) { |
| 1134 | case BT_CONNECTED: |
| 1135 | case BT_CONFIG: |
| 1136 | dc.handle = cpu_to_le16(conn->handle); |
| 1137 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1138 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1139 | break; |
| 1140 | case BT_CONNECT: |
| 1141 | if (conn->type == LE_LINK) |
| 1142 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1143 | 0, NULL); |
| 1144 | else if (conn->type == ACL_LINK) |
| 1145 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1146 | 6, &conn->dst); |
| 1147 | break; |
| 1148 | case BT_CONNECT2: |
| 1149 | bacpy(&rej.bdaddr, &conn->dst); |
| 1150 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1151 | if (conn->type == ACL_LINK) |
| 1152 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1153 | sizeof(rej), &rej); |
| 1154 | else if (conn->type == SCO_LINK) |
| 1155 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1156 | sizeof(rej), &rej); |
| 1157 | break; |
| 1158 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | return hci_req_run(&req, clean_up_hci_complete); |
| 1162 | } |
| 1163 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1164 | 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] | 1165 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1166 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1167 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1168 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1169 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1170 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1171 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1172 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1173 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1174 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1175 | MGMT_STATUS_INVALID_PARAMS); |
| 1176 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1177 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1178 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1179 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1180 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1181 | MGMT_STATUS_BUSY); |
| 1182 | goto failed; |
| 1183 | } |
| 1184 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1185 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1186 | cancel_delayed_work(&hdev->power_off); |
| 1187 | |
| 1188 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1189 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1190 | data, len); |
| 1191 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1192 | goto failed; |
| 1193 | } |
| 1194 | } |
| 1195 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1196 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1197 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1198 | goto failed; |
| 1199 | } |
| 1200 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1201 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1202 | if (!cmd) { |
| 1203 | err = -ENOMEM; |
| 1204 | goto failed; |
| 1205 | } |
| 1206 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1207 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1208 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1209 | err = 0; |
| 1210 | } else { |
| 1211 | /* Disconnect connections, stop scans, etc */ |
| 1212 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1213 | if (!err) |
| 1214 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1215 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1216 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1217 | /* ENODATA means there were no HCI commands queued */ |
| 1218 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1219 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1220 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1221 | err = 0; |
| 1222 | } |
| 1223 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1224 | |
| 1225 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1226 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1227 | return err; |
| 1228 | } |
| 1229 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1230 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1231 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1232 | { |
| 1233 | struct sk_buff *skb; |
| 1234 | struct mgmt_hdr *hdr; |
| 1235 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1236 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1237 | if (!skb) |
| 1238 | return -ENOMEM; |
| 1239 | |
| 1240 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1241 | hdr->opcode = cpu_to_le16(event); |
| 1242 | if (hdev) |
| 1243 | hdr->index = cpu_to_le16(hdev->id); |
| 1244 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1245 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1246 | hdr->len = cpu_to_le16(data_len); |
| 1247 | |
| 1248 | if (data) |
| 1249 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1250 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1251 | /* Time stamp */ |
| 1252 | __net_timestamp(skb); |
| 1253 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1254 | hci_send_to_control(skb, skip_sk); |
| 1255 | kfree_skb(skb); |
| 1256 | |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
| 1260 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1261 | { |
| 1262 | __le32 ev; |
| 1263 | |
| 1264 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1265 | |
| 1266 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1267 | } |
| 1268 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1269 | struct cmd_lookup { |
| 1270 | struct sock *sk; |
| 1271 | struct hci_dev *hdev; |
| 1272 | u8 mgmt_status; |
| 1273 | }; |
| 1274 | |
| 1275 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1276 | { |
| 1277 | struct cmd_lookup *match = data; |
| 1278 | |
| 1279 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1280 | |
| 1281 | list_del(&cmd->list); |
| 1282 | |
| 1283 | if (match->sk == NULL) { |
| 1284 | match->sk = cmd->sk; |
| 1285 | sock_hold(match->sk); |
| 1286 | } |
| 1287 | |
| 1288 | mgmt_pending_free(cmd); |
| 1289 | } |
| 1290 | |
| 1291 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1292 | { |
| 1293 | u8 *status = data; |
| 1294 | |
| 1295 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1296 | mgmt_pending_remove(cmd); |
| 1297 | } |
| 1298 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1299 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1300 | { |
| 1301 | if (!lmp_bredr_capable(hdev)) |
| 1302 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1303 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1304 | return MGMT_STATUS_REJECTED; |
| 1305 | else |
| 1306 | return MGMT_STATUS_SUCCESS; |
| 1307 | } |
| 1308 | |
| 1309 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1310 | { |
| 1311 | if (!lmp_le_capable(hdev)) |
| 1312 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1313 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1314 | return MGMT_STATUS_REJECTED; |
| 1315 | else |
| 1316 | return MGMT_STATUS_SUCCESS; |
| 1317 | } |
| 1318 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1319 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1320 | { |
| 1321 | struct pending_cmd *cmd; |
| 1322 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1323 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1324 | bool changed; |
| 1325 | |
| 1326 | BT_DBG("status 0x%02x", status); |
| 1327 | |
| 1328 | hci_dev_lock(hdev); |
| 1329 | |
| 1330 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1331 | if (!cmd) |
| 1332 | goto unlock; |
| 1333 | |
| 1334 | if (status) { |
| 1335 | u8 mgmt_err = mgmt_status(status); |
| 1336 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1337 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1338 | goto remove_cmd; |
| 1339 | } |
| 1340 | |
| 1341 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1342 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1343 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1344 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1345 | |
| 1346 | if (hdev->discov_timeout > 0) { |
| 1347 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1348 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1349 | to); |
| 1350 | } |
| 1351 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1352 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1353 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1354 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1355 | |
| 1356 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1357 | |
| 1358 | if (changed) |
| 1359 | new_settings(hdev, cmd->sk); |
| 1360 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1361 | /* When the discoverable mode gets changed, make sure |
| 1362 | * that class of device has the limited discoverable |
| 1363 | * bit correctly set. |
| 1364 | */ |
| 1365 | hci_req_init(&req, hdev); |
| 1366 | update_class(&req); |
| 1367 | hci_req_run(&req, NULL); |
| 1368 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1369 | remove_cmd: |
| 1370 | mgmt_pending_remove(cmd); |
| 1371 | |
| 1372 | unlock: |
| 1373 | hci_dev_unlock(hdev); |
| 1374 | } |
| 1375 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1376 | 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] | 1377 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1378 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1379 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1380 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1381 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1382 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1383 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1384 | int err; |
| 1385 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1386 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1387 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1388 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1389 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1390 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1391 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1392 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1393 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1394 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1395 | MGMT_STATUS_INVALID_PARAMS); |
| 1396 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1397 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1398 | |
| 1399 | /* Disabling discoverable requires that no timeout is set, |
| 1400 | * and enabling limited discoverable requires a timeout. |
| 1401 | */ |
| 1402 | if ((cp->val == 0x00 && timeout > 0) || |
| 1403 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1404 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1405 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1406 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1407 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1408 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1409 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1410 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1411 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1412 | goto failed; |
| 1413 | } |
| 1414 | |
| 1415 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1416 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1417 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1418 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1419 | goto failed; |
| 1420 | } |
| 1421 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1422 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1423 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1424 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1425 | goto failed; |
| 1426 | } |
| 1427 | |
| 1428 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1429 | bool changed = false; |
| 1430 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1431 | /* Setting limited discoverable when powered off is |
| 1432 | * not a valid operation since it requires a timeout |
| 1433 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1434 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1435 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1436 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1437 | changed = true; |
| 1438 | } |
| 1439 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1440 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1441 | if (err < 0) |
| 1442 | goto failed; |
| 1443 | |
| 1444 | if (changed) |
| 1445 | err = new_settings(hdev, sk); |
| 1446 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1447 | goto failed; |
| 1448 | } |
| 1449 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1450 | /* If the current mode is the same, then just update the timeout |
| 1451 | * value with the new value. And if only the timeout gets updated, |
| 1452 | * then no need for any HCI transactions. |
| 1453 | */ |
| 1454 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1455 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1456 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1457 | cancel_delayed_work(&hdev->discov_off); |
| 1458 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1459 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1460 | if (cp->val && hdev->discov_timeout > 0) { |
| 1461 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1462 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1463 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1464 | } |
| 1465 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1466 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1467 | goto failed; |
| 1468 | } |
| 1469 | |
| 1470 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1471 | if (!cmd) { |
| 1472 | err = -ENOMEM; |
| 1473 | goto failed; |
| 1474 | } |
| 1475 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1476 | /* Cancel any potential discoverable timeout that might be |
| 1477 | * still active and store new timeout value. The arming of |
| 1478 | * the timeout happens in the complete handler. |
| 1479 | */ |
| 1480 | cancel_delayed_work(&hdev->discov_off); |
| 1481 | hdev->discov_timeout = timeout; |
| 1482 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1483 | /* Limited discoverable mode */ |
| 1484 | if (cp->val == 0x02) |
| 1485 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1486 | else |
| 1487 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1488 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1489 | hci_req_init(&req, hdev); |
| 1490 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1491 | /* The procedure for LE-only controllers is much simpler - just |
| 1492 | * update the advertising data. |
| 1493 | */ |
| 1494 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1495 | goto update_ad; |
| 1496 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1497 | scan = SCAN_PAGE; |
| 1498 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1499 | if (cp->val) { |
| 1500 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1501 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1502 | if (cp->val == 0x02) { |
| 1503 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1504 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1505 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1506 | hci_cp.iac_lap[1] = 0x8b; |
| 1507 | hci_cp.iac_lap[2] = 0x9e; |
| 1508 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1509 | hci_cp.iac_lap[4] = 0x8b; |
| 1510 | hci_cp.iac_lap[5] = 0x9e; |
| 1511 | } else { |
| 1512 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1513 | hci_cp.num_iac = 1; |
| 1514 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1515 | hci_cp.iac_lap[1] = 0x8b; |
| 1516 | hci_cp.iac_lap[2] = 0x9e; |
| 1517 | } |
| 1518 | |
| 1519 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1520 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1521 | |
| 1522 | scan |= SCAN_INQUIRY; |
| 1523 | } else { |
| 1524 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1525 | } |
| 1526 | |
| 1527 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1528 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1529 | update_ad: |
| 1530 | update_adv_data(&req); |
| 1531 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1532 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1533 | if (err < 0) |
| 1534 | mgmt_pending_remove(cmd); |
| 1535 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1536 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1537 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1538 | return err; |
| 1539 | } |
| 1540 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1541 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1542 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1543 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1544 | struct hci_cp_write_page_scan_activity acp; |
| 1545 | u8 type; |
| 1546 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1547 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1548 | return; |
| 1549 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1550 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1551 | return; |
| 1552 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1553 | if (enable) { |
| 1554 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1555 | |
| 1556 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1557 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1558 | } else { |
| 1559 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1560 | |
| 1561 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1562 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1563 | } |
| 1564 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1565 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1566 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1567 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1568 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1569 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1570 | sizeof(acp), &acp); |
| 1571 | |
| 1572 | if (hdev->page_scan_type != type) |
| 1573 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1574 | } |
| 1575 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1576 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1577 | { |
| 1578 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1579 | struct mgmt_mode *cp; |
| 1580 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1581 | |
| 1582 | BT_DBG("status 0x%02x", status); |
| 1583 | |
| 1584 | hci_dev_lock(hdev); |
| 1585 | |
| 1586 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1587 | if (!cmd) |
| 1588 | goto unlock; |
| 1589 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1590 | if (status) { |
| 1591 | u8 mgmt_err = mgmt_status(status); |
| 1592 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1593 | goto remove_cmd; |
| 1594 | } |
| 1595 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1596 | cp = cmd->param; |
| 1597 | if (cp->val) |
| 1598 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1599 | else |
| 1600 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1601 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1602 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1603 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1604 | if (changed) |
| 1605 | new_settings(hdev, cmd->sk); |
| 1606 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1607 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1608 | mgmt_pending_remove(cmd); |
| 1609 | |
| 1610 | unlock: |
| 1611 | hci_dev_unlock(hdev); |
| 1612 | } |
| 1613 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1614 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1615 | struct sock *sk, u8 val) |
| 1616 | { |
| 1617 | bool changed = false; |
| 1618 | int err; |
| 1619 | |
| 1620 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1621 | changed = true; |
| 1622 | |
| 1623 | if (val) { |
| 1624 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1625 | } else { |
| 1626 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1627 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1628 | } |
| 1629 | |
| 1630 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1631 | if (err < 0) |
| 1632 | return err; |
| 1633 | |
| 1634 | if (changed) |
| 1635 | return new_settings(hdev, sk); |
| 1636 | |
| 1637 | return 0; |
| 1638 | } |
| 1639 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1640 | 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] | 1641 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1642 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1643 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1644 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1645 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1646 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1647 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1648 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1649 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1650 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1651 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1652 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1653 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1654 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1655 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1656 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1657 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1658 | MGMT_STATUS_INVALID_PARAMS); |
| 1659 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1660 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1661 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1662 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1663 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1664 | goto failed; |
| 1665 | } |
| 1666 | |
| 1667 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1668 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1669 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1670 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1671 | goto failed; |
| 1672 | } |
| 1673 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1674 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1675 | if (!cmd) { |
| 1676 | err = -ENOMEM; |
| 1677 | goto failed; |
| 1678 | } |
| 1679 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1680 | hci_req_init(&req, hdev); |
| 1681 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1682 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1683 | * by-product of disabling connectable, we need to update the |
| 1684 | * advertising flags. |
| 1685 | */ |
| 1686 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1687 | if (!cp->val) { |
| 1688 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1689 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1690 | } |
| 1691 | update_adv_data(&req); |
| 1692 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1693 | if (cp->val) { |
| 1694 | scan = SCAN_PAGE; |
| 1695 | } else { |
| 1696 | scan = 0; |
| 1697 | |
| 1698 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1699 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1700 | cancel_delayed_work(&hdev->discov_off); |
| 1701 | } |
| 1702 | |
| 1703 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1704 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1705 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1706 | /* If we're going from non-connectable to connectable or |
| 1707 | * vice-versa when fast connectable is enabled ensure that fast |
| 1708 | * connectable gets disabled. write_fast_connectable won't do |
| 1709 | * anything if the page scan parameters are already what they |
| 1710 | * should be. |
| 1711 | */ |
| 1712 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1713 | write_fast_connectable(&req, false); |
| 1714 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1715 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1716 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1717 | disable_advertising(&req); |
| 1718 | enable_advertising(&req); |
| 1719 | } |
| 1720 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1721 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1722 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1723 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1724 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1725 | err = set_connectable_update_settings(hdev, sk, |
| 1726 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1727 | goto failed; |
| 1728 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1729 | |
| 1730 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1731 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1732 | return err; |
| 1733 | } |
| 1734 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1735 | 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] | 1736 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1737 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1738 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1739 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1740 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1741 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1742 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1743 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1744 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1745 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1746 | MGMT_STATUS_INVALID_PARAMS); |
| 1747 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1748 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1749 | |
| 1750 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1751 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1752 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1753 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1754 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1755 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1756 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1757 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1758 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1759 | if (changed) |
| 1760 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1761 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1762 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1763 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1764 | return err; |
| 1765 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1766 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1767 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1768 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1769 | { |
| 1770 | struct mgmt_mode *cp = data; |
| 1771 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1772 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1773 | int err; |
| 1774 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1775 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1776 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1777 | status = mgmt_bredr_support(hdev); |
| 1778 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1779 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1780 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1781 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1782 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1783 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1784 | MGMT_STATUS_INVALID_PARAMS); |
| 1785 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1786 | hci_dev_lock(hdev); |
| 1787 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1788 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1789 | bool changed = false; |
| 1790 | |
| 1791 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1792 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1793 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1794 | changed = true; |
| 1795 | } |
| 1796 | |
| 1797 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1798 | if (err < 0) |
| 1799 | goto failed; |
| 1800 | |
| 1801 | if (changed) |
| 1802 | err = new_settings(hdev, sk); |
| 1803 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1804 | goto failed; |
| 1805 | } |
| 1806 | |
| 1807 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1808 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1809 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1810 | goto failed; |
| 1811 | } |
| 1812 | |
| 1813 | val = !!cp->val; |
| 1814 | |
| 1815 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1816 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1817 | goto failed; |
| 1818 | } |
| 1819 | |
| 1820 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1821 | if (!cmd) { |
| 1822 | err = -ENOMEM; |
| 1823 | goto failed; |
| 1824 | } |
| 1825 | |
| 1826 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1827 | if (err < 0) { |
| 1828 | mgmt_pending_remove(cmd); |
| 1829 | goto failed; |
| 1830 | } |
| 1831 | |
| 1832 | failed: |
| 1833 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1834 | return err; |
| 1835 | } |
| 1836 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1837 | 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] | 1838 | { |
| 1839 | struct mgmt_mode *cp = data; |
| 1840 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1841 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1842 | int err; |
| 1843 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1844 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1845 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1846 | status = mgmt_bredr_support(hdev); |
| 1847 | if (status) |
| 1848 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1849 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1850 | if (!lmp_ssp_capable(hdev)) |
| 1851 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1852 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1853 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1854 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1855 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1856 | MGMT_STATUS_INVALID_PARAMS); |
| 1857 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1858 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1859 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1860 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1861 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1862 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1863 | if (cp->val) { |
| 1864 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1865 | &hdev->dev_flags); |
| 1866 | } else { |
| 1867 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1868 | &hdev->dev_flags); |
| 1869 | if (!changed) |
| 1870 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1871 | &hdev->dev_flags); |
| 1872 | else |
| 1873 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1877 | if (err < 0) |
| 1878 | goto failed; |
| 1879 | |
| 1880 | if (changed) |
| 1881 | err = new_settings(hdev, sk); |
| 1882 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1883 | goto failed; |
| 1884 | } |
| 1885 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1886 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1887 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1888 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1889 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1890 | goto failed; |
| 1891 | } |
| 1892 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1893 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1894 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1895 | goto failed; |
| 1896 | } |
| 1897 | |
| 1898 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1899 | if (!cmd) { |
| 1900 | err = -ENOMEM; |
| 1901 | goto failed; |
| 1902 | } |
| 1903 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1904 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 1905 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1906 | sizeof(cp->val), &cp->val); |
| 1907 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1908 | 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] | 1909 | if (err < 0) { |
| 1910 | mgmt_pending_remove(cmd); |
| 1911 | goto failed; |
| 1912 | } |
| 1913 | |
| 1914 | failed: |
| 1915 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1916 | return err; |
| 1917 | } |
| 1918 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1919 | 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] | 1920 | { |
| 1921 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1922 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1923 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1924 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1925 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1926 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1927 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1928 | status = mgmt_bredr_support(hdev); |
| 1929 | if (status) |
| 1930 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1931 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1932 | if (!lmp_ssp_capable(hdev)) |
| 1933 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1934 | MGMT_STATUS_NOT_SUPPORTED); |
| 1935 | |
| 1936 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1937 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1938 | MGMT_STATUS_REJECTED); |
| 1939 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1940 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1941 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1942 | MGMT_STATUS_INVALID_PARAMS); |
| 1943 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1944 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1945 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1946 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1947 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1948 | } else { |
| 1949 | if (hdev_is_powered(hdev)) { |
| 1950 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1951 | MGMT_STATUS_REJECTED); |
| 1952 | goto unlock; |
| 1953 | } |
| 1954 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1955 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1956 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1957 | |
| 1958 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1959 | if (err < 0) |
| 1960 | goto unlock; |
| 1961 | |
| 1962 | if (changed) |
| 1963 | err = new_settings(hdev, sk); |
| 1964 | |
| 1965 | unlock: |
| 1966 | hci_dev_unlock(hdev); |
| 1967 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1968 | } |
| 1969 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1970 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1971 | { |
| 1972 | struct cmd_lookup match = { NULL, hdev }; |
| 1973 | |
| 1974 | if (status) { |
| 1975 | u8 mgmt_err = mgmt_status(status); |
| 1976 | |
| 1977 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1978 | &mgmt_err); |
| 1979 | return; |
| 1980 | } |
| 1981 | |
| 1982 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1983 | |
| 1984 | new_settings(hdev, match.sk); |
| 1985 | |
| 1986 | if (match.sk) |
| 1987 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1988 | |
| 1989 | /* Make sure the controller has a good default for |
| 1990 | * advertising data. Restrict the update to when LE |
| 1991 | * has actually been enabled. During power on, the |
| 1992 | * update in powered_update_hci will take care of it. |
| 1993 | */ |
| 1994 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1995 | struct hci_request req; |
| 1996 | |
| 1997 | hci_dev_lock(hdev); |
| 1998 | |
| 1999 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 2000 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 2001 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2002 | hci_req_run(&req, NULL); |
| 2003 | |
| 2004 | hci_dev_unlock(hdev); |
| 2005 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2006 | } |
| 2007 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2008 | 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] | 2009 | { |
| 2010 | struct mgmt_mode *cp = data; |
| 2011 | struct hci_cp_write_le_host_supported hci_cp; |
| 2012 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2013 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2014 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2015 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2016 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2017 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2018 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2019 | if (!lmp_le_capable(hdev)) |
| 2020 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2021 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2022 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2023 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2024 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2025 | MGMT_STATUS_INVALID_PARAMS); |
| 2026 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2027 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2028 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2029 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2030 | MGMT_STATUS_REJECTED); |
| 2031 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2032 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2033 | |
| 2034 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2035 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2036 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2037 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2038 | bool changed = false; |
| 2039 | |
| 2040 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2041 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2042 | changed = true; |
| 2043 | } |
| 2044 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2045 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2046 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2047 | changed = true; |
| 2048 | } |
| 2049 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2050 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2051 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2052 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2053 | |
| 2054 | if (changed) |
| 2055 | err = new_settings(hdev, sk); |
| 2056 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2057 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2058 | } |
| 2059 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2060 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2061 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2062 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2063 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2064 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2068 | if (!cmd) { |
| 2069 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2070 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2071 | } |
| 2072 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2073 | hci_req_init(&req, hdev); |
| 2074 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2075 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2076 | |
| 2077 | if (val) { |
| 2078 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2079 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2080 | } else { |
| 2081 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2082 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2083 | } |
| 2084 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2085 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2086 | &hci_cp); |
| 2087 | |
| 2088 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2089 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2090 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2091 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2092 | unlock: |
| 2093 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2094 | return err; |
| 2095 | } |
| 2096 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2097 | /* This is a helper function to test for pending mgmt commands that can |
| 2098 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2099 | * mgmt command at a time since otherwise we cannot easily track what |
| 2100 | * the current values are, will be, and based on that calculate if a new |
| 2101 | * HCI command needs to be sent and if yes with what value. |
| 2102 | */ |
| 2103 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2104 | { |
| 2105 | struct pending_cmd *cmd; |
| 2106 | |
| 2107 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2108 | switch (cmd->opcode) { |
| 2109 | case MGMT_OP_ADD_UUID: |
| 2110 | case MGMT_OP_REMOVE_UUID: |
| 2111 | case MGMT_OP_SET_DEV_CLASS: |
| 2112 | case MGMT_OP_SET_POWERED: |
| 2113 | return true; |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | return false; |
| 2118 | } |
| 2119 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2120 | static const u8 bluetooth_base_uuid[] = { |
| 2121 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2122 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2123 | }; |
| 2124 | |
| 2125 | static u8 get_uuid_size(const u8 *uuid) |
| 2126 | { |
| 2127 | u32 val; |
| 2128 | |
| 2129 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2130 | return 128; |
| 2131 | |
| 2132 | val = get_unaligned_le32(&uuid[12]); |
| 2133 | if (val > 0xffff) |
| 2134 | return 32; |
| 2135 | |
| 2136 | return 16; |
| 2137 | } |
| 2138 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2139 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2140 | { |
| 2141 | struct pending_cmd *cmd; |
| 2142 | |
| 2143 | hci_dev_lock(hdev); |
| 2144 | |
| 2145 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2146 | if (!cmd) |
| 2147 | goto unlock; |
| 2148 | |
| 2149 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2150 | hdev->dev_class, 3); |
| 2151 | |
| 2152 | mgmt_pending_remove(cmd); |
| 2153 | |
| 2154 | unlock: |
| 2155 | hci_dev_unlock(hdev); |
| 2156 | } |
| 2157 | |
| 2158 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2159 | { |
| 2160 | BT_DBG("status 0x%02x", status); |
| 2161 | |
| 2162 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2163 | } |
| 2164 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2165 | 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] | 2166 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2167 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2168 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2169 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2170 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2171 | int err; |
| 2172 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2173 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2174 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2175 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2176 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2177 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2178 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2179 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2180 | goto failed; |
| 2181 | } |
| 2182 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2183 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2184 | if (!uuid) { |
| 2185 | err = -ENOMEM; |
| 2186 | goto failed; |
| 2187 | } |
| 2188 | |
| 2189 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2190 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2191 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2192 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2193 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2194 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2195 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2196 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2197 | update_class(&req); |
| 2198 | update_eir(&req); |
| 2199 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2200 | err = hci_req_run(&req, add_uuid_complete); |
| 2201 | if (err < 0) { |
| 2202 | if (err != -ENODATA) |
| 2203 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2204 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2205 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2206 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2207 | goto failed; |
| 2208 | } |
| 2209 | |
| 2210 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2211 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2212 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2213 | goto failed; |
| 2214 | } |
| 2215 | |
| 2216 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2217 | |
| 2218 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2219 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2220 | return err; |
| 2221 | } |
| 2222 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2223 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2224 | { |
| 2225 | if (!hdev_is_powered(hdev)) |
| 2226 | return false; |
| 2227 | |
| 2228 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2229 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2230 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2231 | return true; |
| 2232 | } |
| 2233 | |
| 2234 | return false; |
| 2235 | } |
| 2236 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2237 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2238 | { |
| 2239 | BT_DBG("status 0x%02x", status); |
| 2240 | |
| 2241 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2242 | } |
| 2243 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2244 | 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] | 2245 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2246 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2247 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2248 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2249 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2250 | 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] | 2251 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2252 | int err, found; |
| 2253 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2254 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2255 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2256 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2257 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2258 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2259 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2260 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2261 | goto unlock; |
| 2262 | } |
| 2263 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2264 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2265 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2266 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2267 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2268 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2269 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2270 | goto unlock; |
| 2271 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2272 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2273 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | found = 0; |
| 2277 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2278 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2279 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2280 | continue; |
| 2281 | |
| 2282 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2283 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2284 | found++; |
| 2285 | } |
| 2286 | |
| 2287 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2288 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2289 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2290 | goto unlock; |
| 2291 | } |
| 2292 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2293 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2294 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2295 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2296 | update_class(&req); |
| 2297 | update_eir(&req); |
| 2298 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2299 | err = hci_req_run(&req, remove_uuid_complete); |
| 2300 | if (err < 0) { |
| 2301 | if (err != -ENODATA) |
| 2302 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2303 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2304 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2305 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2306 | goto unlock; |
| 2307 | } |
| 2308 | |
| 2309 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2310 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2311 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2312 | goto unlock; |
| 2313 | } |
| 2314 | |
| 2315 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2316 | |
| 2317 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2318 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2319 | return err; |
| 2320 | } |
| 2321 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2322 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2323 | { |
| 2324 | BT_DBG("status 0x%02x", status); |
| 2325 | |
| 2326 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2327 | } |
| 2328 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2329 | 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] | 2330 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2331 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2332 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2333 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2334 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2335 | int err; |
| 2336 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2337 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2338 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2339 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2340 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2341 | MGMT_STATUS_NOT_SUPPORTED); |
| 2342 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2343 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2344 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2345 | if (pending_eir_or_class(hdev)) { |
| 2346 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2347 | MGMT_STATUS_BUSY); |
| 2348 | goto unlock; |
| 2349 | } |
| 2350 | |
| 2351 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2352 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2353 | MGMT_STATUS_INVALID_PARAMS); |
| 2354 | goto unlock; |
| 2355 | } |
| 2356 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2357 | hdev->major_class = cp->major; |
| 2358 | hdev->minor_class = cp->minor; |
| 2359 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2360 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2361 | 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] | 2362 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2363 | goto unlock; |
| 2364 | } |
| 2365 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2366 | hci_req_init(&req, hdev); |
| 2367 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2368 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2369 | hci_dev_unlock(hdev); |
| 2370 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2371 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2372 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2373 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2374 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2375 | update_class(&req); |
| 2376 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2377 | err = hci_req_run(&req, set_class_complete); |
| 2378 | if (err < 0) { |
| 2379 | if (err != -ENODATA) |
| 2380 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2381 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2382 | 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] | 2383 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2384 | goto unlock; |
| 2385 | } |
| 2386 | |
| 2387 | 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] | 2388 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2389 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2390 | goto unlock; |
| 2391 | } |
| 2392 | |
| 2393 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2394 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2395 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2396 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2397 | return err; |
| 2398 | } |
| 2399 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2400 | 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] | 2401 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2402 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2403 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2404 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2405 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2406 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2407 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2408 | BT_DBG("request for %s", hdev->name); |
| 2409 | |
| 2410 | if (!lmp_bredr_capable(hdev)) |
| 2411 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2412 | MGMT_STATUS_NOT_SUPPORTED); |
| 2413 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2414 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2415 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2416 | expected_len = sizeof(*cp) + key_count * |
| 2417 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2418 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2419 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2420 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2421 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2422 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2423 | } |
| 2424 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2425 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2426 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2427 | MGMT_STATUS_INVALID_PARAMS); |
| 2428 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2429 | 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] | 2430 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2431 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2432 | for (i = 0; i < key_count; i++) { |
| 2433 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2434 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2435 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2436 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2437 | MGMT_STATUS_INVALID_PARAMS); |
| 2438 | } |
| 2439 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2440 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2441 | |
| 2442 | hci_link_keys_clear(hdev); |
| 2443 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2444 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2445 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2446 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2447 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2448 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2449 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2450 | |
| 2451 | if (changed) |
| 2452 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2453 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2454 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2455 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2456 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2457 | /* Always ignore debug keys and require a new pairing if |
| 2458 | * the user wants to use them. |
| 2459 | */ |
| 2460 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2461 | continue; |
| 2462 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2463 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2464 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2465 | } |
| 2466 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2467 | 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] | 2468 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2469 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2470 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2471 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2472 | } |
| 2473 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2474 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2475 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2476 | { |
| 2477 | struct mgmt_ev_device_unpaired ev; |
| 2478 | |
| 2479 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2480 | ev.addr.type = addr_type; |
| 2481 | |
| 2482 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2483 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2484 | } |
| 2485 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2486 | 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] | 2487 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2488 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2489 | struct mgmt_cp_unpair_device *cp = data; |
| 2490 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2491 | struct hci_cp_disconnect dc; |
| 2492 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2493 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2494 | int err; |
| 2495 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2496 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2497 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2498 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2499 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2500 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2501 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2502 | MGMT_STATUS_INVALID_PARAMS, |
| 2503 | &rp, sizeof(rp)); |
| 2504 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2505 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2506 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2507 | MGMT_STATUS_INVALID_PARAMS, |
| 2508 | &rp, sizeof(rp)); |
| 2509 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2510 | hci_dev_lock(hdev); |
| 2511 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2512 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2513 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2514 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2515 | goto unlock; |
| 2516 | } |
| 2517 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2518 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2519 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2520 | } else { |
| 2521 | u8 addr_type; |
| 2522 | |
| 2523 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2524 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2525 | else |
| 2526 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2527 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2528 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2529 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2530 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2531 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2532 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2533 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2534 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2535 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2536 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2537 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2538 | goto unlock; |
| 2539 | } |
| 2540 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2541 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2542 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2543 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2544 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2545 | else |
| 2546 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2547 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2548 | } else { |
| 2549 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2550 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2551 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2552 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2553 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2554 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2555 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2556 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2557 | } |
| 2558 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2559 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2560 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2561 | if (!cmd) { |
| 2562 | err = -ENOMEM; |
| 2563 | goto unlock; |
| 2564 | } |
| 2565 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2566 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2567 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2568 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2569 | if (err < 0) |
| 2570 | mgmt_pending_remove(cmd); |
| 2571 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2572 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2573 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2574 | return err; |
| 2575 | } |
| 2576 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2577 | 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] | 2578 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2579 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2580 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2581 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2582 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2583 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2584 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2585 | int err; |
| 2586 | |
| 2587 | BT_DBG(""); |
| 2588 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2589 | memset(&rp, 0, sizeof(rp)); |
| 2590 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2591 | rp.addr.type = cp->addr.type; |
| 2592 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2593 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2594 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2595 | MGMT_STATUS_INVALID_PARAMS, |
| 2596 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2597 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2598 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2599 | |
| 2600 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2601 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2602 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2603 | goto failed; |
| 2604 | } |
| 2605 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2606 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2607 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2608 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2609 | goto failed; |
| 2610 | } |
| 2611 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2612 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2613 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2614 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2615 | else |
| 2616 | 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] | 2617 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2618 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2619 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2620 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2621 | goto failed; |
| 2622 | } |
| 2623 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2624 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2625 | if (!cmd) { |
| 2626 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2627 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2628 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2629 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2630 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2631 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2632 | |
| 2633 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2634 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2635 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2636 | |
| 2637 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2638 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2639 | return err; |
| 2640 | } |
| 2641 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2642 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2643 | { |
| 2644 | switch (link_type) { |
| 2645 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2646 | switch (addr_type) { |
| 2647 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2648 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2649 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2650 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2651 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2652 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2653 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2654 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2655 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2656 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2657 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2658 | } |
| 2659 | } |
| 2660 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2661 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2662 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2663 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2664 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2665 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2666 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2667 | int err; |
| 2668 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2669 | |
| 2670 | BT_DBG(""); |
| 2671 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2672 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2673 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2674 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2675 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2676 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2677 | goto unlock; |
| 2678 | } |
| 2679 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2680 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2681 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2682 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2683 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2684 | } |
| 2685 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2686 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2687 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2688 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2689 | err = -ENOMEM; |
| 2690 | goto unlock; |
| 2691 | } |
| 2692 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2693 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2694 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2695 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2696 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2697 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2698 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2699 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2700 | continue; |
| 2701 | i++; |
| 2702 | } |
| 2703 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2704 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2705 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2706 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2707 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2708 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2709 | 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] | 2710 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2711 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2712 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2713 | |
| 2714 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2715 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2716 | return err; |
| 2717 | } |
| 2718 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2719 | 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] | 2720 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2721 | { |
| 2722 | struct pending_cmd *cmd; |
| 2723 | int err; |
| 2724 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2725 | 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] | 2726 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2727 | if (!cmd) |
| 2728 | return -ENOMEM; |
| 2729 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2730 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2731 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2732 | if (err < 0) |
| 2733 | mgmt_pending_remove(cmd); |
| 2734 | |
| 2735 | return err; |
| 2736 | } |
| 2737 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2738 | 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] | 2739 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2740 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2741 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2742 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2743 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2744 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2745 | int err; |
| 2746 | |
| 2747 | BT_DBG(""); |
| 2748 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2749 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2750 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2751 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2752 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2753 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2754 | goto failed; |
| 2755 | } |
| 2756 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2757 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2758 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2759 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2760 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2761 | goto failed; |
| 2762 | } |
| 2763 | |
| 2764 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2765 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2766 | |
| 2767 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2768 | |
| 2769 | BT_ERR("PIN code is not 16 bytes long"); |
| 2770 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2771 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2772 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2773 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2774 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2775 | |
| 2776 | goto failed; |
| 2777 | } |
| 2778 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2779 | 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] | 2780 | if (!cmd) { |
| 2781 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2782 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2783 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2784 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2785 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2786 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2787 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2788 | |
| 2789 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2790 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2791 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2792 | |
| 2793 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2794 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2795 | return err; |
| 2796 | } |
| 2797 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2798 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2799 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2800 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2801 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2802 | |
| 2803 | BT_DBG(""); |
| 2804 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2805 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2806 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2807 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2808 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2809 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2810 | |
| 2811 | hdev->io_capability = cp->io_capability; |
| 2812 | |
| 2813 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2814 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2815 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2816 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2817 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2818 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2819 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2820 | } |
| 2821 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2822 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2823 | { |
| 2824 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2825 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2826 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2827 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2828 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2829 | continue; |
| 2830 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2831 | if (cmd->user_data != conn) |
| 2832 | continue; |
| 2833 | |
| 2834 | return cmd; |
| 2835 | } |
| 2836 | |
| 2837 | return NULL; |
| 2838 | } |
| 2839 | |
| 2840 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2841 | { |
| 2842 | struct mgmt_rp_pair_device rp; |
| 2843 | struct hci_conn *conn = cmd->user_data; |
| 2844 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2845 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2846 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2847 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2848 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2849 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2850 | |
| 2851 | /* So we don't get further callbacks for this connection */ |
| 2852 | conn->connect_cfm_cb = NULL; |
| 2853 | conn->security_cfm_cb = NULL; |
| 2854 | conn->disconn_cfm_cb = NULL; |
| 2855 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2856 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2857 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2858 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2859 | } |
| 2860 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2861 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2862 | { |
| 2863 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2864 | struct pending_cmd *cmd; |
| 2865 | |
| 2866 | cmd = find_pairing(conn); |
| 2867 | if (cmd) |
| 2868 | pairing_complete(cmd, status); |
| 2869 | } |
| 2870 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2871 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2872 | { |
| 2873 | struct pending_cmd *cmd; |
| 2874 | |
| 2875 | BT_DBG("status %u", status); |
| 2876 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2877 | cmd = find_pairing(conn); |
| 2878 | if (!cmd) |
| 2879 | BT_DBG("Unable to find a pending command"); |
| 2880 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2881 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2882 | } |
| 2883 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2884 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2885 | { |
| 2886 | struct pending_cmd *cmd; |
| 2887 | |
| 2888 | BT_DBG("status %u", status); |
| 2889 | |
| 2890 | if (!status) |
| 2891 | return; |
| 2892 | |
| 2893 | cmd = find_pairing(conn); |
| 2894 | if (!cmd) |
| 2895 | BT_DBG("Unable to find a pending command"); |
| 2896 | else |
| 2897 | pairing_complete(cmd, mgmt_status(status)); |
| 2898 | } |
| 2899 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2900 | 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] | 2901 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2902 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2903 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2904 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2905 | struct pending_cmd *cmd; |
| 2906 | u8 sec_level, auth_type; |
| 2907 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2908 | int err; |
| 2909 | |
| 2910 | BT_DBG(""); |
| 2911 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2912 | memset(&rp, 0, sizeof(rp)); |
| 2913 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2914 | rp.addr.type = cp->addr.type; |
| 2915 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2916 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2917 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2918 | MGMT_STATUS_INVALID_PARAMS, |
| 2919 | &rp, sizeof(rp)); |
| 2920 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2921 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 2922 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2923 | MGMT_STATUS_INVALID_PARAMS, |
| 2924 | &rp, sizeof(rp)); |
| 2925 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2926 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2927 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2928 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2929 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2930 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2931 | goto unlock; |
| 2932 | } |
| 2933 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2934 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2935 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2936 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2937 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2938 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2939 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2940 | } else { |
| 2941 | u8 addr_type; |
| 2942 | |
| 2943 | /* Convert from L2CAP channel address type to HCI address type |
| 2944 | */ |
| 2945 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2946 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2947 | else |
| 2948 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2949 | |
| 2950 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2951 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2952 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2953 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2954 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2955 | int status; |
| 2956 | |
| 2957 | if (PTR_ERR(conn) == -EBUSY) |
| 2958 | status = MGMT_STATUS_BUSY; |
| 2959 | else |
| 2960 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2961 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2962 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2963 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2964 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2965 | goto unlock; |
| 2966 | } |
| 2967 | |
| 2968 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2969 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2970 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2971 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2972 | goto unlock; |
| 2973 | } |
| 2974 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2975 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2976 | if (!cmd) { |
| 2977 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2978 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2979 | goto unlock; |
| 2980 | } |
| 2981 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2982 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2983 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2984 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2985 | conn->security_cfm_cb = pairing_complete_cb; |
| 2986 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2987 | } else { |
| 2988 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2989 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2990 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2991 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2992 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2993 | conn->io_capability = cp->io_cap; |
| 2994 | cmd->user_data = conn; |
| 2995 | |
| 2996 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2997 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2998 | pairing_complete(cmd, 0); |
| 2999 | |
| 3000 | err = 0; |
| 3001 | |
| 3002 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3003 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3004 | return err; |
| 3005 | } |
| 3006 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3007 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3008 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3009 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3010 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3011 | struct pending_cmd *cmd; |
| 3012 | struct hci_conn *conn; |
| 3013 | int err; |
| 3014 | |
| 3015 | BT_DBG(""); |
| 3016 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3017 | hci_dev_lock(hdev); |
| 3018 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3019 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3020 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3021 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3022 | goto unlock; |
| 3023 | } |
| 3024 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3025 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3026 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3027 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3028 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3029 | goto unlock; |
| 3030 | } |
| 3031 | |
| 3032 | conn = cmd->user_data; |
| 3033 | |
| 3034 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3035 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3036 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3037 | goto unlock; |
| 3038 | } |
| 3039 | |
| 3040 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3041 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3042 | 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] | 3043 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3044 | unlock: |
| 3045 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3046 | return err; |
| 3047 | } |
| 3048 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3049 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3050 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3051 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3052 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3053 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3054 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3055 | int err; |
| 3056 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3057 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3058 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3059 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3060 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3061 | MGMT_STATUS_NOT_POWERED, addr, |
| 3062 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3063 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3064 | } |
| 3065 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3066 | if (addr->type == BDADDR_BREDR) |
| 3067 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3068 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3069 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3070 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3071 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3072 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3073 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3074 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3075 | goto done; |
| 3076 | } |
| 3077 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3078 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3079 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3080 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3081 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3082 | MGMT_STATUS_SUCCESS, addr, |
| 3083 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3084 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3085 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3086 | MGMT_STATUS_FAILED, addr, |
| 3087 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3088 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3089 | goto done; |
| 3090 | } |
| 3091 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3092 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3093 | if (!cmd) { |
| 3094 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3095 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3096 | } |
| 3097 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3098 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3099 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3100 | struct hci_cp_user_passkey_reply cp; |
| 3101 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3102 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3103 | cp.passkey = passkey; |
| 3104 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3105 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3106 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3107 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3108 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3109 | if (err < 0) |
| 3110 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3111 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3112 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3113 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3114 | return err; |
| 3115 | } |
| 3116 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3117 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3118 | void *data, u16 len) |
| 3119 | { |
| 3120 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3121 | |
| 3122 | BT_DBG(""); |
| 3123 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3124 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3125 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3126 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3127 | } |
| 3128 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3129 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3130 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3131 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3132 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3133 | |
| 3134 | BT_DBG(""); |
| 3135 | |
| 3136 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3137 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3138 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3139 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3140 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3141 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3142 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3143 | } |
| 3144 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3145 | 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] | 3146 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3147 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3148 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3149 | |
| 3150 | BT_DBG(""); |
| 3151 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3152 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3153 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3154 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3155 | } |
| 3156 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3157 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3158 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3159 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3160 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3161 | |
| 3162 | BT_DBG(""); |
| 3163 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3164 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3165 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3166 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3167 | } |
| 3168 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3169 | 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] | 3170 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3171 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3172 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3173 | |
| 3174 | BT_DBG(""); |
| 3175 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3176 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3177 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3178 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3179 | } |
| 3180 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3181 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3182 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3183 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3184 | struct hci_cp_write_local_name cp; |
| 3185 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3186 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3187 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3188 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3189 | } |
| 3190 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3191 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3192 | { |
| 3193 | struct mgmt_cp_set_local_name *cp; |
| 3194 | struct pending_cmd *cmd; |
| 3195 | |
| 3196 | BT_DBG("status 0x%02x", status); |
| 3197 | |
| 3198 | hci_dev_lock(hdev); |
| 3199 | |
| 3200 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3201 | if (!cmd) |
| 3202 | goto unlock; |
| 3203 | |
| 3204 | cp = cmd->param; |
| 3205 | |
| 3206 | if (status) |
| 3207 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3208 | mgmt_status(status)); |
| 3209 | else |
| 3210 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3211 | cp, sizeof(*cp)); |
| 3212 | |
| 3213 | mgmt_pending_remove(cmd); |
| 3214 | |
| 3215 | unlock: |
| 3216 | hci_dev_unlock(hdev); |
| 3217 | } |
| 3218 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3219 | 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] | 3220 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3221 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3222 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3223 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3224 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3225 | int err; |
| 3226 | |
| 3227 | BT_DBG(""); |
| 3228 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3229 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3230 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3231 | /* If the old values are the same as the new ones just return a |
| 3232 | * direct command complete event. |
| 3233 | */ |
| 3234 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3235 | !memcmp(hdev->short_name, cp->short_name, |
| 3236 | sizeof(hdev->short_name))) { |
| 3237 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3238 | data, len); |
| 3239 | goto failed; |
| 3240 | } |
| 3241 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3242 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3243 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3244 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3245 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3246 | |
| 3247 | 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] | 3248 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3249 | if (err < 0) |
| 3250 | goto failed; |
| 3251 | |
| 3252 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3253 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3254 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3255 | goto failed; |
| 3256 | } |
| 3257 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3258 | 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] | 3259 | if (!cmd) { |
| 3260 | err = -ENOMEM; |
| 3261 | goto failed; |
| 3262 | } |
| 3263 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3264 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3265 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3266 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3267 | |
| 3268 | if (lmp_bredr_capable(hdev)) { |
| 3269 | update_name(&req); |
| 3270 | update_eir(&req); |
| 3271 | } |
| 3272 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3273 | /* The name is stored in the scan response data and so |
| 3274 | * no need to udpate the advertising data here. |
| 3275 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3276 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3277 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3278 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3279 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3280 | if (err < 0) |
| 3281 | mgmt_pending_remove(cmd); |
| 3282 | |
| 3283 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3284 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3285 | return err; |
| 3286 | } |
| 3287 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3288 | 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] | 3289 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3290 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3291 | struct pending_cmd *cmd; |
| 3292 | int err; |
| 3293 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3294 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3295 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3296 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3297 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3298 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3299 | 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] | 3300 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3301 | goto unlock; |
| 3302 | } |
| 3303 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3304 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3305 | 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] | 3306 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3307 | goto unlock; |
| 3308 | } |
| 3309 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3310 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3311 | 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] | 3312 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3313 | goto unlock; |
| 3314 | } |
| 3315 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3316 | 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] | 3317 | if (!cmd) { |
| 3318 | err = -ENOMEM; |
| 3319 | goto unlock; |
| 3320 | } |
| 3321 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3322 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3323 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3324 | 0, NULL); |
| 3325 | else |
| 3326 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3327 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3328 | if (err < 0) |
| 3329 | mgmt_pending_remove(cmd); |
| 3330 | |
| 3331 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3332 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3333 | return err; |
| 3334 | } |
| 3335 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3336 | 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] | 3337 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3338 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3339 | int err; |
| 3340 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3341 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3342 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3343 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3344 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3345 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3346 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3347 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3348 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3349 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3350 | cp->hash, cp->randomizer); |
| 3351 | if (err < 0) |
| 3352 | status = MGMT_STATUS_FAILED; |
| 3353 | else |
| 3354 | status = MGMT_STATUS_SUCCESS; |
| 3355 | |
| 3356 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3357 | status, &cp->addr, sizeof(cp->addr)); |
| 3358 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3359 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3360 | u8 status; |
| 3361 | |
| 3362 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3363 | cp->hash192, |
| 3364 | cp->randomizer192, |
| 3365 | cp->hash256, |
| 3366 | cp->randomizer256); |
| 3367 | if (err < 0) |
| 3368 | status = MGMT_STATUS_FAILED; |
| 3369 | else |
| 3370 | status = MGMT_STATUS_SUCCESS; |
| 3371 | |
| 3372 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3373 | status, &cp->addr, sizeof(cp->addr)); |
| 3374 | } else { |
| 3375 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3376 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3377 | MGMT_STATUS_INVALID_PARAMS); |
| 3378 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3379 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3380 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3381 | return err; |
| 3382 | } |
| 3383 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3384 | 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] | 3385 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3386 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3387 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3388 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3389 | int err; |
| 3390 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3391 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3392 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3393 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3394 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3395 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3396 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3397 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3398 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3399 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3400 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3401 | 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] | 3402 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3403 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3404 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3405 | return err; |
| 3406 | } |
| 3407 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3408 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3409 | { |
| 3410 | struct pending_cmd *cmd; |
| 3411 | u8 type; |
| 3412 | int err; |
| 3413 | |
| 3414 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3415 | |
| 3416 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3417 | if (!cmd) |
| 3418 | return -ENOENT; |
| 3419 | |
| 3420 | type = hdev->discovery.type; |
| 3421 | |
| 3422 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3423 | &type, sizeof(type)); |
| 3424 | mgmt_pending_remove(cmd); |
| 3425 | |
| 3426 | return err; |
| 3427 | } |
| 3428 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3429 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3430 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3431 | unsigned long timeout = 0; |
| 3432 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3433 | BT_DBG("status %d", status); |
| 3434 | |
| 3435 | if (status) { |
| 3436 | hci_dev_lock(hdev); |
| 3437 | mgmt_start_discovery_failed(hdev, status); |
| 3438 | hci_dev_unlock(hdev); |
| 3439 | return; |
| 3440 | } |
| 3441 | |
| 3442 | hci_dev_lock(hdev); |
| 3443 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3444 | hci_dev_unlock(hdev); |
| 3445 | |
| 3446 | switch (hdev->discovery.type) { |
| 3447 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3448 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3449 | break; |
| 3450 | |
| 3451 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3452 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3453 | break; |
| 3454 | |
| 3455 | case DISCOV_TYPE_BREDR: |
| 3456 | break; |
| 3457 | |
| 3458 | default: |
| 3459 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3460 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3461 | |
| 3462 | if (!timeout) |
| 3463 | return; |
| 3464 | |
| 3465 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3466 | } |
| 3467 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3468 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3469 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3470 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3471 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3472 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3473 | struct hci_cp_le_set_scan_param param_cp; |
| 3474 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3475 | struct hci_cp_inquiry inq_cp; |
| 3476 | struct hci_request req; |
| 3477 | /* General inquiry access code (GIAC) */ |
| 3478 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3479 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3480 | int err; |
| 3481 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3482 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3483 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3484 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3485 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3486 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3487 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3488 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3489 | goto failed; |
| 3490 | } |
| 3491 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3492 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3493 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3494 | MGMT_STATUS_BUSY); |
| 3495 | goto failed; |
| 3496 | } |
| 3497 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3498 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3499 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3500 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3501 | goto failed; |
| 3502 | } |
| 3503 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3504 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3505 | if (!cmd) { |
| 3506 | err = -ENOMEM; |
| 3507 | goto failed; |
| 3508 | } |
| 3509 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3510 | hdev->discovery.type = cp->type; |
| 3511 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3512 | hci_req_init(&req, hdev); |
| 3513 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3514 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3515 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3516 | status = mgmt_bredr_support(hdev); |
| 3517 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3518 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3519 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3520 | mgmt_pending_remove(cmd); |
| 3521 | goto failed; |
| 3522 | } |
| 3523 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3524 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3525 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3526 | MGMT_STATUS_BUSY); |
| 3527 | mgmt_pending_remove(cmd); |
| 3528 | goto failed; |
| 3529 | } |
| 3530 | |
| 3531 | hci_inquiry_cache_flush(hdev); |
| 3532 | |
| 3533 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3534 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3535 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3536 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3537 | break; |
| 3538 | |
| 3539 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3540 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3541 | status = mgmt_le_support(hdev); |
| 3542 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3543 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3544 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3545 | mgmt_pending_remove(cmd); |
| 3546 | goto failed; |
| 3547 | } |
| 3548 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3549 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3550 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3551 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3552 | MGMT_STATUS_NOT_SUPPORTED); |
| 3553 | mgmt_pending_remove(cmd); |
| 3554 | goto failed; |
| 3555 | } |
| 3556 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3557 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3558 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3559 | MGMT_STATUS_REJECTED); |
| 3560 | mgmt_pending_remove(cmd); |
| 3561 | goto failed; |
| 3562 | } |
| 3563 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3564 | /* If controller is scanning, it means the background scanning |
| 3565 | * is running. Thus, we should temporarily stop it in order to |
| 3566 | * set the discovery scanning parameters. |
| 3567 | */ |
| 3568 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3569 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3570 | |
| 3571 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3572 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3573 | /* All active scans will be done with either a resolvable |
| 3574 | * private address (when privacy feature has been enabled) |
| 3575 | * or unresolvable private address. |
| 3576 | */ |
| 3577 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3578 | if (err < 0) { |
| 3579 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3580 | MGMT_STATUS_FAILED); |
| 3581 | mgmt_pending_remove(cmd); |
| 3582 | goto failed; |
| 3583 | } |
| 3584 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3585 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3586 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3587 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3588 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3589 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3590 | ¶m_cp); |
| 3591 | |
| 3592 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3593 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3594 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3595 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3596 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3597 | break; |
| 3598 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3599 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3600 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3601 | MGMT_STATUS_INVALID_PARAMS); |
| 3602 | mgmt_pending_remove(cmd); |
| 3603 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3604 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3605 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3606 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3607 | if (err < 0) |
| 3608 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3609 | else |
| 3610 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3611 | |
| 3612 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3613 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3614 | return err; |
| 3615 | } |
| 3616 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3617 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3618 | { |
| 3619 | struct pending_cmd *cmd; |
| 3620 | int err; |
| 3621 | |
| 3622 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3623 | if (!cmd) |
| 3624 | return -ENOENT; |
| 3625 | |
| 3626 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3627 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3628 | mgmt_pending_remove(cmd); |
| 3629 | |
| 3630 | return err; |
| 3631 | } |
| 3632 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3633 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3634 | { |
| 3635 | BT_DBG("status %d", status); |
| 3636 | |
| 3637 | hci_dev_lock(hdev); |
| 3638 | |
| 3639 | if (status) { |
| 3640 | mgmt_stop_discovery_failed(hdev, status); |
| 3641 | goto unlock; |
| 3642 | } |
| 3643 | |
| 3644 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3645 | |
| 3646 | unlock: |
| 3647 | hci_dev_unlock(hdev); |
| 3648 | } |
| 3649 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3650 | 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] | 3651 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3652 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3653 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3654 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3655 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3656 | int err; |
| 3657 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3658 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3659 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3660 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3661 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3662 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3663 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3664 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3665 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3666 | goto unlock; |
| 3667 | } |
| 3668 | |
| 3669 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3670 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3671 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3672 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3673 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3674 | } |
| 3675 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3676 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3677 | if (!cmd) { |
| 3678 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3679 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3680 | } |
| 3681 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3682 | hci_req_init(&req, hdev); |
| 3683 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3684 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3685 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3686 | err = hci_req_run(&req, stop_discovery_complete); |
| 3687 | if (!err) { |
| 3688 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3689 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3690 | } |
| 3691 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3692 | mgmt_pending_remove(cmd); |
| 3693 | |
| 3694 | /* If no HCI commands were sent we're done */ |
| 3695 | if (err == -ENODATA) { |
| 3696 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3697 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3698 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3699 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3700 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3701 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3702 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3703 | return err; |
| 3704 | } |
| 3705 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3706 | 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] | 3707 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3708 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3709 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3710 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3711 | int err; |
| 3712 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3713 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3714 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3715 | hci_dev_lock(hdev); |
| 3716 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3717 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3718 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3719 | MGMT_STATUS_FAILED, &cp->addr, |
| 3720 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3721 | goto failed; |
| 3722 | } |
| 3723 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3724 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3725 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3726 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3727 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3728 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3729 | goto failed; |
| 3730 | } |
| 3731 | |
| 3732 | if (cp->name_known) { |
| 3733 | e->name_state = NAME_KNOWN; |
| 3734 | list_del(&e->list); |
| 3735 | } else { |
| 3736 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3737 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3738 | } |
| 3739 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3740 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3741 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3742 | |
| 3743 | failed: |
| 3744 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3745 | return err; |
| 3746 | } |
| 3747 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3748 | 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] | 3749 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3750 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3751 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3752 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3753 | int err; |
| 3754 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3755 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3756 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3757 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3758 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3759 | MGMT_STATUS_INVALID_PARAMS, |
| 3760 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3761 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3762 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3763 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3764 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3765 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3766 | status = MGMT_STATUS_FAILED; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3767 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3768 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3769 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3770 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3771 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3772 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3773 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3774 | |
| 3775 | return err; |
| 3776 | } |
| 3777 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3778 | 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] | 3779 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3780 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3781 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3782 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3783 | int err; |
| 3784 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3785 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3786 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3787 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3788 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3789 | MGMT_STATUS_INVALID_PARAMS, |
| 3790 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3791 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3792 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3793 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3794 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3795 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3796 | status = MGMT_STATUS_INVALID_PARAMS; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3797 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3798 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3799 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3800 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3801 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3802 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3803 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3804 | |
| 3805 | return err; |
| 3806 | } |
| 3807 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3808 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3809 | u16 len) |
| 3810 | { |
| 3811 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3812 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3813 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3814 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3815 | |
| 3816 | BT_DBG("%s", hdev->name); |
| 3817 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3818 | source = __le16_to_cpu(cp->source); |
| 3819 | |
| 3820 | if (source > 0x0002) |
| 3821 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3822 | MGMT_STATUS_INVALID_PARAMS); |
| 3823 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3824 | hci_dev_lock(hdev); |
| 3825 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3826 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3827 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3828 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3829 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3830 | |
| 3831 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3832 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3833 | hci_req_init(&req, hdev); |
| 3834 | update_eir(&req); |
| 3835 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3836 | |
| 3837 | hci_dev_unlock(hdev); |
| 3838 | |
| 3839 | return err; |
| 3840 | } |
| 3841 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3842 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3843 | { |
| 3844 | struct cmd_lookup match = { NULL, hdev }; |
| 3845 | |
| 3846 | if (status) { |
| 3847 | u8 mgmt_err = mgmt_status(status); |
| 3848 | |
| 3849 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3850 | cmd_status_rsp, &mgmt_err); |
| 3851 | return; |
| 3852 | } |
| 3853 | |
| 3854 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3855 | &match); |
| 3856 | |
| 3857 | new_settings(hdev, match.sk); |
| 3858 | |
| 3859 | if (match.sk) |
| 3860 | sock_put(match.sk); |
| 3861 | } |
| 3862 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3863 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3864 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3865 | { |
| 3866 | struct mgmt_mode *cp = data; |
| 3867 | struct pending_cmd *cmd; |
| 3868 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3869 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3870 | int err; |
| 3871 | |
| 3872 | BT_DBG("request for %s", hdev->name); |
| 3873 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3874 | status = mgmt_le_support(hdev); |
| 3875 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3876 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3877 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3878 | |
| 3879 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3880 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3881 | MGMT_STATUS_INVALID_PARAMS); |
| 3882 | |
| 3883 | hci_dev_lock(hdev); |
| 3884 | |
| 3885 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3886 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3887 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3888 | /* The following conditions are ones which mean that we should |
| 3889 | * not do any HCI communication but directly send a mgmt |
| 3890 | * response to user space (after toggling the flag if |
| 3891 | * necessary). |
| 3892 | */ |
| 3893 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3894 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3895 | bool changed = false; |
| 3896 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3897 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3898 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3899 | changed = true; |
| 3900 | } |
| 3901 | |
| 3902 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3903 | if (err < 0) |
| 3904 | goto unlock; |
| 3905 | |
| 3906 | if (changed) |
| 3907 | err = new_settings(hdev, sk); |
| 3908 | |
| 3909 | goto unlock; |
| 3910 | } |
| 3911 | |
| 3912 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3913 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3914 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3915 | MGMT_STATUS_BUSY); |
| 3916 | goto unlock; |
| 3917 | } |
| 3918 | |
| 3919 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3920 | if (!cmd) { |
| 3921 | err = -ENOMEM; |
| 3922 | goto unlock; |
| 3923 | } |
| 3924 | |
| 3925 | hci_req_init(&req, hdev); |
| 3926 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3927 | if (val) |
| 3928 | enable_advertising(&req); |
| 3929 | else |
| 3930 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3931 | |
| 3932 | err = hci_req_run(&req, set_advertising_complete); |
| 3933 | if (err < 0) |
| 3934 | mgmt_pending_remove(cmd); |
| 3935 | |
| 3936 | unlock: |
| 3937 | hci_dev_unlock(hdev); |
| 3938 | return err; |
| 3939 | } |
| 3940 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3941 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3942 | void *data, u16 len) |
| 3943 | { |
| 3944 | struct mgmt_cp_set_static_address *cp = data; |
| 3945 | int err; |
| 3946 | |
| 3947 | BT_DBG("%s", hdev->name); |
| 3948 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3949 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3950 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3951 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3952 | |
| 3953 | if (hdev_is_powered(hdev)) |
| 3954 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3955 | MGMT_STATUS_REJECTED); |
| 3956 | |
| 3957 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3958 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3959 | return cmd_status(sk, hdev->id, |
| 3960 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3961 | MGMT_STATUS_INVALID_PARAMS); |
| 3962 | |
| 3963 | /* Two most significant bits shall be set */ |
| 3964 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3965 | return cmd_status(sk, hdev->id, |
| 3966 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3967 | MGMT_STATUS_INVALID_PARAMS); |
| 3968 | } |
| 3969 | |
| 3970 | hci_dev_lock(hdev); |
| 3971 | |
| 3972 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3973 | |
| 3974 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3975 | |
| 3976 | hci_dev_unlock(hdev); |
| 3977 | |
| 3978 | return err; |
| 3979 | } |
| 3980 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3981 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 3982 | void *data, u16 len) |
| 3983 | { |
| 3984 | struct mgmt_cp_set_scan_params *cp = data; |
| 3985 | __u16 interval, window; |
| 3986 | int err; |
| 3987 | |
| 3988 | BT_DBG("%s", hdev->name); |
| 3989 | |
| 3990 | if (!lmp_le_capable(hdev)) |
| 3991 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3992 | MGMT_STATUS_NOT_SUPPORTED); |
| 3993 | |
| 3994 | interval = __le16_to_cpu(cp->interval); |
| 3995 | |
| 3996 | if (interval < 0x0004 || interval > 0x4000) |
| 3997 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3998 | MGMT_STATUS_INVALID_PARAMS); |
| 3999 | |
| 4000 | window = __le16_to_cpu(cp->window); |
| 4001 | |
| 4002 | if (window < 0x0004 || window > 0x4000) |
| 4003 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4004 | MGMT_STATUS_INVALID_PARAMS); |
| 4005 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4006 | if (window > interval) |
| 4007 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4008 | MGMT_STATUS_INVALID_PARAMS); |
| 4009 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4010 | hci_dev_lock(hdev); |
| 4011 | |
| 4012 | hdev->le_scan_interval = interval; |
| 4013 | hdev->le_scan_window = window; |
| 4014 | |
| 4015 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4016 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4017 | /* If background scan is running, restart it so new parameters are |
| 4018 | * loaded. |
| 4019 | */ |
| 4020 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4021 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4022 | struct hci_request req; |
| 4023 | |
| 4024 | hci_req_init(&req, hdev); |
| 4025 | |
| 4026 | hci_req_add_le_scan_disable(&req); |
| 4027 | hci_req_add_le_passive_scan(&req); |
| 4028 | |
| 4029 | hci_req_run(&req, NULL); |
| 4030 | } |
| 4031 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4032 | hci_dev_unlock(hdev); |
| 4033 | |
| 4034 | return err; |
| 4035 | } |
| 4036 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4037 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4038 | { |
| 4039 | struct pending_cmd *cmd; |
| 4040 | |
| 4041 | BT_DBG("status 0x%02x", status); |
| 4042 | |
| 4043 | hci_dev_lock(hdev); |
| 4044 | |
| 4045 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4046 | if (!cmd) |
| 4047 | goto unlock; |
| 4048 | |
| 4049 | if (status) { |
| 4050 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4051 | mgmt_status(status)); |
| 4052 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4053 | struct mgmt_mode *cp = cmd->param; |
| 4054 | |
| 4055 | if (cp->val) |
| 4056 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4057 | else |
| 4058 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4059 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4060 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4061 | new_settings(hdev, cmd->sk); |
| 4062 | } |
| 4063 | |
| 4064 | mgmt_pending_remove(cmd); |
| 4065 | |
| 4066 | unlock: |
| 4067 | hci_dev_unlock(hdev); |
| 4068 | } |
| 4069 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4070 | 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] | 4071 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4072 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4073 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4074 | struct pending_cmd *cmd; |
| 4075 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4076 | int err; |
| 4077 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4078 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4079 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4080 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4081 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4082 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4083 | MGMT_STATUS_NOT_SUPPORTED); |
| 4084 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4085 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4086 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4087 | MGMT_STATUS_INVALID_PARAMS); |
| 4088 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4089 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4090 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4091 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4092 | |
| 4093 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4094 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4095 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4096 | |
| 4097 | hci_dev_lock(hdev); |
| 4098 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4099 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4100 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4101 | MGMT_STATUS_BUSY); |
| 4102 | goto unlock; |
| 4103 | } |
| 4104 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4105 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4106 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4107 | hdev); |
| 4108 | goto unlock; |
| 4109 | } |
| 4110 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4111 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4112 | data, len); |
| 4113 | if (!cmd) { |
| 4114 | err = -ENOMEM; |
| 4115 | goto unlock; |
| 4116 | } |
| 4117 | |
| 4118 | hci_req_init(&req, hdev); |
| 4119 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4120 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4121 | |
| 4122 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4123 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4124 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4125 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4126 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4127 | } |
| 4128 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4129 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4130 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4131 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4132 | return err; |
| 4133 | } |
| 4134 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4135 | static void set_bredr_scan(struct hci_request *req) |
| 4136 | { |
| 4137 | struct hci_dev *hdev = req->hdev; |
| 4138 | u8 scan = 0; |
| 4139 | |
| 4140 | /* Ensure that fast connectable is disabled. This function will |
| 4141 | * not do anything if the page scan parameters are already what |
| 4142 | * they should be. |
| 4143 | */ |
| 4144 | write_fast_connectable(req, false); |
| 4145 | |
| 4146 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4147 | scan |= SCAN_PAGE; |
| 4148 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4149 | scan |= SCAN_INQUIRY; |
| 4150 | |
| 4151 | if (scan) |
| 4152 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4153 | } |
| 4154 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4155 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4156 | { |
| 4157 | struct pending_cmd *cmd; |
| 4158 | |
| 4159 | BT_DBG("status 0x%02x", status); |
| 4160 | |
| 4161 | hci_dev_lock(hdev); |
| 4162 | |
| 4163 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4164 | if (!cmd) |
| 4165 | goto unlock; |
| 4166 | |
| 4167 | if (status) { |
| 4168 | u8 mgmt_err = mgmt_status(status); |
| 4169 | |
| 4170 | /* We need to restore the flag if related HCI commands |
| 4171 | * failed. |
| 4172 | */ |
| 4173 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4174 | |
| 4175 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4176 | } else { |
| 4177 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4178 | new_settings(hdev, cmd->sk); |
| 4179 | } |
| 4180 | |
| 4181 | mgmt_pending_remove(cmd); |
| 4182 | |
| 4183 | unlock: |
| 4184 | hci_dev_unlock(hdev); |
| 4185 | } |
| 4186 | |
| 4187 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4188 | { |
| 4189 | struct mgmt_mode *cp = data; |
| 4190 | struct pending_cmd *cmd; |
| 4191 | struct hci_request req; |
| 4192 | int err; |
| 4193 | |
| 4194 | BT_DBG("request for %s", hdev->name); |
| 4195 | |
| 4196 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4197 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4198 | MGMT_STATUS_NOT_SUPPORTED); |
| 4199 | |
| 4200 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4201 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4202 | MGMT_STATUS_REJECTED); |
| 4203 | |
| 4204 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4205 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4206 | MGMT_STATUS_INVALID_PARAMS); |
| 4207 | |
| 4208 | hci_dev_lock(hdev); |
| 4209 | |
| 4210 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4211 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4212 | goto unlock; |
| 4213 | } |
| 4214 | |
| 4215 | if (!hdev_is_powered(hdev)) { |
| 4216 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4217 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4218 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4219 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4220 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4221 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4222 | } |
| 4223 | |
| 4224 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4225 | |
| 4226 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4227 | if (err < 0) |
| 4228 | goto unlock; |
| 4229 | |
| 4230 | err = new_settings(hdev, sk); |
| 4231 | goto unlock; |
| 4232 | } |
| 4233 | |
| 4234 | /* Reject disabling when powered on */ |
| 4235 | if (!cp->val) { |
| 4236 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4237 | MGMT_STATUS_REJECTED); |
| 4238 | goto unlock; |
| 4239 | } |
| 4240 | |
| 4241 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4242 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4243 | MGMT_STATUS_BUSY); |
| 4244 | goto unlock; |
| 4245 | } |
| 4246 | |
| 4247 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4248 | if (!cmd) { |
| 4249 | err = -ENOMEM; |
| 4250 | goto unlock; |
| 4251 | } |
| 4252 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4253 | /* 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] | 4254 | * generates the correct flags. |
| 4255 | */ |
| 4256 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4257 | |
| 4258 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4259 | |
| 4260 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4261 | set_bredr_scan(&req); |
| 4262 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4263 | /* Since only the advertising data flags will change, there |
| 4264 | * is no need to update the scan response data. |
| 4265 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4266 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4267 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4268 | err = hci_req_run(&req, set_bredr_complete); |
| 4269 | if (err < 0) |
| 4270 | mgmt_pending_remove(cmd); |
| 4271 | |
| 4272 | unlock: |
| 4273 | hci_dev_unlock(hdev); |
| 4274 | return err; |
| 4275 | } |
| 4276 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4277 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4278 | void *data, u16 len) |
| 4279 | { |
| 4280 | struct mgmt_mode *cp = data; |
| 4281 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4282 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4283 | int err; |
| 4284 | |
| 4285 | BT_DBG("request for %s", hdev->name); |
| 4286 | |
| 4287 | status = mgmt_bredr_support(hdev); |
| 4288 | if (status) |
| 4289 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4290 | status); |
| 4291 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4292 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4293 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4294 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4295 | MGMT_STATUS_NOT_SUPPORTED); |
| 4296 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4297 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4298 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4299 | MGMT_STATUS_INVALID_PARAMS); |
| 4300 | |
| 4301 | hci_dev_lock(hdev); |
| 4302 | |
| 4303 | if (!hdev_is_powered(hdev)) { |
| 4304 | bool changed; |
| 4305 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4306 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4307 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4308 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4309 | if (cp->val == 0x02) |
| 4310 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4311 | else |
| 4312 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4313 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4314 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4315 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4316 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4317 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4318 | |
| 4319 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4320 | if (err < 0) |
| 4321 | goto failed; |
| 4322 | |
| 4323 | if (changed) |
| 4324 | err = new_settings(hdev, sk); |
| 4325 | |
| 4326 | goto failed; |
| 4327 | } |
| 4328 | |
| 4329 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4330 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4331 | MGMT_STATUS_BUSY); |
| 4332 | goto failed; |
| 4333 | } |
| 4334 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4335 | val = !!cp->val; |
| 4336 | |
| 4337 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4338 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4339 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4340 | goto failed; |
| 4341 | } |
| 4342 | |
| 4343 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4344 | if (!cmd) { |
| 4345 | err = -ENOMEM; |
| 4346 | goto failed; |
| 4347 | } |
| 4348 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4349 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4350 | if (err < 0) { |
| 4351 | mgmt_pending_remove(cmd); |
| 4352 | goto failed; |
| 4353 | } |
| 4354 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4355 | if (cp->val == 0x02) |
| 4356 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4357 | else |
| 4358 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4359 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4360 | failed: |
| 4361 | hci_dev_unlock(hdev); |
| 4362 | return err; |
| 4363 | } |
| 4364 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4365 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4366 | void *data, u16 len) |
| 4367 | { |
| 4368 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4369 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4370 | int err; |
| 4371 | |
| 4372 | BT_DBG("request for %s", hdev->name); |
| 4373 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4374 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4375 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4376 | MGMT_STATUS_INVALID_PARAMS); |
| 4377 | |
| 4378 | hci_dev_lock(hdev); |
| 4379 | |
| 4380 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4381 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4382 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4383 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4384 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4385 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4386 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4387 | if (cp->val == 0x02) |
| 4388 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4389 | &hdev->dev_flags); |
| 4390 | else |
| 4391 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4392 | &hdev->dev_flags); |
| 4393 | |
| 4394 | if (hdev_is_powered(hdev) && use_changed && |
| 4395 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4396 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4397 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4398 | sizeof(mode), &mode); |
| 4399 | } |
| 4400 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4401 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4402 | if (err < 0) |
| 4403 | goto unlock; |
| 4404 | |
| 4405 | if (changed) |
| 4406 | err = new_settings(hdev, sk); |
| 4407 | |
| 4408 | unlock: |
| 4409 | hci_dev_unlock(hdev); |
| 4410 | return err; |
| 4411 | } |
| 4412 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4413 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4414 | u16 len) |
| 4415 | { |
| 4416 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4417 | bool changed; |
| 4418 | int err; |
| 4419 | |
| 4420 | BT_DBG("request for %s", hdev->name); |
| 4421 | |
| 4422 | if (!lmp_le_capable(hdev)) |
| 4423 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4424 | MGMT_STATUS_NOT_SUPPORTED); |
| 4425 | |
| 4426 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4427 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4428 | MGMT_STATUS_INVALID_PARAMS); |
| 4429 | |
| 4430 | if (hdev_is_powered(hdev)) |
| 4431 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4432 | MGMT_STATUS_REJECTED); |
| 4433 | |
| 4434 | hci_dev_lock(hdev); |
| 4435 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4436 | /* If user space supports this command it is also expected to |
| 4437 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4438 | */ |
| 4439 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4440 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4441 | if (cp->privacy) { |
| 4442 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4443 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4444 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4445 | } else { |
| 4446 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4447 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4448 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4449 | } |
| 4450 | |
| 4451 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4452 | if (err < 0) |
| 4453 | goto unlock; |
| 4454 | |
| 4455 | if (changed) |
| 4456 | err = new_settings(hdev, sk); |
| 4457 | |
| 4458 | unlock: |
| 4459 | hci_dev_unlock(hdev); |
| 4460 | return err; |
| 4461 | } |
| 4462 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4463 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4464 | { |
| 4465 | switch (irk->addr.type) { |
| 4466 | case BDADDR_LE_PUBLIC: |
| 4467 | return true; |
| 4468 | |
| 4469 | case BDADDR_LE_RANDOM: |
| 4470 | /* Two most significant bits shall be set */ |
| 4471 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4472 | return false; |
| 4473 | return true; |
| 4474 | } |
| 4475 | |
| 4476 | return false; |
| 4477 | } |
| 4478 | |
| 4479 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4480 | u16 len) |
| 4481 | { |
| 4482 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4483 | u16 irk_count, expected_len; |
| 4484 | int i, err; |
| 4485 | |
| 4486 | BT_DBG("request for %s", hdev->name); |
| 4487 | |
| 4488 | if (!lmp_le_capable(hdev)) |
| 4489 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4490 | MGMT_STATUS_NOT_SUPPORTED); |
| 4491 | |
| 4492 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4493 | |
| 4494 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4495 | if (expected_len != len) { |
| 4496 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4497 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4498 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4499 | MGMT_STATUS_INVALID_PARAMS); |
| 4500 | } |
| 4501 | |
| 4502 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4503 | |
| 4504 | for (i = 0; i < irk_count; i++) { |
| 4505 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4506 | |
| 4507 | if (!irk_is_valid(key)) |
| 4508 | return cmd_status(sk, hdev->id, |
| 4509 | MGMT_OP_LOAD_IRKS, |
| 4510 | MGMT_STATUS_INVALID_PARAMS); |
| 4511 | } |
| 4512 | |
| 4513 | hci_dev_lock(hdev); |
| 4514 | |
| 4515 | hci_smp_irks_clear(hdev); |
| 4516 | |
| 4517 | for (i = 0; i < irk_count; i++) { |
| 4518 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4519 | u8 addr_type; |
| 4520 | |
| 4521 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4522 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4523 | else |
| 4524 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4525 | |
| 4526 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4527 | BDADDR_ANY); |
| 4528 | } |
| 4529 | |
| 4530 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4531 | |
| 4532 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4533 | |
| 4534 | hci_dev_unlock(hdev); |
| 4535 | |
| 4536 | return err; |
| 4537 | } |
| 4538 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4539 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4540 | { |
| 4541 | if (key->master != 0x00 && key->master != 0x01) |
| 4542 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4543 | |
| 4544 | switch (key->addr.type) { |
| 4545 | case BDADDR_LE_PUBLIC: |
| 4546 | return true; |
| 4547 | |
| 4548 | case BDADDR_LE_RANDOM: |
| 4549 | /* Two most significant bits shall be set */ |
| 4550 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4551 | return false; |
| 4552 | return true; |
| 4553 | } |
| 4554 | |
| 4555 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4556 | } |
| 4557 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4558 | 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] | 4559 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4560 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4561 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4562 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4563 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4564 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4565 | BT_DBG("request for %s", hdev->name); |
| 4566 | |
| 4567 | if (!lmp_le_capable(hdev)) |
| 4568 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4569 | MGMT_STATUS_NOT_SUPPORTED); |
| 4570 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4571 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4572 | |
| 4573 | expected_len = sizeof(*cp) + key_count * |
| 4574 | sizeof(struct mgmt_ltk_info); |
| 4575 | if (expected_len != len) { |
| 4576 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4577 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4578 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4579 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4580 | } |
| 4581 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4582 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4583 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4584 | for (i = 0; i < key_count; i++) { |
| 4585 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4586 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4587 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4588 | return cmd_status(sk, hdev->id, |
| 4589 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4590 | MGMT_STATUS_INVALID_PARAMS); |
| 4591 | } |
| 4592 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4593 | hci_dev_lock(hdev); |
| 4594 | |
| 4595 | hci_smp_ltks_clear(hdev); |
| 4596 | |
| 4597 | for (i = 0; i < key_count; i++) { |
| 4598 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4599 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4600 | |
| 4601 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4602 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4603 | else |
| 4604 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4605 | |
| 4606 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4607 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4608 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4609 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4610 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4611 | switch (key->type) { |
| 4612 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4613 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4614 | break; |
| 4615 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4616 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4617 | break; |
| 4618 | default: |
| 4619 | continue; |
| 4620 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4621 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4622 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4623 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4624 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4625 | } |
| 4626 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4627 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4628 | NULL, 0); |
| 4629 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4630 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4631 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4632 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4633 | } |
| 4634 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4635 | struct cmd_conn_lookup { |
| 4636 | struct hci_conn *conn; |
| 4637 | bool valid_tx_power; |
| 4638 | u8 mgmt_status; |
| 4639 | }; |
| 4640 | |
| 4641 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4642 | { |
| 4643 | struct cmd_conn_lookup *match = data; |
| 4644 | struct mgmt_cp_get_conn_info *cp; |
| 4645 | struct mgmt_rp_get_conn_info rp; |
| 4646 | struct hci_conn *conn = cmd->user_data; |
| 4647 | |
| 4648 | if (conn != match->conn) |
| 4649 | return; |
| 4650 | |
| 4651 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4652 | |
| 4653 | memset(&rp, 0, sizeof(rp)); |
| 4654 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4655 | rp.addr.type = cp->addr.type; |
| 4656 | |
| 4657 | if (!match->mgmt_status) { |
| 4658 | rp.rssi = conn->rssi; |
| 4659 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4660 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4661 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4662 | rp.max_tx_power = conn->max_tx_power; |
| 4663 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4664 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4665 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4666 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4670 | match->mgmt_status, &rp, sizeof(rp)); |
| 4671 | |
| 4672 | hci_conn_drop(conn); |
| 4673 | |
| 4674 | mgmt_pending_remove(cmd); |
| 4675 | } |
| 4676 | |
| 4677 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4678 | { |
| 4679 | struct hci_cp_read_rssi *cp; |
| 4680 | struct hci_conn *conn; |
| 4681 | struct cmd_conn_lookup match; |
| 4682 | u16 handle; |
| 4683 | |
| 4684 | BT_DBG("status 0x%02x", status); |
| 4685 | |
| 4686 | hci_dev_lock(hdev); |
| 4687 | |
| 4688 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4689 | * otherwise we assume it's not valid. At the moment we assume that |
| 4690 | * either both or none of current and max values are valid to keep code |
| 4691 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4692 | */ |
| 4693 | match.valid_tx_power = !status; |
| 4694 | |
| 4695 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4696 | * Level so we check which one was last sent to retrieve connection |
| 4697 | * handle. Both commands have handle as first parameter so it's safe to |
| 4698 | * cast data on the same command struct. |
| 4699 | * |
| 4700 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4701 | * In other case we simply override error to indicate success as we |
| 4702 | * already remembered if TX power value is actually valid. |
| 4703 | */ |
| 4704 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4705 | if (!cp) { |
| 4706 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4707 | status = 0; |
| 4708 | } |
| 4709 | |
| 4710 | if (!cp) { |
| 4711 | BT_ERR("invalid sent_cmd in response"); |
| 4712 | goto unlock; |
| 4713 | } |
| 4714 | |
| 4715 | handle = __le16_to_cpu(cp->handle); |
| 4716 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4717 | if (!conn) { |
| 4718 | BT_ERR("unknown handle (%d) in response", handle); |
| 4719 | goto unlock; |
| 4720 | } |
| 4721 | |
| 4722 | match.conn = conn; |
| 4723 | match.mgmt_status = mgmt_status(status); |
| 4724 | |
| 4725 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4726 | * connection only. |
| 4727 | */ |
| 4728 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4729 | get_conn_info_complete, &match); |
| 4730 | |
| 4731 | unlock: |
| 4732 | hci_dev_unlock(hdev); |
| 4733 | } |
| 4734 | |
| 4735 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4736 | u16 len) |
| 4737 | { |
| 4738 | struct mgmt_cp_get_conn_info *cp = data; |
| 4739 | struct mgmt_rp_get_conn_info rp; |
| 4740 | struct hci_conn *conn; |
| 4741 | unsigned long conn_info_age; |
| 4742 | int err = 0; |
| 4743 | |
| 4744 | BT_DBG("%s", hdev->name); |
| 4745 | |
| 4746 | memset(&rp, 0, sizeof(rp)); |
| 4747 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4748 | rp.addr.type = cp->addr.type; |
| 4749 | |
| 4750 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4751 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4752 | MGMT_STATUS_INVALID_PARAMS, |
| 4753 | &rp, sizeof(rp)); |
| 4754 | |
| 4755 | hci_dev_lock(hdev); |
| 4756 | |
| 4757 | if (!hdev_is_powered(hdev)) { |
| 4758 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4759 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4760 | goto unlock; |
| 4761 | } |
| 4762 | |
| 4763 | if (cp->addr.type == BDADDR_BREDR) |
| 4764 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4765 | &cp->addr.bdaddr); |
| 4766 | else |
| 4767 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4768 | |
| 4769 | if (!conn || conn->state != BT_CONNECTED) { |
| 4770 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4771 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4772 | goto unlock; |
| 4773 | } |
| 4774 | |
| 4775 | /* To avoid client trying to guess when to poll again for information we |
| 4776 | * calculate conn info age as random value between min/max set in hdev. |
| 4777 | */ |
| 4778 | conn_info_age = hdev->conn_info_min_age + |
| 4779 | prandom_u32_max(hdev->conn_info_max_age - |
| 4780 | hdev->conn_info_min_age); |
| 4781 | |
| 4782 | /* Query controller to refresh cached values if they are too old or were |
| 4783 | * never read. |
| 4784 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4785 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4786 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4787 | !conn->conn_info_timestamp) { |
| 4788 | struct hci_request req; |
| 4789 | struct hci_cp_read_tx_power req_txp_cp; |
| 4790 | struct hci_cp_read_rssi req_rssi_cp; |
| 4791 | struct pending_cmd *cmd; |
| 4792 | |
| 4793 | hci_req_init(&req, hdev); |
| 4794 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4795 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4796 | &req_rssi_cp); |
| 4797 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4798 | /* For LE links TX power does not change thus we don't need to |
| 4799 | * query for it once value is known. |
| 4800 | */ |
| 4801 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4802 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4803 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4804 | req_txp_cp.type = 0x00; |
| 4805 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4806 | sizeof(req_txp_cp), &req_txp_cp); |
| 4807 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4808 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4809 | /* Max TX power needs to be read only once per connection */ |
| 4810 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4811 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4812 | req_txp_cp.type = 0x01; |
| 4813 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4814 | sizeof(req_txp_cp), &req_txp_cp); |
| 4815 | } |
| 4816 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4817 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4818 | if (err < 0) |
| 4819 | goto unlock; |
| 4820 | |
| 4821 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4822 | data, len); |
| 4823 | if (!cmd) { |
| 4824 | err = -ENOMEM; |
| 4825 | goto unlock; |
| 4826 | } |
| 4827 | |
| 4828 | hci_conn_hold(conn); |
| 4829 | cmd->user_data = conn; |
| 4830 | |
| 4831 | conn->conn_info_timestamp = jiffies; |
| 4832 | } else { |
| 4833 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4834 | rp.rssi = conn->rssi; |
| 4835 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4836 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4837 | |
| 4838 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4839 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4840 | } |
| 4841 | |
| 4842 | unlock: |
| 4843 | hci_dev_unlock(hdev); |
| 4844 | return err; |
| 4845 | } |
| 4846 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 4847 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 4848 | { |
| 4849 | struct mgmt_cp_get_clock_info *cp; |
| 4850 | struct mgmt_rp_get_clock_info rp; |
| 4851 | struct hci_cp_read_clock *hci_cp; |
| 4852 | struct pending_cmd *cmd; |
| 4853 | struct hci_conn *conn; |
| 4854 | |
| 4855 | BT_DBG("%s status %u", hdev->name, status); |
| 4856 | |
| 4857 | hci_dev_lock(hdev); |
| 4858 | |
| 4859 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 4860 | if (!hci_cp) |
| 4861 | goto unlock; |
| 4862 | |
| 4863 | if (hci_cp->which) { |
| 4864 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 4865 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4866 | } else { |
| 4867 | conn = NULL; |
| 4868 | } |
| 4869 | |
| 4870 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 4871 | if (!cmd) |
| 4872 | goto unlock; |
| 4873 | |
| 4874 | cp = cmd->param; |
| 4875 | |
| 4876 | memset(&rp, 0, sizeof(rp)); |
| 4877 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 4878 | |
| 4879 | if (status) |
| 4880 | goto send_rsp; |
| 4881 | |
| 4882 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 4883 | |
| 4884 | if (conn) { |
| 4885 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 4886 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 4887 | } |
| 4888 | |
| 4889 | send_rsp: |
| 4890 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 4891 | &rp, sizeof(rp)); |
| 4892 | mgmt_pending_remove(cmd); |
| 4893 | if (conn) |
| 4894 | hci_conn_drop(conn); |
| 4895 | |
| 4896 | unlock: |
| 4897 | hci_dev_unlock(hdev); |
| 4898 | } |
| 4899 | |
| 4900 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4901 | u16 len) |
| 4902 | { |
| 4903 | struct mgmt_cp_get_clock_info *cp = data; |
| 4904 | struct mgmt_rp_get_clock_info rp; |
| 4905 | struct hci_cp_read_clock hci_cp; |
| 4906 | struct pending_cmd *cmd; |
| 4907 | struct hci_request req; |
| 4908 | struct hci_conn *conn; |
| 4909 | int err; |
| 4910 | |
| 4911 | BT_DBG("%s", hdev->name); |
| 4912 | |
| 4913 | memset(&rp, 0, sizeof(rp)); |
| 4914 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4915 | rp.addr.type = cp->addr.type; |
| 4916 | |
| 4917 | if (cp->addr.type != BDADDR_BREDR) |
| 4918 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4919 | MGMT_STATUS_INVALID_PARAMS, |
| 4920 | &rp, sizeof(rp)); |
| 4921 | |
| 4922 | hci_dev_lock(hdev); |
| 4923 | |
| 4924 | if (!hdev_is_powered(hdev)) { |
| 4925 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4926 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4927 | goto unlock; |
| 4928 | } |
| 4929 | |
| 4930 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4931 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4932 | &cp->addr.bdaddr); |
| 4933 | if (!conn || conn->state != BT_CONNECTED) { |
| 4934 | err = cmd_complete(sk, hdev->id, |
| 4935 | MGMT_OP_GET_CLOCK_INFO, |
| 4936 | MGMT_STATUS_NOT_CONNECTED, |
| 4937 | &rp, sizeof(rp)); |
| 4938 | goto unlock; |
| 4939 | } |
| 4940 | } else { |
| 4941 | conn = NULL; |
| 4942 | } |
| 4943 | |
| 4944 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 4945 | if (!cmd) { |
| 4946 | err = -ENOMEM; |
| 4947 | goto unlock; |
| 4948 | } |
| 4949 | |
| 4950 | hci_req_init(&req, hdev); |
| 4951 | |
| 4952 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 4953 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4954 | |
| 4955 | if (conn) { |
| 4956 | hci_conn_hold(conn); |
| 4957 | cmd->user_data = conn; |
| 4958 | |
| 4959 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 4960 | hci_cp.which = 0x01; /* Piconet clock */ |
| 4961 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4962 | } |
| 4963 | |
| 4964 | err = hci_req_run(&req, get_clock_info_complete); |
| 4965 | if (err < 0) |
| 4966 | mgmt_pending_remove(cmd); |
| 4967 | |
| 4968 | unlock: |
| 4969 | hci_dev_unlock(hdev); |
| 4970 | return err; |
| 4971 | } |
| 4972 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame^] | 4973 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 4974 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 4975 | { |
| 4976 | struct mgmt_ev_device_added ev; |
| 4977 | |
| 4978 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4979 | ev.addr.type = type; |
| 4980 | ev.action = action; |
| 4981 | |
| 4982 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 4983 | } |
| 4984 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 4985 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 4986 | void *data, u16 len) |
| 4987 | { |
| 4988 | struct mgmt_cp_add_device *cp = data; |
| 4989 | u8 auto_conn, addr_type; |
| 4990 | int err; |
| 4991 | |
| 4992 | BT_DBG("%s", hdev->name); |
| 4993 | |
| 4994 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4995 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 4996 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 4997 | MGMT_STATUS_INVALID_PARAMS, |
| 4998 | &cp->addr, sizeof(cp->addr)); |
| 4999 | |
| 5000 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5001 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5002 | MGMT_STATUS_INVALID_PARAMS, |
| 5003 | &cp->addr, sizeof(cp->addr)); |
| 5004 | |
| 5005 | hci_dev_lock(hdev); |
| 5006 | |
| 5007 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5008 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5009 | else |
| 5010 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5011 | |
| 5012 | if (cp->action) |
| 5013 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5014 | else |
| 5015 | auto_conn = HCI_AUTO_CONN_DISABLED; |
| 5016 | |
| 5017 | if (hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type, auto_conn, |
| 5018 | hdev->le_conn_min_interval, |
| 5019 | hdev->le_conn_max_interval) < 0) { |
| 5020 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5021 | MGMT_STATUS_FAILED, |
| 5022 | &cp->addr, sizeof(cp->addr)); |
| 5023 | goto unlock; |
| 5024 | } |
| 5025 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame^] | 5026 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5027 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5028 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5029 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5030 | |
| 5031 | unlock: |
| 5032 | hci_dev_unlock(hdev); |
| 5033 | return err; |
| 5034 | } |
| 5035 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame^] | 5036 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5037 | bdaddr_t *bdaddr, u8 type) |
| 5038 | { |
| 5039 | struct mgmt_ev_device_removed ev; |
| 5040 | |
| 5041 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5042 | ev.addr.type = type; |
| 5043 | |
| 5044 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5045 | } |
| 5046 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5047 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5048 | void *data, u16 len) |
| 5049 | { |
| 5050 | struct mgmt_cp_remove_device *cp = data; |
| 5051 | int err; |
| 5052 | |
| 5053 | BT_DBG("%s", hdev->name); |
| 5054 | |
| 5055 | hci_dev_lock(hdev); |
| 5056 | |
| 5057 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5058 | u8 addr_type; |
| 5059 | |
| 5060 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5061 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5062 | MGMT_STATUS_INVALID_PARAMS, |
| 5063 | &cp->addr, sizeof(cp->addr)); |
| 5064 | goto unlock; |
| 5065 | } |
| 5066 | |
| 5067 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5068 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5069 | else |
| 5070 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5071 | |
| 5072 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame^] | 5073 | |
| 5074 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5075 | } else { |
| 5076 | if (cp->addr.type) { |
| 5077 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5078 | MGMT_STATUS_INVALID_PARAMS, |
| 5079 | &cp->addr, sizeof(cp->addr)); |
| 5080 | goto unlock; |
| 5081 | } |
| 5082 | |
| 5083 | hci_conn_params_clear(hdev); |
| 5084 | } |
| 5085 | |
| 5086 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5087 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5088 | |
| 5089 | unlock: |
| 5090 | hci_dev_unlock(hdev); |
| 5091 | return err; |
| 5092 | } |
| 5093 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5094 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5095 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5096 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5097 | bool var_len; |
| 5098 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5099 | } mgmt_handlers[] = { |
| 5100 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5101 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5102 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5103 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5104 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5105 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5106 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5107 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5108 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5109 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5110 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5111 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5112 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5113 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5114 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5115 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5116 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5117 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5118 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5119 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5120 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5121 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5122 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5123 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5124 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5125 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5126 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5127 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5128 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5129 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5130 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5131 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5132 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5133 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5134 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5135 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5136 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5137 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5138 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5139 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5140 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5141 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5142 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5143 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5144 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5145 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5146 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5147 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5148 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5149 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5150 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5151 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5152 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5153 | }; |
| 5154 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5155 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5156 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5157 | void *buf; |
| 5158 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5159 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5160 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5161 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5162 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5163 | int err; |
| 5164 | |
| 5165 | BT_DBG("got %zu bytes", msglen); |
| 5166 | |
| 5167 | if (msglen < sizeof(*hdr)) |
| 5168 | return -EINVAL; |
| 5169 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5170 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5171 | if (!buf) |
| 5172 | return -ENOMEM; |
| 5173 | |
| 5174 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5175 | err = -EFAULT; |
| 5176 | goto done; |
| 5177 | } |
| 5178 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5179 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5180 | opcode = __le16_to_cpu(hdr->opcode); |
| 5181 | index = __le16_to_cpu(hdr->index); |
| 5182 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5183 | |
| 5184 | if (len != msglen - sizeof(*hdr)) { |
| 5185 | err = -EINVAL; |
| 5186 | goto done; |
| 5187 | } |
| 5188 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5189 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5190 | hdev = hci_dev_get(index); |
| 5191 | if (!hdev) { |
| 5192 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5193 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5194 | goto done; |
| 5195 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5196 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5197 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 5198 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) || |
| 5199 | test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5200 | err = cmd_status(sk, index, opcode, |
| 5201 | MGMT_STATUS_INVALID_INDEX); |
| 5202 | goto done; |
| 5203 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5204 | } |
| 5205 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5206 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5207 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5208 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5209 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5210 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5211 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5212 | } |
| 5213 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5214 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5215 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5216 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5217 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5218 | goto done; |
| 5219 | } |
| 5220 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5221 | handler = &mgmt_handlers[opcode]; |
| 5222 | |
| 5223 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5224 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5225 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5226 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5227 | goto done; |
| 5228 | } |
| 5229 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5230 | if (hdev) |
| 5231 | mgmt_init_hdev(sk, hdev); |
| 5232 | |
| 5233 | cp = buf + sizeof(*hdr); |
| 5234 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5235 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5236 | if (err < 0) |
| 5237 | goto done; |
| 5238 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5239 | err = msglen; |
| 5240 | |
| 5241 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5242 | if (hdev) |
| 5243 | hci_dev_put(hdev); |
| 5244 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5245 | kfree(buf); |
| 5246 | return err; |
| 5247 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5248 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5249 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5250 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5251 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5252 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5253 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5254 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5255 | } |
| 5256 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5257 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5258 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5259 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5260 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5261 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5262 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5263 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5264 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5265 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5266 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5267 | } |
| 5268 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5269 | /* This function requires the caller holds hdev->lock */ |
| 5270 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 5271 | { |
| 5272 | struct hci_conn_params *p; |
| 5273 | |
| 5274 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
| 5275 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) |
| 5276 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
| 5277 | } |
| 5278 | } |
| 5279 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5280 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5281 | { |
| 5282 | struct cmd_lookup match = { NULL, hdev }; |
| 5283 | |
| 5284 | BT_DBG("status 0x%02x", status); |
| 5285 | |
| 5286 | hci_dev_lock(hdev); |
| 5287 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5288 | restart_le_auto_conns(hdev); |
| 5289 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5290 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5291 | |
| 5292 | new_settings(hdev, match.sk); |
| 5293 | |
| 5294 | hci_dev_unlock(hdev); |
| 5295 | |
| 5296 | if (match.sk) |
| 5297 | sock_put(match.sk); |
| 5298 | } |
| 5299 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5300 | static int powered_update_hci(struct hci_dev *hdev) |
| 5301 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5302 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5303 | u8 link_sec; |
| 5304 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5305 | hci_req_init(&req, hdev); |
| 5306 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5307 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5308 | !lmp_host_ssp_capable(hdev)) { |
| 5309 | u8 ssp = 1; |
| 5310 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5311 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5312 | } |
| 5313 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5314 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5315 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5316 | struct hci_cp_write_le_host_supported cp; |
| 5317 | |
| 5318 | cp.le = 1; |
| 5319 | cp.simul = lmp_le_br_capable(hdev); |
| 5320 | |
| 5321 | /* Check first if we already have the right |
| 5322 | * host state (host features set) |
| 5323 | */ |
| 5324 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5325 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5326 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5327 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5328 | } |
| 5329 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5330 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5331 | /* Make sure the controller has a good default for |
| 5332 | * advertising data. This also applies to the case |
| 5333 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5334 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5335 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5336 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5337 | update_scan_rsp_data(&req); |
| 5338 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5339 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5340 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5341 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5342 | } |
| 5343 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5344 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5345 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5346 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5347 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5348 | |
| 5349 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5350 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5351 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5352 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5353 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5354 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5355 | } |
| 5356 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5357 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5358 | } |
| 5359 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5360 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5361 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5362 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5363 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5364 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5365 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5366 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5367 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5368 | return 0; |
| 5369 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5370 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5371 | if (powered_update_hci(hdev) == 0) |
| 5372 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5373 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5374 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5375 | &match); |
| 5376 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5377 | } |
| 5378 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5379 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5380 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5381 | |
| 5382 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5383 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5384 | zero_cod, sizeof(zero_cod), NULL); |
| 5385 | |
| 5386 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5387 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5388 | |
| 5389 | if (match.sk) |
| 5390 | sock_put(match.sk); |
| 5391 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5392 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5393 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5394 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5395 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5396 | { |
| 5397 | struct pending_cmd *cmd; |
| 5398 | u8 status; |
| 5399 | |
| 5400 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5401 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5402 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5403 | |
| 5404 | if (err == -ERFKILL) |
| 5405 | status = MGMT_STATUS_RFKILLED; |
| 5406 | else |
| 5407 | status = MGMT_STATUS_FAILED; |
| 5408 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5409 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5410 | |
| 5411 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5412 | } |
| 5413 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5414 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5415 | { |
| 5416 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5417 | |
| 5418 | hci_dev_lock(hdev); |
| 5419 | |
| 5420 | /* When discoverable timeout triggers, then just make sure |
| 5421 | * the limited discoverable flag is cleared. Even in the case |
| 5422 | * of a timeout triggered from general discoverable, it is |
| 5423 | * safe to unconditionally clear the flag. |
| 5424 | */ |
| 5425 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5426 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5427 | |
| 5428 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5429 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5430 | u8 scan = SCAN_PAGE; |
| 5431 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5432 | sizeof(scan), &scan); |
| 5433 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5434 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5435 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5436 | hci_req_run(&req, NULL); |
| 5437 | |
| 5438 | hdev->discov_timeout = 0; |
| 5439 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5440 | new_settings(hdev, NULL); |
| 5441 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5442 | hci_dev_unlock(hdev); |
| 5443 | } |
| 5444 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5445 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5446 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5447 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5448 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5449 | /* Nothing needed here if there's a pending command since that |
| 5450 | * commands request completion callback takes care of everything |
| 5451 | * necessary. |
| 5452 | */ |
| 5453 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5454 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5455 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5456 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5457 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5458 | return; |
| 5459 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5460 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5461 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5462 | } else { |
| 5463 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5464 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5465 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5466 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5467 | if (changed) { |
| 5468 | struct hci_request req; |
| 5469 | |
| 5470 | /* In case this change in discoverable was triggered by |
| 5471 | * a disabling of connectable there could be a need to |
| 5472 | * update the advertising flags. |
| 5473 | */ |
| 5474 | hci_req_init(&req, hdev); |
| 5475 | update_adv_data(&req); |
| 5476 | hci_req_run(&req, NULL); |
| 5477 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5478 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5479 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5480 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5481 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5482 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5483 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5484 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5485 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5486 | /* Nothing needed here if there's a pending command since that |
| 5487 | * commands request completion callback takes care of everything |
| 5488 | * necessary. |
| 5489 | */ |
| 5490 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5491 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5492 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5493 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5494 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5495 | return; |
| 5496 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5497 | if (connectable) |
| 5498 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5499 | else |
| 5500 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5501 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5502 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5503 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5504 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5505 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5506 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5507 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5508 | /* Powering off may stop advertising - don't let that interfere */ |
| 5509 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5510 | return; |
| 5511 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5512 | if (advertising) |
| 5513 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5514 | else |
| 5515 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5516 | } |
| 5517 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5518 | 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] | 5519 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5520 | u8 mgmt_err = mgmt_status(status); |
| 5521 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5522 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5523 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5524 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5525 | |
| 5526 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5527 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5528 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5529 | } |
| 5530 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5531 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5532 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5533 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5534 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5535 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5536 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5537 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5538 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5539 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5540 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5541 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5542 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5543 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5544 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5545 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5546 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5547 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5548 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5549 | { |
| 5550 | if (ltk->authenticated) |
| 5551 | return MGMT_LTK_AUTHENTICATED; |
| 5552 | |
| 5553 | return MGMT_LTK_UNAUTHENTICATED; |
| 5554 | } |
| 5555 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5556 | 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] | 5557 | { |
| 5558 | struct mgmt_ev_new_long_term_key ev; |
| 5559 | |
| 5560 | memset(&ev, 0, sizeof(ev)); |
| 5561 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5562 | /* Devices using resolvable or non-resolvable random addresses |
| 5563 | * without providing an indentity resolving key don't require |
| 5564 | * to store long term keys. Their addresses will change the |
| 5565 | * next time around. |
| 5566 | * |
| 5567 | * Only when a remote device provides an identity address |
| 5568 | * make sure the long term key is stored. If the remote |
| 5569 | * identity is known, the long term keys are internally |
| 5570 | * mapped to the identity address. So allow static random |
| 5571 | * and public addresses here. |
| 5572 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5573 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5574 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5575 | ev.store_hint = 0x00; |
| 5576 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5577 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5578 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5579 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5580 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5581 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5582 | ev.key.enc_size = key->enc_size; |
| 5583 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5584 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5585 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5586 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5587 | ev.key.master = 1; |
| 5588 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5589 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5590 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5591 | 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] | 5592 | } |
| 5593 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5594 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5595 | { |
| 5596 | struct mgmt_ev_new_irk ev; |
| 5597 | |
| 5598 | memset(&ev, 0, sizeof(ev)); |
| 5599 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5600 | /* For identity resolving keys from devices that are already |
| 5601 | * using a public address or static random address, do not |
| 5602 | * ask for storing this key. The identity resolving key really |
| 5603 | * is only mandatory for devices using resovlable random |
| 5604 | * addresses. |
| 5605 | * |
| 5606 | * Storing all identity resolving keys has the downside that |
| 5607 | * they will be also loaded on next boot of they system. More |
| 5608 | * identity resolving keys, means more time during scanning is |
| 5609 | * needed to actually resolve these addresses. |
| 5610 | */ |
| 5611 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5612 | ev.store_hint = 0x01; |
| 5613 | else |
| 5614 | ev.store_hint = 0x00; |
| 5615 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5616 | bacpy(&ev.rpa, &irk->rpa); |
| 5617 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5618 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5619 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5620 | |
| 5621 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5622 | } |
| 5623 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5624 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5625 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5626 | { |
| 5627 | struct mgmt_ev_new_csrk ev; |
| 5628 | |
| 5629 | memset(&ev, 0, sizeof(ev)); |
| 5630 | |
| 5631 | /* Devices using resolvable or non-resolvable random addresses |
| 5632 | * without providing an indentity resolving key don't require |
| 5633 | * to store signature resolving keys. Their addresses will change |
| 5634 | * the next time around. |
| 5635 | * |
| 5636 | * Only when a remote device provides an identity address |
| 5637 | * make sure the signature resolving key is stored. So allow |
| 5638 | * static random and public addresses here. |
| 5639 | */ |
| 5640 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5641 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5642 | ev.store_hint = 0x00; |
| 5643 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5644 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5645 | |
| 5646 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5647 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5648 | ev.key.master = csrk->master; |
| 5649 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5650 | |
| 5651 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5652 | } |
| 5653 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5654 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5655 | u8 data_len) |
| 5656 | { |
| 5657 | eir[eir_len++] = sizeof(type) + data_len; |
| 5658 | eir[eir_len++] = type; |
| 5659 | memcpy(&eir[eir_len], data, data_len); |
| 5660 | eir_len += data_len; |
| 5661 | |
| 5662 | return eir_len; |
| 5663 | } |
| 5664 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5665 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5666 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5667 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5668 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5669 | char buf[512]; |
| 5670 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5671 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5672 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5673 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5674 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5675 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5676 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5677 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5678 | if (name_len > 0) |
| 5679 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5680 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5681 | |
| 5682 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5683 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5684 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5685 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5686 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5687 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5688 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5689 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5690 | } |
| 5691 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5692 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5693 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5694 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5695 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5696 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5697 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5698 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5699 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5700 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5701 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5702 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5703 | |
| 5704 | *sk = cmd->sk; |
| 5705 | sock_hold(*sk); |
| 5706 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5707 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5708 | } |
| 5709 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5710 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5711 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5712 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5713 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5714 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5715 | |
| 5716 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5717 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5718 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5719 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5720 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5721 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5722 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5723 | |
| 5724 | mgmt_pending_remove(cmd); |
| 5725 | } |
| 5726 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5727 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5728 | u8 link_type, u8 addr_type, u8 reason, |
| 5729 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5730 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5731 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5732 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5733 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5734 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5735 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5736 | if (power_off) { |
| 5737 | struct mgmt_mode *cp = power_off->param; |
| 5738 | |
| 5739 | /* The connection is still in hci_conn_hash so test for 1 |
| 5740 | * instead of 0 to know if this is the last one. |
| 5741 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5742 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5743 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5744 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5745 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5746 | } |
| 5747 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5748 | if (!mgmt_connected) |
| 5749 | return; |
| 5750 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5751 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5752 | return; |
| 5753 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5754 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5755 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5756 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5757 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5758 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5759 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5760 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5761 | |
| 5762 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 5763 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5764 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5765 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5766 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5767 | } |
| 5768 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5769 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5770 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5771 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5772 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 5773 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5774 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5775 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5776 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 5777 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 5778 | hdev); |
| 5779 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5780 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5781 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5782 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5783 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5784 | cp = cmd->param; |
| 5785 | |
| 5786 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 5787 | return; |
| 5788 | |
| 5789 | if (cp->addr.type != bdaddr_type) |
| 5790 | return; |
| 5791 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5792 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5793 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 5794 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5795 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 5796 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5797 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5798 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5799 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5800 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5801 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5802 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5803 | { |
| 5804 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5805 | struct pending_cmd *power_off; |
| 5806 | |
| 5807 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5808 | if (power_off) { |
| 5809 | struct mgmt_mode *cp = power_off->param; |
| 5810 | |
| 5811 | /* The connection is still in hci_conn_hash so test for 1 |
| 5812 | * instead of 0 to know if this is the last one. |
| 5813 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5814 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5815 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5816 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5817 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5818 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5819 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 5820 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5821 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5822 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5823 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5824 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5825 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5826 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5827 | 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] | 5828 | { |
| 5829 | struct mgmt_ev_pin_code_request ev; |
| 5830 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5831 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5832 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 5833 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5834 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5835 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5836 | } |
| 5837 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5838 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5839 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5840 | { |
| 5841 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5842 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5843 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5844 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5845 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5846 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5847 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5848 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5849 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5850 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5851 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 5852 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5853 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5854 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5855 | } |
| 5856 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5857 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5858 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5859 | { |
| 5860 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5861 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5862 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5863 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5864 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5865 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5866 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5867 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5868 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5869 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5870 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 5871 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5872 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5873 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5874 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5875 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5876 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5877 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5878 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5879 | { |
| 5880 | struct mgmt_ev_user_confirm_request ev; |
| 5881 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5882 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5883 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5884 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5885 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 5886 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5887 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5888 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5889 | 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] | 5890 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5891 | } |
| 5892 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5893 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5894 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5895 | { |
| 5896 | struct mgmt_ev_user_passkey_request ev; |
| 5897 | |
| 5898 | BT_DBG("%s", hdev->name); |
| 5899 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5900 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5901 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5902 | |
| 5903 | 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] | 5904 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5905 | } |
| 5906 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 5907 | 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] | 5908 | u8 link_type, u8 addr_type, u8 status, |
| 5909 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5910 | { |
| 5911 | struct pending_cmd *cmd; |
| 5912 | struct mgmt_rp_user_confirm_reply rp; |
| 5913 | int err; |
| 5914 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5915 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5916 | if (!cmd) |
| 5917 | return -ENOENT; |
| 5918 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5919 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5920 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5921 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5922 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5923 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5924 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5925 | |
| 5926 | return err; |
| 5927 | } |
| 5928 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5929 | 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] | 5930 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5931 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5932 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5933 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5934 | } |
| 5935 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5936 | 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] | 5937 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5938 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5939 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5940 | status, |
| 5941 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5942 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5943 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5944 | 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] | 5945 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5946 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5947 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5948 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5949 | } |
| 5950 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5951 | 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] | 5952 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5953 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5954 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5955 | status, |
| 5956 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5957 | } |
| 5958 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 5959 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5960 | u8 link_type, u8 addr_type, u32 passkey, |
| 5961 | u8 entered) |
| 5962 | { |
| 5963 | struct mgmt_ev_passkey_notify ev; |
| 5964 | |
| 5965 | BT_DBG("%s", hdev->name); |
| 5966 | |
| 5967 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5968 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5969 | ev.passkey = __cpu_to_le32(passkey); |
| 5970 | ev.entered = entered; |
| 5971 | |
| 5972 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 5973 | } |
| 5974 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5975 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5976 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5977 | { |
| 5978 | struct mgmt_ev_auth_failed ev; |
| 5979 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 5980 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5981 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5982 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5983 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5984 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5985 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5986 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5987 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5988 | { |
| 5989 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5990 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5991 | |
| 5992 | if (status) { |
| 5993 | u8 mgmt_err = mgmt_status(status); |
| 5994 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5995 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5996 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5997 | } |
| 5998 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5999 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6000 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6001 | &hdev->dev_flags); |
| 6002 | else |
| 6003 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6004 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6005 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6006 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6007 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6008 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6009 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6010 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6011 | |
| 6012 | if (match.sk) |
| 6013 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6014 | } |
| 6015 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6016 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6017 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6018 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6019 | struct hci_cp_write_eir cp; |
| 6020 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6021 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6022 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6023 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6024 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6025 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6026 | memset(&cp, 0, sizeof(cp)); |
| 6027 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6028 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6029 | } |
| 6030 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6031 | 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] | 6032 | { |
| 6033 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6034 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6035 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6036 | |
| 6037 | if (status) { |
| 6038 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6039 | |
| 6040 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6041 | &hdev->dev_flags)) { |
| 6042 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6043 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6044 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6045 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6046 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6047 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6048 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6049 | } |
| 6050 | |
| 6051 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6052 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6053 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6054 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6055 | if (!changed) |
| 6056 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6057 | &hdev->dev_flags); |
| 6058 | else |
| 6059 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6060 | } |
| 6061 | |
| 6062 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6063 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6064 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6065 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6066 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6067 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6068 | sock_put(match.sk); |
| 6069 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6070 | hci_req_init(&req, hdev); |
| 6071 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6072 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6073 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6074 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6075 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6076 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6077 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6078 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6079 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6080 | |
| 6081 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6082 | } |
| 6083 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6084 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6085 | { |
| 6086 | struct cmd_lookup match = { NULL, hdev }; |
| 6087 | bool changed = false; |
| 6088 | |
| 6089 | if (status) { |
| 6090 | u8 mgmt_err = mgmt_status(status); |
| 6091 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6092 | if (enable) { |
| 6093 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6094 | &hdev->dev_flags)) |
| 6095 | new_settings(hdev, NULL); |
| 6096 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6097 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6098 | |
| 6099 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6100 | cmd_status_rsp, &mgmt_err); |
| 6101 | return; |
| 6102 | } |
| 6103 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6104 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6105 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6106 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6107 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6108 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6109 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6110 | |
| 6111 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6112 | settings_rsp, &match); |
| 6113 | |
| 6114 | if (changed) |
| 6115 | new_settings(hdev, match.sk); |
| 6116 | |
| 6117 | if (match.sk) |
| 6118 | sock_put(match.sk); |
| 6119 | } |
| 6120 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6121 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6122 | { |
| 6123 | struct cmd_lookup *match = data; |
| 6124 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6125 | if (match->sk == NULL) { |
| 6126 | match->sk = cmd->sk; |
| 6127 | sock_hold(match->sk); |
| 6128 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6129 | } |
| 6130 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6131 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6132 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6133 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6134 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6135 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6136 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6137 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6138 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6139 | |
| 6140 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6141 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6142 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6143 | |
| 6144 | if (match.sk) |
| 6145 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6146 | } |
| 6147 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6148 | 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] | 6149 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6150 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6151 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6152 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6153 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6154 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6155 | |
| 6156 | memset(&ev, 0, sizeof(ev)); |
| 6157 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6158 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6159 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6160 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6161 | if (!cmd) { |
| 6162 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6163 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6164 | /* If this is a HCI command related to powering on the |
| 6165 | * HCI dev don't send any mgmt signals. |
| 6166 | */ |
| 6167 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6168 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6169 | } |
| 6170 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6171 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6172 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6173 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6174 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6175 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6176 | u8 *randomizer192, u8 *hash256, |
| 6177 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6178 | { |
| 6179 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6180 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6181 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6182 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6183 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6184 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6185 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6186 | |
| 6187 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6188 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6189 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6190 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6191 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6192 | hash256 && randomizer256) { |
| 6193 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6194 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6195 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6196 | memcpy(rp.randomizer192, randomizer192, |
| 6197 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6198 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6199 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6200 | memcpy(rp.randomizer256, randomizer256, |
| 6201 | sizeof(rp.randomizer256)); |
| 6202 | |
| 6203 | cmd_complete(cmd->sk, hdev->id, |
| 6204 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6205 | &rp, sizeof(rp)); |
| 6206 | } else { |
| 6207 | struct mgmt_rp_read_local_oob_data rp; |
| 6208 | |
| 6209 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6210 | memcpy(rp.randomizer, randomizer192, |
| 6211 | sizeof(rp.randomizer)); |
| 6212 | |
| 6213 | cmd_complete(cmd->sk, hdev->id, |
| 6214 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6215 | &rp, sizeof(rp)); |
| 6216 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6217 | } |
| 6218 | |
| 6219 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6220 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6221 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6222 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Johan Hedberg | 73cf71d | 2014-03-25 12:06:19 +0200 | [diff] [blame] | 6223 | u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, |
| 6224 | u8 ssp, u8 *eir, u16 eir_len, u8 *scan_rsp, |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6225 | u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6226 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6227 | char buf[512]; |
| 6228 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6229 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6230 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6231 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6232 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6233 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6234 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6235 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6236 | * are for the potential CoD field. |
| 6237 | */ |
| 6238 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6239 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6240 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6241 | memset(buf, 0, sizeof(buf)); |
| 6242 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6243 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6244 | if (irk) { |
| 6245 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6246 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6247 | } else { |
| 6248 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6249 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6250 | } |
| 6251 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6252 | ev->rssi = rssi; |
Johan Hedberg | 9a395a8 | 2012-02-23 00:00:32 +0200 | [diff] [blame] | 6253 | if (cfm_name) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 6254 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME); |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 6255 | if (!ssp) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 6256 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6257 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6258 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6259 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6260 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6261 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6262 | 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] | 6263 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6264 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6265 | if (scan_rsp_len > 0) |
| 6266 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6267 | |
| 6268 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6269 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6270 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6271 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6272 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6273 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6274 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6275 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6276 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6277 | struct mgmt_ev_device_found *ev; |
| 6278 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6279 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6280 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6281 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6282 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6283 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6284 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6285 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6286 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6287 | ev->rssi = rssi; |
| 6288 | |
| 6289 | 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] | 6290 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6291 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6292 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6293 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6294 | 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] | 6295 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6296 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6297 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6298 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6299 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6300 | struct pending_cmd *cmd; |
| 6301 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6302 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6303 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6304 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6305 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6306 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6307 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6308 | |
| 6309 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6310 | u8 type = hdev->discovery.type; |
| 6311 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6312 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6313 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6314 | mgmt_pending_remove(cmd); |
| 6315 | } |
| 6316 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6317 | memset(&ev, 0, sizeof(ev)); |
| 6318 | ev.type = hdev->discovery.type; |
| 6319 | ev.discovering = discovering; |
| 6320 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6321 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6322 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6323 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6324 | int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6325 | { |
| 6326 | struct pending_cmd *cmd; |
| 6327 | struct mgmt_ev_device_blocked ev; |
| 6328 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6329 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6330 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6331 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6332 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6333 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6334 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6335 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6336 | } |
| 6337 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6338 | int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6339 | { |
| 6340 | struct pending_cmd *cmd; |
| 6341 | struct mgmt_ev_device_unblocked ev; |
| 6342 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6343 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6344 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6345 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6346 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6347 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6348 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6349 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6350 | } |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6351 | |
| 6352 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6353 | { |
| 6354 | BT_DBG("%s status %u", hdev->name, status); |
| 6355 | |
| 6356 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6357 | if (status) { |
| 6358 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6359 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6360 | } |
| 6361 | } |
| 6362 | |
| 6363 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6364 | { |
| 6365 | struct hci_request req; |
| 6366 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6367 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6368 | return; |
| 6369 | |
| 6370 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6371 | return; |
| 6372 | |
| 6373 | hci_req_init(&req, hdev); |
| 6374 | enable_advertising(&req); |
| 6375 | |
| 6376 | /* If this fails we have no option but to let user space know |
| 6377 | * that we've disabled advertising. |
| 6378 | */ |
| 6379 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6380 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6381 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6382 | } |
| 6383 | } |