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> |
| 32 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 33 | |
| 34 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 35 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 36 | #define MGMT_VERSION 1 |
Marcel Holtmann | 4045664 | 2014-01-28 15:39:01 -0800 | [diff] [blame] | 37 | #define MGMT_REVISION 5 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 38 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 39 | static const u16 mgmt_commands[] = { |
| 40 | MGMT_OP_READ_INDEX_LIST, |
| 41 | MGMT_OP_READ_INFO, |
| 42 | MGMT_OP_SET_POWERED, |
| 43 | MGMT_OP_SET_DISCOVERABLE, |
| 44 | MGMT_OP_SET_CONNECTABLE, |
| 45 | MGMT_OP_SET_FAST_CONNECTABLE, |
| 46 | MGMT_OP_SET_PAIRABLE, |
| 47 | MGMT_OP_SET_LINK_SECURITY, |
| 48 | MGMT_OP_SET_SSP, |
| 49 | MGMT_OP_SET_HS, |
| 50 | MGMT_OP_SET_LE, |
| 51 | MGMT_OP_SET_DEV_CLASS, |
| 52 | MGMT_OP_SET_LOCAL_NAME, |
| 53 | MGMT_OP_ADD_UUID, |
| 54 | MGMT_OP_REMOVE_UUID, |
| 55 | MGMT_OP_LOAD_LINK_KEYS, |
| 56 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 57 | MGMT_OP_DISCONNECT, |
| 58 | MGMT_OP_GET_CONNECTIONS, |
| 59 | MGMT_OP_PIN_CODE_REPLY, |
| 60 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 61 | MGMT_OP_SET_IO_CAPABILITY, |
| 62 | MGMT_OP_PAIR_DEVICE, |
| 63 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 64 | MGMT_OP_UNPAIR_DEVICE, |
| 65 | MGMT_OP_USER_CONFIRM_REPLY, |
| 66 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 67 | MGMT_OP_USER_PASSKEY_REPLY, |
| 68 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 69 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 70 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 71 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 72 | MGMT_OP_START_DISCOVERY, |
| 73 | MGMT_OP_STOP_DISCOVERY, |
| 74 | MGMT_OP_CONFIRM_NAME, |
| 75 | MGMT_OP_BLOCK_DEVICE, |
| 76 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 77 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 78 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 79 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 80 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 82 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 83 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 84 | MGMT_OP_LOAD_IRKS, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static const u16 mgmt_events[] = { |
| 88 | MGMT_EV_CONTROLLER_ERROR, |
| 89 | MGMT_EV_INDEX_ADDED, |
| 90 | MGMT_EV_INDEX_REMOVED, |
| 91 | MGMT_EV_NEW_SETTINGS, |
| 92 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 93 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 94 | MGMT_EV_NEW_LINK_KEY, |
| 95 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 96 | MGMT_EV_DEVICE_CONNECTED, |
| 97 | MGMT_EV_DEVICE_DISCONNECTED, |
| 98 | MGMT_EV_CONNECT_FAILED, |
| 99 | MGMT_EV_PIN_CODE_REQUEST, |
| 100 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 101 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 102 | MGMT_EV_AUTH_FAILED, |
| 103 | MGMT_EV_DEVICE_FOUND, |
| 104 | MGMT_EV_DISCOVERING, |
| 105 | MGMT_EV_DEVICE_BLOCKED, |
| 106 | MGMT_EV_DEVICE_UNBLOCKED, |
| 107 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 108 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 109 | MGMT_EV_NEW_IRK, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 110 | }; |
| 111 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 112 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 113 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 114 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 115 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 116 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 117 | struct pending_cmd { |
| 118 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 119 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 120 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 121 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 122 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 123 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 126 | /* HCI to MGMT error code conversion table */ |
| 127 | static u8 mgmt_status_table[] = { |
| 128 | MGMT_STATUS_SUCCESS, |
| 129 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 130 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 131 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 132 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 133 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 134 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 135 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 136 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 137 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 138 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 139 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 140 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 141 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 142 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 143 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 144 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 145 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 146 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 147 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 148 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 149 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 150 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 151 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 152 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 153 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 154 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 155 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 156 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 157 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 158 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 159 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 160 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 161 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 162 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 163 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 164 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 165 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 166 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 167 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 168 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 169 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 170 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 171 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 172 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 173 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 174 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 175 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 176 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 177 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 178 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 179 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 180 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 181 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 182 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 183 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 184 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 185 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 186 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 187 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 188 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 189 | }; |
| 190 | |
| 191 | static u8 mgmt_status(u8 hci_status) |
| 192 | { |
| 193 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 194 | return mgmt_status_table[hci_status]; |
| 195 | |
| 196 | return MGMT_STATUS_FAILED; |
| 197 | } |
| 198 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 199 | 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] | 200 | { |
| 201 | struct sk_buff *skb; |
| 202 | struct mgmt_hdr *hdr; |
| 203 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 204 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 205 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 206 | 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] | 207 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 208 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 209 | if (!skb) |
| 210 | return -ENOMEM; |
| 211 | |
| 212 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 213 | |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 214 | hdr->opcode = __constant_cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 215 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 216 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 217 | |
| 218 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 219 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 220 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 221 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 222 | err = sock_queue_rcv_skb(sk, skb); |
| 223 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 224 | kfree_skb(skb); |
| 225 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 226 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 229 | 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] | 230 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 231 | { |
| 232 | struct sk_buff *skb; |
| 233 | struct mgmt_hdr *hdr; |
| 234 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 235 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 236 | |
| 237 | BT_DBG("sock %p", sk); |
| 238 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 239 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 240 | if (!skb) |
| 241 | return -ENOMEM; |
| 242 | |
| 243 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 244 | |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 245 | hdr->opcode = __constant_cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 246 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 247 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 248 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 249 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 250 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 251 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 252 | |
| 253 | if (rp) |
| 254 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 255 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 256 | err = sock_queue_rcv_skb(sk, skb); |
| 257 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 258 | kfree_skb(skb); |
| 259 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 260 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 263 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 264 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 265 | { |
| 266 | struct mgmt_rp_read_version rp; |
| 267 | |
| 268 | BT_DBG("sock %p", sk); |
| 269 | |
| 270 | rp.version = MGMT_VERSION; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 271 | rp.revision = __constant_cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 272 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 273 | 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] | 274 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 277 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 278 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 279 | { |
| 280 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 281 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 282 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 283 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 284 | size_t rp_size; |
| 285 | int i, err; |
| 286 | |
| 287 | BT_DBG("sock %p", sk); |
| 288 | |
| 289 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 290 | |
| 291 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 292 | if (!rp) |
| 293 | return -ENOMEM; |
| 294 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 295 | rp->num_commands = __constant_cpu_to_le16(num_commands); |
| 296 | rp->num_events = __constant_cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 297 | |
| 298 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 299 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 300 | |
| 301 | for (i = 0; i < num_events; i++, opcode++) |
| 302 | put_unaligned_le16(mgmt_events[i], opcode); |
| 303 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 304 | 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] | 305 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 306 | kfree(rp); |
| 307 | |
| 308 | return err; |
| 309 | } |
| 310 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 311 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 312 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 313 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 314 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 315 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 316 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 317 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 318 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 319 | |
| 320 | BT_DBG("sock %p", sk); |
| 321 | |
| 322 | read_lock(&hci_dev_list_lock); |
| 323 | |
| 324 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 325 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 326 | if (d->dev_type == HCI_BREDR) |
| 327 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 330 | rp_len = sizeof(*rp) + (2 * count); |
| 331 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 332 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 333 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 334 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 335 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 336 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 337 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 338 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 339 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 340 | continue; |
| 341 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 342 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 343 | continue; |
| 344 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 345 | if (d->dev_type == HCI_BREDR) { |
| 346 | rp->index[count++] = cpu_to_le16(d->id); |
| 347 | BT_DBG("Added hci%u", d->id); |
| 348 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 351 | rp->num_controllers = cpu_to_le16(count); |
| 352 | rp_len = sizeof(*rp) + (2 * count); |
| 353 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 354 | read_unlock(&hci_dev_list_lock); |
| 355 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 356 | 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] | 357 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 358 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 359 | kfree(rp); |
| 360 | |
| 361 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 364 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 365 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 366 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 367 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 368 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 369 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 370 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 371 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 372 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 373 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 374 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 375 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 376 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 377 | settings |= MGMT_SETTING_BREDR; |
| 378 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 379 | |
| 380 | if (lmp_ssp_capable(hdev)) { |
| 381 | settings |= MGMT_SETTING_SSP; |
| 382 | settings |= MGMT_SETTING_HS; |
| 383 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 384 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 385 | if (lmp_sc_capable(hdev) || |
| 386 | test_bit(HCI_FORCE_SC, &hdev->dev_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 387 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 388 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 389 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 390 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 391 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 392 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame^] | 393 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 394 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 395 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 396 | return settings; |
| 397 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 398 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 399 | static u32 get_current_settings(struct hci_dev *hdev) |
| 400 | { |
| 401 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 402 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 403 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 404 | settings |= MGMT_SETTING_POWERED; |
| 405 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 406 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 407 | settings |= MGMT_SETTING_CONNECTABLE; |
| 408 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 409 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 410 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 411 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 412 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 413 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 414 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 415 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 416 | settings |= MGMT_SETTING_PAIRABLE; |
| 417 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 418 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 419 | settings |= MGMT_SETTING_BREDR; |
| 420 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 421 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 422 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 423 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 424 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 425 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 426 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 427 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 428 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 429 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 430 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 431 | settings |= MGMT_SETTING_HS; |
| 432 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 433 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 434 | settings |= MGMT_SETTING_ADVERTISING; |
| 435 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 436 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 437 | settings |= MGMT_SETTING_SECURE_CONN; |
| 438 | |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 439 | if (test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags)) |
| 440 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 441 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame^] | 442 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 443 | settings |= MGMT_SETTING_PRIVACY; |
| 444 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 445 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 446 | } |
| 447 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 448 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 449 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 450 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 451 | { |
| 452 | u8 *ptr = data, *uuids_start = NULL; |
| 453 | struct bt_uuid *uuid; |
| 454 | |
| 455 | if (len < 4) |
| 456 | return ptr; |
| 457 | |
| 458 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 459 | u16 uuid16; |
| 460 | |
| 461 | if (uuid->size != 16) |
| 462 | continue; |
| 463 | |
| 464 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 465 | if (uuid16 < 0x1100) |
| 466 | continue; |
| 467 | |
| 468 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 469 | continue; |
| 470 | |
| 471 | if (!uuids_start) { |
| 472 | uuids_start = ptr; |
| 473 | uuids_start[0] = 1; |
| 474 | uuids_start[1] = EIR_UUID16_ALL; |
| 475 | ptr += 2; |
| 476 | } |
| 477 | |
| 478 | /* Stop if not enough space to put next UUID */ |
| 479 | if ((ptr - data) + sizeof(u16) > len) { |
| 480 | uuids_start[1] = EIR_UUID16_SOME; |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | *ptr++ = (uuid16 & 0x00ff); |
| 485 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 486 | uuids_start[0] += sizeof(uuid16); |
| 487 | } |
| 488 | |
| 489 | return ptr; |
| 490 | } |
| 491 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 492 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 493 | { |
| 494 | u8 *ptr = data, *uuids_start = NULL; |
| 495 | struct bt_uuid *uuid; |
| 496 | |
| 497 | if (len < 6) |
| 498 | return ptr; |
| 499 | |
| 500 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 501 | if (uuid->size != 32) |
| 502 | continue; |
| 503 | |
| 504 | if (!uuids_start) { |
| 505 | uuids_start = ptr; |
| 506 | uuids_start[0] = 1; |
| 507 | uuids_start[1] = EIR_UUID32_ALL; |
| 508 | ptr += 2; |
| 509 | } |
| 510 | |
| 511 | /* Stop if not enough space to put next UUID */ |
| 512 | if ((ptr - data) + sizeof(u32) > len) { |
| 513 | uuids_start[1] = EIR_UUID32_SOME; |
| 514 | break; |
| 515 | } |
| 516 | |
| 517 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 518 | ptr += sizeof(u32); |
| 519 | uuids_start[0] += sizeof(u32); |
| 520 | } |
| 521 | |
| 522 | return ptr; |
| 523 | } |
| 524 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 525 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 526 | { |
| 527 | u8 *ptr = data, *uuids_start = NULL; |
| 528 | struct bt_uuid *uuid; |
| 529 | |
| 530 | if (len < 18) |
| 531 | return ptr; |
| 532 | |
| 533 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 534 | if (uuid->size != 128) |
| 535 | continue; |
| 536 | |
| 537 | if (!uuids_start) { |
| 538 | uuids_start = ptr; |
| 539 | uuids_start[0] = 1; |
| 540 | uuids_start[1] = EIR_UUID128_ALL; |
| 541 | ptr += 2; |
| 542 | } |
| 543 | |
| 544 | /* Stop if not enough space to put next UUID */ |
| 545 | if ((ptr - data) + 16 > len) { |
| 546 | uuids_start[1] = EIR_UUID128_SOME; |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | memcpy(ptr, uuid->uuid, 16); |
| 551 | ptr += 16; |
| 552 | uuids_start[0] += 16; |
| 553 | } |
| 554 | |
| 555 | return ptr; |
| 556 | } |
| 557 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 558 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 559 | { |
| 560 | struct pending_cmd *cmd; |
| 561 | |
| 562 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 563 | if (cmd->opcode == opcode) |
| 564 | return cmd; |
| 565 | } |
| 566 | |
| 567 | return NULL; |
| 568 | } |
| 569 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 570 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 571 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 572 | u8 ad_len = 0; |
| 573 | size_t name_len; |
| 574 | |
| 575 | name_len = strlen(hdev->dev_name); |
| 576 | if (name_len > 0) { |
| 577 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 578 | |
| 579 | if (name_len > max_len) { |
| 580 | name_len = max_len; |
| 581 | ptr[1] = EIR_NAME_SHORT; |
| 582 | } else |
| 583 | ptr[1] = EIR_NAME_COMPLETE; |
| 584 | |
| 585 | ptr[0] = name_len + 1; |
| 586 | |
| 587 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 588 | |
| 589 | ad_len += (name_len + 2); |
| 590 | ptr += (name_len + 2); |
| 591 | } |
| 592 | |
| 593 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static void update_scan_rsp_data(struct hci_request *req) |
| 597 | { |
| 598 | struct hci_dev *hdev = req->hdev; |
| 599 | struct hci_cp_le_set_scan_rsp_data cp; |
| 600 | u8 len; |
| 601 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 602 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 603 | return; |
| 604 | |
| 605 | memset(&cp, 0, sizeof(cp)); |
| 606 | |
| 607 | len = create_scan_rsp_data(hdev, cp.data); |
| 608 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 609 | if (hdev->scan_rsp_data_len == len && |
| 610 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 611 | return; |
| 612 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 613 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 614 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 615 | |
| 616 | cp.length = len; |
| 617 | |
| 618 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 619 | } |
| 620 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 621 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 622 | { |
| 623 | struct pending_cmd *cmd; |
| 624 | |
| 625 | /* If there's a pending mgmt command the flags will not yet have |
| 626 | * their final values, so check for this first. |
| 627 | */ |
| 628 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 629 | if (cmd) { |
| 630 | struct mgmt_mode *cp = cmd->param; |
| 631 | if (cp->val == 0x01) |
| 632 | return LE_AD_GENERAL; |
| 633 | else if (cp->val == 0x02) |
| 634 | return LE_AD_LIMITED; |
| 635 | } else { |
| 636 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 637 | return LE_AD_LIMITED; |
| 638 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 639 | return LE_AD_GENERAL; |
| 640 | } |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 645 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 646 | { |
| 647 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 648 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 649 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 650 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 651 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 652 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 653 | |
| 654 | if (flags) { |
| 655 | BT_DBG("adv flags 0x%02x", flags); |
| 656 | |
| 657 | ptr[0] = 2; |
| 658 | ptr[1] = EIR_FLAGS; |
| 659 | ptr[2] = flags; |
| 660 | |
| 661 | ad_len += 3; |
| 662 | ptr += 3; |
| 663 | } |
| 664 | |
| 665 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 666 | ptr[0] = 2; |
| 667 | ptr[1] = EIR_TX_POWER; |
| 668 | ptr[2] = (u8) hdev->adv_tx_power; |
| 669 | |
| 670 | ad_len += 3; |
| 671 | ptr += 3; |
| 672 | } |
| 673 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 674 | return ad_len; |
| 675 | } |
| 676 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 677 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 678 | { |
| 679 | struct hci_dev *hdev = req->hdev; |
| 680 | struct hci_cp_le_set_adv_data cp; |
| 681 | u8 len; |
| 682 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 683 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 684 | return; |
| 685 | |
| 686 | memset(&cp, 0, sizeof(cp)); |
| 687 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 688 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 689 | |
| 690 | if (hdev->adv_data_len == len && |
| 691 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 692 | return; |
| 693 | |
| 694 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 695 | hdev->adv_data_len = len; |
| 696 | |
| 697 | cp.length = len; |
| 698 | |
| 699 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 700 | } |
| 701 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 702 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 703 | { |
| 704 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 705 | size_t name_len; |
| 706 | |
| 707 | name_len = strlen(hdev->dev_name); |
| 708 | |
| 709 | if (name_len > 0) { |
| 710 | /* EIR Data type */ |
| 711 | if (name_len > 48) { |
| 712 | name_len = 48; |
| 713 | ptr[1] = EIR_NAME_SHORT; |
| 714 | } else |
| 715 | ptr[1] = EIR_NAME_COMPLETE; |
| 716 | |
| 717 | /* EIR Data length */ |
| 718 | ptr[0] = name_len + 1; |
| 719 | |
| 720 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 721 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 722 | ptr += (name_len + 2); |
| 723 | } |
| 724 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 725 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 726 | ptr[0] = 2; |
| 727 | ptr[1] = EIR_TX_POWER; |
| 728 | ptr[2] = (u8) hdev->inq_tx_power; |
| 729 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 730 | ptr += 3; |
| 731 | } |
| 732 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 733 | if (hdev->devid_source > 0) { |
| 734 | ptr[0] = 9; |
| 735 | ptr[1] = EIR_DEVICE_ID; |
| 736 | |
| 737 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 738 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 739 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 740 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 741 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 742 | ptr += 10; |
| 743 | } |
| 744 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 745 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 746 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 747 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 748 | } |
| 749 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 750 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 751 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 752 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 753 | struct hci_cp_write_eir cp; |
| 754 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 755 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 756 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 757 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 758 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 759 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 760 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 761 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 762 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 763 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 764 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 765 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 766 | |
| 767 | memset(&cp, 0, sizeof(cp)); |
| 768 | |
| 769 | create_eir(hdev, cp.data); |
| 770 | |
| 771 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 772 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 773 | |
| 774 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 775 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 776 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 777 | } |
| 778 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 779 | static u8 get_service_classes(struct hci_dev *hdev) |
| 780 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 781 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 782 | u8 val = 0; |
| 783 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 784 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 785 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 786 | |
| 787 | return val; |
| 788 | } |
| 789 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 790 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 791 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 792 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 793 | u8 cod[3]; |
| 794 | |
| 795 | BT_DBG("%s", hdev->name); |
| 796 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 797 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 798 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 799 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 800 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 801 | return; |
| 802 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 803 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 804 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 805 | |
| 806 | cod[0] = hdev->minor_class; |
| 807 | cod[1] = hdev->major_class; |
| 808 | cod[2] = get_service_classes(hdev); |
| 809 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 810 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 811 | cod[1] |= 0x20; |
| 812 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 813 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 814 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 815 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 816 | 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] | 817 | } |
| 818 | |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 819 | static u8 get_adv_type(struct hci_dev *hdev) |
| 820 | { |
| 821 | struct pending_cmd *cmd; |
| 822 | bool connectable; |
| 823 | |
| 824 | /* If there's a pending mgmt command the flag will not yet have |
| 825 | * it's final value, so check for this first. |
| 826 | */ |
| 827 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 828 | if (cmd) { |
| 829 | struct mgmt_mode *cp = cmd->param; |
| 830 | connectable = !!cp->val; |
| 831 | } else { |
| 832 | connectable = test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 833 | } |
| 834 | |
| 835 | return connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
| 836 | } |
| 837 | |
| 838 | static void enable_advertising(struct hci_request *req) |
| 839 | { |
| 840 | struct hci_dev *hdev = req->hdev; |
| 841 | struct hci_cp_le_set_adv_param cp; |
| 842 | u8 enable = 0x01; |
| 843 | |
| 844 | memset(&cp, 0, sizeof(cp)); |
| 845 | cp.min_interval = __constant_cpu_to_le16(0x0800); |
| 846 | cp.max_interval = __constant_cpu_to_le16(0x0800); |
| 847 | cp.type = get_adv_type(hdev); |
| 848 | cp.own_address_type = hdev->own_addr_type; |
| 849 | cp.channel_map = hdev->le_adv_channel_map; |
| 850 | |
| 851 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 852 | |
| 853 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 854 | } |
| 855 | |
| 856 | static void disable_advertising(struct hci_request *req) |
| 857 | { |
| 858 | u8 enable = 0x00; |
| 859 | |
| 860 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 861 | } |
| 862 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 863 | static void service_cache_off(struct work_struct *work) |
| 864 | { |
| 865 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 866 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 867 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 868 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 869 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 870 | return; |
| 871 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 872 | hci_req_init(&req, hdev); |
| 873 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 874 | hci_dev_lock(hdev); |
| 875 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 876 | update_eir(&req); |
| 877 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 878 | |
| 879 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 880 | |
| 881 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 882 | } |
| 883 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 884 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 885 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 886 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 887 | return; |
| 888 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 889 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 890 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 891 | /* Non-mgmt controlled devices get this bit set |
| 892 | * implicitly so that pairing works for them, however |
| 893 | * for mgmt we require user-space to explicitly enable |
| 894 | * it |
| 895 | */ |
| 896 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 897 | } |
| 898 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 899 | 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] | 900 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 901 | { |
| 902 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 903 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 904 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 905 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 906 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 907 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 908 | memset(&rp, 0, sizeof(rp)); |
| 909 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 910 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 911 | |
| 912 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 913 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 914 | |
| 915 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 916 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 917 | |
| 918 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 919 | |
| 920 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 921 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 922 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 923 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 924 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 925 | 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] | 926 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 930 | { |
| 931 | sock_put(cmd->sk); |
| 932 | kfree(cmd->param); |
| 933 | kfree(cmd); |
| 934 | } |
| 935 | |
| 936 | 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] | 937 | struct hci_dev *hdev, void *data, |
| 938 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 939 | { |
| 940 | struct pending_cmd *cmd; |
| 941 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 942 | cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 943 | if (!cmd) |
| 944 | return NULL; |
| 945 | |
| 946 | cmd->opcode = opcode; |
| 947 | cmd->index = hdev->id; |
| 948 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 949 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 950 | if (!cmd->param) { |
| 951 | kfree(cmd); |
| 952 | return NULL; |
| 953 | } |
| 954 | |
| 955 | if (data) |
| 956 | memcpy(cmd->param, data, len); |
| 957 | |
| 958 | cmd->sk = sk; |
| 959 | sock_hold(sk); |
| 960 | |
| 961 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 962 | |
| 963 | return cmd; |
| 964 | } |
| 965 | |
| 966 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 967 | void (*cb)(struct pending_cmd *cmd, |
| 968 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 969 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 970 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 971 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 972 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 973 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 974 | if (opcode > 0 && cmd->opcode != opcode) |
| 975 | continue; |
| 976 | |
| 977 | cb(cmd, data); |
| 978 | } |
| 979 | } |
| 980 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 981 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 982 | { |
| 983 | list_del(&cmd->list); |
| 984 | mgmt_pending_free(cmd); |
| 985 | } |
| 986 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 987 | 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] | 988 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 989 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 990 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 991 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 992 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 993 | } |
| 994 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 995 | 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] | 996 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 997 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 998 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 999 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1000 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1001 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1002 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1003 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1004 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1005 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1006 | MGMT_STATUS_INVALID_PARAMS); |
| 1007 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1008 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1009 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1010 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1011 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1012 | MGMT_STATUS_BUSY); |
| 1013 | goto failed; |
| 1014 | } |
| 1015 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1016 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1017 | cancel_delayed_work(&hdev->power_off); |
| 1018 | |
| 1019 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1020 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1021 | data, len); |
| 1022 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1023 | goto failed; |
| 1024 | } |
| 1025 | } |
| 1026 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1027 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1028 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1029 | goto failed; |
| 1030 | } |
| 1031 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1032 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1033 | if (!cmd) { |
| 1034 | err = -ENOMEM; |
| 1035 | goto failed; |
| 1036 | } |
| 1037 | |
| 1038 | if (cp->val) |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1039 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1040 | else |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1041 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1042 | |
| 1043 | err = 0; |
| 1044 | |
| 1045 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1046 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1047 | return err; |
| 1048 | } |
| 1049 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1050 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1051 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1052 | { |
| 1053 | struct sk_buff *skb; |
| 1054 | struct mgmt_hdr *hdr; |
| 1055 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1056 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1057 | if (!skb) |
| 1058 | return -ENOMEM; |
| 1059 | |
| 1060 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1061 | hdr->opcode = cpu_to_le16(event); |
| 1062 | if (hdev) |
| 1063 | hdr->index = cpu_to_le16(hdev->id); |
| 1064 | else |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 1065 | hdr->index = __constant_cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1066 | hdr->len = cpu_to_le16(data_len); |
| 1067 | |
| 1068 | if (data) |
| 1069 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1070 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1071 | /* Time stamp */ |
| 1072 | __net_timestamp(skb); |
| 1073 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1074 | hci_send_to_control(skb, skip_sk); |
| 1075 | kfree_skb(skb); |
| 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1081 | { |
| 1082 | __le32 ev; |
| 1083 | |
| 1084 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1085 | |
| 1086 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1087 | } |
| 1088 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1089 | struct cmd_lookup { |
| 1090 | struct sock *sk; |
| 1091 | struct hci_dev *hdev; |
| 1092 | u8 mgmt_status; |
| 1093 | }; |
| 1094 | |
| 1095 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1096 | { |
| 1097 | struct cmd_lookup *match = data; |
| 1098 | |
| 1099 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1100 | |
| 1101 | list_del(&cmd->list); |
| 1102 | |
| 1103 | if (match->sk == NULL) { |
| 1104 | match->sk = cmd->sk; |
| 1105 | sock_hold(match->sk); |
| 1106 | } |
| 1107 | |
| 1108 | mgmt_pending_free(cmd); |
| 1109 | } |
| 1110 | |
| 1111 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1112 | { |
| 1113 | u8 *status = data; |
| 1114 | |
| 1115 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1116 | mgmt_pending_remove(cmd); |
| 1117 | } |
| 1118 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1119 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1120 | { |
| 1121 | if (!lmp_bredr_capable(hdev)) |
| 1122 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1123 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1124 | return MGMT_STATUS_REJECTED; |
| 1125 | else |
| 1126 | return MGMT_STATUS_SUCCESS; |
| 1127 | } |
| 1128 | |
| 1129 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1130 | { |
| 1131 | if (!lmp_le_capable(hdev)) |
| 1132 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1133 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1134 | return MGMT_STATUS_REJECTED; |
| 1135 | else |
| 1136 | return MGMT_STATUS_SUCCESS; |
| 1137 | } |
| 1138 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1139 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1140 | { |
| 1141 | struct pending_cmd *cmd; |
| 1142 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1143 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1144 | bool changed; |
| 1145 | |
| 1146 | BT_DBG("status 0x%02x", status); |
| 1147 | |
| 1148 | hci_dev_lock(hdev); |
| 1149 | |
| 1150 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1151 | if (!cmd) |
| 1152 | goto unlock; |
| 1153 | |
| 1154 | if (status) { |
| 1155 | u8 mgmt_err = mgmt_status(status); |
| 1156 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1157 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1158 | goto remove_cmd; |
| 1159 | } |
| 1160 | |
| 1161 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1162 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1163 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1164 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1165 | |
| 1166 | if (hdev->discov_timeout > 0) { |
| 1167 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1168 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1169 | to); |
| 1170 | } |
| 1171 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1172 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1173 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1174 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1175 | |
| 1176 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1177 | |
| 1178 | if (changed) |
| 1179 | new_settings(hdev, cmd->sk); |
| 1180 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1181 | /* When the discoverable mode gets changed, make sure |
| 1182 | * that class of device has the limited discoverable |
| 1183 | * bit correctly set. |
| 1184 | */ |
| 1185 | hci_req_init(&req, hdev); |
| 1186 | update_class(&req); |
| 1187 | hci_req_run(&req, NULL); |
| 1188 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1189 | remove_cmd: |
| 1190 | mgmt_pending_remove(cmd); |
| 1191 | |
| 1192 | unlock: |
| 1193 | hci_dev_unlock(hdev); |
| 1194 | } |
| 1195 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1196 | 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] | 1197 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1198 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1199 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1200 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1201 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1202 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1203 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1204 | int err; |
| 1205 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1206 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1207 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1208 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1209 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1210 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1211 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1212 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1213 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1214 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1215 | MGMT_STATUS_INVALID_PARAMS); |
| 1216 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1217 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1218 | |
| 1219 | /* Disabling discoverable requires that no timeout is set, |
| 1220 | * and enabling limited discoverable requires a timeout. |
| 1221 | */ |
| 1222 | if ((cp->val == 0x00 && timeout > 0) || |
| 1223 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1224 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1225 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1226 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1227 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1228 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1229 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1230 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1231 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1232 | goto failed; |
| 1233 | } |
| 1234 | |
| 1235 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1236 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1237 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1238 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1239 | goto failed; |
| 1240 | } |
| 1241 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1242 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1243 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1244 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1245 | goto failed; |
| 1246 | } |
| 1247 | |
| 1248 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1249 | bool changed = false; |
| 1250 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1251 | /* Setting limited discoverable when powered off is |
| 1252 | * not a valid operation since it requires a timeout |
| 1253 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1254 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1255 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1256 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1257 | changed = true; |
| 1258 | } |
| 1259 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1260 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1261 | if (err < 0) |
| 1262 | goto failed; |
| 1263 | |
| 1264 | if (changed) |
| 1265 | err = new_settings(hdev, sk); |
| 1266 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1267 | goto failed; |
| 1268 | } |
| 1269 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1270 | /* If the current mode is the same, then just update the timeout |
| 1271 | * value with the new value. And if only the timeout gets updated, |
| 1272 | * then no need for any HCI transactions. |
| 1273 | */ |
| 1274 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1275 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1276 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1277 | cancel_delayed_work(&hdev->discov_off); |
| 1278 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1279 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1280 | if (cp->val && hdev->discov_timeout > 0) { |
| 1281 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1282 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1283 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1284 | } |
| 1285 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1286 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1287 | goto failed; |
| 1288 | } |
| 1289 | |
| 1290 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1291 | if (!cmd) { |
| 1292 | err = -ENOMEM; |
| 1293 | goto failed; |
| 1294 | } |
| 1295 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1296 | /* Cancel any potential discoverable timeout that might be |
| 1297 | * still active and store new timeout value. The arming of |
| 1298 | * the timeout happens in the complete handler. |
| 1299 | */ |
| 1300 | cancel_delayed_work(&hdev->discov_off); |
| 1301 | hdev->discov_timeout = timeout; |
| 1302 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1303 | /* Limited discoverable mode */ |
| 1304 | if (cp->val == 0x02) |
| 1305 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1306 | else |
| 1307 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1308 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1309 | hci_req_init(&req, hdev); |
| 1310 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1311 | /* The procedure for LE-only controllers is much simpler - just |
| 1312 | * update the advertising data. |
| 1313 | */ |
| 1314 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1315 | goto update_ad; |
| 1316 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1317 | scan = SCAN_PAGE; |
| 1318 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1319 | if (cp->val) { |
| 1320 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1321 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1322 | if (cp->val == 0x02) { |
| 1323 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1324 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1325 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1326 | hci_cp.iac_lap[1] = 0x8b; |
| 1327 | hci_cp.iac_lap[2] = 0x9e; |
| 1328 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1329 | hci_cp.iac_lap[4] = 0x8b; |
| 1330 | hci_cp.iac_lap[5] = 0x9e; |
| 1331 | } else { |
| 1332 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1333 | hci_cp.num_iac = 1; |
| 1334 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1335 | hci_cp.iac_lap[1] = 0x8b; |
| 1336 | hci_cp.iac_lap[2] = 0x9e; |
| 1337 | } |
| 1338 | |
| 1339 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1340 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1341 | |
| 1342 | scan |= SCAN_INQUIRY; |
| 1343 | } else { |
| 1344 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1345 | } |
| 1346 | |
| 1347 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1348 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1349 | update_ad: |
| 1350 | update_adv_data(&req); |
| 1351 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1352 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1353 | if (err < 0) |
| 1354 | mgmt_pending_remove(cmd); |
| 1355 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1356 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1357 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1358 | return err; |
| 1359 | } |
| 1360 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1361 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1362 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1363 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1364 | struct hci_cp_write_page_scan_activity acp; |
| 1365 | u8 type; |
| 1366 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1367 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1368 | return; |
| 1369 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1370 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1371 | return; |
| 1372 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1373 | if (enable) { |
| 1374 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1375 | |
| 1376 | /* 160 msec page scan interval */ |
| 1377 | acp.interval = __constant_cpu_to_le16(0x0100); |
| 1378 | } else { |
| 1379 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1380 | |
| 1381 | /* default 1.28 sec page scan */ |
| 1382 | acp.interval = __constant_cpu_to_le16(0x0800); |
| 1383 | } |
| 1384 | |
| 1385 | acp.window = __constant_cpu_to_le16(0x0012); |
| 1386 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1387 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1388 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1389 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1390 | sizeof(acp), &acp); |
| 1391 | |
| 1392 | if (hdev->page_scan_type != type) |
| 1393 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1394 | } |
| 1395 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1396 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1397 | { |
| 1398 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1399 | struct mgmt_mode *cp; |
| 1400 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1401 | |
| 1402 | BT_DBG("status 0x%02x", status); |
| 1403 | |
| 1404 | hci_dev_lock(hdev); |
| 1405 | |
| 1406 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1407 | if (!cmd) |
| 1408 | goto unlock; |
| 1409 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1410 | if (status) { |
| 1411 | u8 mgmt_err = mgmt_status(status); |
| 1412 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1413 | goto remove_cmd; |
| 1414 | } |
| 1415 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1416 | cp = cmd->param; |
| 1417 | if (cp->val) |
| 1418 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1419 | else |
| 1420 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1421 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1422 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1423 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1424 | if (changed) |
| 1425 | new_settings(hdev, cmd->sk); |
| 1426 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1427 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1428 | mgmt_pending_remove(cmd); |
| 1429 | |
| 1430 | unlock: |
| 1431 | hci_dev_unlock(hdev); |
| 1432 | } |
| 1433 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1434 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1435 | struct sock *sk, u8 val) |
| 1436 | { |
| 1437 | bool changed = false; |
| 1438 | int err; |
| 1439 | |
| 1440 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1441 | changed = true; |
| 1442 | |
| 1443 | if (val) { |
| 1444 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1445 | } else { |
| 1446 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1447 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1448 | } |
| 1449 | |
| 1450 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1451 | if (err < 0) |
| 1452 | return err; |
| 1453 | |
| 1454 | if (changed) |
| 1455 | return new_settings(hdev, sk); |
| 1456 | |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1460 | 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] | 1461 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1462 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1463 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1464 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1465 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1466 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1467 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1468 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1469 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1470 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1471 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1472 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1473 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1474 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1475 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1476 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1477 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1478 | MGMT_STATUS_INVALID_PARAMS); |
| 1479 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1480 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1481 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1482 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1483 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1484 | goto failed; |
| 1485 | } |
| 1486 | |
| 1487 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1488 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1489 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1490 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1491 | goto failed; |
| 1492 | } |
| 1493 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1494 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1495 | if (!cmd) { |
| 1496 | err = -ENOMEM; |
| 1497 | goto failed; |
| 1498 | } |
| 1499 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1500 | hci_req_init(&req, hdev); |
| 1501 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1502 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1503 | * by-product of disabling connectable, we need to update the |
| 1504 | * advertising flags. |
| 1505 | */ |
| 1506 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1507 | if (!cp->val) { |
| 1508 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1509 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1510 | } |
| 1511 | update_adv_data(&req); |
| 1512 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1513 | if (cp->val) { |
| 1514 | scan = SCAN_PAGE; |
| 1515 | } else { |
| 1516 | scan = 0; |
| 1517 | |
| 1518 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1519 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1520 | cancel_delayed_work(&hdev->discov_off); |
| 1521 | } |
| 1522 | |
| 1523 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1524 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1525 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1526 | /* If we're going from non-connectable to connectable or |
| 1527 | * vice-versa when fast connectable is enabled ensure that fast |
| 1528 | * connectable gets disabled. write_fast_connectable won't do |
| 1529 | * anything if the page scan parameters are already what they |
| 1530 | * should be. |
| 1531 | */ |
| 1532 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1533 | write_fast_connectable(&req, false); |
| 1534 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1535 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1536 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1537 | disable_advertising(&req); |
| 1538 | enable_advertising(&req); |
| 1539 | } |
| 1540 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1541 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1542 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1543 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1544 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1545 | err = set_connectable_update_settings(hdev, sk, |
| 1546 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1547 | goto failed; |
| 1548 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1549 | |
| 1550 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1551 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1552 | return err; |
| 1553 | } |
| 1554 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1555 | 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] | 1556 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1557 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1558 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1559 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1560 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1561 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1562 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1563 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1564 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1565 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1566 | MGMT_STATUS_INVALID_PARAMS); |
| 1567 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1568 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1569 | |
| 1570 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1571 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1572 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1573 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1574 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1575 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1576 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1577 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1578 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1579 | if (changed) |
| 1580 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1581 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1582 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1583 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1584 | return err; |
| 1585 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1586 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1587 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1588 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1589 | { |
| 1590 | struct mgmt_mode *cp = data; |
| 1591 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1592 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1593 | int err; |
| 1594 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1595 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1596 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1597 | status = mgmt_bredr_support(hdev); |
| 1598 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1599 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1600 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1601 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1602 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1603 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1604 | MGMT_STATUS_INVALID_PARAMS); |
| 1605 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1606 | hci_dev_lock(hdev); |
| 1607 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1608 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1609 | bool changed = false; |
| 1610 | |
| 1611 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1612 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1613 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1614 | changed = true; |
| 1615 | } |
| 1616 | |
| 1617 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1618 | if (err < 0) |
| 1619 | goto failed; |
| 1620 | |
| 1621 | if (changed) |
| 1622 | err = new_settings(hdev, sk); |
| 1623 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1624 | goto failed; |
| 1625 | } |
| 1626 | |
| 1627 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1628 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1629 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1630 | goto failed; |
| 1631 | } |
| 1632 | |
| 1633 | val = !!cp->val; |
| 1634 | |
| 1635 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1636 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1637 | goto failed; |
| 1638 | } |
| 1639 | |
| 1640 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1641 | if (!cmd) { |
| 1642 | err = -ENOMEM; |
| 1643 | goto failed; |
| 1644 | } |
| 1645 | |
| 1646 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1647 | if (err < 0) { |
| 1648 | mgmt_pending_remove(cmd); |
| 1649 | goto failed; |
| 1650 | } |
| 1651 | |
| 1652 | failed: |
| 1653 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1654 | return err; |
| 1655 | } |
| 1656 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1657 | 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] | 1658 | { |
| 1659 | struct mgmt_mode *cp = data; |
| 1660 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1661 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1662 | int err; |
| 1663 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1664 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1665 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1666 | status = mgmt_bredr_support(hdev); |
| 1667 | if (status) |
| 1668 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1669 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1670 | if (!lmp_ssp_capable(hdev)) |
| 1671 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1672 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1673 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1674 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1675 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1676 | MGMT_STATUS_INVALID_PARAMS); |
| 1677 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1678 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1679 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1680 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1681 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1682 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1683 | if (cp->val) { |
| 1684 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1685 | &hdev->dev_flags); |
| 1686 | } else { |
| 1687 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1688 | &hdev->dev_flags); |
| 1689 | if (!changed) |
| 1690 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1691 | &hdev->dev_flags); |
| 1692 | else |
| 1693 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1697 | if (err < 0) |
| 1698 | goto failed; |
| 1699 | |
| 1700 | if (changed) |
| 1701 | err = new_settings(hdev, sk); |
| 1702 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1703 | goto failed; |
| 1704 | } |
| 1705 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1706 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1707 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1708 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1709 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1710 | goto failed; |
| 1711 | } |
| 1712 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1713 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1714 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1715 | goto failed; |
| 1716 | } |
| 1717 | |
| 1718 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1719 | if (!cmd) { |
| 1720 | err = -ENOMEM; |
| 1721 | goto failed; |
| 1722 | } |
| 1723 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1724 | 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] | 1725 | if (err < 0) { |
| 1726 | mgmt_pending_remove(cmd); |
| 1727 | goto failed; |
| 1728 | } |
| 1729 | |
| 1730 | failed: |
| 1731 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1732 | return err; |
| 1733 | } |
| 1734 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1735 | 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] | 1736 | { |
| 1737 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1738 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1739 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1740 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50: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 | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1743 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1744 | status = mgmt_bredr_support(hdev); |
| 1745 | if (status) |
| 1746 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1747 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1748 | if (!lmp_ssp_capable(hdev)) |
| 1749 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1750 | MGMT_STATUS_NOT_SUPPORTED); |
| 1751 | |
| 1752 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1753 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1754 | MGMT_STATUS_REJECTED); |
| 1755 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1756 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1757 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1758 | MGMT_STATUS_INVALID_PARAMS); |
| 1759 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1760 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1761 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1762 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1763 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1764 | } else { |
| 1765 | if (hdev_is_powered(hdev)) { |
| 1766 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1767 | MGMT_STATUS_REJECTED); |
| 1768 | goto unlock; |
| 1769 | } |
| 1770 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1771 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1772 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1773 | |
| 1774 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1775 | if (err < 0) |
| 1776 | goto unlock; |
| 1777 | |
| 1778 | if (changed) |
| 1779 | err = new_settings(hdev, sk); |
| 1780 | |
| 1781 | unlock: |
| 1782 | hci_dev_unlock(hdev); |
| 1783 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1784 | } |
| 1785 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1786 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1787 | { |
| 1788 | struct cmd_lookup match = { NULL, hdev }; |
| 1789 | |
| 1790 | if (status) { |
| 1791 | u8 mgmt_err = mgmt_status(status); |
| 1792 | |
| 1793 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1794 | &mgmt_err); |
| 1795 | return; |
| 1796 | } |
| 1797 | |
| 1798 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1799 | |
| 1800 | new_settings(hdev, match.sk); |
| 1801 | |
| 1802 | if (match.sk) |
| 1803 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1804 | |
| 1805 | /* Make sure the controller has a good default for |
| 1806 | * advertising data. Restrict the update to when LE |
| 1807 | * has actually been enabled. During power on, the |
| 1808 | * update in powered_update_hci will take care of it. |
| 1809 | */ |
| 1810 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1811 | struct hci_request req; |
| 1812 | |
| 1813 | hci_dev_lock(hdev); |
| 1814 | |
| 1815 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 1816 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 1817 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1818 | hci_req_run(&req, NULL); |
| 1819 | |
| 1820 | hci_dev_unlock(hdev); |
| 1821 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1822 | } |
| 1823 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1824 | 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] | 1825 | { |
| 1826 | struct mgmt_mode *cp = data; |
| 1827 | struct hci_cp_write_le_host_supported hci_cp; |
| 1828 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1829 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1830 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1831 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1832 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1833 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1834 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1835 | if (!lmp_le_capable(hdev)) |
| 1836 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1837 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1838 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1839 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1840 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1841 | MGMT_STATUS_INVALID_PARAMS); |
| 1842 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1843 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1844 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1845 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1846 | MGMT_STATUS_REJECTED); |
| 1847 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1848 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1849 | |
| 1850 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1851 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1852 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1853 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1854 | bool changed = false; |
| 1855 | |
| 1856 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1857 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 1858 | changed = true; |
| 1859 | } |
| 1860 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 1861 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 1862 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1863 | changed = true; |
| 1864 | } |
| 1865 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1866 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1867 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1868 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1869 | |
| 1870 | if (changed) |
| 1871 | err = new_settings(hdev, sk); |
| 1872 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1873 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1874 | } |
| 1875 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 1876 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 1877 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1878 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1879 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1880 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1884 | if (!cmd) { |
| 1885 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1886 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1887 | } |
| 1888 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1889 | hci_req_init(&req, hdev); |
| 1890 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1891 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1892 | |
| 1893 | if (val) { |
| 1894 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1895 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1896 | } else { |
| 1897 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1898 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1899 | } |
| 1900 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1901 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 1902 | &hci_cp); |
| 1903 | |
| 1904 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1905 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1906 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1907 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1908 | unlock: |
| 1909 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1910 | return err; |
| 1911 | } |
| 1912 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1913 | /* This is a helper function to test for pending mgmt commands that can |
| 1914 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 1915 | * mgmt command at a time since otherwise we cannot easily track what |
| 1916 | * the current values are, will be, and based on that calculate if a new |
| 1917 | * HCI command needs to be sent and if yes with what value. |
| 1918 | */ |
| 1919 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 1920 | { |
| 1921 | struct pending_cmd *cmd; |
| 1922 | |
| 1923 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 1924 | switch (cmd->opcode) { |
| 1925 | case MGMT_OP_ADD_UUID: |
| 1926 | case MGMT_OP_REMOVE_UUID: |
| 1927 | case MGMT_OP_SET_DEV_CLASS: |
| 1928 | case MGMT_OP_SET_POWERED: |
| 1929 | return true; |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | return false; |
| 1934 | } |
| 1935 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 1936 | static const u8 bluetooth_base_uuid[] = { |
| 1937 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 1938 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1939 | }; |
| 1940 | |
| 1941 | static u8 get_uuid_size(const u8 *uuid) |
| 1942 | { |
| 1943 | u32 val; |
| 1944 | |
| 1945 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 1946 | return 128; |
| 1947 | |
| 1948 | val = get_unaligned_le32(&uuid[12]); |
| 1949 | if (val > 0xffff) |
| 1950 | return 32; |
| 1951 | |
| 1952 | return 16; |
| 1953 | } |
| 1954 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1955 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 1956 | { |
| 1957 | struct pending_cmd *cmd; |
| 1958 | |
| 1959 | hci_dev_lock(hdev); |
| 1960 | |
| 1961 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 1962 | if (!cmd) |
| 1963 | goto unlock; |
| 1964 | |
| 1965 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 1966 | hdev->dev_class, 3); |
| 1967 | |
| 1968 | mgmt_pending_remove(cmd); |
| 1969 | |
| 1970 | unlock: |
| 1971 | hci_dev_unlock(hdev); |
| 1972 | } |
| 1973 | |
| 1974 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 1975 | { |
| 1976 | BT_DBG("status 0x%02x", status); |
| 1977 | |
| 1978 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 1979 | } |
| 1980 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1981 | 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] | 1982 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1983 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1984 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1985 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1986 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1987 | int err; |
| 1988 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1989 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1990 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1991 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1992 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1993 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1994 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1995 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1996 | goto failed; |
| 1997 | } |
| 1998 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 1999 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2000 | if (!uuid) { |
| 2001 | err = -ENOMEM; |
| 2002 | goto failed; |
| 2003 | } |
| 2004 | |
| 2005 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2006 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2007 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2008 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2009 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2010 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2011 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2012 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2013 | update_class(&req); |
| 2014 | update_eir(&req); |
| 2015 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2016 | err = hci_req_run(&req, add_uuid_complete); |
| 2017 | if (err < 0) { |
| 2018 | if (err != -ENODATA) |
| 2019 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2020 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2021 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2022 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2023 | goto failed; |
| 2024 | } |
| 2025 | |
| 2026 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2027 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2028 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2029 | goto failed; |
| 2030 | } |
| 2031 | |
| 2032 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2033 | |
| 2034 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2035 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2036 | return err; |
| 2037 | } |
| 2038 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2039 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2040 | { |
| 2041 | if (!hdev_is_powered(hdev)) |
| 2042 | return false; |
| 2043 | |
| 2044 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2045 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2046 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2047 | return true; |
| 2048 | } |
| 2049 | |
| 2050 | return false; |
| 2051 | } |
| 2052 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2053 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2054 | { |
| 2055 | BT_DBG("status 0x%02x", status); |
| 2056 | |
| 2057 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2058 | } |
| 2059 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2060 | 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] | 2061 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2062 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2063 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2064 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2065 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2066 | 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] | 2067 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2068 | int err, found; |
| 2069 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2070 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2071 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2072 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2073 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2074 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2075 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2076 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2077 | goto unlock; |
| 2078 | } |
| 2079 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2080 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2081 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2082 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2083 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2084 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2085 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2086 | goto unlock; |
| 2087 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2088 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2089 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | found = 0; |
| 2093 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2094 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2095 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2096 | continue; |
| 2097 | |
| 2098 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2099 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2100 | found++; |
| 2101 | } |
| 2102 | |
| 2103 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2104 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2105 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2106 | goto unlock; |
| 2107 | } |
| 2108 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2109 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2110 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2111 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2112 | update_class(&req); |
| 2113 | update_eir(&req); |
| 2114 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2115 | err = hci_req_run(&req, remove_uuid_complete); |
| 2116 | if (err < 0) { |
| 2117 | if (err != -ENODATA) |
| 2118 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2119 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2120 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2121 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2122 | goto unlock; |
| 2123 | } |
| 2124 | |
| 2125 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2126 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2127 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2128 | goto unlock; |
| 2129 | } |
| 2130 | |
| 2131 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2132 | |
| 2133 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2134 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2135 | return err; |
| 2136 | } |
| 2137 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2138 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2139 | { |
| 2140 | BT_DBG("status 0x%02x", status); |
| 2141 | |
| 2142 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2143 | } |
| 2144 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2145 | 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] | 2146 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2147 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2148 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2149 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2150 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2151 | int err; |
| 2152 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2153 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2154 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2155 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2156 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2157 | MGMT_STATUS_NOT_SUPPORTED); |
| 2158 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2159 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2160 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2161 | if (pending_eir_or_class(hdev)) { |
| 2162 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2163 | MGMT_STATUS_BUSY); |
| 2164 | goto unlock; |
| 2165 | } |
| 2166 | |
| 2167 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2168 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2169 | MGMT_STATUS_INVALID_PARAMS); |
| 2170 | goto unlock; |
| 2171 | } |
| 2172 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2173 | hdev->major_class = cp->major; |
| 2174 | hdev->minor_class = cp->minor; |
| 2175 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2176 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2177 | 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] | 2178 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2179 | goto unlock; |
| 2180 | } |
| 2181 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2182 | hci_req_init(&req, hdev); |
| 2183 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2184 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2185 | hci_dev_unlock(hdev); |
| 2186 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2187 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2188 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2189 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2190 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2191 | update_class(&req); |
| 2192 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2193 | err = hci_req_run(&req, set_class_complete); |
| 2194 | if (err < 0) { |
| 2195 | if (err != -ENODATA) |
| 2196 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2197 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2198 | 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] | 2199 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2200 | goto unlock; |
| 2201 | } |
| 2202 | |
| 2203 | 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] | 2204 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2205 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2206 | goto unlock; |
| 2207 | } |
| 2208 | |
| 2209 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2210 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2211 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2212 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2213 | return err; |
| 2214 | } |
| 2215 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2216 | 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] | 2217 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2218 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2219 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2220 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2221 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2222 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2223 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2224 | BT_DBG("request for %s", hdev->name); |
| 2225 | |
| 2226 | if (!lmp_bredr_capable(hdev)) |
| 2227 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2228 | MGMT_STATUS_NOT_SUPPORTED); |
| 2229 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2230 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2231 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2232 | expected_len = sizeof(*cp) + key_count * |
| 2233 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2234 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2235 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2236 | len, expected_len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2237 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2238 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2239 | } |
| 2240 | |
Johan Hedberg | 4ae1430 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2241 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2242 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2243 | MGMT_STATUS_INVALID_PARAMS); |
| 2244 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2245 | 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] | 2246 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2247 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2248 | for (i = 0; i < key_count; i++) { |
| 2249 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2250 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2251 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2252 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2253 | MGMT_STATUS_INVALID_PARAMS); |
| 2254 | } |
| 2255 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2256 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2257 | |
| 2258 | hci_link_keys_clear(hdev); |
| 2259 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2260 | if (cp->debug_keys) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2261 | changed = !test_and_set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2262 | else |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2263 | changed = test_and_clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 2264 | |
| 2265 | if (changed) |
| 2266 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2267 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2268 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2269 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2270 | |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 2271 | hci_add_link_key(hdev, NULL, 0, &key->addr.bdaddr, key->val, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2272 | key->type, key->pin_len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2273 | } |
| 2274 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2275 | 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] | 2276 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2277 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2278 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2279 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2280 | } |
| 2281 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2282 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2283 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2284 | { |
| 2285 | struct mgmt_ev_device_unpaired ev; |
| 2286 | |
| 2287 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2288 | ev.addr.type = addr_type; |
| 2289 | |
| 2290 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2291 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2292 | } |
| 2293 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2294 | 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] | 2295 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2296 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2297 | struct mgmt_cp_unpair_device *cp = data; |
| 2298 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2299 | struct hci_cp_disconnect dc; |
| 2300 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2301 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2302 | int err; |
| 2303 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2304 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2305 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2306 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2307 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2308 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2309 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2310 | MGMT_STATUS_INVALID_PARAMS, |
| 2311 | &rp, sizeof(rp)); |
| 2312 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2313 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2314 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2315 | MGMT_STATUS_INVALID_PARAMS, |
| 2316 | &rp, sizeof(rp)); |
| 2317 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2318 | hci_dev_lock(hdev); |
| 2319 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2320 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2321 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2322 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2323 | goto unlock; |
| 2324 | } |
| 2325 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2326 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2327 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2328 | } else { |
| 2329 | u8 addr_type; |
| 2330 | |
| 2331 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2332 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2333 | else |
| 2334 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2335 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2336 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2337 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2338 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2339 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2340 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2341 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2342 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2343 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2344 | goto unlock; |
| 2345 | } |
| 2346 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2347 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2348 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2349 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2350 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2351 | else |
| 2352 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2353 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2354 | } else { |
| 2355 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2356 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2357 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2358 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2359 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2360 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2361 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2362 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2363 | } |
| 2364 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2365 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2366 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2367 | if (!cmd) { |
| 2368 | err = -ENOMEM; |
| 2369 | goto unlock; |
| 2370 | } |
| 2371 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2372 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2373 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2374 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2375 | if (err < 0) |
| 2376 | mgmt_pending_remove(cmd); |
| 2377 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2378 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2379 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2380 | return err; |
| 2381 | } |
| 2382 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2383 | 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] | 2384 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2385 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2386 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2387 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2388 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2389 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2390 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2391 | int err; |
| 2392 | |
| 2393 | BT_DBG(""); |
| 2394 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2395 | memset(&rp, 0, sizeof(rp)); |
| 2396 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2397 | rp.addr.type = cp->addr.type; |
| 2398 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2399 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2400 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2401 | MGMT_STATUS_INVALID_PARAMS, |
| 2402 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2403 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2404 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2405 | |
| 2406 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2407 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2408 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2409 | goto failed; |
| 2410 | } |
| 2411 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2412 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2413 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2414 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2415 | goto failed; |
| 2416 | } |
| 2417 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2418 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2419 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2420 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2421 | else |
| 2422 | 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] | 2423 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2424 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2425 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2426 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2427 | goto failed; |
| 2428 | } |
| 2429 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2430 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2431 | if (!cmd) { |
| 2432 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2433 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2434 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2435 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2436 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2437 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2438 | |
| 2439 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2440 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2441 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2442 | |
| 2443 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2444 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2445 | return err; |
| 2446 | } |
| 2447 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2448 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2449 | { |
| 2450 | switch (link_type) { |
| 2451 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2452 | switch (addr_type) { |
| 2453 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2454 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2455 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2456 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2457 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2458 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2459 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2460 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2461 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2462 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2463 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2467 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2468 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2469 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2470 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2471 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2472 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2473 | int err; |
| 2474 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2475 | |
| 2476 | BT_DBG(""); |
| 2477 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2478 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2479 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2480 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2481 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2482 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2483 | goto unlock; |
| 2484 | } |
| 2485 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2486 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2487 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2488 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2489 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2490 | } |
| 2491 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2492 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2493 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2494 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2495 | err = -ENOMEM; |
| 2496 | goto unlock; |
| 2497 | } |
| 2498 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2499 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2500 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2501 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2502 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2503 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2504 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2505 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2506 | continue; |
| 2507 | i++; |
| 2508 | } |
| 2509 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2510 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2511 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2512 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2513 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2514 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2515 | 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] | 2516 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2517 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2518 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2519 | |
| 2520 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2521 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2522 | return err; |
| 2523 | } |
| 2524 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2525 | 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] | 2526 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2527 | { |
| 2528 | struct pending_cmd *cmd; |
| 2529 | int err; |
| 2530 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2531 | 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] | 2532 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2533 | if (!cmd) |
| 2534 | return -ENOMEM; |
| 2535 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2536 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2537 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2538 | if (err < 0) |
| 2539 | mgmt_pending_remove(cmd); |
| 2540 | |
| 2541 | return err; |
| 2542 | } |
| 2543 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2544 | 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] | 2545 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2546 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2547 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2548 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2549 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2550 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2551 | int err; |
| 2552 | |
| 2553 | BT_DBG(""); |
| 2554 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2555 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2556 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2557 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2558 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2559 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2560 | goto failed; |
| 2561 | } |
| 2562 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2563 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2564 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2565 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2566 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2567 | goto failed; |
| 2568 | } |
| 2569 | |
| 2570 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2571 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2572 | |
| 2573 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2574 | |
| 2575 | BT_ERR("PIN code is not 16 bytes long"); |
| 2576 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2577 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2578 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2579 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2580 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2581 | |
| 2582 | goto failed; |
| 2583 | } |
| 2584 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2585 | 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] | 2586 | if (!cmd) { |
| 2587 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2588 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2589 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2590 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2591 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2592 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2593 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2594 | |
| 2595 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2596 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2597 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2598 | |
| 2599 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2600 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2601 | return err; |
| 2602 | } |
| 2603 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2604 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2605 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2606 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2607 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2608 | |
| 2609 | BT_DBG(""); |
| 2610 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2611 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2612 | |
| 2613 | hdev->io_capability = cp->io_capability; |
| 2614 | |
| 2615 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2616 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2617 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2618 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2619 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2620 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2621 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2622 | } |
| 2623 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2624 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2625 | { |
| 2626 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2627 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2628 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2629 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2630 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2631 | continue; |
| 2632 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2633 | if (cmd->user_data != conn) |
| 2634 | continue; |
| 2635 | |
| 2636 | return cmd; |
| 2637 | } |
| 2638 | |
| 2639 | return NULL; |
| 2640 | } |
| 2641 | |
| 2642 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2643 | { |
| 2644 | struct mgmt_rp_pair_device rp; |
| 2645 | struct hci_conn *conn = cmd->user_data; |
| 2646 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 2647 | bacpy(&rp.addr.bdaddr, &conn->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2648 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2649 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2650 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2651 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2652 | |
| 2653 | /* So we don't get further callbacks for this connection */ |
| 2654 | conn->connect_cfm_cb = NULL; |
| 2655 | conn->security_cfm_cb = NULL; |
| 2656 | conn->disconn_cfm_cb = NULL; |
| 2657 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2658 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2659 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2660 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2661 | } |
| 2662 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2663 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2664 | { |
| 2665 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2666 | struct pending_cmd *cmd; |
| 2667 | |
| 2668 | cmd = find_pairing(conn); |
| 2669 | if (cmd) |
| 2670 | pairing_complete(cmd, status); |
| 2671 | } |
| 2672 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2673 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2674 | { |
| 2675 | struct pending_cmd *cmd; |
| 2676 | |
| 2677 | BT_DBG("status %u", status); |
| 2678 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2679 | cmd = find_pairing(conn); |
| 2680 | if (!cmd) |
| 2681 | BT_DBG("Unable to find a pending command"); |
| 2682 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2683 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2684 | } |
| 2685 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2686 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2687 | { |
| 2688 | struct pending_cmd *cmd; |
| 2689 | |
| 2690 | BT_DBG("status %u", status); |
| 2691 | |
| 2692 | if (!status) |
| 2693 | return; |
| 2694 | |
| 2695 | cmd = find_pairing(conn); |
| 2696 | if (!cmd) |
| 2697 | BT_DBG("Unable to find a pending command"); |
| 2698 | else |
| 2699 | pairing_complete(cmd, mgmt_status(status)); |
| 2700 | } |
| 2701 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2702 | 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] | 2703 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2704 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2705 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2706 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2707 | struct pending_cmd *cmd; |
| 2708 | u8 sec_level, auth_type; |
| 2709 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2710 | int err; |
| 2711 | |
| 2712 | BT_DBG(""); |
| 2713 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2714 | memset(&rp, 0, sizeof(rp)); |
| 2715 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2716 | rp.addr.type = cp->addr.type; |
| 2717 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2718 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2719 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2720 | MGMT_STATUS_INVALID_PARAMS, |
| 2721 | &rp, sizeof(rp)); |
| 2722 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2723 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2724 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2725 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2726 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2727 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2728 | goto unlock; |
| 2729 | } |
| 2730 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2731 | sec_level = BT_SECURITY_MEDIUM; |
| 2732 | if (cp->io_cap == 0x03) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2733 | auth_type = HCI_AT_DEDICATED_BONDING; |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2734 | else |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2735 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2736 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2737 | if (cp->addr.type == BDADDR_BREDR) |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 2738 | conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr, |
| 2739 | cp->addr.type, sec_level, auth_type); |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2740 | else |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 2741 | conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr, |
| 2742 | cp->addr.type, sec_level, auth_type); |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2743 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2744 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2745 | int status; |
| 2746 | |
| 2747 | if (PTR_ERR(conn) == -EBUSY) |
| 2748 | status = MGMT_STATUS_BUSY; |
| 2749 | else |
| 2750 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2751 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2752 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2753 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2754 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2755 | goto unlock; |
| 2756 | } |
| 2757 | |
| 2758 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2759 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2760 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2761 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2762 | goto unlock; |
| 2763 | } |
| 2764 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2765 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2766 | if (!cmd) { |
| 2767 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2768 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2769 | goto unlock; |
| 2770 | } |
| 2771 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2772 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2773 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2774 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2775 | conn->security_cfm_cb = pairing_complete_cb; |
| 2776 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2777 | } else { |
| 2778 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2779 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2780 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2781 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2782 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2783 | conn->io_capability = cp->io_cap; |
| 2784 | cmd->user_data = conn; |
| 2785 | |
| 2786 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2787 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2788 | pairing_complete(cmd, 0); |
| 2789 | |
| 2790 | err = 0; |
| 2791 | |
| 2792 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2793 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2794 | return err; |
| 2795 | } |
| 2796 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2797 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2798 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2799 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2800 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2801 | struct pending_cmd *cmd; |
| 2802 | struct hci_conn *conn; |
| 2803 | int err; |
| 2804 | |
| 2805 | BT_DBG(""); |
| 2806 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2807 | hci_dev_lock(hdev); |
| 2808 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2809 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2810 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2811 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2812 | goto unlock; |
| 2813 | } |
| 2814 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2815 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 2816 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2817 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2818 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2819 | goto unlock; |
| 2820 | } |
| 2821 | |
| 2822 | conn = cmd->user_data; |
| 2823 | |
| 2824 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2825 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2826 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2827 | goto unlock; |
| 2828 | } |
| 2829 | |
| 2830 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 2831 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2832 | 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] | 2833 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2834 | unlock: |
| 2835 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2836 | return err; |
| 2837 | } |
| 2838 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2839 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2840 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2841 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2842 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2843 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2844 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2845 | int err; |
| 2846 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2847 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 2848 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2849 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2850 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2851 | MGMT_STATUS_NOT_POWERED, addr, |
| 2852 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2853 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2854 | } |
| 2855 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2856 | if (addr->type == BDADDR_BREDR) |
| 2857 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2858 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2859 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2860 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2861 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2862 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2863 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 2864 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2865 | goto done; |
| 2866 | } |
| 2867 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2868 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2869 | /* Continue with pairing via SMP */ |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2870 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2871 | |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2872 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2873 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2874 | MGMT_STATUS_SUCCESS, addr, |
| 2875 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2876 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2877 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2878 | MGMT_STATUS_FAILED, addr, |
| 2879 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2880 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2881 | goto done; |
| 2882 | } |
| 2883 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2884 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2885 | if (!cmd) { |
| 2886 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2887 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2888 | } |
| 2889 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2890 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2891 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 2892 | struct hci_cp_user_passkey_reply cp; |
| 2893 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2894 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2895 | cp.passkey = passkey; |
| 2896 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 2897 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2898 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 2899 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2900 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2901 | if (err < 0) |
| 2902 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2903 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2904 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2905 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2906 | return err; |
| 2907 | } |
| 2908 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 2909 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 2910 | void *data, u16 len) |
| 2911 | { |
| 2912 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 2913 | |
| 2914 | BT_DBG(""); |
| 2915 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2916 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 2917 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 2918 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 2919 | } |
| 2920 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2921 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2922 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2923 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2924 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2925 | |
| 2926 | BT_DBG(""); |
| 2927 | |
| 2928 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2929 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2930 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2931 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2932 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2933 | MGMT_OP_USER_CONFIRM_REPLY, |
| 2934 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2935 | } |
| 2936 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2937 | 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] | 2938 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2939 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 2940 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2941 | |
| 2942 | BT_DBG(""); |
| 2943 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2944 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2945 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 2946 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2947 | } |
| 2948 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2949 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2950 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2951 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2952 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2953 | |
| 2954 | BT_DBG(""); |
| 2955 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2956 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2957 | MGMT_OP_USER_PASSKEY_REPLY, |
| 2958 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2959 | } |
| 2960 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2961 | 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] | 2962 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2963 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2964 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2965 | |
| 2966 | BT_DBG(""); |
| 2967 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2968 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2969 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 2970 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2971 | } |
| 2972 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2973 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2974 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2975 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2976 | struct hci_cp_write_local_name cp; |
| 2977 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2978 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2979 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2980 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2981 | } |
| 2982 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2983 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 2984 | { |
| 2985 | struct mgmt_cp_set_local_name *cp; |
| 2986 | struct pending_cmd *cmd; |
| 2987 | |
| 2988 | BT_DBG("status 0x%02x", status); |
| 2989 | |
| 2990 | hci_dev_lock(hdev); |
| 2991 | |
| 2992 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 2993 | if (!cmd) |
| 2994 | goto unlock; |
| 2995 | |
| 2996 | cp = cmd->param; |
| 2997 | |
| 2998 | if (status) |
| 2999 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3000 | mgmt_status(status)); |
| 3001 | else |
| 3002 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3003 | cp, sizeof(*cp)); |
| 3004 | |
| 3005 | mgmt_pending_remove(cmd); |
| 3006 | |
| 3007 | unlock: |
| 3008 | hci_dev_unlock(hdev); |
| 3009 | } |
| 3010 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3011 | 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] | 3012 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3013 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3014 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3015 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3016 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3017 | int err; |
| 3018 | |
| 3019 | BT_DBG(""); |
| 3020 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3021 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3022 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3023 | /* If the old values are the same as the new ones just return a |
| 3024 | * direct command complete event. |
| 3025 | */ |
| 3026 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3027 | !memcmp(hdev->short_name, cp->short_name, |
| 3028 | sizeof(hdev->short_name))) { |
| 3029 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3030 | data, len); |
| 3031 | goto failed; |
| 3032 | } |
| 3033 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3034 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3035 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3036 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3037 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3038 | |
| 3039 | 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] | 3040 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3041 | if (err < 0) |
| 3042 | goto failed; |
| 3043 | |
| 3044 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3045 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3046 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3047 | goto failed; |
| 3048 | } |
| 3049 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3050 | 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] | 3051 | if (!cmd) { |
| 3052 | err = -ENOMEM; |
| 3053 | goto failed; |
| 3054 | } |
| 3055 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3056 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3057 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3058 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3059 | |
| 3060 | if (lmp_bredr_capable(hdev)) { |
| 3061 | update_name(&req); |
| 3062 | update_eir(&req); |
| 3063 | } |
| 3064 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3065 | /* The name is stored in the scan response data and so |
| 3066 | * no need to udpate the advertising data here. |
| 3067 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3068 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3069 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3070 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3071 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3072 | if (err < 0) |
| 3073 | mgmt_pending_remove(cmd); |
| 3074 | |
| 3075 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3076 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3077 | return err; |
| 3078 | } |
| 3079 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3080 | 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] | 3081 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3082 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3083 | struct pending_cmd *cmd; |
| 3084 | int err; |
| 3085 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3086 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3087 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3088 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3089 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3090 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3091 | 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] | 3092 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3093 | goto unlock; |
| 3094 | } |
| 3095 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3096 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3097 | 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] | 3098 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3099 | goto unlock; |
| 3100 | } |
| 3101 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3102 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3103 | 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] | 3104 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3105 | goto unlock; |
| 3106 | } |
| 3107 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3108 | 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] | 3109 | if (!cmd) { |
| 3110 | err = -ENOMEM; |
| 3111 | goto unlock; |
| 3112 | } |
| 3113 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3114 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3115 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3116 | 0, NULL); |
| 3117 | else |
| 3118 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3119 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3120 | if (err < 0) |
| 3121 | mgmt_pending_remove(cmd); |
| 3122 | |
| 3123 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3124 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3125 | return err; |
| 3126 | } |
| 3127 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3128 | 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] | 3129 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3130 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3131 | int err; |
| 3132 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3133 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3134 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3135 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3136 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3137 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3138 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3139 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3140 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3141 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3142 | cp->hash, cp->randomizer); |
| 3143 | if (err < 0) |
| 3144 | status = MGMT_STATUS_FAILED; |
| 3145 | else |
| 3146 | status = MGMT_STATUS_SUCCESS; |
| 3147 | |
| 3148 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3149 | status, &cp->addr, sizeof(cp->addr)); |
| 3150 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3151 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3152 | u8 status; |
| 3153 | |
| 3154 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3155 | cp->hash192, |
| 3156 | cp->randomizer192, |
| 3157 | cp->hash256, |
| 3158 | cp->randomizer256); |
| 3159 | if (err < 0) |
| 3160 | status = MGMT_STATUS_FAILED; |
| 3161 | else |
| 3162 | status = MGMT_STATUS_SUCCESS; |
| 3163 | |
| 3164 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3165 | status, &cp->addr, sizeof(cp->addr)); |
| 3166 | } else { |
| 3167 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3168 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3169 | MGMT_STATUS_INVALID_PARAMS); |
| 3170 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3171 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3172 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3173 | return err; |
| 3174 | } |
| 3175 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3176 | 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] | 3177 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3178 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3179 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3180 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3181 | int err; |
| 3182 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3183 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3184 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3185 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3186 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3187 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3188 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3189 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3190 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3191 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3192 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3193 | 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] | 3194 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3195 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3196 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3197 | return err; |
| 3198 | } |
| 3199 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3200 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3201 | { |
| 3202 | struct pending_cmd *cmd; |
| 3203 | u8 type; |
| 3204 | int err; |
| 3205 | |
| 3206 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3207 | |
| 3208 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3209 | if (!cmd) |
| 3210 | return -ENOENT; |
| 3211 | |
| 3212 | type = hdev->discovery.type; |
| 3213 | |
| 3214 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3215 | &type, sizeof(type)); |
| 3216 | mgmt_pending_remove(cmd); |
| 3217 | |
| 3218 | return err; |
| 3219 | } |
| 3220 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3221 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3222 | { |
| 3223 | BT_DBG("status %d", status); |
| 3224 | |
| 3225 | if (status) { |
| 3226 | hci_dev_lock(hdev); |
| 3227 | mgmt_start_discovery_failed(hdev, status); |
| 3228 | hci_dev_unlock(hdev); |
| 3229 | return; |
| 3230 | } |
| 3231 | |
| 3232 | hci_dev_lock(hdev); |
| 3233 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3234 | hci_dev_unlock(hdev); |
| 3235 | |
| 3236 | switch (hdev->discovery.type) { |
| 3237 | case DISCOV_TYPE_LE: |
| 3238 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3239 | DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3240 | break; |
| 3241 | |
| 3242 | case DISCOV_TYPE_INTERLEAVED: |
| 3243 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3244 | DISCOV_INTERLEAVED_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3245 | break; |
| 3246 | |
| 3247 | case DISCOV_TYPE_BREDR: |
| 3248 | break; |
| 3249 | |
| 3250 | default: |
| 3251 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3252 | } |
| 3253 | } |
| 3254 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3255 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3256 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3257 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3258 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3259 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3260 | struct hci_cp_le_set_scan_param param_cp; |
| 3261 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3262 | struct hci_cp_inquiry inq_cp; |
| 3263 | struct hci_request req; |
| 3264 | /* General inquiry access code (GIAC) */ |
| 3265 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3266 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3267 | int err; |
| 3268 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3269 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3270 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3271 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3272 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3273 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3274 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3275 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3276 | goto failed; |
| 3277 | } |
| 3278 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3279 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3280 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3281 | MGMT_STATUS_BUSY); |
| 3282 | goto failed; |
| 3283 | } |
| 3284 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3285 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3286 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3287 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3288 | goto failed; |
| 3289 | } |
| 3290 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3291 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3292 | if (!cmd) { |
| 3293 | err = -ENOMEM; |
| 3294 | goto failed; |
| 3295 | } |
| 3296 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3297 | hdev->discovery.type = cp->type; |
| 3298 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3299 | hci_req_init(&req, hdev); |
| 3300 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3301 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3302 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3303 | status = mgmt_bredr_support(hdev); |
| 3304 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3305 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3306 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3307 | mgmt_pending_remove(cmd); |
| 3308 | goto failed; |
| 3309 | } |
| 3310 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3311 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3312 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3313 | MGMT_STATUS_BUSY); |
| 3314 | mgmt_pending_remove(cmd); |
| 3315 | goto failed; |
| 3316 | } |
| 3317 | |
| 3318 | hci_inquiry_cache_flush(hdev); |
| 3319 | |
| 3320 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3321 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3322 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3323 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3324 | break; |
| 3325 | |
| 3326 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3327 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3328 | status = mgmt_le_support(hdev); |
| 3329 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3330 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3331 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3332 | mgmt_pending_remove(cmd); |
| 3333 | goto failed; |
| 3334 | } |
| 3335 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3336 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3337 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3338 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3339 | MGMT_STATUS_NOT_SUPPORTED); |
| 3340 | mgmt_pending_remove(cmd); |
| 3341 | goto failed; |
| 3342 | } |
| 3343 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3344 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3345 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3346 | MGMT_STATUS_REJECTED); |
| 3347 | mgmt_pending_remove(cmd); |
| 3348 | goto failed; |
| 3349 | } |
| 3350 | |
| 3351 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { |
| 3352 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3353 | MGMT_STATUS_BUSY); |
| 3354 | mgmt_pending_remove(cmd); |
| 3355 | goto failed; |
| 3356 | } |
| 3357 | |
| 3358 | memset(¶m_cp, 0, sizeof(param_cp)); |
| 3359 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3360 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3361 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Marcel Holtmann | 79830f6 | 2013-10-18 16:38:09 -0700 | [diff] [blame] | 3362 | param_cp.own_address_type = hdev->own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3363 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3364 | ¶m_cp); |
| 3365 | |
| 3366 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3367 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3368 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3369 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3370 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3371 | break; |
| 3372 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3373 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3374 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3375 | MGMT_STATUS_INVALID_PARAMS); |
| 3376 | mgmt_pending_remove(cmd); |
| 3377 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3378 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3379 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3380 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3381 | if (err < 0) |
| 3382 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3383 | else |
| 3384 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3385 | |
| 3386 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3387 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3388 | return err; |
| 3389 | } |
| 3390 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3391 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3392 | { |
| 3393 | struct pending_cmd *cmd; |
| 3394 | int err; |
| 3395 | |
| 3396 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3397 | if (!cmd) |
| 3398 | return -ENOENT; |
| 3399 | |
| 3400 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3401 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3402 | mgmt_pending_remove(cmd); |
| 3403 | |
| 3404 | return err; |
| 3405 | } |
| 3406 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3407 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3408 | { |
| 3409 | BT_DBG("status %d", status); |
| 3410 | |
| 3411 | hci_dev_lock(hdev); |
| 3412 | |
| 3413 | if (status) { |
| 3414 | mgmt_stop_discovery_failed(hdev, status); |
| 3415 | goto unlock; |
| 3416 | } |
| 3417 | |
| 3418 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3419 | |
| 3420 | unlock: |
| 3421 | hci_dev_unlock(hdev); |
| 3422 | } |
| 3423 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3424 | 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] | 3425 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3426 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3427 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3428 | struct pending_cmd *cmd; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3429 | struct hci_cp_remote_name_req_cancel cp; |
| 3430 | struct inquiry_entry *e; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3431 | struct hci_request req; |
| 3432 | struct hci_cp_le_set_scan_enable enable_cp; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3433 | int err; |
| 3434 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3435 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3436 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3437 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3438 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3439 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3440 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3441 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3442 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3443 | goto unlock; |
| 3444 | } |
| 3445 | |
| 3446 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3447 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3448 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3449 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3450 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3451 | } |
| 3452 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3453 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3454 | if (!cmd) { |
| 3455 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3456 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3457 | } |
| 3458 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3459 | hci_req_init(&req, hdev); |
| 3460 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3461 | switch (hdev->discovery.state) { |
| 3462 | case DISCOVERY_FINDING: |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3463 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3464 | hci_req_add(&req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 3465 | } else { |
| 3466 | cancel_delayed_work(&hdev->le_scan_disable); |
| 3467 | |
| 3468 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3469 | enable_cp.enable = LE_SCAN_DISABLE; |
| 3470 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, |
| 3471 | sizeof(enable_cp), &enable_cp); |
| 3472 | } |
Andre Guedes | c9ecc48 | 2012-03-15 16:52:08 -0300 | [diff] [blame] | 3473 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3474 | break; |
| 3475 | |
| 3476 | case DISCOVERY_RESOLVING: |
| 3477 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3478 | NAME_PENDING); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3479 | if (!e) { |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3480 | mgmt_pending_remove(cmd); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3481 | err = cmd_complete(sk, hdev->id, |
| 3482 | MGMT_OP_STOP_DISCOVERY, 0, |
| 3483 | &mgmt_cp->type, |
| 3484 | sizeof(mgmt_cp->type)); |
| 3485 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3486 | goto unlock; |
| 3487 | } |
| 3488 | |
| 3489 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3490 | hci_req_add(&req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 3491 | &cp); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3492 | |
| 3493 | break; |
| 3494 | |
| 3495 | default: |
| 3496 | BT_DBG("unknown discovery state %u", hdev->discovery.state); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3497 | |
| 3498 | mgmt_pending_remove(cmd); |
| 3499 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 3500 | MGMT_STATUS_FAILED, &mgmt_cp->type, |
| 3501 | sizeof(mgmt_cp->type)); |
| 3502 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3503 | } |
| 3504 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3505 | err = hci_req_run(&req, stop_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3506 | if (err < 0) |
| 3507 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3508 | else |
| 3509 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3510 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3511 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3512 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3513 | return err; |
| 3514 | } |
| 3515 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3516 | 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] | 3517 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3518 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3519 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3520 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3521 | int err; |
| 3522 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3523 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3524 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3525 | hci_dev_lock(hdev); |
| 3526 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3527 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3528 | err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3529 | MGMT_STATUS_FAILED); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3530 | goto failed; |
| 3531 | } |
| 3532 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3533 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3534 | if (!e) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3535 | err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3536 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3537 | goto failed; |
| 3538 | } |
| 3539 | |
| 3540 | if (cp->name_known) { |
| 3541 | e->name_state = NAME_KNOWN; |
| 3542 | list_del(&e->list); |
| 3543 | } else { |
| 3544 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3545 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3546 | } |
| 3547 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3548 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3549 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3550 | |
| 3551 | failed: |
| 3552 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3553 | return err; |
| 3554 | } |
| 3555 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3556 | 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] | 3557 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3558 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3559 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3560 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3561 | int err; |
| 3562 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3563 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3564 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3565 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3566 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3567 | MGMT_STATUS_INVALID_PARAMS, |
| 3568 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3569 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3570 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3571 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3572 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3573 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3574 | status = MGMT_STATUS_FAILED; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3575 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3576 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3577 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3578 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3579 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3580 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3581 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3582 | |
| 3583 | return err; |
| 3584 | } |
| 3585 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3586 | 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] | 3587 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3588 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3589 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3590 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3591 | int err; |
| 3592 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3593 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3594 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3595 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3596 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3597 | MGMT_STATUS_INVALID_PARAMS, |
| 3598 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3599 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3600 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3601 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3602 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3603 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3604 | status = MGMT_STATUS_INVALID_PARAMS; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3605 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3606 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3607 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3608 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3609 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3610 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3611 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3612 | |
| 3613 | return err; |
| 3614 | } |
| 3615 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3616 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3617 | u16 len) |
| 3618 | { |
| 3619 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3620 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3621 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3622 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3623 | |
| 3624 | BT_DBG("%s", hdev->name); |
| 3625 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3626 | source = __le16_to_cpu(cp->source); |
| 3627 | |
| 3628 | if (source > 0x0002) |
| 3629 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3630 | MGMT_STATUS_INVALID_PARAMS); |
| 3631 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3632 | hci_dev_lock(hdev); |
| 3633 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3634 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3635 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3636 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3637 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3638 | |
| 3639 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3640 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3641 | hci_req_init(&req, hdev); |
| 3642 | update_eir(&req); |
| 3643 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3644 | |
| 3645 | hci_dev_unlock(hdev); |
| 3646 | |
| 3647 | return err; |
| 3648 | } |
| 3649 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3650 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3651 | { |
| 3652 | struct cmd_lookup match = { NULL, hdev }; |
| 3653 | |
| 3654 | if (status) { |
| 3655 | u8 mgmt_err = mgmt_status(status); |
| 3656 | |
| 3657 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3658 | cmd_status_rsp, &mgmt_err); |
| 3659 | return; |
| 3660 | } |
| 3661 | |
| 3662 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3663 | &match); |
| 3664 | |
| 3665 | new_settings(hdev, match.sk); |
| 3666 | |
| 3667 | if (match.sk) |
| 3668 | sock_put(match.sk); |
| 3669 | } |
| 3670 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3671 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3672 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3673 | { |
| 3674 | struct mgmt_mode *cp = data; |
| 3675 | struct pending_cmd *cmd; |
| 3676 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3677 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3678 | int err; |
| 3679 | |
| 3680 | BT_DBG("request for %s", hdev->name); |
| 3681 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3682 | status = mgmt_le_support(hdev); |
| 3683 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3684 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3685 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3686 | |
| 3687 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3688 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3689 | MGMT_STATUS_INVALID_PARAMS); |
| 3690 | |
| 3691 | hci_dev_lock(hdev); |
| 3692 | |
| 3693 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3694 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3695 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3696 | /* The following conditions are ones which mean that we should |
| 3697 | * not do any HCI communication but directly send a mgmt |
| 3698 | * response to user space (after toggling the flag if |
| 3699 | * necessary). |
| 3700 | */ |
| 3701 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3702 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3703 | bool changed = false; |
| 3704 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3705 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3706 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3707 | changed = true; |
| 3708 | } |
| 3709 | |
| 3710 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3711 | if (err < 0) |
| 3712 | goto unlock; |
| 3713 | |
| 3714 | if (changed) |
| 3715 | err = new_settings(hdev, sk); |
| 3716 | |
| 3717 | goto unlock; |
| 3718 | } |
| 3719 | |
| 3720 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3721 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3722 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3723 | MGMT_STATUS_BUSY); |
| 3724 | goto unlock; |
| 3725 | } |
| 3726 | |
| 3727 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3728 | if (!cmd) { |
| 3729 | err = -ENOMEM; |
| 3730 | goto unlock; |
| 3731 | } |
| 3732 | |
| 3733 | hci_req_init(&req, hdev); |
| 3734 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3735 | if (val) |
| 3736 | enable_advertising(&req); |
| 3737 | else |
| 3738 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3739 | |
| 3740 | err = hci_req_run(&req, set_advertising_complete); |
| 3741 | if (err < 0) |
| 3742 | mgmt_pending_remove(cmd); |
| 3743 | |
| 3744 | unlock: |
| 3745 | hci_dev_unlock(hdev); |
| 3746 | return err; |
| 3747 | } |
| 3748 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3749 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3750 | void *data, u16 len) |
| 3751 | { |
| 3752 | struct mgmt_cp_set_static_address *cp = data; |
| 3753 | int err; |
| 3754 | |
| 3755 | BT_DBG("%s", hdev->name); |
| 3756 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3757 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3758 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3759 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3760 | |
| 3761 | if (hdev_is_powered(hdev)) |
| 3762 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3763 | MGMT_STATUS_REJECTED); |
| 3764 | |
| 3765 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3766 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3767 | return cmd_status(sk, hdev->id, |
| 3768 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3769 | MGMT_STATUS_INVALID_PARAMS); |
| 3770 | |
| 3771 | /* Two most significant bits shall be set */ |
| 3772 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3773 | return cmd_status(sk, hdev->id, |
| 3774 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3775 | MGMT_STATUS_INVALID_PARAMS); |
| 3776 | } |
| 3777 | |
| 3778 | hci_dev_lock(hdev); |
| 3779 | |
| 3780 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3781 | |
| 3782 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3783 | |
| 3784 | hci_dev_unlock(hdev); |
| 3785 | |
| 3786 | return err; |
| 3787 | } |
| 3788 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3789 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 3790 | void *data, u16 len) |
| 3791 | { |
| 3792 | struct mgmt_cp_set_scan_params *cp = data; |
| 3793 | __u16 interval, window; |
| 3794 | int err; |
| 3795 | |
| 3796 | BT_DBG("%s", hdev->name); |
| 3797 | |
| 3798 | if (!lmp_le_capable(hdev)) |
| 3799 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3800 | MGMT_STATUS_NOT_SUPPORTED); |
| 3801 | |
| 3802 | interval = __le16_to_cpu(cp->interval); |
| 3803 | |
| 3804 | if (interval < 0x0004 || interval > 0x4000) |
| 3805 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3806 | MGMT_STATUS_INVALID_PARAMS); |
| 3807 | |
| 3808 | window = __le16_to_cpu(cp->window); |
| 3809 | |
| 3810 | if (window < 0x0004 || window > 0x4000) |
| 3811 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3812 | MGMT_STATUS_INVALID_PARAMS); |
| 3813 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 3814 | if (window > interval) |
| 3815 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3816 | MGMT_STATUS_INVALID_PARAMS); |
| 3817 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3818 | hci_dev_lock(hdev); |
| 3819 | |
| 3820 | hdev->le_scan_interval = interval; |
| 3821 | hdev->le_scan_window = window; |
| 3822 | |
| 3823 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 3824 | |
| 3825 | hci_dev_unlock(hdev); |
| 3826 | |
| 3827 | return err; |
| 3828 | } |
| 3829 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3830 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 3831 | { |
| 3832 | struct pending_cmd *cmd; |
| 3833 | |
| 3834 | BT_DBG("status 0x%02x", status); |
| 3835 | |
| 3836 | hci_dev_lock(hdev); |
| 3837 | |
| 3838 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 3839 | if (!cmd) |
| 3840 | goto unlock; |
| 3841 | |
| 3842 | if (status) { |
| 3843 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3844 | mgmt_status(status)); |
| 3845 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 3846 | struct mgmt_mode *cp = cmd->param; |
| 3847 | |
| 3848 | if (cp->val) |
| 3849 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 3850 | else |
| 3851 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 3852 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3853 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 3854 | new_settings(hdev, cmd->sk); |
| 3855 | } |
| 3856 | |
| 3857 | mgmt_pending_remove(cmd); |
| 3858 | |
| 3859 | unlock: |
| 3860 | hci_dev_unlock(hdev); |
| 3861 | } |
| 3862 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3863 | 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] | 3864 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3865 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3866 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3867 | struct pending_cmd *cmd; |
| 3868 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3869 | int err; |
| 3870 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3871 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3872 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3873 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 3874 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 3875 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3876 | MGMT_STATUS_NOT_SUPPORTED); |
| 3877 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 3878 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3879 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3880 | MGMT_STATUS_INVALID_PARAMS); |
| 3881 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 3882 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3883 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3884 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 3885 | |
| 3886 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3887 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3888 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3889 | |
| 3890 | hci_dev_lock(hdev); |
| 3891 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 3892 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 3893 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3894 | MGMT_STATUS_BUSY); |
| 3895 | goto unlock; |
| 3896 | } |
| 3897 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 3898 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 3899 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3900 | hdev); |
| 3901 | goto unlock; |
| 3902 | } |
| 3903 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3904 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 3905 | data, len); |
| 3906 | if (!cmd) { |
| 3907 | err = -ENOMEM; |
| 3908 | goto unlock; |
| 3909 | } |
| 3910 | |
| 3911 | hci_req_init(&req, hdev); |
| 3912 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 3913 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3914 | |
| 3915 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3916 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3917 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3918 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3919 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3920 | } |
| 3921 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3922 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3923 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3924 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3925 | return err; |
| 3926 | } |
| 3927 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 3928 | static void set_bredr_scan(struct hci_request *req) |
| 3929 | { |
| 3930 | struct hci_dev *hdev = req->hdev; |
| 3931 | u8 scan = 0; |
| 3932 | |
| 3933 | /* Ensure that fast connectable is disabled. This function will |
| 3934 | * not do anything if the page scan parameters are already what |
| 3935 | * they should be. |
| 3936 | */ |
| 3937 | write_fast_connectable(req, false); |
| 3938 | |
| 3939 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 3940 | scan |= SCAN_PAGE; |
| 3941 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 3942 | scan |= SCAN_INQUIRY; |
| 3943 | |
| 3944 | if (scan) |
| 3945 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 3946 | } |
| 3947 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 3948 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 3949 | { |
| 3950 | struct pending_cmd *cmd; |
| 3951 | |
| 3952 | BT_DBG("status 0x%02x", status); |
| 3953 | |
| 3954 | hci_dev_lock(hdev); |
| 3955 | |
| 3956 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 3957 | if (!cmd) |
| 3958 | goto unlock; |
| 3959 | |
| 3960 | if (status) { |
| 3961 | u8 mgmt_err = mgmt_status(status); |
| 3962 | |
| 3963 | /* We need to restore the flag if related HCI commands |
| 3964 | * failed. |
| 3965 | */ |
| 3966 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 3967 | |
| 3968 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 3969 | } else { |
| 3970 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 3971 | new_settings(hdev, cmd->sk); |
| 3972 | } |
| 3973 | |
| 3974 | mgmt_pending_remove(cmd); |
| 3975 | |
| 3976 | unlock: |
| 3977 | hci_dev_unlock(hdev); |
| 3978 | } |
| 3979 | |
| 3980 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 3981 | { |
| 3982 | struct mgmt_mode *cp = data; |
| 3983 | struct pending_cmd *cmd; |
| 3984 | struct hci_request req; |
| 3985 | int err; |
| 3986 | |
| 3987 | BT_DBG("request for %s", hdev->name); |
| 3988 | |
| 3989 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 3990 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 3991 | MGMT_STATUS_NOT_SUPPORTED); |
| 3992 | |
| 3993 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 3994 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 3995 | MGMT_STATUS_REJECTED); |
| 3996 | |
| 3997 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3998 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 3999 | MGMT_STATUS_INVALID_PARAMS); |
| 4000 | |
| 4001 | hci_dev_lock(hdev); |
| 4002 | |
| 4003 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4004 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4005 | goto unlock; |
| 4006 | } |
| 4007 | |
| 4008 | if (!hdev_is_powered(hdev)) { |
| 4009 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4010 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4011 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4012 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4013 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4014 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4015 | } |
| 4016 | |
| 4017 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4018 | |
| 4019 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4020 | if (err < 0) |
| 4021 | goto unlock; |
| 4022 | |
| 4023 | err = new_settings(hdev, sk); |
| 4024 | goto unlock; |
| 4025 | } |
| 4026 | |
| 4027 | /* Reject disabling when powered on */ |
| 4028 | if (!cp->val) { |
| 4029 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4030 | MGMT_STATUS_REJECTED); |
| 4031 | goto unlock; |
| 4032 | } |
| 4033 | |
| 4034 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4035 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4036 | MGMT_STATUS_BUSY); |
| 4037 | goto unlock; |
| 4038 | } |
| 4039 | |
| 4040 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4041 | if (!cmd) { |
| 4042 | err = -ENOMEM; |
| 4043 | goto unlock; |
| 4044 | } |
| 4045 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4046 | /* 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] | 4047 | * generates the correct flags. |
| 4048 | */ |
| 4049 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4050 | |
| 4051 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4052 | |
| 4053 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4054 | set_bredr_scan(&req); |
| 4055 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4056 | /* Since only the advertising data flags will change, there |
| 4057 | * is no need to update the scan response data. |
| 4058 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4059 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4060 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4061 | err = hci_req_run(&req, set_bredr_complete); |
| 4062 | if (err < 0) |
| 4063 | mgmt_pending_remove(cmd); |
| 4064 | |
| 4065 | unlock: |
| 4066 | hci_dev_unlock(hdev); |
| 4067 | return err; |
| 4068 | } |
| 4069 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4070 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4071 | void *data, u16 len) |
| 4072 | { |
| 4073 | struct mgmt_mode *cp = data; |
| 4074 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4075 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4076 | int err; |
| 4077 | |
| 4078 | BT_DBG("request for %s", hdev->name); |
| 4079 | |
| 4080 | status = mgmt_bredr_support(hdev); |
| 4081 | if (status) |
| 4082 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4083 | status); |
| 4084 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4085 | if (!lmp_sc_capable(hdev) && |
| 4086 | !test_bit(HCI_FORCE_SC, &hdev->dev_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4087 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4088 | MGMT_STATUS_NOT_SUPPORTED); |
| 4089 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4090 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4091 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4092 | MGMT_STATUS_INVALID_PARAMS); |
| 4093 | |
| 4094 | hci_dev_lock(hdev); |
| 4095 | |
| 4096 | if (!hdev_is_powered(hdev)) { |
| 4097 | bool changed; |
| 4098 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4099 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4100 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4101 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4102 | if (cp->val == 0x02) |
| 4103 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4104 | else |
| 4105 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4106 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4107 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4108 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4109 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4110 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4111 | |
| 4112 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4113 | if (err < 0) |
| 4114 | goto failed; |
| 4115 | |
| 4116 | if (changed) |
| 4117 | err = new_settings(hdev, sk); |
| 4118 | |
| 4119 | goto failed; |
| 4120 | } |
| 4121 | |
| 4122 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4123 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4124 | MGMT_STATUS_BUSY); |
| 4125 | goto failed; |
| 4126 | } |
| 4127 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4128 | val = !!cp->val; |
| 4129 | |
| 4130 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4131 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4132 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4133 | goto failed; |
| 4134 | } |
| 4135 | |
| 4136 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4137 | if (!cmd) { |
| 4138 | err = -ENOMEM; |
| 4139 | goto failed; |
| 4140 | } |
| 4141 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4142 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4143 | if (err < 0) { |
| 4144 | mgmt_pending_remove(cmd); |
| 4145 | goto failed; |
| 4146 | } |
| 4147 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4148 | if (cp->val == 0x02) |
| 4149 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4150 | else |
| 4151 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4152 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4153 | failed: |
| 4154 | hci_dev_unlock(hdev); |
| 4155 | return err; |
| 4156 | } |
| 4157 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4158 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4159 | void *data, u16 len) |
| 4160 | { |
| 4161 | struct mgmt_mode *cp = data; |
| 4162 | bool changed; |
| 4163 | int err; |
| 4164 | |
| 4165 | BT_DBG("request for %s", hdev->name); |
| 4166 | |
| 4167 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4168 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4169 | MGMT_STATUS_INVALID_PARAMS); |
| 4170 | |
| 4171 | hci_dev_lock(hdev); |
| 4172 | |
| 4173 | if (cp->val) |
| 4174 | changed = !test_and_set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 4175 | else |
| 4176 | changed = test_and_clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 4177 | |
| 4178 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4179 | if (err < 0) |
| 4180 | goto unlock; |
| 4181 | |
| 4182 | if (changed) |
| 4183 | err = new_settings(hdev, sk); |
| 4184 | |
| 4185 | unlock: |
| 4186 | hci_dev_unlock(hdev); |
| 4187 | return err; |
| 4188 | } |
| 4189 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4190 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4191 | { |
| 4192 | switch (irk->addr.type) { |
| 4193 | case BDADDR_LE_PUBLIC: |
| 4194 | return true; |
| 4195 | |
| 4196 | case BDADDR_LE_RANDOM: |
| 4197 | /* Two most significant bits shall be set */ |
| 4198 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4199 | return false; |
| 4200 | return true; |
| 4201 | } |
| 4202 | |
| 4203 | return false; |
| 4204 | } |
| 4205 | |
| 4206 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4207 | u16 len) |
| 4208 | { |
| 4209 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4210 | u16 irk_count, expected_len; |
| 4211 | int i, err; |
| 4212 | |
| 4213 | BT_DBG("request for %s", hdev->name); |
| 4214 | |
| 4215 | if (!lmp_le_capable(hdev)) |
| 4216 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4217 | MGMT_STATUS_NOT_SUPPORTED); |
| 4218 | |
| 4219 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4220 | |
| 4221 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4222 | if (expected_len != len) { |
| 4223 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
| 4224 | len, expected_len); |
| 4225 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4226 | MGMT_STATUS_INVALID_PARAMS); |
| 4227 | } |
| 4228 | |
| 4229 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4230 | |
| 4231 | for (i = 0; i < irk_count; i++) { |
| 4232 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4233 | |
| 4234 | if (!irk_is_valid(key)) |
| 4235 | return cmd_status(sk, hdev->id, |
| 4236 | MGMT_OP_LOAD_IRKS, |
| 4237 | MGMT_STATUS_INVALID_PARAMS); |
| 4238 | } |
| 4239 | |
| 4240 | hci_dev_lock(hdev); |
| 4241 | |
| 4242 | hci_smp_irks_clear(hdev); |
| 4243 | |
| 4244 | for (i = 0; i < irk_count; i++) { |
| 4245 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4246 | u8 addr_type; |
| 4247 | |
| 4248 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4249 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4250 | else |
| 4251 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4252 | |
| 4253 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4254 | BDADDR_ANY); |
| 4255 | } |
| 4256 | |
| 4257 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4258 | |
| 4259 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4260 | |
| 4261 | hci_dev_unlock(hdev); |
| 4262 | |
| 4263 | return err; |
| 4264 | } |
| 4265 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4266 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4267 | { |
| 4268 | if (key->master != 0x00 && key->master != 0x01) |
| 4269 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4270 | |
| 4271 | switch (key->addr.type) { |
| 4272 | case BDADDR_LE_PUBLIC: |
| 4273 | return true; |
| 4274 | |
| 4275 | case BDADDR_LE_RANDOM: |
| 4276 | /* Two most significant bits shall be set */ |
| 4277 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4278 | return false; |
| 4279 | return true; |
| 4280 | } |
| 4281 | |
| 4282 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4283 | } |
| 4284 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4285 | 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] | 4286 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4287 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4288 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4289 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4290 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4291 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4292 | BT_DBG("request for %s", hdev->name); |
| 4293 | |
| 4294 | if (!lmp_le_capable(hdev)) |
| 4295 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4296 | MGMT_STATUS_NOT_SUPPORTED); |
| 4297 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4298 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4299 | |
| 4300 | expected_len = sizeof(*cp) + key_count * |
| 4301 | sizeof(struct mgmt_ltk_info); |
| 4302 | if (expected_len != len) { |
| 4303 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4304 | len, expected_len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4305 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4306 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4307 | } |
| 4308 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4309 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4310 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4311 | for (i = 0; i < key_count; i++) { |
| 4312 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4313 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4314 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4315 | return cmd_status(sk, hdev->id, |
| 4316 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4317 | MGMT_STATUS_INVALID_PARAMS); |
| 4318 | } |
| 4319 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4320 | hci_dev_lock(hdev); |
| 4321 | |
| 4322 | hci_smp_ltks_clear(hdev); |
| 4323 | |
| 4324 | for (i = 0; i < key_count; i++) { |
| 4325 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4326 | u8 type, addr_type; |
| 4327 | |
| 4328 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4329 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4330 | else |
| 4331 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4332 | |
| 4333 | if (key->master) |
| 4334 | type = HCI_SMP_LTK; |
| 4335 | else |
| 4336 | type = HCI_SMP_LTK_SLAVE; |
| 4337 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4338 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
| 4339 | key->type, key->val, key->enc_size, key->ediv, |
| 4340 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4341 | } |
| 4342 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4343 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4344 | NULL, 0); |
| 4345 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4346 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4347 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4348 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4349 | } |
| 4350 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4351 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4352 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 4353 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4354 | bool var_len; |
| 4355 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4356 | } mgmt_handlers[] = { |
| 4357 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4358 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 4359 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 4360 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 4361 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 4362 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 4363 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 4364 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 4365 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 4366 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 4367 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 4368 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 4369 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 4370 | { set_le, false, MGMT_SETTING_SIZE }, |
| 4371 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 4372 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 4373 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 4374 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 4375 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 4376 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 4377 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 4378 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 4379 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 4380 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 4381 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 4382 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 4383 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 4384 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 4385 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 4386 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 4387 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 4388 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 4389 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4390 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4391 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 4392 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 4393 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 4394 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 4395 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 4396 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4397 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4398 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4399 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4400 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4401 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4402 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4403 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4404 | { }, |
| 4405 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4406 | }; |
| 4407 | |
| 4408 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4409 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 4410 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4411 | void *buf; |
| 4412 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4413 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 4414 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4415 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4416 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4417 | int err; |
| 4418 | |
| 4419 | BT_DBG("got %zu bytes", msglen); |
| 4420 | |
| 4421 | if (msglen < sizeof(*hdr)) |
| 4422 | return -EINVAL; |
| 4423 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 4424 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4425 | if (!buf) |
| 4426 | return -ENOMEM; |
| 4427 | |
| 4428 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 4429 | err = -EFAULT; |
| 4430 | goto done; |
| 4431 | } |
| 4432 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4433 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4434 | opcode = __le16_to_cpu(hdr->opcode); |
| 4435 | index = __le16_to_cpu(hdr->index); |
| 4436 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4437 | |
| 4438 | if (len != msglen - sizeof(*hdr)) { |
| 4439 | err = -EINVAL; |
| 4440 | goto done; |
| 4441 | } |
| 4442 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4443 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4444 | hdev = hci_dev_get(index); |
| 4445 | if (!hdev) { |
| 4446 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4447 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4448 | goto done; |
| 4449 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4450 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 4451 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
| 4452 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4453 | err = cmd_status(sk, index, opcode, |
| 4454 | MGMT_STATUS_INVALID_INDEX); |
| 4455 | goto done; |
| 4456 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4457 | } |
| 4458 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4459 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4460 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4461 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4462 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4463 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4464 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4465 | } |
| 4466 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4467 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4468 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4469 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4470 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4471 | goto done; |
| 4472 | } |
| 4473 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4474 | handler = &mgmt_handlers[opcode]; |
| 4475 | |
| 4476 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4477 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4478 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4479 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4480 | goto done; |
| 4481 | } |
| 4482 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4483 | if (hdev) |
| 4484 | mgmt_init_hdev(sk, hdev); |
| 4485 | |
| 4486 | cp = buf + sizeof(*hdr); |
| 4487 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4488 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 4489 | if (err < 0) |
| 4490 | goto done; |
| 4491 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4492 | err = msglen; |
| 4493 | |
| 4494 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4495 | if (hdev) |
| 4496 | hci_dev_put(hdev); |
| 4497 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4498 | kfree(buf); |
| 4499 | return err; |
| 4500 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4501 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4502 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4503 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4504 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4505 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4506 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4507 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4508 | } |
| 4509 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4510 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4511 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 4512 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4513 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4514 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4515 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4516 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4517 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4518 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4519 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 4520 | } |
| 4521 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4522 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 4523 | { |
| 4524 | struct cmd_lookup match = { NULL, hdev }; |
| 4525 | |
| 4526 | BT_DBG("status 0x%02x", status); |
| 4527 | |
| 4528 | hci_dev_lock(hdev); |
| 4529 | |
| 4530 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 4531 | |
| 4532 | new_settings(hdev, match.sk); |
| 4533 | |
| 4534 | hci_dev_unlock(hdev); |
| 4535 | |
| 4536 | if (match.sk) |
| 4537 | sock_put(match.sk); |
| 4538 | } |
| 4539 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4540 | static int powered_update_hci(struct hci_dev *hdev) |
| 4541 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4542 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4543 | u8 link_sec; |
| 4544 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4545 | hci_req_init(&req, hdev); |
| 4546 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4547 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 4548 | !lmp_host_ssp_capable(hdev)) { |
| 4549 | u8 ssp = 1; |
| 4550 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4551 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4552 | } |
| 4553 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 4554 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 4555 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4556 | struct hci_cp_write_le_host_supported cp; |
| 4557 | |
| 4558 | cp.le = 1; |
| 4559 | cp.simul = lmp_le_br_capable(hdev); |
| 4560 | |
| 4561 | /* Check first if we already have the right |
| 4562 | * host state (host features set) |
| 4563 | */ |
| 4564 | if (cp.le != lmp_host_le_capable(hdev) || |
| 4565 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4566 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 4567 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4568 | } |
| 4569 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4570 | if (lmp_le_capable(hdev)) { |
| 4571 | /* Set random address to static address if configured */ |
| 4572 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 4573 | hci_req_add(&req, HCI_OP_LE_SET_RANDOM_ADDR, 6, |
| 4574 | &hdev->static_addr); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4575 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 4576 | /* Make sure the controller has a good default for |
| 4577 | * advertising data. This also applies to the case |
| 4578 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 4579 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4580 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4581 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4582 | update_scan_rsp_data(&req); |
| 4583 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 4584 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4585 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 4586 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 4587 | } |
| 4588 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4589 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4590 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4591 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 4592 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4593 | |
| 4594 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4595 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 4596 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4597 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 4598 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4599 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4600 | } |
| 4601 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4602 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4603 | } |
| 4604 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4605 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 4606 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 4607 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4608 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 4609 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 4610 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 4611 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 4612 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 4613 | return 0; |
| 4614 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 4615 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4616 | if (powered_update_hci(hdev) == 0) |
| 4617 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 4618 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4619 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 4620 | &match); |
| 4621 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4622 | } |
| 4623 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4624 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 4625 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 4626 | |
| 4627 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 4628 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 4629 | zero_cod, sizeof(zero_cod), NULL); |
| 4630 | |
| 4631 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 4632 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 4633 | |
| 4634 | if (match.sk) |
| 4635 | sock_put(match.sk); |
| 4636 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 4637 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 4638 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4639 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 4640 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4641 | { |
| 4642 | struct pending_cmd *cmd; |
| 4643 | u8 status; |
| 4644 | |
| 4645 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 4646 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 4647 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4648 | |
| 4649 | if (err == -ERFKILL) |
| 4650 | status = MGMT_STATUS_RFKILLED; |
| 4651 | else |
| 4652 | status = MGMT_STATUS_FAILED; |
| 4653 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 4654 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4655 | |
| 4656 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4657 | } |
| 4658 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4659 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 4660 | { |
| 4661 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4662 | |
| 4663 | hci_dev_lock(hdev); |
| 4664 | |
| 4665 | /* When discoverable timeout triggers, then just make sure |
| 4666 | * the limited discoverable flag is cleared. Even in the case |
| 4667 | * of a timeout triggered from general discoverable, it is |
| 4668 | * safe to unconditionally clear the flag. |
| 4669 | */ |
| 4670 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4671 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4672 | |
| 4673 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 4674 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4675 | u8 scan = SCAN_PAGE; |
| 4676 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 4677 | sizeof(scan), &scan); |
| 4678 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4679 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4680 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4681 | hci_req_run(&req, NULL); |
| 4682 | |
| 4683 | hdev->discov_timeout = 0; |
| 4684 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4685 | new_settings(hdev, NULL); |
| 4686 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4687 | hci_dev_unlock(hdev); |
| 4688 | } |
| 4689 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4690 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4691 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4692 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4693 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 4694 | /* Nothing needed here if there's a pending command since that |
| 4695 | * commands request completion callback takes care of everything |
| 4696 | * necessary. |
| 4697 | */ |
| 4698 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4699 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 4700 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4701 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4702 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4703 | } else { |
| 4704 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4705 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4706 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4707 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4708 | if (changed) { |
| 4709 | struct hci_request req; |
| 4710 | |
| 4711 | /* In case this change in discoverable was triggered by |
| 4712 | * a disabling of connectable there could be a need to |
| 4713 | * update the advertising flags. |
| 4714 | */ |
| 4715 | hci_req_init(&req, hdev); |
| 4716 | update_adv_data(&req); |
| 4717 | hci_req_run(&req, NULL); |
| 4718 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4719 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4720 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4721 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4722 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4723 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4724 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4725 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4726 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 4727 | /* Nothing needed here if there's a pending command since that |
| 4728 | * commands request completion callback takes care of everything |
| 4729 | * necessary. |
| 4730 | */ |
| 4731 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4732 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 4733 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4734 | if (connectable) |
| 4735 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 4736 | else |
| 4737 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4738 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 4739 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4740 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4741 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4742 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 4743 | 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] | 4744 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4745 | u8 mgmt_err = mgmt_status(status); |
| 4746 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 4747 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4748 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4749 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 4750 | |
| 4751 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4752 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4753 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 4754 | } |
| 4755 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 4756 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 4757 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4758 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 4759 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4760 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4761 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4762 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4763 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 4764 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 4765 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4766 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 4767 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4768 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4769 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 4770 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4771 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4772 | |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 4773 | void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4774 | { |
| 4775 | struct mgmt_ev_new_long_term_key ev; |
| 4776 | |
| 4777 | memset(&ev, 0, sizeof(ev)); |
| 4778 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 4779 | /* Devices using resolvable or non-resolvable random addresses |
| 4780 | * without providing an indentity resolving key don't require |
| 4781 | * to store long term keys. Their addresses will change the |
| 4782 | * next time around. |
| 4783 | * |
| 4784 | * Only when a remote device provides an identity address |
| 4785 | * make sure the long term key is stored. If the remote |
| 4786 | * identity is known, the long term keys are internally |
| 4787 | * mapped to the identity address. So allow static random |
| 4788 | * and public addresses here. |
| 4789 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 4790 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 4791 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4792 | ev.store_hint = 0x00; |
| 4793 | else |
| 4794 | ev.store_hint = 0x01; |
| 4795 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4796 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4797 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Marcel Holtmann | d40f3ee | 2014-01-31 18:42:17 -0800 | [diff] [blame] | 4798 | ev.key.type = key->authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4799 | ev.key.enc_size = key->enc_size; |
| 4800 | ev.key.ediv = key->ediv; |
| 4801 | |
| 4802 | if (key->type == HCI_SMP_LTK) |
| 4803 | ev.key.master = 1; |
| 4804 | |
| 4805 | memcpy(ev.key.rand, key->rand, sizeof(key->rand)); |
| 4806 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 4807 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 4808 | 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] | 4809 | } |
| 4810 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 4811 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 4812 | { |
| 4813 | struct mgmt_ev_new_irk ev; |
| 4814 | |
| 4815 | memset(&ev, 0, sizeof(ev)); |
| 4816 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 4817 | /* For identity resolving keys from devices that are already |
| 4818 | * using a public address or static random address, do not |
| 4819 | * ask for storing this key. The identity resolving key really |
| 4820 | * is only mandatory for devices using resovlable random |
| 4821 | * addresses. |
| 4822 | * |
| 4823 | * Storing all identity resolving keys has the downside that |
| 4824 | * they will be also loaded on next boot of they system. More |
| 4825 | * identity resolving keys, means more time during scanning is |
| 4826 | * needed to actually resolve these addresses. |
| 4827 | */ |
| 4828 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 4829 | ev.store_hint = 0x01; |
| 4830 | else |
| 4831 | ev.store_hint = 0x00; |
| 4832 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 4833 | bacpy(&ev.rpa, &irk->rpa); |
| 4834 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 4835 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 4836 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 4837 | |
| 4838 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 4839 | } |
| 4840 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 4841 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 4842 | u8 data_len) |
| 4843 | { |
| 4844 | eir[eir_len++] = sizeof(type) + data_len; |
| 4845 | eir[eir_len++] = type; |
| 4846 | memcpy(&eir[eir_len], data, data_len); |
| 4847 | eir_len += data_len; |
| 4848 | |
| 4849 | return eir_len; |
| 4850 | } |
| 4851 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 4852 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 4853 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 4854 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4855 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4856 | char buf[512]; |
| 4857 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 4858 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4859 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4860 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4861 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4862 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 4863 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 4864 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4865 | if (name_len > 0) |
| 4866 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4867 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4868 | |
| 4869 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 4870 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4871 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4872 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 4873 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4874 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 4875 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 4876 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4877 | } |
| 4878 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4879 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 4880 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 4881 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4882 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 4883 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4884 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 4885 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4886 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4887 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 4888 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4889 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4890 | |
| 4891 | *sk = cmd->sk; |
| 4892 | sock_hold(*sk); |
| 4893 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 4894 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4895 | } |
| 4896 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4897 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4898 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 4899 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4900 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 4901 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4902 | |
| 4903 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4904 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4905 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4906 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 4907 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 4908 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 4909 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4910 | |
| 4911 | mgmt_pending_remove(cmd); |
| 4912 | } |
| 4913 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 4914 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4915 | u8 link_type, u8 addr_type, u8 reason) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4916 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 4917 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4918 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4919 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 4920 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 4921 | return; |
| 4922 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4923 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4924 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 4925 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4926 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 4927 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4928 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 4929 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4930 | |
| 4931 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 4932 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4933 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4934 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4935 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4936 | } |
| 4937 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 4938 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4939 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4940 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 4941 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 4942 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 4943 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4944 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4945 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 4946 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 4947 | hdev); |
| 4948 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4949 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4950 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 4951 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4952 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 4953 | cp = cmd->param; |
| 4954 | |
| 4955 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 4956 | return; |
| 4957 | |
| 4958 | if (cp->addr.type != bdaddr_type) |
| 4959 | return; |
| 4960 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 4961 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 4962 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 4963 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 4964 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 4965 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4966 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 4967 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4968 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4969 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 4970 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 4971 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4972 | { |
| 4973 | struct mgmt_ev_connect_failed ev; |
| 4974 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 4975 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4976 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4977 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4978 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 4979 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4980 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4981 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 4982 | 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] | 4983 | { |
| 4984 | struct mgmt_ev_pin_code_request ev; |
| 4985 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 4986 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 4987 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 4988 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4989 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 4990 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4991 | } |
| 4992 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 4993 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4994 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4995 | { |
| 4996 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 4997 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4998 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4999 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5000 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5001 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5002 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5003 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5004 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5005 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5006 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 5007 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5008 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5009 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5010 | } |
| 5011 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5012 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5013 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5014 | { |
| 5015 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5016 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5017 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5018 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5019 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5020 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5021 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5022 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5023 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5024 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5025 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 5026 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5027 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5028 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5029 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5030 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5031 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5032 | u8 link_type, u8 addr_type, __le32 value, |
| 5033 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5034 | { |
| 5035 | struct mgmt_ev_user_confirm_request ev; |
| 5036 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5037 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5038 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5039 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5040 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 5041 | ev.confirm_hint = confirm_hint; |
Andrei Emeltchenko | 78e8098 | 2012-03-09 13:00:50 +0200 | [diff] [blame] | 5042 | ev.value = value; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5043 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5044 | 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] | 5045 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5046 | } |
| 5047 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5048 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5049 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5050 | { |
| 5051 | struct mgmt_ev_user_passkey_request ev; |
| 5052 | |
| 5053 | BT_DBG("%s", hdev->name); |
| 5054 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5055 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5056 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5057 | |
| 5058 | 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] | 5059 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5060 | } |
| 5061 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 5062 | 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] | 5063 | u8 link_type, u8 addr_type, u8 status, |
| 5064 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5065 | { |
| 5066 | struct pending_cmd *cmd; |
| 5067 | struct mgmt_rp_user_confirm_reply rp; |
| 5068 | int err; |
| 5069 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5070 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5071 | if (!cmd) |
| 5072 | return -ENOENT; |
| 5073 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5074 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5075 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5076 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5077 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5078 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5079 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5080 | |
| 5081 | return err; |
| 5082 | } |
| 5083 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5084 | 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] | 5085 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5086 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5087 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5088 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5089 | } |
| 5090 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5091 | 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] | 5092 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5093 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5094 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5095 | status, |
| 5096 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5097 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5098 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5099 | 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] | 5100 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5101 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5102 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5103 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5104 | } |
| 5105 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5106 | 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] | 5107 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5108 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5109 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5110 | status, |
| 5111 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5112 | } |
| 5113 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 5114 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5115 | u8 link_type, u8 addr_type, u32 passkey, |
| 5116 | u8 entered) |
| 5117 | { |
| 5118 | struct mgmt_ev_passkey_notify ev; |
| 5119 | |
| 5120 | BT_DBG("%s", hdev->name); |
| 5121 | |
| 5122 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5123 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5124 | ev.passkey = __cpu_to_le32(passkey); |
| 5125 | ev.entered = entered; |
| 5126 | |
| 5127 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 5128 | } |
| 5129 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5130 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5131 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5132 | { |
| 5133 | struct mgmt_ev_auth_failed ev; |
| 5134 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 5135 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5136 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5137 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5138 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5139 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5140 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5141 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5142 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5143 | { |
| 5144 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5145 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5146 | |
| 5147 | if (status) { |
| 5148 | u8 mgmt_err = mgmt_status(status); |
| 5149 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5150 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5151 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5152 | } |
| 5153 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5154 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 5155 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 5156 | &hdev->dev_flags); |
| 5157 | else |
| 5158 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 5159 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5160 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5161 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5162 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5163 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5164 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5165 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5166 | |
| 5167 | if (match.sk) |
| 5168 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5169 | } |
| 5170 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5171 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5172 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5173 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5174 | struct hci_cp_write_eir cp; |
| 5175 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 5176 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5177 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5178 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 5179 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 5180 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5181 | memset(&cp, 0, sizeof(cp)); |
| 5182 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5183 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5184 | } |
| 5185 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5186 | 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] | 5187 | { |
| 5188 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5189 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5190 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5191 | |
| 5192 | if (status) { |
| 5193 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5194 | |
| 5195 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5196 | &hdev->dev_flags)) { |
| 5197 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5198 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5199 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5200 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5201 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 5202 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5203 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5207 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5208 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5209 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 5210 | if (!changed) |
| 5211 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 5212 | &hdev->dev_flags); |
| 5213 | else |
| 5214 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5215 | } |
| 5216 | |
| 5217 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 5218 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5219 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5220 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5221 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5222 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5223 | sock_put(match.sk); |
| 5224 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5225 | hci_req_init(&req, hdev); |
| 5226 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5227 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5228 | update_eir(&req); |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5229 | else |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5230 | clear_eir(&req); |
| 5231 | |
| 5232 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5233 | } |
| 5234 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5235 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 5236 | { |
| 5237 | struct cmd_lookup match = { NULL, hdev }; |
| 5238 | bool changed = false; |
| 5239 | |
| 5240 | if (status) { |
| 5241 | u8 mgmt_err = mgmt_status(status); |
| 5242 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5243 | if (enable) { |
| 5244 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 5245 | &hdev->dev_flags)) |
| 5246 | new_settings(hdev, NULL); |
| 5247 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5248 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5249 | |
| 5250 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5251 | cmd_status_rsp, &mgmt_err); |
| 5252 | return; |
| 5253 | } |
| 5254 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5255 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5256 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5257 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5258 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5259 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5260 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5261 | |
| 5262 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5263 | settings_rsp, &match); |
| 5264 | |
| 5265 | if (changed) |
| 5266 | new_settings(hdev, match.sk); |
| 5267 | |
| 5268 | if (match.sk) |
| 5269 | sock_put(match.sk); |
| 5270 | } |
| 5271 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5272 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5273 | { |
| 5274 | struct cmd_lookup *match = data; |
| 5275 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5276 | if (match->sk == NULL) { |
| 5277 | match->sk = cmd->sk; |
| 5278 | sock_hold(match->sk); |
| 5279 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5280 | } |
| 5281 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5282 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 5283 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5284 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5285 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5286 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5287 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 5288 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 5289 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5290 | |
| 5291 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5292 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 5293 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5294 | |
| 5295 | if (match.sk) |
| 5296 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5297 | } |
| 5298 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5299 | 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] | 5300 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5301 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5302 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5303 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5304 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5305 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5306 | |
| 5307 | memset(&ev, 0, sizeof(ev)); |
| 5308 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5309 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5310 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5311 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5312 | if (!cmd) { |
| 5313 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5314 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5315 | /* If this is a HCI command related to powering on the |
| 5316 | * HCI dev don't send any mgmt signals. |
| 5317 | */ |
| 5318 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5319 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5320 | } |
| 5321 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5322 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 5323 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5324 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5325 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5326 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 5327 | u8 *randomizer192, u8 *hash256, |
| 5328 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5329 | { |
| 5330 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5331 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5332 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5333 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5334 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5335 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5336 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5337 | |
| 5338 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5339 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 5340 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5341 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5342 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 5343 | hash256 && randomizer256) { |
| 5344 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5345 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5346 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 5347 | memcpy(rp.randomizer192, randomizer192, |
| 5348 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5349 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5350 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 5351 | memcpy(rp.randomizer256, randomizer256, |
| 5352 | sizeof(rp.randomizer256)); |
| 5353 | |
| 5354 | cmd_complete(cmd->sk, hdev->id, |
| 5355 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5356 | &rp, sizeof(rp)); |
| 5357 | } else { |
| 5358 | struct mgmt_rp_read_local_oob_data rp; |
| 5359 | |
| 5360 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 5361 | memcpy(rp.randomizer, randomizer192, |
| 5362 | sizeof(rp.randomizer)); |
| 5363 | |
| 5364 | cmd_complete(cmd->sk, hdev->id, |
| 5365 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5366 | &rp, sizeof(rp)); |
| 5367 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5371 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5372 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5373 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5374 | u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, u8 |
| 5375 | ssp, u8 *eir, u16 eir_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5376 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5377 | char buf[512]; |
| 5378 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 5379 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5380 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5381 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5382 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5383 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5384 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5385 | /* Leave 5 bytes for a potential CoD field */ |
| 5386 | if (sizeof(*ev) + eir_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5387 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 5388 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5389 | memset(buf, 0, sizeof(buf)); |
| 5390 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 5391 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 5392 | if (irk) { |
| 5393 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 5394 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 5395 | } else { |
| 5396 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 5397 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 5398 | } |
| 5399 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5400 | ev->rssi = rssi; |
Johan Hedberg | 9a395a8 | 2012-02-23 00:00:32 +0200 | [diff] [blame] | 5401 | if (cfm_name) |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 5402 | ev->flags |= __constant_cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME); |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 5403 | if (!ssp) |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 5404 | ev->flags |= __constant_cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5405 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5406 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5407 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5408 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5409 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 5410 | 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] | 5411 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5412 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5413 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5414 | ev_size = sizeof(*ev) + eir_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 5415 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5416 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5417 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5418 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 5419 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5420 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5421 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5422 | struct mgmt_ev_device_found *ev; |
| 5423 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 5424 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5425 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5426 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5427 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5428 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5429 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5430 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5431 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5432 | ev->rssi = rssi; |
| 5433 | |
| 5434 | 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] | 5435 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5436 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5437 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5438 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 5439 | 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] | 5440 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5441 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 5442 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5443 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 5444 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5445 | struct pending_cmd *cmd; |
| 5446 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 5447 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 5448 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5449 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5450 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5451 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5452 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5453 | |
| 5454 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 5455 | u8 type = hdev->discovery.type; |
| 5456 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5457 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 5458 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5459 | mgmt_pending_remove(cmd); |
| 5460 | } |
| 5461 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 5462 | memset(&ev, 0, sizeof(ev)); |
| 5463 | ev.type = hdev->discovery.type; |
| 5464 | ev.discovering = discovering; |
| 5465 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 5466 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5467 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5468 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5469 | int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5470 | { |
| 5471 | struct pending_cmd *cmd; |
| 5472 | struct mgmt_ev_device_blocked ev; |
| 5473 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5474 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5475 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5476 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5477 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5478 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5479 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5480 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5481 | } |
| 5482 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5483 | int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5484 | { |
| 5485 | struct pending_cmd *cmd; |
| 5486 | struct mgmt_ev_device_unblocked ev; |
| 5487 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5488 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5489 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5490 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5491 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5492 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5493 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5494 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5495 | } |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5496 | |
| 5497 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 5498 | { |
| 5499 | BT_DBG("%s status %u", hdev->name, status); |
| 5500 | |
| 5501 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 5502 | if (status) { |
| 5503 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 5504 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5505 | } |
| 5506 | } |
| 5507 | |
| 5508 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 5509 | { |
| 5510 | struct hci_request req; |
| 5511 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 5512 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5513 | return; |
| 5514 | |
| 5515 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5516 | return; |
| 5517 | |
| 5518 | hci_req_init(&req, hdev); |
| 5519 | enable_advertising(&req); |
| 5520 | |
| 5521 | /* If this fails we have no option but to let user space know |
| 5522 | * that we've disabled advertising. |
| 5523 | */ |
| 5524 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 5525 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 5526 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5527 | } |
| 5528 | } |