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 | 84bde9d6c | 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 | 84bde9d6c | 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 | 4ae1430 | 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 | |
Marcel Holtmann | 7c264b1 | 2014-06-30 12:34:40 +0200 | [diff] [blame] | 2950 | /* When pairing a new device, it is expected to remember |
| 2951 | * this device for future connections. Adding the connection |
| 2952 | * parameter information ahead of time allows tracking |
| 2953 | * of the slave preferred values and will speed up any |
| 2954 | * further connection establishment. |
| 2955 | * |
| 2956 | * If connection parameters already exist, then they |
| 2957 | * will be kept and this function does nothing. |
| 2958 | */ |
| 2959 | hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type); |
| 2960 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2961 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2962 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2963 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2964 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2965 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2966 | int status; |
| 2967 | |
| 2968 | if (PTR_ERR(conn) == -EBUSY) |
| 2969 | status = MGMT_STATUS_BUSY; |
| 2970 | else |
| 2971 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2972 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2973 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2974 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2975 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2976 | goto unlock; |
| 2977 | } |
| 2978 | |
| 2979 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2980 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2981 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2982 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2983 | goto unlock; |
| 2984 | } |
| 2985 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2986 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2987 | if (!cmd) { |
| 2988 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2989 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2990 | goto unlock; |
| 2991 | } |
| 2992 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2993 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2994 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2995 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2996 | conn->security_cfm_cb = pairing_complete_cb; |
| 2997 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2998 | } else { |
| 2999 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 3000 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 3001 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 3002 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 3003 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3004 | conn->io_capability = cp->io_cap; |
| 3005 | cmd->user_data = conn; |
| 3006 | |
| 3007 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3008 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3009 | pairing_complete(cmd, 0); |
| 3010 | |
| 3011 | err = 0; |
| 3012 | |
| 3013 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3014 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 3015 | return err; |
| 3016 | } |
| 3017 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3018 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3019 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3020 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3021 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3022 | struct pending_cmd *cmd; |
| 3023 | struct hci_conn *conn; |
| 3024 | int err; |
| 3025 | |
| 3026 | BT_DBG(""); |
| 3027 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3028 | hci_dev_lock(hdev); |
| 3029 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3030 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3031 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3032 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3033 | goto unlock; |
| 3034 | } |
| 3035 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3036 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3037 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3038 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3039 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3040 | goto unlock; |
| 3041 | } |
| 3042 | |
| 3043 | conn = cmd->user_data; |
| 3044 | |
| 3045 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3046 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3047 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3048 | goto unlock; |
| 3049 | } |
| 3050 | |
| 3051 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3052 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3053 | 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] | 3054 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3055 | unlock: |
| 3056 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3057 | return err; |
| 3058 | } |
| 3059 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3060 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3061 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3062 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3063 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3064 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3065 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3066 | int err; |
| 3067 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3068 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3069 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3070 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3071 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3072 | MGMT_STATUS_NOT_POWERED, addr, |
| 3073 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3074 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3075 | } |
| 3076 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3077 | if (addr->type == BDADDR_BREDR) |
| 3078 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3079 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3080 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3081 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3082 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3083 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3084 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3085 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3086 | goto done; |
| 3087 | } |
| 3088 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3089 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3090 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3091 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3092 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3093 | MGMT_STATUS_SUCCESS, addr, |
| 3094 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3095 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3096 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3097 | MGMT_STATUS_FAILED, addr, |
| 3098 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3099 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3100 | goto done; |
| 3101 | } |
| 3102 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3103 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3104 | if (!cmd) { |
| 3105 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3106 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3107 | } |
| 3108 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3109 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3110 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3111 | struct hci_cp_user_passkey_reply cp; |
| 3112 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3113 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3114 | cp.passkey = passkey; |
| 3115 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3116 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3117 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3118 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3119 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3120 | if (err < 0) |
| 3121 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3122 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3123 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3124 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3125 | return err; |
| 3126 | } |
| 3127 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3128 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3129 | void *data, u16 len) |
| 3130 | { |
| 3131 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3132 | |
| 3133 | BT_DBG(""); |
| 3134 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3135 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3136 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3137 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3138 | } |
| 3139 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3140 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3141 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3142 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3143 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3144 | |
| 3145 | BT_DBG(""); |
| 3146 | |
| 3147 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3148 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3149 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3150 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3151 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3152 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3153 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3154 | } |
| 3155 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3156 | 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] | 3157 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3158 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3159 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3160 | |
| 3161 | BT_DBG(""); |
| 3162 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3163 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3164 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3165 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3166 | } |
| 3167 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3168 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3169 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3170 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3171 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3172 | |
| 3173 | BT_DBG(""); |
| 3174 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3175 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3176 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3177 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3178 | } |
| 3179 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3180 | 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] | 3181 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3182 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3183 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3184 | |
| 3185 | BT_DBG(""); |
| 3186 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3187 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3188 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3189 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3190 | } |
| 3191 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3192 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3193 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3194 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3195 | struct hci_cp_write_local_name cp; |
| 3196 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3197 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3198 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3199 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3200 | } |
| 3201 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3202 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3203 | { |
| 3204 | struct mgmt_cp_set_local_name *cp; |
| 3205 | struct pending_cmd *cmd; |
| 3206 | |
| 3207 | BT_DBG("status 0x%02x", status); |
| 3208 | |
| 3209 | hci_dev_lock(hdev); |
| 3210 | |
| 3211 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3212 | if (!cmd) |
| 3213 | goto unlock; |
| 3214 | |
| 3215 | cp = cmd->param; |
| 3216 | |
| 3217 | if (status) |
| 3218 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3219 | mgmt_status(status)); |
| 3220 | else |
| 3221 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3222 | cp, sizeof(*cp)); |
| 3223 | |
| 3224 | mgmt_pending_remove(cmd); |
| 3225 | |
| 3226 | unlock: |
| 3227 | hci_dev_unlock(hdev); |
| 3228 | } |
| 3229 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3230 | 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] | 3231 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3232 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3233 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3234 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3235 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3236 | int err; |
| 3237 | |
| 3238 | BT_DBG(""); |
| 3239 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3240 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3241 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3242 | /* If the old values are the same as the new ones just return a |
| 3243 | * direct command complete event. |
| 3244 | */ |
| 3245 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3246 | !memcmp(hdev->short_name, cp->short_name, |
| 3247 | sizeof(hdev->short_name))) { |
| 3248 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3249 | data, len); |
| 3250 | goto failed; |
| 3251 | } |
| 3252 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3253 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
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 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3256 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3257 | |
| 3258 | 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] | 3259 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3260 | if (err < 0) |
| 3261 | goto failed; |
| 3262 | |
| 3263 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3264 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3265 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3266 | goto failed; |
| 3267 | } |
| 3268 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3269 | 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] | 3270 | if (!cmd) { |
| 3271 | err = -ENOMEM; |
| 3272 | goto failed; |
| 3273 | } |
| 3274 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3275 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3276 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3277 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3278 | |
| 3279 | if (lmp_bredr_capable(hdev)) { |
| 3280 | update_name(&req); |
| 3281 | update_eir(&req); |
| 3282 | } |
| 3283 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3284 | /* The name is stored in the scan response data and so |
| 3285 | * no need to udpate the advertising data here. |
| 3286 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3287 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3288 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3289 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3290 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3291 | if (err < 0) |
| 3292 | mgmt_pending_remove(cmd); |
| 3293 | |
| 3294 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3295 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3296 | return err; |
| 3297 | } |
| 3298 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3299 | 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] | 3300 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3301 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3302 | struct pending_cmd *cmd; |
| 3303 | int err; |
| 3304 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3305 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3306 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3307 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3308 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3309 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3310 | 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] | 3311 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3312 | goto unlock; |
| 3313 | } |
| 3314 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3315 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3316 | 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] | 3317 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3318 | goto unlock; |
| 3319 | } |
| 3320 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3321 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3322 | 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] | 3323 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3324 | goto unlock; |
| 3325 | } |
| 3326 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3327 | 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] | 3328 | if (!cmd) { |
| 3329 | err = -ENOMEM; |
| 3330 | goto unlock; |
| 3331 | } |
| 3332 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3333 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3334 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3335 | 0, NULL); |
| 3336 | else |
| 3337 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3338 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3339 | if (err < 0) |
| 3340 | mgmt_pending_remove(cmd); |
| 3341 | |
| 3342 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3343 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3344 | return err; |
| 3345 | } |
| 3346 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3347 | 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] | 3348 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3349 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3350 | int err; |
| 3351 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3352 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3353 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3354 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3355 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3356 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3357 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3358 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3359 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3360 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3361 | cp->hash, cp->randomizer); |
| 3362 | if (err < 0) |
| 3363 | status = MGMT_STATUS_FAILED; |
| 3364 | else |
| 3365 | status = MGMT_STATUS_SUCCESS; |
| 3366 | |
| 3367 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3368 | status, &cp->addr, sizeof(cp->addr)); |
| 3369 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3370 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3371 | u8 status; |
| 3372 | |
| 3373 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3374 | cp->hash192, |
| 3375 | cp->randomizer192, |
| 3376 | cp->hash256, |
| 3377 | cp->randomizer256); |
| 3378 | if (err < 0) |
| 3379 | status = MGMT_STATUS_FAILED; |
| 3380 | else |
| 3381 | status = MGMT_STATUS_SUCCESS; |
| 3382 | |
| 3383 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3384 | status, &cp->addr, sizeof(cp->addr)); |
| 3385 | } else { |
| 3386 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3387 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3388 | MGMT_STATUS_INVALID_PARAMS); |
| 3389 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3390 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3391 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3392 | return err; |
| 3393 | } |
| 3394 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3395 | 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] | 3396 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3397 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3398 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3399 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3400 | int err; |
| 3401 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3402 | BT_DBG("%s", hdev->name); |
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_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3405 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3406 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3407 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3408 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3409 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3410 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3411 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3412 | 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] | 3413 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3414 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3415 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3416 | return err; |
| 3417 | } |
| 3418 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3419 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3420 | { |
| 3421 | struct pending_cmd *cmd; |
| 3422 | u8 type; |
| 3423 | int err; |
| 3424 | |
| 3425 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3426 | |
| 3427 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3428 | if (!cmd) |
| 3429 | return -ENOENT; |
| 3430 | |
| 3431 | type = hdev->discovery.type; |
| 3432 | |
| 3433 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3434 | &type, sizeof(type)); |
| 3435 | mgmt_pending_remove(cmd); |
| 3436 | |
| 3437 | return err; |
| 3438 | } |
| 3439 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3440 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3441 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3442 | unsigned long timeout = 0; |
| 3443 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3444 | BT_DBG("status %d", status); |
| 3445 | |
| 3446 | if (status) { |
| 3447 | hci_dev_lock(hdev); |
| 3448 | mgmt_start_discovery_failed(hdev, status); |
| 3449 | hci_dev_unlock(hdev); |
| 3450 | return; |
| 3451 | } |
| 3452 | |
| 3453 | hci_dev_lock(hdev); |
| 3454 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3455 | hci_dev_unlock(hdev); |
| 3456 | |
| 3457 | switch (hdev->discovery.type) { |
| 3458 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3459 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3460 | break; |
| 3461 | |
| 3462 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3463 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3464 | break; |
| 3465 | |
| 3466 | case DISCOV_TYPE_BREDR: |
| 3467 | break; |
| 3468 | |
| 3469 | default: |
| 3470 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3471 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3472 | |
| 3473 | if (!timeout) |
| 3474 | return; |
| 3475 | |
| 3476 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3477 | } |
| 3478 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3479 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3480 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3481 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3482 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3483 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3484 | struct hci_cp_le_set_scan_param param_cp; |
| 3485 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3486 | struct hci_cp_inquiry inq_cp; |
| 3487 | struct hci_request req; |
| 3488 | /* General inquiry access code (GIAC) */ |
| 3489 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3490 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3491 | int err; |
| 3492 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3493 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3494 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3495 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3496 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3497 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3498 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3499 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3500 | goto failed; |
| 3501 | } |
| 3502 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3503 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3504 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3505 | MGMT_STATUS_BUSY); |
| 3506 | goto failed; |
| 3507 | } |
| 3508 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3509 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3510 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3511 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3512 | goto failed; |
| 3513 | } |
| 3514 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3515 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3516 | if (!cmd) { |
| 3517 | err = -ENOMEM; |
| 3518 | goto failed; |
| 3519 | } |
| 3520 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3521 | hdev->discovery.type = cp->type; |
| 3522 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3523 | hci_req_init(&req, hdev); |
| 3524 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3525 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3526 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3527 | status = mgmt_bredr_support(hdev); |
| 3528 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3529 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3530 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3531 | mgmt_pending_remove(cmd); |
| 3532 | goto failed; |
| 3533 | } |
| 3534 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3535 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3536 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3537 | MGMT_STATUS_BUSY); |
| 3538 | mgmt_pending_remove(cmd); |
| 3539 | goto failed; |
| 3540 | } |
| 3541 | |
| 3542 | hci_inquiry_cache_flush(hdev); |
| 3543 | |
| 3544 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3545 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3546 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3547 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3548 | break; |
| 3549 | |
| 3550 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3551 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3552 | status = mgmt_le_support(hdev); |
| 3553 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3554 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3555 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3556 | mgmt_pending_remove(cmd); |
| 3557 | goto failed; |
| 3558 | } |
| 3559 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3560 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3561 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3562 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3563 | MGMT_STATUS_NOT_SUPPORTED); |
| 3564 | mgmt_pending_remove(cmd); |
| 3565 | goto failed; |
| 3566 | } |
| 3567 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3568 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3569 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3570 | MGMT_STATUS_REJECTED); |
| 3571 | mgmt_pending_remove(cmd); |
| 3572 | goto failed; |
| 3573 | } |
| 3574 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3575 | /* If controller is scanning, it means the background scanning |
| 3576 | * is running. Thus, we should temporarily stop it in order to |
| 3577 | * set the discovery scanning parameters. |
| 3578 | */ |
| 3579 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3580 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3581 | |
| 3582 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3583 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3584 | /* All active scans will be done with either a resolvable |
| 3585 | * private address (when privacy feature has been enabled) |
| 3586 | * or unresolvable private address. |
| 3587 | */ |
| 3588 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3589 | if (err < 0) { |
| 3590 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3591 | MGMT_STATUS_FAILED); |
| 3592 | mgmt_pending_remove(cmd); |
| 3593 | goto failed; |
| 3594 | } |
| 3595 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3596 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3597 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3598 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3599 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3600 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3601 | ¶m_cp); |
| 3602 | |
| 3603 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3604 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3605 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3606 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3607 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3608 | break; |
| 3609 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3610 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3611 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3612 | MGMT_STATUS_INVALID_PARAMS); |
| 3613 | mgmt_pending_remove(cmd); |
| 3614 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3615 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3616 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3617 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3618 | if (err < 0) |
| 3619 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3620 | else |
| 3621 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3622 | |
| 3623 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3624 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3625 | return err; |
| 3626 | } |
| 3627 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3628 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3629 | { |
| 3630 | struct pending_cmd *cmd; |
| 3631 | int err; |
| 3632 | |
| 3633 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3634 | if (!cmd) |
| 3635 | return -ENOENT; |
| 3636 | |
| 3637 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3638 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3639 | mgmt_pending_remove(cmd); |
| 3640 | |
| 3641 | return err; |
| 3642 | } |
| 3643 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3644 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3645 | { |
| 3646 | BT_DBG("status %d", status); |
| 3647 | |
| 3648 | hci_dev_lock(hdev); |
| 3649 | |
| 3650 | if (status) { |
| 3651 | mgmt_stop_discovery_failed(hdev, status); |
| 3652 | goto unlock; |
| 3653 | } |
| 3654 | |
| 3655 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3656 | |
| 3657 | unlock: |
| 3658 | hci_dev_unlock(hdev); |
| 3659 | } |
| 3660 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3661 | 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] | 3662 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3663 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3664 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3665 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3666 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3667 | int err; |
| 3668 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3669 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3670 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3671 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3672 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3673 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3674 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3675 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3676 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3677 | goto unlock; |
| 3678 | } |
| 3679 | |
| 3680 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3681 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3682 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3683 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3684 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3685 | } |
| 3686 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3687 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3688 | if (!cmd) { |
| 3689 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3690 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3691 | } |
| 3692 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3693 | hci_req_init(&req, hdev); |
| 3694 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3695 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3696 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3697 | err = hci_req_run(&req, stop_discovery_complete); |
| 3698 | if (!err) { |
| 3699 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3700 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3701 | } |
| 3702 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3703 | mgmt_pending_remove(cmd); |
| 3704 | |
| 3705 | /* If no HCI commands were sent we're done */ |
| 3706 | if (err == -ENODATA) { |
| 3707 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3708 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3709 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3710 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3711 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3712 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3713 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3714 | return err; |
| 3715 | } |
| 3716 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3717 | 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] | 3718 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3719 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3720 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3721 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3722 | int err; |
| 3723 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3724 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3725 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3726 | hci_dev_lock(hdev); |
| 3727 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3728 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3729 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3730 | MGMT_STATUS_FAILED, &cp->addr, |
| 3731 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3732 | goto failed; |
| 3733 | } |
| 3734 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3735 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3736 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3737 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3738 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3739 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3740 | goto failed; |
| 3741 | } |
| 3742 | |
| 3743 | if (cp->name_known) { |
| 3744 | e->name_state = NAME_KNOWN; |
| 3745 | list_del(&e->list); |
| 3746 | } else { |
| 3747 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20 | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3748 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3749 | } |
| 3750 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3751 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3752 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3753 | |
| 3754 | failed: |
| 3755 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3756 | return err; |
| 3757 | } |
| 3758 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3759 | 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] | 3760 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3761 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3762 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3763 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3764 | int err; |
| 3765 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3766 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3767 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3768 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3769 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3770 | MGMT_STATUS_INVALID_PARAMS, |
| 3771 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3772 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3773 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3774 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3775 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame^] | 3776 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3777 | status = MGMT_STATUS_FAILED; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame^] | 3778 | goto done; |
| 3779 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3780 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame^] | 3781 | mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3782 | sk); |
| 3783 | status = MGMT_STATUS_SUCCESS; |
| 3784 | |
| 3785 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3786 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3787 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3788 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3789 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3790 | |
| 3791 | return err; |
| 3792 | } |
| 3793 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3794 | 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] | 3795 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3796 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3797 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3798 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3799 | int err; |
| 3800 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3801 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3802 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3803 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3804 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3805 | MGMT_STATUS_INVALID_PARAMS, |
| 3806 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3807 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3808 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3809 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3810 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame^] | 3811 | if (err < 0) { |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3812 | status = MGMT_STATUS_INVALID_PARAMS; |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame^] | 3813 | goto done; |
| 3814 | } |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3815 | |
Johan Hedberg | 2a8357f | 2014-07-01 22:09:47 +0300 | [diff] [blame^] | 3816 | mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr), |
| 3817 | sk); |
| 3818 | status = MGMT_STATUS_SUCCESS; |
| 3819 | |
| 3820 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3821 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3822 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3823 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3824 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3825 | |
| 3826 | return err; |
| 3827 | } |
| 3828 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3829 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3830 | u16 len) |
| 3831 | { |
| 3832 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3833 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3834 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3835 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3836 | |
| 3837 | BT_DBG("%s", hdev->name); |
| 3838 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3839 | source = __le16_to_cpu(cp->source); |
| 3840 | |
| 3841 | if (source > 0x0002) |
| 3842 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3843 | MGMT_STATUS_INVALID_PARAMS); |
| 3844 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3845 | hci_dev_lock(hdev); |
| 3846 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3847 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3848 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3849 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3850 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3851 | |
| 3852 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3853 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3854 | hci_req_init(&req, hdev); |
| 3855 | update_eir(&req); |
| 3856 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3857 | |
| 3858 | hci_dev_unlock(hdev); |
| 3859 | |
| 3860 | return err; |
| 3861 | } |
| 3862 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3863 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3864 | { |
| 3865 | struct cmd_lookup match = { NULL, hdev }; |
| 3866 | |
| 3867 | if (status) { |
| 3868 | u8 mgmt_err = mgmt_status(status); |
| 3869 | |
| 3870 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3871 | cmd_status_rsp, &mgmt_err); |
| 3872 | return; |
| 3873 | } |
| 3874 | |
| 3875 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3876 | &match); |
| 3877 | |
| 3878 | new_settings(hdev, match.sk); |
| 3879 | |
| 3880 | if (match.sk) |
| 3881 | sock_put(match.sk); |
| 3882 | } |
| 3883 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3884 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3885 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3886 | { |
| 3887 | struct mgmt_mode *cp = data; |
| 3888 | struct pending_cmd *cmd; |
| 3889 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3890 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3891 | int err; |
| 3892 | |
| 3893 | BT_DBG("request for %s", hdev->name); |
| 3894 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3895 | status = mgmt_le_support(hdev); |
| 3896 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3897 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3898 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3899 | |
| 3900 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3901 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3902 | MGMT_STATUS_INVALID_PARAMS); |
| 3903 | |
| 3904 | hci_dev_lock(hdev); |
| 3905 | |
| 3906 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3907 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3908 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3909 | /* The following conditions are ones which mean that we should |
| 3910 | * not do any HCI communication but directly send a mgmt |
| 3911 | * response to user space (after toggling the flag if |
| 3912 | * necessary). |
| 3913 | */ |
| 3914 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3915 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3916 | bool changed = false; |
| 3917 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3918 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3919 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3920 | changed = true; |
| 3921 | } |
| 3922 | |
| 3923 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3924 | if (err < 0) |
| 3925 | goto unlock; |
| 3926 | |
| 3927 | if (changed) |
| 3928 | err = new_settings(hdev, sk); |
| 3929 | |
| 3930 | goto unlock; |
| 3931 | } |
| 3932 | |
| 3933 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3934 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3935 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3936 | MGMT_STATUS_BUSY); |
| 3937 | goto unlock; |
| 3938 | } |
| 3939 | |
| 3940 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3941 | if (!cmd) { |
| 3942 | err = -ENOMEM; |
| 3943 | goto unlock; |
| 3944 | } |
| 3945 | |
| 3946 | hci_req_init(&req, hdev); |
| 3947 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3948 | if (val) |
| 3949 | enable_advertising(&req); |
| 3950 | else |
| 3951 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3952 | |
| 3953 | err = hci_req_run(&req, set_advertising_complete); |
| 3954 | if (err < 0) |
| 3955 | mgmt_pending_remove(cmd); |
| 3956 | |
| 3957 | unlock: |
| 3958 | hci_dev_unlock(hdev); |
| 3959 | return err; |
| 3960 | } |
| 3961 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3962 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3963 | void *data, u16 len) |
| 3964 | { |
| 3965 | struct mgmt_cp_set_static_address *cp = data; |
| 3966 | int err; |
| 3967 | |
| 3968 | BT_DBG("%s", hdev->name); |
| 3969 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3970 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3971 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3972 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3973 | |
| 3974 | if (hdev_is_powered(hdev)) |
| 3975 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3976 | MGMT_STATUS_REJECTED); |
| 3977 | |
| 3978 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3979 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3980 | return cmd_status(sk, hdev->id, |
| 3981 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3982 | MGMT_STATUS_INVALID_PARAMS); |
| 3983 | |
| 3984 | /* Two most significant bits shall be set */ |
| 3985 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3986 | return cmd_status(sk, hdev->id, |
| 3987 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3988 | MGMT_STATUS_INVALID_PARAMS); |
| 3989 | } |
| 3990 | |
| 3991 | hci_dev_lock(hdev); |
| 3992 | |
| 3993 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3994 | |
| 3995 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3996 | |
| 3997 | hci_dev_unlock(hdev); |
| 3998 | |
| 3999 | return err; |
| 4000 | } |
| 4001 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4002 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 4003 | void *data, u16 len) |
| 4004 | { |
| 4005 | struct mgmt_cp_set_scan_params *cp = data; |
| 4006 | __u16 interval, window; |
| 4007 | int err; |
| 4008 | |
| 4009 | BT_DBG("%s", hdev->name); |
| 4010 | |
| 4011 | if (!lmp_le_capable(hdev)) |
| 4012 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4013 | MGMT_STATUS_NOT_SUPPORTED); |
| 4014 | |
| 4015 | interval = __le16_to_cpu(cp->interval); |
| 4016 | |
| 4017 | if (interval < 0x0004 || interval > 0x4000) |
| 4018 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4019 | MGMT_STATUS_INVALID_PARAMS); |
| 4020 | |
| 4021 | window = __le16_to_cpu(cp->window); |
| 4022 | |
| 4023 | if (window < 0x0004 || window > 0x4000) |
| 4024 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4025 | MGMT_STATUS_INVALID_PARAMS); |
| 4026 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 4027 | if (window > interval) |
| 4028 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4029 | MGMT_STATUS_INVALID_PARAMS); |
| 4030 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4031 | hci_dev_lock(hdev); |
| 4032 | |
| 4033 | hdev->le_scan_interval = interval; |
| 4034 | hdev->le_scan_window = window; |
| 4035 | |
| 4036 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4037 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4038 | /* If background scan is running, restart it so new parameters are |
| 4039 | * loaded. |
| 4040 | */ |
| 4041 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4042 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4043 | struct hci_request req; |
| 4044 | |
| 4045 | hci_req_init(&req, hdev); |
| 4046 | |
| 4047 | hci_req_add_le_scan_disable(&req); |
| 4048 | hci_req_add_le_passive_scan(&req); |
| 4049 | |
| 4050 | hci_req_run(&req, NULL); |
| 4051 | } |
| 4052 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4053 | hci_dev_unlock(hdev); |
| 4054 | |
| 4055 | return err; |
| 4056 | } |
| 4057 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4058 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4059 | { |
| 4060 | struct pending_cmd *cmd; |
| 4061 | |
| 4062 | BT_DBG("status 0x%02x", status); |
| 4063 | |
| 4064 | hci_dev_lock(hdev); |
| 4065 | |
| 4066 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4067 | if (!cmd) |
| 4068 | goto unlock; |
| 4069 | |
| 4070 | if (status) { |
| 4071 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4072 | mgmt_status(status)); |
| 4073 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4074 | struct mgmt_mode *cp = cmd->param; |
| 4075 | |
| 4076 | if (cp->val) |
| 4077 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4078 | else |
| 4079 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4080 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4081 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4082 | new_settings(hdev, cmd->sk); |
| 4083 | } |
| 4084 | |
| 4085 | mgmt_pending_remove(cmd); |
| 4086 | |
| 4087 | unlock: |
| 4088 | hci_dev_unlock(hdev); |
| 4089 | } |
| 4090 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4091 | 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] | 4092 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4093 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4094 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4095 | struct pending_cmd *cmd; |
| 4096 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4097 | int err; |
| 4098 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4099 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4100 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4101 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4102 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4103 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4104 | MGMT_STATUS_NOT_SUPPORTED); |
| 4105 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4106 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4107 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4108 | MGMT_STATUS_INVALID_PARAMS); |
| 4109 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4110 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4111 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4112 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4113 | |
| 4114 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4115 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4116 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4117 | |
| 4118 | hci_dev_lock(hdev); |
| 4119 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4120 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4121 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4122 | MGMT_STATUS_BUSY); |
| 4123 | goto unlock; |
| 4124 | } |
| 4125 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4126 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4127 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4128 | hdev); |
| 4129 | goto unlock; |
| 4130 | } |
| 4131 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4132 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4133 | data, len); |
| 4134 | if (!cmd) { |
| 4135 | err = -ENOMEM; |
| 4136 | goto unlock; |
| 4137 | } |
| 4138 | |
| 4139 | hci_req_init(&req, hdev); |
| 4140 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4141 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4142 | |
| 4143 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4144 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4145 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4146 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4147 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4148 | } |
| 4149 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4150 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4151 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4152 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4153 | return err; |
| 4154 | } |
| 4155 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4156 | static void set_bredr_scan(struct hci_request *req) |
| 4157 | { |
| 4158 | struct hci_dev *hdev = req->hdev; |
| 4159 | u8 scan = 0; |
| 4160 | |
| 4161 | /* Ensure that fast connectable is disabled. This function will |
| 4162 | * not do anything if the page scan parameters are already what |
| 4163 | * they should be. |
| 4164 | */ |
| 4165 | write_fast_connectable(req, false); |
| 4166 | |
| 4167 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4168 | scan |= SCAN_PAGE; |
| 4169 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4170 | scan |= SCAN_INQUIRY; |
| 4171 | |
| 4172 | if (scan) |
| 4173 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4174 | } |
| 4175 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4176 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4177 | { |
| 4178 | struct pending_cmd *cmd; |
| 4179 | |
| 4180 | BT_DBG("status 0x%02x", status); |
| 4181 | |
| 4182 | hci_dev_lock(hdev); |
| 4183 | |
| 4184 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4185 | if (!cmd) |
| 4186 | goto unlock; |
| 4187 | |
| 4188 | if (status) { |
| 4189 | u8 mgmt_err = mgmt_status(status); |
| 4190 | |
| 4191 | /* We need to restore the flag if related HCI commands |
| 4192 | * failed. |
| 4193 | */ |
| 4194 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4195 | |
| 4196 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4197 | } else { |
| 4198 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4199 | new_settings(hdev, cmd->sk); |
| 4200 | } |
| 4201 | |
| 4202 | mgmt_pending_remove(cmd); |
| 4203 | |
| 4204 | unlock: |
| 4205 | hci_dev_unlock(hdev); |
| 4206 | } |
| 4207 | |
| 4208 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4209 | { |
| 4210 | struct mgmt_mode *cp = data; |
| 4211 | struct pending_cmd *cmd; |
| 4212 | struct hci_request req; |
| 4213 | int err; |
| 4214 | |
| 4215 | BT_DBG("request for %s", hdev->name); |
| 4216 | |
| 4217 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4218 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4219 | MGMT_STATUS_NOT_SUPPORTED); |
| 4220 | |
| 4221 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4222 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4223 | MGMT_STATUS_REJECTED); |
| 4224 | |
| 4225 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4226 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4227 | MGMT_STATUS_INVALID_PARAMS); |
| 4228 | |
| 4229 | hci_dev_lock(hdev); |
| 4230 | |
| 4231 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4232 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4233 | goto unlock; |
| 4234 | } |
| 4235 | |
| 4236 | if (!hdev_is_powered(hdev)) { |
| 4237 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4238 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4239 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4240 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4241 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4242 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4243 | } |
| 4244 | |
| 4245 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4246 | |
| 4247 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4248 | if (err < 0) |
| 4249 | goto unlock; |
| 4250 | |
| 4251 | err = new_settings(hdev, sk); |
| 4252 | goto unlock; |
| 4253 | } |
| 4254 | |
| 4255 | /* Reject disabling when powered on */ |
| 4256 | if (!cp->val) { |
| 4257 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4258 | MGMT_STATUS_REJECTED); |
| 4259 | goto unlock; |
| 4260 | } |
| 4261 | |
| 4262 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4263 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4264 | MGMT_STATUS_BUSY); |
| 4265 | goto unlock; |
| 4266 | } |
| 4267 | |
| 4268 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4269 | if (!cmd) { |
| 4270 | err = -ENOMEM; |
| 4271 | goto unlock; |
| 4272 | } |
| 4273 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4274 | /* 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] | 4275 | * generates the correct flags. |
| 4276 | */ |
| 4277 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4278 | |
| 4279 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4280 | |
| 4281 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4282 | set_bredr_scan(&req); |
| 4283 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4284 | /* Since only the advertising data flags will change, there |
| 4285 | * is no need to update the scan response data. |
| 4286 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4287 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4288 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4289 | err = hci_req_run(&req, set_bredr_complete); |
| 4290 | if (err < 0) |
| 4291 | mgmt_pending_remove(cmd); |
| 4292 | |
| 4293 | unlock: |
| 4294 | hci_dev_unlock(hdev); |
| 4295 | return err; |
| 4296 | } |
| 4297 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4298 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4299 | void *data, u16 len) |
| 4300 | { |
| 4301 | struct mgmt_mode *cp = data; |
| 4302 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4303 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4304 | int err; |
| 4305 | |
| 4306 | BT_DBG("request for %s", hdev->name); |
| 4307 | |
| 4308 | status = mgmt_bredr_support(hdev); |
| 4309 | if (status) |
| 4310 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4311 | status); |
| 4312 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4313 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4314 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4315 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4316 | MGMT_STATUS_NOT_SUPPORTED); |
| 4317 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4318 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4319 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4320 | MGMT_STATUS_INVALID_PARAMS); |
| 4321 | |
| 4322 | hci_dev_lock(hdev); |
| 4323 | |
| 4324 | if (!hdev_is_powered(hdev)) { |
| 4325 | bool changed; |
| 4326 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4327 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4328 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4329 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4330 | if (cp->val == 0x02) |
| 4331 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4332 | else |
| 4333 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4334 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4335 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4336 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4337 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4338 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4339 | |
| 4340 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4341 | if (err < 0) |
| 4342 | goto failed; |
| 4343 | |
| 4344 | if (changed) |
| 4345 | err = new_settings(hdev, sk); |
| 4346 | |
| 4347 | goto failed; |
| 4348 | } |
| 4349 | |
| 4350 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4351 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4352 | MGMT_STATUS_BUSY); |
| 4353 | goto failed; |
| 4354 | } |
| 4355 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4356 | val = !!cp->val; |
| 4357 | |
| 4358 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4359 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4360 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4361 | goto failed; |
| 4362 | } |
| 4363 | |
| 4364 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4365 | if (!cmd) { |
| 4366 | err = -ENOMEM; |
| 4367 | goto failed; |
| 4368 | } |
| 4369 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4370 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4371 | if (err < 0) { |
| 4372 | mgmt_pending_remove(cmd); |
| 4373 | goto failed; |
| 4374 | } |
| 4375 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4376 | if (cp->val == 0x02) |
| 4377 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4378 | else |
| 4379 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4380 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4381 | failed: |
| 4382 | hci_dev_unlock(hdev); |
| 4383 | return err; |
| 4384 | } |
| 4385 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4386 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4387 | void *data, u16 len) |
| 4388 | { |
| 4389 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4390 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4391 | int err; |
| 4392 | |
| 4393 | BT_DBG("request for %s", hdev->name); |
| 4394 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4395 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4396 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4397 | MGMT_STATUS_INVALID_PARAMS); |
| 4398 | |
| 4399 | hci_dev_lock(hdev); |
| 4400 | |
| 4401 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4402 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4403 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4404 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4405 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4406 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4407 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4408 | if (cp->val == 0x02) |
| 4409 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4410 | &hdev->dev_flags); |
| 4411 | else |
| 4412 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4413 | &hdev->dev_flags); |
| 4414 | |
| 4415 | if (hdev_is_powered(hdev) && use_changed && |
| 4416 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4417 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4418 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4419 | sizeof(mode), &mode); |
| 4420 | } |
| 4421 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4422 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4423 | if (err < 0) |
| 4424 | goto unlock; |
| 4425 | |
| 4426 | if (changed) |
| 4427 | err = new_settings(hdev, sk); |
| 4428 | |
| 4429 | unlock: |
| 4430 | hci_dev_unlock(hdev); |
| 4431 | return err; |
| 4432 | } |
| 4433 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4434 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4435 | u16 len) |
| 4436 | { |
| 4437 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4438 | bool changed; |
| 4439 | int err; |
| 4440 | |
| 4441 | BT_DBG("request for %s", hdev->name); |
| 4442 | |
| 4443 | if (!lmp_le_capable(hdev)) |
| 4444 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4445 | MGMT_STATUS_NOT_SUPPORTED); |
| 4446 | |
| 4447 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4448 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4449 | MGMT_STATUS_INVALID_PARAMS); |
| 4450 | |
| 4451 | if (hdev_is_powered(hdev)) |
| 4452 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4453 | MGMT_STATUS_REJECTED); |
| 4454 | |
| 4455 | hci_dev_lock(hdev); |
| 4456 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4457 | /* If user space supports this command it is also expected to |
| 4458 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4459 | */ |
| 4460 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4461 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4462 | if (cp->privacy) { |
| 4463 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4464 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4465 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4466 | } else { |
| 4467 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4468 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4469 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4470 | } |
| 4471 | |
| 4472 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4473 | if (err < 0) |
| 4474 | goto unlock; |
| 4475 | |
| 4476 | if (changed) |
| 4477 | err = new_settings(hdev, sk); |
| 4478 | |
| 4479 | unlock: |
| 4480 | hci_dev_unlock(hdev); |
| 4481 | return err; |
| 4482 | } |
| 4483 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4484 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4485 | { |
| 4486 | switch (irk->addr.type) { |
| 4487 | case BDADDR_LE_PUBLIC: |
| 4488 | return true; |
| 4489 | |
| 4490 | case BDADDR_LE_RANDOM: |
| 4491 | /* Two most significant bits shall be set */ |
| 4492 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4493 | return false; |
| 4494 | return true; |
| 4495 | } |
| 4496 | |
| 4497 | return false; |
| 4498 | } |
| 4499 | |
| 4500 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4501 | u16 len) |
| 4502 | { |
| 4503 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4504 | u16 irk_count, expected_len; |
| 4505 | int i, err; |
| 4506 | |
| 4507 | BT_DBG("request for %s", hdev->name); |
| 4508 | |
| 4509 | if (!lmp_le_capable(hdev)) |
| 4510 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4511 | MGMT_STATUS_NOT_SUPPORTED); |
| 4512 | |
| 4513 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4514 | |
| 4515 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4516 | if (expected_len != len) { |
| 4517 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4518 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4519 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4520 | MGMT_STATUS_INVALID_PARAMS); |
| 4521 | } |
| 4522 | |
| 4523 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4524 | |
| 4525 | for (i = 0; i < irk_count; i++) { |
| 4526 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4527 | |
| 4528 | if (!irk_is_valid(key)) |
| 4529 | return cmd_status(sk, hdev->id, |
| 4530 | MGMT_OP_LOAD_IRKS, |
| 4531 | MGMT_STATUS_INVALID_PARAMS); |
| 4532 | } |
| 4533 | |
| 4534 | hci_dev_lock(hdev); |
| 4535 | |
| 4536 | hci_smp_irks_clear(hdev); |
| 4537 | |
| 4538 | for (i = 0; i < irk_count; i++) { |
| 4539 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4540 | u8 addr_type; |
| 4541 | |
| 4542 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4543 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4544 | else |
| 4545 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4546 | |
| 4547 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4548 | BDADDR_ANY); |
| 4549 | } |
| 4550 | |
| 4551 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4552 | |
| 4553 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4554 | |
| 4555 | hci_dev_unlock(hdev); |
| 4556 | |
| 4557 | return err; |
| 4558 | } |
| 4559 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4560 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4561 | { |
| 4562 | if (key->master != 0x00 && key->master != 0x01) |
| 4563 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4564 | |
| 4565 | switch (key->addr.type) { |
| 4566 | case BDADDR_LE_PUBLIC: |
| 4567 | return true; |
| 4568 | |
| 4569 | case BDADDR_LE_RANDOM: |
| 4570 | /* Two most significant bits shall be set */ |
| 4571 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4572 | return false; |
| 4573 | return true; |
| 4574 | } |
| 4575 | |
| 4576 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4577 | } |
| 4578 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4579 | 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] | 4580 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4581 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4582 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4583 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4584 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4585 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4586 | BT_DBG("request for %s", hdev->name); |
| 4587 | |
| 4588 | if (!lmp_le_capable(hdev)) |
| 4589 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4590 | MGMT_STATUS_NOT_SUPPORTED); |
| 4591 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4592 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4593 | |
| 4594 | expected_len = sizeof(*cp) + key_count * |
| 4595 | sizeof(struct mgmt_ltk_info); |
| 4596 | if (expected_len != len) { |
| 4597 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4598 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4599 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4600 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4601 | } |
| 4602 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4603 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4604 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4605 | for (i = 0; i < key_count; i++) { |
| 4606 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4607 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4608 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4609 | return cmd_status(sk, hdev->id, |
| 4610 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4611 | MGMT_STATUS_INVALID_PARAMS); |
| 4612 | } |
| 4613 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4614 | hci_dev_lock(hdev); |
| 4615 | |
| 4616 | hci_smp_ltks_clear(hdev); |
| 4617 | |
| 4618 | for (i = 0; i < key_count; i++) { |
| 4619 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4620 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4621 | |
| 4622 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4623 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4624 | else |
| 4625 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4626 | |
| 4627 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4628 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4629 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4630 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4631 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4632 | switch (key->type) { |
| 4633 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4634 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4635 | break; |
| 4636 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4637 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4638 | break; |
| 4639 | default: |
| 4640 | continue; |
| 4641 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4642 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4643 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4644 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4645 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4646 | } |
| 4647 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4648 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4649 | NULL, 0); |
| 4650 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4651 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4652 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4653 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4654 | } |
| 4655 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4656 | struct cmd_conn_lookup { |
| 4657 | struct hci_conn *conn; |
| 4658 | bool valid_tx_power; |
| 4659 | u8 mgmt_status; |
| 4660 | }; |
| 4661 | |
| 4662 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4663 | { |
| 4664 | struct cmd_conn_lookup *match = data; |
| 4665 | struct mgmt_cp_get_conn_info *cp; |
| 4666 | struct mgmt_rp_get_conn_info rp; |
| 4667 | struct hci_conn *conn = cmd->user_data; |
| 4668 | |
| 4669 | if (conn != match->conn) |
| 4670 | return; |
| 4671 | |
| 4672 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4673 | |
| 4674 | memset(&rp, 0, sizeof(rp)); |
| 4675 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4676 | rp.addr.type = cp->addr.type; |
| 4677 | |
| 4678 | if (!match->mgmt_status) { |
| 4679 | rp.rssi = conn->rssi; |
| 4680 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4681 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4682 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4683 | rp.max_tx_power = conn->max_tx_power; |
| 4684 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4685 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4686 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4687 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4688 | } |
| 4689 | |
| 4690 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4691 | match->mgmt_status, &rp, sizeof(rp)); |
| 4692 | |
| 4693 | hci_conn_drop(conn); |
| 4694 | |
| 4695 | mgmt_pending_remove(cmd); |
| 4696 | } |
| 4697 | |
| 4698 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4699 | { |
| 4700 | struct hci_cp_read_rssi *cp; |
| 4701 | struct hci_conn *conn; |
| 4702 | struct cmd_conn_lookup match; |
| 4703 | u16 handle; |
| 4704 | |
| 4705 | BT_DBG("status 0x%02x", status); |
| 4706 | |
| 4707 | hci_dev_lock(hdev); |
| 4708 | |
| 4709 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4710 | * otherwise we assume it's not valid. At the moment we assume that |
| 4711 | * either both or none of current and max values are valid to keep code |
| 4712 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4713 | */ |
| 4714 | match.valid_tx_power = !status; |
| 4715 | |
| 4716 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4717 | * Level so we check which one was last sent to retrieve connection |
| 4718 | * handle. Both commands have handle as first parameter so it's safe to |
| 4719 | * cast data on the same command struct. |
| 4720 | * |
| 4721 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4722 | * In other case we simply override error to indicate success as we |
| 4723 | * already remembered if TX power value is actually valid. |
| 4724 | */ |
| 4725 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4726 | if (!cp) { |
| 4727 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4728 | status = 0; |
| 4729 | } |
| 4730 | |
| 4731 | if (!cp) { |
| 4732 | BT_ERR("invalid sent_cmd in response"); |
| 4733 | goto unlock; |
| 4734 | } |
| 4735 | |
| 4736 | handle = __le16_to_cpu(cp->handle); |
| 4737 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4738 | if (!conn) { |
| 4739 | BT_ERR("unknown handle (%d) in response", handle); |
| 4740 | goto unlock; |
| 4741 | } |
| 4742 | |
| 4743 | match.conn = conn; |
| 4744 | match.mgmt_status = mgmt_status(status); |
| 4745 | |
| 4746 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4747 | * connection only. |
| 4748 | */ |
| 4749 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4750 | get_conn_info_complete, &match); |
| 4751 | |
| 4752 | unlock: |
| 4753 | hci_dev_unlock(hdev); |
| 4754 | } |
| 4755 | |
| 4756 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4757 | u16 len) |
| 4758 | { |
| 4759 | struct mgmt_cp_get_conn_info *cp = data; |
| 4760 | struct mgmt_rp_get_conn_info rp; |
| 4761 | struct hci_conn *conn; |
| 4762 | unsigned long conn_info_age; |
| 4763 | int err = 0; |
| 4764 | |
| 4765 | BT_DBG("%s", hdev->name); |
| 4766 | |
| 4767 | memset(&rp, 0, sizeof(rp)); |
| 4768 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4769 | rp.addr.type = cp->addr.type; |
| 4770 | |
| 4771 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4772 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4773 | MGMT_STATUS_INVALID_PARAMS, |
| 4774 | &rp, sizeof(rp)); |
| 4775 | |
| 4776 | hci_dev_lock(hdev); |
| 4777 | |
| 4778 | if (!hdev_is_powered(hdev)) { |
| 4779 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4780 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4781 | goto unlock; |
| 4782 | } |
| 4783 | |
| 4784 | if (cp->addr.type == BDADDR_BREDR) |
| 4785 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4786 | &cp->addr.bdaddr); |
| 4787 | else |
| 4788 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4789 | |
| 4790 | if (!conn || conn->state != BT_CONNECTED) { |
| 4791 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4792 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4793 | goto unlock; |
| 4794 | } |
| 4795 | |
| 4796 | /* To avoid client trying to guess when to poll again for information we |
| 4797 | * calculate conn info age as random value between min/max set in hdev. |
| 4798 | */ |
| 4799 | conn_info_age = hdev->conn_info_min_age + |
| 4800 | prandom_u32_max(hdev->conn_info_max_age - |
| 4801 | hdev->conn_info_min_age); |
| 4802 | |
| 4803 | /* Query controller to refresh cached values if they are too old or were |
| 4804 | * never read. |
| 4805 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4806 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4807 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4808 | !conn->conn_info_timestamp) { |
| 4809 | struct hci_request req; |
| 4810 | struct hci_cp_read_tx_power req_txp_cp; |
| 4811 | struct hci_cp_read_rssi req_rssi_cp; |
| 4812 | struct pending_cmd *cmd; |
| 4813 | |
| 4814 | hci_req_init(&req, hdev); |
| 4815 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4816 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4817 | &req_rssi_cp); |
| 4818 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4819 | /* For LE links TX power does not change thus we don't need to |
| 4820 | * query for it once value is known. |
| 4821 | */ |
| 4822 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4823 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4824 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4825 | req_txp_cp.type = 0x00; |
| 4826 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4827 | sizeof(req_txp_cp), &req_txp_cp); |
| 4828 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4829 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4830 | /* Max TX power needs to be read only once per connection */ |
| 4831 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4832 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4833 | req_txp_cp.type = 0x01; |
| 4834 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4835 | sizeof(req_txp_cp), &req_txp_cp); |
| 4836 | } |
| 4837 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4838 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4839 | if (err < 0) |
| 4840 | goto unlock; |
| 4841 | |
| 4842 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4843 | data, len); |
| 4844 | if (!cmd) { |
| 4845 | err = -ENOMEM; |
| 4846 | goto unlock; |
| 4847 | } |
| 4848 | |
| 4849 | hci_conn_hold(conn); |
| 4850 | cmd->user_data = conn; |
| 4851 | |
| 4852 | conn->conn_info_timestamp = jiffies; |
| 4853 | } else { |
| 4854 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4855 | rp.rssi = conn->rssi; |
| 4856 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4857 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4858 | |
| 4859 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4860 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4861 | } |
| 4862 | |
| 4863 | unlock: |
| 4864 | hci_dev_unlock(hdev); |
| 4865 | return err; |
| 4866 | } |
| 4867 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 4868 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 4869 | { |
| 4870 | struct mgmt_cp_get_clock_info *cp; |
| 4871 | struct mgmt_rp_get_clock_info rp; |
| 4872 | struct hci_cp_read_clock *hci_cp; |
| 4873 | struct pending_cmd *cmd; |
| 4874 | struct hci_conn *conn; |
| 4875 | |
| 4876 | BT_DBG("%s status %u", hdev->name, status); |
| 4877 | |
| 4878 | hci_dev_lock(hdev); |
| 4879 | |
| 4880 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 4881 | if (!hci_cp) |
| 4882 | goto unlock; |
| 4883 | |
| 4884 | if (hci_cp->which) { |
| 4885 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 4886 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4887 | } else { |
| 4888 | conn = NULL; |
| 4889 | } |
| 4890 | |
| 4891 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 4892 | if (!cmd) |
| 4893 | goto unlock; |
| 4894 | |
| 4895 | cp = cmd->param; |
| 4896 | |
| 4897 | memset(&rp, 0, sizeof(rp)); |
| 4898 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 4899 | |
| 4900 | if (status) |
| 4901 | goto send_rsp; |
| 4902 | |
| 4903 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 4904 | |
| 4905 | if (conn) { |
| 4906 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 4907 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 4908 | } |
| 4909 | |
| 4910 | send_rsp: |
| 4911 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 4912 | &rp, sizeof(rp)); |
| 4913 | mgmt_pending_remove(cmd); |
| 4914 | if (conn) |
| 4915 | hci_conn_drop(conn); |
| 4916 | |
| 4917 | unlock: |
| 4918 | hci_dev_unlock(hdev); |
| 4919 | } |
| 4920 | |
| 4921 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4922 | u16 len) |
| 4923 | { |
| 4924 | struct mgmt_cp_get_clock_info *cp = data; |
| 4925 | struct mgmt_rp_get_clock_info rp; |
| 4926 | struct hci_cp_read_clock hci_cp; |
| 4927 | struct pending_cmd *cmd; |
| 4928 | struct hci_request req; |
| 4929 | struct hci_conn *conn; |
| 4930 | int err; |
| 4931 | |
| 4932 | BT_DBG("%s", hdev->name); |
| 4933 | |
| 4934 | memset(&rp, 0, sizeof(rp)); |
| 4935 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4936 | rp.addr.type = cp->addr.type; |
| 4937 | |
| 4938 | if (cp->addr.type != BDADDR_BREDR) |
| 4939 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4940 | MGMT_STATUS_INVALID_PARAMS, |
| 4941 | &rp, sizeof(rp)); |
| 4942 | |
| 4943 | hci_dev_lock(hdev); |
| 4944 | |
| 4945 | if (!hdev_is_powered(hdev)) { |
| 4946 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4947 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4948 | goto unlock; |
| 4949 | } |
| 4950 | |
| 4951 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4952 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4953 | &cp->addr.bdaddr); |
| 4954 | if (!conn || conn->state != BT_CONNECTED) { |
| 4955 | err = cmd_complete(sk, hdev->id, |
| 4956 | MGMT_OP_GET_CLOCK_INFO, |
| 4957 | MGMT_STATUS_NOT_CONNECTED, |
| 4958 | &rp, sizeof(rp)); |
| 4959 | goto unlock; |
| 4960 | } |
| 4961 | } else { |
| 4962 | conn = NULL; |
| 4963 | } |
| 4964 | |
| 4965 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 4966 | if (!cmd) { |
| 4967 | err = -ENOMEM; |
| 4968 | goto unlock; |
| 4969 | } |
| 4970 | |
| 4971 | hci_req_init(&req, hdev); |
| 4972 | |
| 4973 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 4974 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4975 | |
| 4976 | if (conn) { |
| 4977 | hci_conn_hold(conn); |
| 4978 | cmd->user_data = conn; |
| 4979 | |
| 4980 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 4981 | hci_cp.which = 0x01; /* Piconet clock */ |
| 4982 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4983 | } |
| 4984 | |
| 4985 | err = hci_req_run(&req, get_clock_info_complete); |
| 4986 | if (err < 0) |
| 4987 | mgmt_pending_remove(cmd); |
| 4988 | |
| 4989 | unlock: |
| 4990 | hci_dev_unlock(hdev); |
| 4991 | return err; |
| 4992 | } |
| 4993 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 4994 | static void device_added(struct sock *sk, struct hci_dev *hdev, |
| 4995 | bdaddr_t *bdaddr, u8 type, u8 action) |
| 4996 | { |
| 4997 | struct mgmt_ev_device_added ev; |
| 4998 | |
| 4999 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5000 | ev.addr.type = type; |
| 5001 | ev.action = action; |
| 5002 | |
| 5003 | mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk); |
| 5004 | } |
| 5005 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5006 | static int add_device(struct sock *sk, struct hci_dev *hdev, |
| 5007 | void *data, u16 len) |
| 5008 | { |
| 5009 | struct mgmt_cp_add_device *cp = data; |
| 5010 | u8 auto_conn, addr_type; |
| 5011 | int err; |
| 5012 | |
| 5013 | BT_DBG("%s", hdev->name); |
| 5014 | |
| 5015 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 5016 | !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) |
| 5017 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5018 | MGMT_STATUS_INVALID_PARAMS, |
| 5019 | &cp->addr, sizeof(cp->addr)); |
| 5020 | |
| 5021 | if (cp->action != 0x00 && cp->action != 0x01) |
| 5022 | return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5023 | MGMT_STATUS_INVALID_PARAMS, |
| 5024 | &cp->addr, sizeof(cp->addr)); |
| 5025 | |
| 5026 | hci_dev_lock(hdev); |
| 5027 | |
| 5028 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5029 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5030 | else |
| 5031 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5032 | |
| 5033 | if (cp->action) |
| 5034 | auto_conn = HCI_AUTO_CONN_ALWAYS; |
| 5035 | else |
| 5036 | auto_conn = HCI_AUTO_CONN_DISABLED; |
| 5037 | |
Marcel Holtmann | bf5b3c8 | 2014-06-30 12:34:39 +0200 | [diff] [blame] | 5038 | /* If the connection parameters don't exist for this device, |
| 5039 | * they will be created and configured with defaults. |
| 5040 | */ |
Marcel Holtmann | d06b50c | 2014-07-01 12:11:06 +0200 | [diff] [blame] | 5041 | if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type, |
| 5042 | auto_conn) < 0) { |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5043 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5044 | MGMT_STATUS_FAILED, |
| 5045 | &cp->addr, sizeof(cp->addr)); |
| 5046 | goto unlock; |
| 5047 | } |
| 5048 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5049 | device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action); |
| 5050 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5051 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, |
| 5052 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5053 | |
| 5054 | unlock: |
| 5055 | hci_dev_unlock(hdev); |
| 5056 | return err; |
| 5057 | } |
| 5058 | |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5059 | static void device_removed(struct sock *sk, struct hci_dev *hdev, |
| 5060 | bdaddr_t *bdaddr, u8 type) |
| 5061 | { |
| 5062 | struct mgmt_ev_device_removed ev; |
| 5063 | |
| 5064 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5065 | ev.addr.type = type; |
| 5066 | |
| 5067 | mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk); |
| 5068 | } |
| 5069 | |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5070 | static int remove_device(struct sock *sk, struct hci_dev *hdev, |
| 5071 | void *data, u16 len) |
| 5072 | { |
| 5073 | struct mgmt_cp_remove_device *cp = data; |
| 5074 | int err; |
| 5075 | |
| 5076 | BT_DBG("%s", hdev->name); |
| 5077 | |
| 5078 | hci_dev_lock(hdev); |
| 5079 | |
| 5080 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 5081 | u8 addr_type; |
| 5082 | |
| 5083 | if (!bdaddr_type_is_le(cp->addr.type)) { |
| 5084 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5085 | MGMT_STATUS_INVALID_PARAMS, |
| 5086 | &cp->addr, sizeof(cp->addr)); |
| 5087 | goto unlock; |
| 5088 | } |
| 5089 | |
| 5090 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 5091 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 5092 | else |
| 5093 | addr_type = ADDR_LE_DEV_RANDOM; |
| 5094 | |
| 5095 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
Marcel Holtmann | 8afef09 | 2014-06-29 22:28:34 +0200 | [diff] [blame] | 5096 | |
| 5097 | device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type); |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5098 | } else { |
| 5099 | if (cp->addr.type) { |
| 5100 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5101 | MGMT_STATUS_INVALID_PARAMS, |
| 5102 | &cp->addr, sizeof(cp->addr)); |
| 5103 | goto unlock; |
| 5104 | } |
| 5105 | |
| 5106 | hci_conn_params_clear(hdev); |
| 5107 | } |
| 5108 | |
| 5109 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE, |
| 5110 | MGMT_STATUS_SUCCESS, &cp->addr, sizeof(cp->addr)); |
| 5111 | |
| 5112 | unlock: |
| 5113 | hci_dev_unlock(hdev); |
| 5114 | return err; |
| 5115 | } |
| 5116 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5117 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5118 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 5119 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5120 | bool var_len; |
| 5121 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5122 | } mgmt_handlers[] = { |
| 5123 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5124 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 5125 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 5126 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 5127 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 5128 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 5129 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 5130 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 5131 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 5132 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 5133 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 5134 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 5135 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 5136 | { set_le, false, MGMT_SETTING_SIZE }, |
| 5137 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 5138 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 5139 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 5140 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 5141 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 5142 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 5143 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 5144 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 5145 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 5146 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 5147 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 5148 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 5149 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 5150 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5151 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5152 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5153 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5154 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5155 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5156 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5157 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5158 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5159 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5160 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5161 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5162 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5163 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5164 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5165 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5166 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5167 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5168 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5169 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5170 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5171 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5172 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame] | 5173 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Marcel Holtmann | 2faade5 | 2014-06-29 19:44:03 +0200 | [diff] [blame] | 5174 | { add_device, false, MGMT_ADD_DEVICE_SIZE }, |
| 5175 | { remove_device, false, MGMT_REMOVE_DEVICE_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5176 | }; |
| 5177 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5178 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5179 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5180 | void *buf; |
| 5181 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5182 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5183 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5184 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5185 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5186 | int err; |
| 5187 | |
| 5188 | BT_DBG("got %zu bytes", msglen); |
| 5189 | |
| 5190 | if (msglen < sizeof(*hdr)) |
| 5191 | return -EINVAL; |
| 5192 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5193 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5194 | if (!buf) |
| 5195 | return -ENOMEM; |
| 5196 | |
| 5197 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5198 | err = -EFAULT; |
| 5199 | goto done; |
| 5200 | } |
| 5201 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5202 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5203 | opcode = __le16_to_cpu(hdr->opcode); |
| 5204 | index = __le16_to_cpu(hdr->index); |
| 5205 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5206 | |
| 5207 | if (len != msglen - sizeof(*hdr)) { |
| 5208 | err = -EINVAL; |
| 5209 | goto done; |
| 5210 | } |
| 5211 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5212 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5213 | hdev = hci_dev_get(index); |
| 5214 | if (!hdev) { |
| 5215 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5216 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5217 | goto done; |
| 5218 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5219 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5220 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
Marcel Holtmann | fee746b | 2014-06-29 12:13:05 +0200 | [diff] [blame] | 5221 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) || |
| 5222 | test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5223 | err = cmd_status(sk, index, opcode, |
| 5224 | MGMT_STATUS_INVALID_INDEX); |
| 5225 | goto done; |
| 5226 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5227 | } |
| 5228 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5229 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5230 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5231 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5232 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5233 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5234 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5235 | } |
| 5236 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5237 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5238 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5239 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5240 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5241 | goto done; |
| 5242 | } |
| 5243 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5244 | handler = &mgmt_handlers[opcode]; |
| 5245 | |
| 5246 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5247 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5248 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5249 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5250 | goto done; |
| 5251 | } |
| 5252 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5253 | if (hdev) |
| 5254 | mgmt_init_hdev(sk, hdev); |
| 5255 | |
| 5256 | cp = buf + sizeof(*hdr); |
| 5257 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5258 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5259 | if (err < 0) |
| 5260 | goto done; |
| 5261 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5262 | err = msglen; |
| 5263 | |
| 5264 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5265 | if (hdev) |
| 5266 | hci_dev_put(hdev); |
| 5267 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5268 | kfree(buf); |
| 5269 | return err; |
| 5270 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5271 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5272 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5273 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5274 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5275 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5276 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5277 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5278 | } |
| 5279 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5280 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5281 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5282 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5283 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5284 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5285 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5286 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5287 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5288 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5289 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5290 | } |
| 5291 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5292 | /* This function requires the caller holds hdev->lock */ |
| 5293 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 5294 | { |
| 5295 | struct hci_conn_params *p; |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5296 | bool added = false; |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5297 | |
| 5298 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5299 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) { |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5300 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5301 | added = true; |
| 5302 | } |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5303 | } |
Marcel Holtmann | c83ed19 | 2014-07-01 19:28:24 +0200 | [diff] [blame] | 5304 | |
| 5305 | /* Calling hci_pend_le_conn_add will actually already trigger |
| 5306 | * background scanning when needed. So no need to trigger it |
| 5307 | * just another time. |
| 5308 | * |
| 5309 | * This check is here to avoid an unneeded restart of the |
| 5310 | * passive scanning. Since this is during the controller |
| 5311 | * power up phase the duplicate filtering is not an issue. |
| 5312 | */ |
| 5313 | if (added) |
| 5314 | return; |
| 5315 | |
| 5316 | hci_update_background_scan(hdev); |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5317 | } |
| 5318 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5319 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5320 | { |
| 5321 | struct cmd_lookup match = { NULL, hdev }; |
| 5322 | |
| 5323 | BT_DBG("status 0x%02x", status); |
| 5324 | |
| 5325 | hci_dev_lock(hdev); |
| 5326 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5327 | restart_le_auto_conns(hdev); |
| 5328 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5329 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5330 | |
| 5331 | new_settings(hdev, match.sk); |
| 5332 | |
| 5333 | hci_dev_unlock(hdev); |
| 5334 | |
| 5335 | if (match.sk) |
| 5336 | sock_put(match.sk); |
| 5337 | } |
| 5338 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5339 | static int powered_update_hci(struct hci_dev *hdev) |
| 5340 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5341 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5342 | u8 link_sec; |
| 5343 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5344 | hci_req_init(&req, hdev); |
| 5345 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5346 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5347 | !lmp_host_ssp_capable(hdev)) { |
| 5348 | u8 ssp = 1; |
| 5349 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5350 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5351 | } |
| 5352 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5353 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5354 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5355 | struct hci_cp_write_le_host_supported cp; |
| 5356 | |
| 5357 | cp.le = 1; |
| 5358 | cp.simul = lmp_le_br_capable(hdev); |
| 5359 | |
| 5360 | /* Check first if we already have the right |
| 5361 | * host state (host features set) |
| 5362 | */ |
| 5363 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5364 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5365 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5366 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5367 | } |
| 5368 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5369 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5370 | /* Make sure the controller has a good default for |
| 5371 | * advertising data. This also applies to the case |
| 5372 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5373 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5374 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5375 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5376 | update_scan_rsp_data(&req); |
| 5377 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5378 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5379 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5380 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5381 | } |
| 5382 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5383 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5384 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5385 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5386 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5387 | |
| 5388 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5389 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5390 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5391 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5392 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5393 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5394 | } |
| 5395 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5396 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5397 | } |
| 5398 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5399 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5400 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5401 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5402 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5403 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5404 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5405 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5406 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5407 | return 0; |
| 5408 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5409 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5410 | if (powered_update_hci(hdev) == 0) |
| 5411 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5412 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5413 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5414 | &match); |
| 5415 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5416 | } |
| 5417 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5418 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5419 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5420 | |
| 5421 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5422 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5423 | zero_cod, sizeof(zero_cod), NULL); |
| 5424 | |
| 5425 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5426 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5427 | |
| 5428 | if (match.sk) |
| 5429 | sock_put(match.sk); |
| 5430 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5431 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5432 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5433 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5434 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5435 | { |
| 5436 | struct pending_cmd *cmd; |
| 5437 | u8 status; |
| 5438 | |
| 5439 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5440 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5441 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5442 | |
| 5443 | if (err == -ERFKILL) |
| 5444 | status = MGMT_STATUS_RFKILLED; |
| 5445 | else |
| 5446 | status = MGMT_STATUS_FAILED; |
| 5447 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5448 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5449 | |
| 5450 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5451 | } |
| 5452 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5453 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5454 | { |
| 5455 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5456 | |
| 5457 | hci_dev_lock(hdev); |
| 5458 | |
| 5459 | /* When discoverable timeout triggers, then just make sure |
| 5460 | * the limited discoverable flag is cleared. Even in the case |
| 5461 | * of a timeout triggered from general discoverable, it is |
| 5462 | * safe to unconditionally clear the flag. |
| 5463 | */ |
| 5464 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5465 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5466 | |
| 5467 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5468 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5469 | u8 scan = SCAN_PAGE; |
| 5470 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5471 | sizeof(scan), &scan); |
| 5472 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5473 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5474 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5475 | hci_req_run(&req, NULL); |
| 5476 | |
| 5477 | hdev->discov_timeout = 0; |
| 5478 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5479 | new_settings(hdev, NULL); |
| 5480 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5481 | hci_dev_unlock(hdev); |
| 5482 | } |
| 5483 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5484 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5485 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5486 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5487 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5488 | /* Nothing needed here if there's a pending command since that |
| 5489 | * commands request completion callback takes care of everything |
| 5490 | * necessary. |
| 5491 | */ |
| 5492 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5493 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5494 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5495 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5496 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5497 | return; |
| 5498 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5499 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5500 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5501 | } else { |
| 5502 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5503 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5504 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5505 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5506 | if (changed) { |
| 5507 | struct hci_request req; |
| 5508 | |
| 5509 | /* In case this change in discoverable was triggered by |
| 5510 | * a disabling of connectable there could be a need to |
| 5511 | * update the advertising flags. |
| 5512 | */ |
| 5513 | hci_req_init(&req, hdev); |
| 5514 | update_adv_data(&req); |
| 5515 | hci_req_run(&req, NULL); |
| 5516 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5517 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5518 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5519 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5520 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5521 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5522 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5523 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5524 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5525 | /* Nothing needed here if there's a pending command since that |
| 5526 | * commands request completion callback takes care of everything |
| 5527 | * necessary. |
| 5528 | */ |
| 5529 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5530 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5531 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5532 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5533 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5534 | return; |
| 5535 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5536 | if (connectable) |
| 5537 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5538 | else |
| 5539 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5540 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5541 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5542 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5543 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5544 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5545 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5546 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5547 | /* Powering off may stop advertising - don't let that interfere */ |
| 5548 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5549 | return; |
| 5550 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5551 | if (advertising) |
| 5552 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5553 | else |
| 5554 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5555 | } |
| 5556 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5557 | 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] | 5558 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5559 | u8 mgmt_err = mgmt_status(status); |
| 5560 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5561 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5562 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5563 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5564 | |
| 5565 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5566 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5567 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5568 | } |
| 5569 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5570 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5571 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5572 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5573 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5574 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5575 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5576 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5577 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5578 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5579 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5580 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5581 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5582 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5583 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5584 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5585 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5586 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5587 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5588 | { |
| 5589 | if (ltk->authenticated) |
| 5590 | return MGMT_LTK_AUTHENTICATED; |
| 5591 | |
| 5592 | return MGMT_LTK_UNAUTHENTICATED; |
| 5593 | } |
| 5594 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5595 | 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] | 5596 | { |
| 5597 | struct mgmt_ev_new_long_term_key ev; |
| 5598 | |
| 5599 | memset(&ev, 0, sizeof(ev)); |
| 5600 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5601 | /* Devices using resolvable or non-resolvable random addresses |
| 5602 | * without providing an indentity resolving key don't require |
| 5603 | * to store long term keys. Their addresses will change the |
| 5604 | * next time around. |
| 5605 | * |
| 5606 | * Only when a remote device provides an identity address |
| 5607 | * make sure the long term key is stored. If the remote |
| 5608 | * identity is known, the long term keys are internally |
| 5609 | * mapped to the identity address. So allow static random |
| 5610 | * and public addresses here. |
| 5611 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5612 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5613 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5614 | ev.store_hint = 0x00; |
| 5615 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5616 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5617 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5618 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5619 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5620 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5621 | ev.key.enc_size = key->enc_size; |
| 5622 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5623 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5624 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5625 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5626 | ev.key.master = 1; |
| 5627 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5628 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5629 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5630 | 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] | 5631 | } |
| 5632 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5633 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5634 | { |
| 5635 | struct mgmt_ev_new_irk ev; |
| 5636 | |
| 5637 | memset(&ev, 0, sizeof(ev)); |
| 5638 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5639 | /* For identity resolving keys from devices that are already |
| 5640 | * using a public address or static random address, do not |
| 5641 | * ask for storing this key. The identity resolving key really |
| 5642 | * is only mandatory for devices using resovlable random |
| 5643 | * addresses. |
| 5644 | * |
| 5645 | * Storing all identity resolving keys has the downside that |
| 5646 | * they will be also loaded on next boot of they system. More |
| 5647 | * identity resolving keys, means more time during scanning is |
| 5648 | * needed to actually resolve these addresses. |
| 5649 | */ |
| 5650 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5651 | ev.store_hint = 0x01; |
| 5652 | else |
| 5653 | ev.store_hint = 0x00; |
| 5654 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5655 | bacpy(&ev.rpa, &irk->rpa); |
| 5656 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5657 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5658 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5659 | |
| 5660 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5661 | } |
| 5662 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5663 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5664 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5665 | { |
| 5666 | struct mgmt_ev_new_csrk ev; |
| 5667 | |
| 5668 | memset(&ev, 0, sizeof(ev)); |
| 5669 | |
| 5670 | /* Devices using resolvable or non-resolvable random addresses |
| 5671 | * without providing an indentity resolving key don't require |
| 5672 | * to store signature resolving keys. Their addresses will change |
| 5673 | * the next time around. |
| 5674 | * |
| 5675 | * Only when a remote device provides an identity address |
| 5676 | * make sure the signature resolving key is stored. So allow |
| 5677 | * static random and public addresses here. |
| 5678 | */ |
| 5679 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5680 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5681 | ev.store_hint = 0x00; |
| 5682 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5683 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5684 | |
| 5685 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5686 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5687 | ev.key.master = csrk->master; |
| 5688 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5689 | |
| 5690 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5691 | } |
| 5692 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5693 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5694 | u8 data_len) |
| 5695 | { |
| 5696 | eir[eir_len++] = sizeof(type) + data_len; |
| 5697 | eir[eir_len++] = type; |
| 5698 | memcpy(&eir[eir_len], data, data_len); |
| 5699 | eir_len += data_len; |
| 5700 | |
| 5701 | return eir_len; |
| 5702 | } |
| 5703 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5704 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5705 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5706 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5707 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5708 | char buf[512]; |
| 5709 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5710 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5711 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5712 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5713 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5714 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5715 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5716 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5717 | if (name_len > 0) |
| 5718 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5719 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5720 | |
| 5721 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5722 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5723 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5724 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5725 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5726 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5727 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5728 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5729 | } |
| 5730 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5731 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5732 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5733 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5734 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5735 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5736 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5737 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5738 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5739 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5740 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5741 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5742 | |
| 5743 | *sk = cmd->sk; |
| 5744 | sock_hold(*sk); |
| 5745 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5746 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5747 | } |
| 5748 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5749 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5750 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5751 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5752 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5753 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5754 | |
| 5755 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5756 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5757 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5758 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5759 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5760 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5761 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5762 | |
| 5763 | mgmt_pending_remove(cmd); |
| 5764 | } |
| 5765 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5766 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5767 | u8 link_type, u8 addr_type, u8 reason, |
| 5768 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5769 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5770 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5771 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5772 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5773 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5774 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5775 | if (power_off) { |
| 5776 | struct mgmt_mode *cp = power_off->param; |
| 5777 | |
| 5778 | /* The connection is still in hci_conn_hash so test for 1 |
| 5779 | * instead of 0 to know if this is the last one. |
| 5780 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5781 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5782 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5783 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5784 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5785 | } |
| 5786 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5787 | if (!mgmt_connected) |
| 5788 | return; |
| 5789 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5790 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5791 | return; |
| 5792 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5793 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5794 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5795 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5796 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5797 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5798 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5799 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5800 | |
| 5801 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 5802 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5803 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5804 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5805 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5806 | } |
| 5807 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5808 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5809 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5810 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5811 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 5812 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5813 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5814 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5815 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 5816 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 5817 | hdev); |
| 5818 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5819 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5820 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5821 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5822 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5823 | cp = cmd->param; |
| 5824 | |
| 5825 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 5826 | return; |
| 5827 | |
| 5828 | if (cp->addr.type != bdaddr_type) |
| 5829 | return; |
| 5830 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5831 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5832 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 5833 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5834 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 5835 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5836 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5837 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5838 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5839 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5840 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5841 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5842 | { |
| 5843 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5844 | struct pending_cmd *power_off; |
| 5845 | |
| 5846 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5847 | if (power_off) { |
| 5848 | struct mgmt_mode *cp = power_off->param; |
| 5849 | |
| 5850 | /* The connection is still in hci_conn_hash so test for 1 |
| 5851 | * instead of 0 to know if this is the last one. |
| 5852 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5853 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5854 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5855 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5856 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5857 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5858 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 5859 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5860 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5861 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5862 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5863 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5864 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5865 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5866 | 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] | 5867 | { |
| 5868 | struct mgmt_ev_pin_code_request ev; |
| 5869 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5870 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5871 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 5872 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5873 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5874 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5875 | } |
| 5876 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5877 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5878 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5879 | { |
| 5880 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5881 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5882 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5883 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5884 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5885 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5886 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5887 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5888 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5889 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5890 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 5891 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5892 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5893 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5894 | } |
| 5895 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5896 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5897 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5898 | { |
| 5899 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5900 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5901 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5902 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5903 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5904 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5905 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5906 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5907 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5908 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5909 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 5910 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5911 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5912 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5913 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5914 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5915 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5916 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5917 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5918 | { |
| 5919 | struct mgmt_ev_user_confirm_request ev; |
| 5920 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5921 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5922 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5923 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5924 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 5925 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5926 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5927 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5928 | 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] | 5929 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5930 | } |
| 5931 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5932 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5933 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5934 | { |
| 5935 | struct mgmt_ev_user_passkey_request ev; |
| 5936 | |
| 5937 | BT_DBG("%s", hdev->name); |
| 5938 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5939 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5940 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5941 | |
| 5942 | 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] | 5943 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5944 | } |
| 5945 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 5946 | 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] | 5947 | u8 link_type, u8 addr_type, u8 status, |
| 5948 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5949 | { |
| 5950 | struct pending_cmd *cmd; |
| 5951 | struct mgmt_rp_user_confirm_reply rp; |
| 5952 | int err; |
| 5953 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5954 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5955 | if (!cmd) |
| 5956 | return -ENOENT; |
| 5957 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5958 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5959 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5960 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5961 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5962 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5963 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5964 | |
| 5965 | return err; |
| 5966 | } |
| 5967 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5968 | 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] | 5969 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5970 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5971 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5972 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5973 | } |
| 5974 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5975 | 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] | 5976 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5977 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5978 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5979 | status, |
| 5980 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5981 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5982 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5983 | 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] | 5984 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5985 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5986 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5987 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5988 | } |
| 5989 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5990 | 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] | 5991 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5992 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5993 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5994 | status, |
| 5995 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5996 | } |
| 5997 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 5998 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5999 | u8 link_type, u8 addr_type, u32 passkey, |
| 6000 | u8 entered) |
| 6001 | { |
| 6002 | struct mgmt_ev_passkey_notify ev; |
| 6003 | |
| 6004 | BT_DBG("%s", hdev->name); |
| 6005 | |
| 6006 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6007 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 6008 | ev.passkey = __cpu_to_le32(passkey); |
| 6009 | ev.entered = entered; |
| 6010 | |
| 6011 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 6012 | } |
| 6013 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6014 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6015 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6016 | { |
| 6017 | struct mgmt_ev_auth_failed ev; |
| 6018 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 6019 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6020 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 6021 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6022 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 6023 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 6024 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6025 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6026 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6027 | { |
| 6028 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6029 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6030 | |
| 6031 | if (status) { |
| 6032 | u8 mgmt_err = mgmt_status(status); |
| 6033 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6034 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6035 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6036 | } |
| 6037 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6038 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 6039 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 6040 | &hdev->dev_flags); |
| 6041 | else |
| 6042 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 6043 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6044 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6045 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6046 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6047 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 6048 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 6049 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6050 | |
| 6051 | if (match.sk) |
| 6052 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 6053 | } |
| 6054 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6055 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6056 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6057 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6058 | struct hci_cp_write_eir cp; |
| 6059 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 6060 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6061 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6062 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 6063 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 6064 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6065 | memset(&cp, 0, sizeof(cp)); |
| 6066 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6067 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 6068 | } |
| 6069 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6070 | 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] | 6071 | { |
| 6072 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6073 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6074 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6075 | |
| 6076 | if (status) { |
| 6077 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6078 | |
| 6079 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6080 | &hdev->dev_flags)) { |
| 6081 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6082 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6083 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6084 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6085 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 6086 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6087 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6088 | } |
| 6089 | |
| 6090 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6091 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6092 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 6093 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 6094 | if (!changed) |
| 6095 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 6096 | &hdev->dev_flags); |
| 6097 | else |
| 6098 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6099 | } |
| 6100 | |
| 6101 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 6102 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 6103 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 6104 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6105 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 6106 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6107 | sock_put(match.sk); |
| 6108 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6109 | hci_req_init(&req, hdev); |
| 6110 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6111 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 6112 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 6113 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 6114 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6115 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6116 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6117 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 6118 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 6119 | |
| 6120 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 6121 | } |
| 6122 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6123 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 6124 | { |
| 6125 | struct cmd_lookup match = { NULL, hdev }; |
| 6126 | bool changed = false; |
| 6127 | |
| 6128 | if (status) { |
| 6129 | u8 mgmt_err = mgmt_status(status); |
| 6130 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6131 | if (enable) { |
| 6132 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 6133 | &hdev->dev_flags)) |
| 6134 | new_settings(hdev, NULL); |
| 6135 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6136 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6137 | |
| 6138 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6139 | cmd_status_rsp, &mgmt_err); |
| 6140 | return; |
| 6141 | } |
| 6142 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6143 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6144 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6145 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6146 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 6147 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 6148 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 6149 | |
| 6150 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 6151 | settings_rsp, &match); |
| 6152 | |
| 6153 | if (changed) |
| 6154 | new_settings(hdev, match.sk); |
| 6155 | |
| 6156 | if (match.sk) |
| 6157 | sock_put(match.sk); |
| 6158 | } |
| 6159 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6160 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6161 | { |
| 6162 | struct cmd_lookup *match = data; |
| 6163 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6164 | if (match->sk == NULL) { |
| 6165 | match->sk = cmd->sk; |
| 6166 | sock_hold(match->sk); |
| 6167 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6168 | } |
| 6169 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6170 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6171 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6172 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6173 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6174 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6175 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6176 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6177 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6178 | |
| 6179 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6180 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6181 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6182 | |
| 6183 | if (match.sk) |
| 6184 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6185 | } |
| 6186 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6187 | 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] | 6188 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6189 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6190 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6191 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6192 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6193 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6194 | |
| 6195 | memset(&ev, 0, sizeof(ev)); |
| 6196 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6197 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6198 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6199 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6200 | if (!cmd) { |
| 6201 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6202 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6203 | /* If this is a HCI command related to powering on the |
| 6204 | * HCI dev don't send any mgmt signals. |
| 6205 | */ |
| 6206 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6207 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6208 | } |
| 6209 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6210 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6211 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6212 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6213 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6214 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6215 | u8 *randomizer192, u8 *hash256, |
| 6216 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6217 | { |
| 6218 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6219 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6220 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6221 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6222 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6223 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6224 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6225 | |
| 6226 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6227 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6228 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6229 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6230 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6231 | hash256 && randomizer256) { |
| 6232 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6233 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6234 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6235 | memcpy(rp.randomizer192, randomizer192, |
| 6236 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6237 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6238 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6239 | memcpy(rp.randomizer256, randomizer256, |
| 6240 | sizeof(rp.randomizer256)); |
| 6241 | |
| 6242 | cmd_complete(cmd->sk, hdev->id, |
| 6243 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6244 | &rp, sizeof(rp)); |
| 6245 | } else { |
| 6246 | struct mgmt_rp_read_local_oob_data rp; |
| 6247 | |
| 6248 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6249 | memcpy(rp.randomizer, randomizer192, |
| 6250 | sizeof(rp.randomizer)); |
| 6251 | |
| 6252 | cmd_complete(cmd->sk, hdev->id, |
| 6253 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6254 | &rp, sizeof(rp)); |
| 6255 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6256 | } |
| 6257 | |
| 6258 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6259 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6260 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6261 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6262 | u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, |
| 6263 | u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6264 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6265 | char buf[512]; |
| 6266 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6267 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6268 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6269 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6270 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6271 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6272 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6273 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6274 | * are for the potential CoD field. |
| 6275 | */ |
| 6276 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6277 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6278 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6279 | memset(buf, 0, sizeof(buf)); |
| 6280 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6281 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6282 | if (irk) { |
| 6283 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6284 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6285 | } else { |
| 6286 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6287 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6288 | } |
| 6289 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6290 | ev->rssi = rssi; |
Marcel Holtmann | af58925 | 2014-07-01 14:11:20 +0200 | [diff] [blame] | 6291 | ev->flags = cpu_to_le32(flags); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6292 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6293 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6294 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6295 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6296 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6297 | 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] | 6298 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6299 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6300 | if (scan_rsp_len > 0) |
| 6301 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6302 | |
| 6303 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6304 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6305 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6306 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6307 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6308 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6309 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6310 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6311 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6312 | struct mgmt_ev_device_found *ev; |
| 6313 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6314 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6315 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6316 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6317 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6318 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6319 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6320 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6321 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6322 | ev->rssi = rssi; |
| 6323 | |
| 6324 | 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] | 6325 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6326 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6327 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6328 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6329 | 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] | 6330 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6331 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6332 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6333 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6334 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6335 | struct pending_cmd *cmd; |
| 6336 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6337 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6338 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6339 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6340 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6341 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6342 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6343 | |
| 6344 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6345 | u8 type = hdev->discovery.type; |
| 6346 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6347 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6348 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6349 | mgmt_pending_remove(cmd); |
| 6350 | } |
| 6351 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6352 | memset(&ev, 0, sizeof(ev)); |
| 6353 | ev.type = hdev->discovery.type; |
| 6354 | ev.discovering = discovering; |
| 6355 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6356 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6357 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6358 | |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6359 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6360 | { |
| 6361 | BT_DBG("%s status %u", hdev->name, status); |
| 6362 | |
| 6363 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6364 | if (status) { |
| 6365 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6366 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6367 | } |
| 6368 | } |
| 6369 | |
| 6370 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6371 | { |
| 6372 | struct hci_request req; |
| 6373 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6374 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6375 | return; |
| 6376 | |
| 6377 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6378 | return; |
| 6379 | |
| 6380 | hci_req_init(&req, hdev); |
| 6381 | enable_advertising(&req); |
| 6382 | |
| 6383 | /* If this fails we have no option but to let user space know |
| 6384 | * that we've disabled advertising. |
| 6385 | */ |
| 6386 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6387 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6388 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6389 | } |
| 6390 | } |