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; |
| 393 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 394 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 395 | return settings; |
| 396 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 397 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 398 | static u32 get_current_settings(struct hci_dev *hdev) |
| 399 | { |
| 400 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 401 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 402 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 403 | settings |= MGMT_SETTING_POWERED; |
| 404 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 405 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 406 | settings |= MGMT_SETTING_CONNECTABLE; |
| 407 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 408 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 409 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 410 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 411 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 412 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 413 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 414 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 415 | settings |= MGMT_SETTING_PAIRABLE; |
| 416 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 417 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 418 | settings |= MGMT_SETTING_BREDR; |
| 419 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 420 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 421 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 422 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 423 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 424 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 425 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 426 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 427 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 428 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 429 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 430 | settings |= MGMT_SETTING_HS; |
| 431 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 432 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 433 | settings |= MGMT_SETTING_ADVERTISING; |
| 434 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 435 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 436 | settings |= MGMT_SETTING_SECURE_CONN; |
| 437 | |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 438 | if (test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags)) |
| 439 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 440 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 441 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 444 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 445 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 446 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 447 | { |
| 448 | u8 *ptr = data, *uuids_start = NULL; |
| 449 | struct bt_uuid *uuid; |
| 450 | |
| 451 | if (len < 4) |
| 452 | return ptr; |
| 453 | |
| 454 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 455 | u16 uuid16; |
| 456 | |
| 457 | if (uuid->size != 16) |
| 458 | continue; |
| 459 | |
| 460 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 461 | if (uuid16 < 0x1100) |
| 462 | continue; |
| 463 | |
| 464 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 465 | continue; |
| 466 | |
| 467 | if (!uuids_start) { |
| 468 | uuids_start = ptr; |
| 469 | uuids_start[0] = 1; |
| 470 | uuids_start[1] = EIR_UUID16_ALL; |
| 471 | ptr += 2; |
| 472 | } |
| 473 | |
| 474 | /* Stop if not enough space to put next UUID */ |
| 475 | if ((ptr - data) + sizeof(u16) > len) { |
| 476 | uuids_start[1] = EIR_UUID16_SOME; |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | *ptr++ = (uuid16 & 0x00ff); |
| 481 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 482 | uuids_start[0] += sizeof(uuid16); |
| 483 | } |
| 484 | |
| 485 | return ptr; |
| 486 | } |
| 487 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 488 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 489 | { |
| 490 | u8 *ptr = data, *uuids_start = NULL; |
| 491 | struct bt_uuid *uuid; |
| 492 | |
| 493 | if (len < 6) |
| 494 | return ptr; |
| 495 | |
| 496 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 497 | if (uuid->size != 32) |
| 498 | continue; |
| 499 | |
| 500 | if (!uuids_start) { |
| 501 | uuids_start = ptr; |
| 502 | uuids_start[0] = 1; |
| 503 | uuids_start[1] = EIR_UUID32_ALL; |
| 504 | ptr += 2; |
| 505 | } |
| 506 | |
| 507 | /* Stop if not enough space to put next UUID */ |
| 508 | if ((ptr - data) + sizeof(u32) > len) { |
| 509 | uuids_start[1] = EIR_UUID32_SOME; |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 514 | ptr += sizeof(u32); |
| 515 | uuids_start[0] += sizeof(u32); |
| 516 | } |
| 517 | |
| 518 | return ptr; |
| 519 | } |
| 520 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 521 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 522 | { |
| 523 | u8 *ptr = data, *uuids_start = NULL; |
| 524 | struct bt_uuid *uuid; |
| 525 | |
| 526 | if (len < 18) |
| 527 | return ptr; |
| 528 | |
| 529 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 530 | if (uuid->size != 128) |
| 531 | continue; |
| 532 | |
| 533 | if (!uuids_start) { |
| 534 | uuids_start = ptr; |
| 535 | uuids_start[0] = 1; |
| 536 | uuids_start[1] = EIR_UUID128_ALL; |
| 537 | ptr += 2; |
| 538 | } |
| 539 | |
| 540 | /* Stop if not enough space to put next UUID */ |
| 541 | if ((ptr - data) + 16 > len) { |
| 542 | uuids_start[1] = EIR_UUID128_SOME; |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | memcpy(ptr, uuid->uuid, 16); |
| 547 | ptr += 16; |
| 548 | uuids_start[0] += 16; |
| 549 | } |
| 550 | |
| 551 | return ptr; |
| 552 | } |
| 553 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 554 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 555 | { |
| 556 | struct pending_cmd *cmd; |
| 557 | |
| 558 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 559 | if (cmd->opcode == opcode) |
| 560 | return cmd; |
| 561 | } |
| 562 | |
| 563 | return NULL; |
| 564 | } |
| 565 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 566 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 567 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 568 | u8 ad_len = 0; |
| 569 | size_t name_len; |
| 570 | |
| 571 | name_len = strlen(hdev->dev_name); |
| 572 | if (name_len > 0) { |
| 573 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 574 | |
| 575 | if (name_len > max_len) { |
| 576 | name_len = max_len; |
| 577 | ptr[1] = EIR_NAME_SHORT; |
| 578 | } else |
| 579 | ptr[1] = EIR_NAME_COMPLETE; |
| 580 | |
| 581 | ptr[0] = name_len + 1; |
| 582 | |
| 583 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 584 | |
| 585 | ad_len += (name_len + 2); |
| 586 | ptr += (name_len + 2); |
| 587 | } |
| 588 | |
| 589 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | static void update_scan_rsp_data(struct hci_request *req) |
| 593 | { |
| 594 | struct hci_dev *hdev = req->hdev; |
| 595 | struct hci_cp_le_set_scan_rsp_data cp; |
| 596 | u8 len; |
| 597 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 598 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 599 | return; |
| 600 | |
| 601 | memset(&cp, 0, sizeof(cp)); |
| 602 | |
| 603 | len = create_scan_rsp_data(hdev, cp.data); |
| 604 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 605 | if (hdev->scan_rsp_data_len == len && |
| 606 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 607 | return; |
| 608 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 609 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 610 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 611 | |
| 612 | cp.length = len; |
| 613 | |
| 614 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 615 | } |
| 616 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 617 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 618 | { |
| 619 | struct pending_cmd *cmd; |
| 620 | |
| 621 | /* If there's a pending mgmt command the flags will not yet have |
| 622 | * their final values, so check for this first. |
| 623 | */ |
| 624 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 625 | if (cmd) { |
| 626 | struct mgmt_mode *cp = cmd->param; |
| 627 | if (cp->val == 0x01) |
| 628 | return LE_AD_GENERAL; |
| 629 | else if (cp->val == 0x02) |
| 630 | return LE_AD_LIMITED; |
| 631 | } else { |
| 632 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 633 | return LE_AD_LIMITED; |
| 634 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 635 | return LE_AD_GENERAL; |
| 636 | } |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 641 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 642 | { |
| 643 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 644 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 645 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 646 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 647 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 648 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 649 | |
| 650 | if (flags) { |
| 651 | BT_DBG("adv flags 0x%02x", flags); |
| 652 | |
| 653 | ptr[0] = 2; |
| 654 | ptr[1] = EIR_FLAGS; |
| 655 | ptr[2] = flags; |
| 656 | |
| 657 | ad_len += 3; |
| 658 | ptr += 3; |
| 659 | } |
| 660 | |
| 661 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 662 | ptr[0] = 2; |
| 663 | ptr[1] = EIR_TX_POWER; |
| 664 | ptr[2] = (u8) hdev->adv_tx_power; |
| 665 | |
| 666 | ad_len += 3; |
| 667 | ptr += 3; |
| 668 | } |
| 669 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 670 | return ad_len; |
| 671 | } |
| 672 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 673 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 674 | { |
| 675 | struct hci_dev *hdev = req->hdev; |
| 676 | struct hci_cp_le_set_adv_data cp; |
| 677 | u8 len; |
| 678 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 679 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 680 | return; |
| 681 | |
| 682 | memset(&cp, 0, sizeof(cp)); |
| 683 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 684 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 685 | |
| 686 | if (hdev->adv_data_len == len && |
| 687 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 688 | return; |
| 689 | |
| 690 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 691 | hdev->adv_data_len = len; |
| 692 | |
| 693 | cp.length = len; |
| 694 | |
| 695 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 696 | } |
| 697 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 698 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 699 | { |
| 700 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 701 | size_t name_len; |
| 702 | |
| 703 | name_len = strlen(hdev->dev_name); |
| 704 | |
| 705 | if (name_len > 0) { |
| 706 | /* EIR Data type */ |
| 707 | if (name_len > 48) { |
| 708 | name_len = 48; |
| 709 | ptr[1] = EIR_NAME_SHORT; |
| 710 | } else |
| 711 | ptr[1] = EIR_NAME_COMPLETE; |
| 712 | |
| 713 | /* EIR Data length */ |
| 714 | ptr[0] = name_len + 1; |
| 715 | |
| 716 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 717 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 718 | ptr += (name_len + 2); |
| 719 | } |
| 720 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 721 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 722 | ptr[0] = 2; |
| 723 | ptr[1] = EIR_TX_POWER; |
| 724 | ptr[2] = (u8) hdev->inq_tx_power; |
| 725 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 726 | ptr += 3; |
| 727 | } |
| 728 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 729 | if (hdev->devid_source > 0) { |
| 730 | ptr[0] = 9; |
| 731 | ptr[1] = EIR_DEVICE_ID; |
| 732 | |
| 733 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 734 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 735 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 736 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 737 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 738 | ptr += 10; |
| 739 | } |
| 740 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 741 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 742 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 743 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 744 | } |
| 745 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 746 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 747 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 748 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 749 | struct hci_cp_write_eir cp; |
| 750 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 751 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 752 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 753 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 754 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 755 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 756 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 757 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 758 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 759 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 760 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 761 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 762 | |
| 763 | memset(&cp, 0, sizeof(cp)); |
| 764 | |
| 765 | create_eir(hdev, cp.data); |
| 766 | |
| 767 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 768 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 769 | |
| 770 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 771 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 772 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 773 | } |
| 774 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 775 | static u8 get_service_classes(struct hci_dev *hdev) |
| 776 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 777 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 778 | u8 val = 0; |
| 779 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 780 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 781 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 782 | |
| 783 | return val; |
| 784 | } |
| 785 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 786 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 787 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 788 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 789 | u8 cod[3]; |
| 790 | |
| 791 | BT_DBG("%s", hdev->name); |
| 792 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 793 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 794 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 795 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 796 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 797 | return; |
| 798 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 799 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 800 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 801 | |
| 802 | cod[0] = hdev->minor_class; |
| 803 | cod[1] = hdev->major_class; |
| 804 | cod[2] = get_service_classes(hdev); |
| 805 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 806 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 807 | cod[1] |= 0x20; |
| 808 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 809 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 810 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 811 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 812 | 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] | 813 | } |
| 814 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 815 | static void service_cache_off(struct work_struct *work) |
| 816 | { |
| 817 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 818 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 819 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 820 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 821 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 822 | return; |
| 823 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 824 | hci_req_init(&req, hdev); |
| 825 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 826 | hci_dev_lock(hdev); |
| 827 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 828 | update_eir(&req); |
| 829 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 830 | |
| 831 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 832 | |
| 833 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 834 | } |
| 835 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 836 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 837 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 838 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 839 | return; |
| 840 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 841 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 842 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 843 | /* Non-mgmt controlled devices get this bit set |
| 844 | * implicitly so that pairing works for them, however |
| 845 | * for mgmt we require user-space to explicitly enable |
| 846 | * it |
| 847 | */ |
| 848 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 849 | } |
| 850 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 851 | 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] | 852 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 853 | { |
| 854 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 855 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 856 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 857 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 858 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 859 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 860 | memset(&rp, 0, sizeof(rp)); |
| 861 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 862 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 863 | |
| 864 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 865 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 866 | |
| 867 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 868 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 869 | |
| 870 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 871 | |
| 872 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 873 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 874 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 875 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 876 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 877 | 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] | 878 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 882 | { |
| 883 | sock_put(cmd->sk); |
| 884 | kfree(cmd->param); |
| 885 | kfree(cmd); |
| 886 | } |
| 887 | |
| 888 | 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] | 889 | struct hci_dev *hdev, void *data, |
| 890 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 891 | { |
| 892 | struct pending_cmd *cmd; |
| 893 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 894 | cmd = kmalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 895 | if (!cmd) |
| 896 | return NULL; |
| 897 | |
| 898 | cmd->opcode = opcode; |
| 899 | cmd->index = hdev->id; |
| 900 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 901 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 902 | if (!cmd->param) { |
| 903 | kfree(cmd); |
| 904 | return NULL; |
| 905 | } |
| 906 | |
| 907 | if (data) |
| 908 | memcpy(cmd->param, data, len); |
| 909 | |
| 910 | cmd->sk = sk; |
| 911 | sock_hold(sk); |
| 912 | |
| 913 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 914 | |
| 915 | return cmd; |
| 916 | } |
| 917 | |
| 918 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 919 | void (*cb)(struct pending_cmd *cmd, |
| 920 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 921 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 922 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 923 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 924 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 925 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 926 | if (opcode > 0 && cmd->opcode != opcode) |
| 927 | continue; |
| 928 | |
| 929 | cb(cmd, data); |
| 930 | } |
| 931 | } |
| 932 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 933 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 934 | { |
| 935 | list_del(&cmd->list); |
| 936 | mgmt_pending_free(cmd); |
| 937 | } |
| 938 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 939 | 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] | 940 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 941 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 942 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 943 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 944 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 945 | } |
| 946 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 947 | 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] | 948 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 949 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 950 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 951 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 952 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 953 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 954 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 955 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 956 | if (cp->val != 0x00 && cp->val != 0x01) |
| 957 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 958 | MGMT_STATUS_INVALID_PARAMS); |
| 959 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 960 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 961 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 962 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 963 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 964 | MGMT_STATUS_BUSY); |
| 965 | goto failed; |
| 966 | } |
| 967 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 968 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 969 | cancel_delayed_work(&hdev->power_off); |
| 970 | |
| 971 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 972 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 973 | data, len); |
| 974 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 975 | goto failed; |
| 976 | } |
| 977 | } |
| 978 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 979 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 980 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 981 | goto failed; |
| 982 | } |
| 983 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 984 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 985 | if (!cmd) { |
| 986 | err = -ENOMEM; |
| 987 | goto failed; |
| 988 | } |
| 989 | |
| 990 | if (cp->val) |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 991 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 992 | else |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 993 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 994 | |
| 995 | err = 0; |
| 996 | |
| 997 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 998 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 999 | return err; |
| 1000 | } |
| 1001 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1002 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1003 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1004 | { |
| 1005 | struct sk_buff *skb; |
| 1006 | struct mgmt_hdr *hdr; |
| 1007 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1008 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1009 | if (!skb) |
| 1010 | return -ENOMEM; |
| 1011 | |
| 1012 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1013 | hdr->opcode = cpu_to_le16(event); |
| 1014 | if (hdev) |
| 1015 | hdr->index = cpu_to_le16(hdev->id); |
| 1016 | else |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 1017 | hdr->index = __constant_cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1018 | hdr->len = cpu_to_le16(data_len); |
| 1019 | |
| 1020 | if (data) |
| 1021 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1022 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1023 | /* Time stamp */ |
| 1024 | __net_timestamp(skb); |
| 1025 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1026 | hci_send_to_control(skb, skip_sk); |
| 1027 | kfree_skb(skb); |
| 1028 | |
| 1029 | return 0; |
| 1030 | } |
| 1031 | |
| 1032 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1033 | { |
| 1034 | __le32 ev; |
| 1035 | |
| 1036 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1037 | |
| 1038 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1039 | } |
| 1040 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1041 | struct cmd_lookup { |
| 1042 | struct sock *sk; |
| 1043 | struct hci_dev *hdev; |
| 1044 | u8 mgmt_status; |
| 1045 | }; |
| 1046 | |
| 1047 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1048 | { |
| 1049 | struct cmd_lookup *match = data; |
| 1050 | |
| 1051 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1052 | |
| 1053 | list_del(&cmd->list); |
| 1054 | |
| 1055 | if (match->sk == NULL) { |
| 1056 | match->sk = cmd->sk; |
| 1057 | sock_hold(match->sk); |
| 1058 | } |
| 1059 | |
| 1060 | mgmt_pending_free(cmd); |
| 1061 | } |
| 1062 | |
| 1063 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1064 | { |
| 1065 | u8 *status = data; |
| 1066 | |
| 1067 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1068 | mgmt_pending_remove(cmd); |
| 1069 | } |
| 1070 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1071 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1072 | { |
| 1073 | if (!lmp_bredr_capable(hdev)) |
| 1074 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1075 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1076 | return MGMT_STATUS_REJECTED; |
| 1077 | else |
| 1078 | return MGMT_STATUS_SUCCESS; |
| 1079 | } |
| 1080 | |
| 1081 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1082 | { |
| 1083 | if (!lmp_le_capable(hdev)) |
| 1084 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1085 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1086 | return MGMT_STATUS_REJECTED; |
| 1087 | else |
| 1088 | return MGMT_STATUS_SUCCESS; |
| 1089 | } |
| 1090 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1091 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1092 | { |
| 1093 | struct pending_cmd *cmd; |
| 1094 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1095 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1096 | bool changed; |
| 1097 | |
| 1098 | BT_DBG("status 0x%02x", status); |
| 1099 | |
| 1100 | hci_dev_lock(hdev); |
| 1101 | |
| 1102 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1103 | if (!cmd) |
| 1104 | goto unlock; |
| 1105 | |
| 1106 | if (status) { |
| 1107 | u8 mgmt_err = mgmt_status(status); |
| 1108 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1109 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1110 | goto remove_cmd; |
| 1111 | } |
| 1112 | |
| 1113 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1114 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1115 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1116 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1117 | |
| 1118 | if (hdev->discov_timeout > 0) { |
| 1119 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1120 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1121 | to); |
| 1122 | } |
| 1123 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1124 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1125 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1126 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1127 | |
| 1128 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1129 | |
| 1130 | if (changed) |
| 1131 | new_settings(hdev, cmd->sk); |
| 1132 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1133 | /* When the discoverable mode gets changed, make sure |
| 1134 | * that class of device has the limited discoverable |
| 1135 | * bit correctly set. |
| 1136 | */ |
| 1137 | hci_req_init(&req, hdev); |
| 1138 | update_class(&req); |
| 1139 | hci_req_run(&req, NULL); |
| 1140 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1141 | remove_cmd: |
| 1142 | mgmt_pending_remove(cmd); |
| 1143 | |
| 1144 | unlock: |
| 1145 | hci_dev_unlock(hdev); |
| 1146 | } |
| 1147 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1148 | 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] | 1149 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1150 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1151 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1152 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1153 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1154 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1155 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1156 | int err; |
| 1157 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1158 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1159 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1160 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1161 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1162 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1163 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1164 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1165 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1166 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1167 | MGMT_STATUS_INVALID_PARAMS); |
| 1168 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1169 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1170 | |
| 1171 | /* Disabling discoverable requires that no timeout is set, |
| 1172 | * and enabling limited discoverable requires a timeout. |
| 1173 | */ |
| 1174 | if ((cp->val == 0x00 && timeout > 0) || |
| 1175 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1176 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1177 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1178 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1179 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1180 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1181 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1182 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1183 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1184 | goto failed; |
| 1185 | } |
| 1186 | |
| 1187 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1188 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1189 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1190 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1191 | goto failed; |
| 1192 | } |
| 1193 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1194 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1195 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1196 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1197 | goto failed; |
| 1198 | } |
| 1199 | |
| 1200 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1201 | bool changed = false; |
| 1202 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1203 | /* Setting limited discoverable when powered off is |
| 1204 | * not a valid operation since it requires a timeout |
| 1205 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1206 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1207 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1208 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1209 | changed = true; |
| 1210 | } |
| 1211 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1212 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1213 | if (err < 0) |
| 1214 | goto failed; |
| 1215 | |
| 1216 | if (changed) |
| 1217 | err = new_settings(hdev, sk); |
| 1218 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1219 | goto failed; |
| 1220 | } |
| 1221 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1222 | /* If the current mode is the same, then just update the timeout |
| 1223 | * value with the new value. And if only the timeout gets updated, |
| 1224 | * then no need for any HCI transactions. |
| 1225 | */ |
| 1226 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1227 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1228 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1229 | cancel_delayed_work(&hdev->discov_off); |
| 1230 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1231 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1232 | if (cp->val && hdev->discov_timeout > 0) { |
| 1233 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1234 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1235 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1236 | } |
| 1237 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1238 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1239 | goto failed; |
| 1240 | } |
| 1241 | |
| 1242 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1243 | if (!cmd) { |
| 1244 | err = -ENOMEM; |
| 1245 | goto failed; |
| 1246 | } |
| 1247 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1248 | /* Cancel any potential discoverable timeout that might be |
| 1249 | * still active and store new timeout value. The arming of |
| 1250 | * the timeout happens in the complete handler. |
| 1251 | */ |
| 1252 | cancel_delayed_work(&hdev->discov_off); |
| 1253 | hdev->discov_timeout = timeout; |
| 1254 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1255 | /* Limited discoverable mode */ |
| 1256 | if (cp->val == 0x02) |
| 1257 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1258 | else |
| 1259 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1260 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1261 | hci_req_init(&req, hdev); |
| 1262 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1263 | /* The procedure for LE-only controllers is much simpler - just |
| 1264 | * update the advertising data. |
| 1265 | */ |
| 1266 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1267 | goto update_ad; |
| 1268 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1269 | scan = SCAN_PAGE; |
| 1270 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1271 | if (cp->val) { |
| 1272 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1273 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1274 | if (cp->val == 0x02) { |
| 1275 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1276 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1277 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1278 | hci_cp.iac_lap[1] = 0x8b; |
| 1279 | hci_cp.iac_lap[2] = 0x9e; |
| 1280 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1281 | hci_cp.iac_lap[4] = 0x8b; |
| 1282 | hci_cp.iac_lap[5] = 0x9e; |
| 1283 | } else { |
| 1284 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1285 | hci_cp.num_iac = 1; |
| 1286 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1287 | hci_cp.iac_lap[1] = 0x8b; |
| 1288 | hci_cp.iac_lap[2] = 0x9e; |
| 1289 | } |
| 1290 | |
| 1291 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1292 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1293 | |
| 1294 | scan |= SCAN_INQUIRY; |
| 1295 | } else { |
| 1296 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1297 | } |
| 1298 | |
| 1299 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1300 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1301 | update_ad: |
| 1302 | update_adv_data(&req); |
| 1303 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1304 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1305 | if (err < 0) |
| 1306 | mgmt_pending_remove(cmd); |
| 1307 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1308 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1309 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1310 | return err; |
| 1311 | } |
| 1312 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1313 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1314 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1315 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1316 | struct hci_cp_write_page_scan_activity acp; |
| 1317 | u8 type; |
| 1318 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1319 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1320 | return; |
| 1321 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1322 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1323 | return; |
| 1324 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1325 | if (enable) { |
| 1326 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1327 | |
| 1328 | /* 160 msec page scan interval */ |
| 1329 | acp.interval = __constant_cpu_to_le16(0x0100); |
| 1330 | } else { |
| 1331 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1332 | |
| 1333 | /* default 1.28 sec page scan */ |
| 1334 | acp.interval = __constant_cpu_to_le16(0x0800); |
| 1335 | } |
| 1336 | |
| 1337 | acp.window = __constant_cpu_to_le16(0x0012); |
| 1338 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1339 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1340 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1341 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1342 | sizeof(acp), &acp); |
| 1343 | |
| 1344 | if (hdev->page_scan_type != type) |
| 1345 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1346 | } |
| 1347 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1348 | static u8 get_adv_type(struct hci_dev *hdev) |
| 1349 | { |
| 1350 | struct pending_cmd *cmd; |
| 1351 | bool connectable; |
| 1352 | |
| 1353 | /* If there's a pending mgmt command the flag will not yet have |
| 1354 | * it's final value, so check for this first. |
| 1355 | */ |
| 1356 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1357 | if (cmd) { |
| 1358 | struct mgmt_mode *cp = cmd->param; |
| 1359 | connectable = !!cp->val; |
| 1360 | } else { |
| 1361 | connectable = test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1362 | } |
| 1363 | |
| 1364 | return connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
| 1365 | } |
| 1366 | |
Johan Hedberg | 95c66e7 | 2013-10-14 16:20:06 +0300 | [diff] [blame] | 1367 | static void enable_advertising(struct hci_request *req) |
| 1368 | { |
| 1369 | struct hci_dev *hdev = req->hdev; |
| 1370 | struct hci_cp_le_set_adv_param cp; |
| 1371 | u8 enable = 0x01; |
| 1372 | |
| 1373 | memset(&cp, 0, sizeof(cp)); |
| 1374 | cp.min_interval = __constant_cpu_to_le16(0x0800); |
| 1375 | cp.max_interval = __constant_cpu_to_le16(0x0800); |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1376 | cp.type = get_adv_type(hdev); |
Marcel Holtmann | 79830f6 | 2013-10-18 16:38:09 -0700 | [diff] [blame] | 1377 | cp.own_address_type = hdev->own_addr_type; |
Marcel Holtmann | 3f959d4 | 2014-02-20 11:55:56 -0800 | [diff] [blame] | 1378 | cp.channel_map = hdev->le_adv_channel_map; |
Johan Hedberg | 95c66e7 | 2013-10-14 16:20:06 +0300 | [diff] [blame] | 1379 | |
| 1380 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 1381 | |
| 1382 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1383 | } |
| 1384 | |
| 1385 | static void disable_advertising(struct hci_request *req) |
| 1386 | { |
| 1387 | u8 enable = 0x00; |
| 1388 | |
| 1389 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 1390 | } |
| 1391 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1392 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1393 | { |
| 1394 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1395 | struct mgmt_mode *cp; |
| 1396 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1397 | |
| 1398 | BT_DBG("status 0x%02x", status); |
| 1399 | |
| 1400 | hci_dev_lock(hdev); |
| 1401 | |
| 1402 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1403 | if (!cmd) |
| 1404 | goto unlock; |
| 1405 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1406 | if (status) { |
| 1407 | u8 mgmt_err = mgmt_status(status); |
| 1408 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1409 | goto remove_cmd; |
| 1410 | } |
| 1411 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1412 | cp = cmd->param; |
| 1413 | if (cp->val) |
| 1414 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1415 | else |
| 1416 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1417 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1418 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1419 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1420 | if (changed) |
| 1421 | new_settings(hdev, cmd->sk); |
| 1422 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1423 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1424 | mgmt_pending_remove(cmd); |
| 1425 | |
| 1426 | unlock: |
| 1427 | hci_dev_unlock(hdev); |
| 1428 | } |
| 1429 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1430 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1431 | struct sock *sk, u8 val) |
| 1432 | { |
| 1433 | bool changed = false; |
| 1434 | int err; |
| 1435 | |
| 1436 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1437 | changed = true; |
| 1438 | |
| 1439 | if (val) { |
| 1440 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1441 | } else { |
| 1442 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1443 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1444 | } |
| 1445 | |
| 1446 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1447 | if (err < 0) |
| 1448 | return err; |
| 1449 | |
| 1450 | if (changed) |
| 1451 | return new_settings(hdev, sk); |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1456 | 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] | 1457 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1458 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1459 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1460 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1461 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1462 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1463 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1464 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1465 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1466 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1467 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1468 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1469 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1470 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1471 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1472 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1473 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1474 | MGMT_STATUS_INVALID_PARAMS); |
| 1475 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1476 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1477 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1478 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1479 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1480 | goto failed; |
| 1481 | } |
| 1482 | |
| 1483 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1484 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1485 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1486 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1487 | goto failed; |
| 1488 | } |
| 1489 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1490 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1491 | if (!cmd) { |
| 1492 | err = -ENOMEM; |
| 1493 | goto failed; |
| 1494 | } |
| 1495 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1496 | hci_req_init(&req, hdev); |
| 1497 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1498 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1499 | * by-product of disabling connectable, we need to update the |
| 1500 | * advertising flags. |
| 1501 | */ |
| 1502 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1503 | if (!cp->val) { |
| 1504 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1505 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1506 | } |
| 1507 | update_adv_data(&req); |
| 1508 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1509 | if (cp->val) { |
| 1510 | scan = SCAN_PAGE; |
| 1511 | } else { |
| 1512 | scan = 0; |
| 1513 | |
| 1514 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1515 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1516 | cancel_delayed_work(&hdev->discov_off); |
| 1517 | } |
| 1518 | |
| 1519 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1520 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1521 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1522 | /* If we're going from non-connectable to connectable or |
| 1523 | * vice-versa when fast connectable is enabled ensure that fast |
| 1524 | * connectable gets disabled. write_fast_connectable won't do |
| 1525 | * anything if the page scan parameters are already what they |
| 1526 | * should be. |
| 1527 | */ |
| 1528 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1529 | write_fast_connectable(&req, false); |
| 1530 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1531 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1532 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1533 | disable_advertising(&req); |
| 1534 | enable_advertising(&req); |
| 1535 | } |
| 1536 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1537 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1538 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1539 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1540 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1541 | err = set_connectable_update_settings(hdev, sk, |
| 1542 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1543 | goto failed; |
| 1544 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1545 | |
| 1546 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1547 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1548 | return err; |
| 1549 | } |
| 1550 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1551 | 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] | 1552 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1553 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1554 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1555 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1556 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1557 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1558 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1559 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1560 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1561 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1562 | MGMT_STATUS_INVALID_PARAMS); |
| 1563 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1564 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1565 | |
| 1566 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1567 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1568 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1569 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1570 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1571 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1572 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1573 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1574 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1575 | if (changed) |
| 1576 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1577 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1578 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1579 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1580 | return err; |
| 1581 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1582 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1583 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1584 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1585 | { |
| 1586 | struct mgmt_mode *cp = data; |
| 1587 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1588 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1589 | int err; |
| 1590 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1591 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1592 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1593 | status = mgmt_bredr_support(hdev); |
| 1594 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1595 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1596 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1597 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1598 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1599 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1600 | MGMT_STATUS_INVALID_PARAMS); |
| 1601 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1602 | hci_dev_lock(hdev); |
| 1603 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1604 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1605 | bool changed = false; |
| 1606 | |
| 1607 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1608 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1609 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1610 | changed = true; |
| 1611 | } |
| 1612 | |
| 1613 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1614 | if (err < 0) |
| 1615 | goto failed; |
| 1616 | |
| 1617 | if (changed) |
| 1618 | err = new_settings(hdev, sk); |
| 1619 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1620 | goto failed; |
| 1621 | } |
| 1622 | |
| 1623 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1624 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1625 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1626 | goto failed; |
| 1627 | } |
| 1628 | |
| 1629 | val = !!cp->val; |
| 1630 | |
| 1631 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1632 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1633 | goto failed; |
| 1634 | } |
| 1635 | |
| 1636 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1637 | if (!cmd) { |
| 1638 | err = -ENOMEM; |
| 1639 | goto failed; |
| 1640 | } |
| 1641 | |
| 1642 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1643 | if (err < 0) { |
| 1644 | mgmt_pending_remove(cmd); |
| 1645 | goto failed; |
| 1646 | } |
| 1647 | |
| 1648 | failed: |
| 1649 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1650 | return err; |
| 1651 | } |
| 1652 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1653 | 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] | 1654 | { |
| 1655 | struct mgmt_mode *cp = data; |
| 1656 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1657 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1658 | int err; |
| 1659 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1660 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1661 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1662 | status = mgmt_bredr_support(hdev); |
| 1663 | if (status) |
| 1664 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1665 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1666 | if (!lmp_ssp_capable(hdev)) |
| 1667 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1668 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1669 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1670 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1671 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1672 | MGMT_STATUS_INVALID_PARAMS); |
| 1673 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1674 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1675 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1676 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1677 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1678 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1679 | if (cp->val) { |
| 1680 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1681 | &hdev->dev_flags); |
| 1682 | } else { |
| 1683 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1684 | &hdev->dev_flags); |
| 1685 | if (!changed) |
| 1686 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1687 | &hdev->dev_flags); |
| 1688 | else |
| 1689 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1693 | if (err < 0) |
| 1694 | goto failed; |
| 1695 | |
| 1696 | if (changed) |
| 1697 | err = new_settings(hdev, sk); |
| 1698 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1699 | goto failed; |
| 1700 | } |
| 1701 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1702 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1703 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1704 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1705 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1706 | goto failed; |
| 1707 | } |
| 1708 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1709 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1710 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1711 | goto failed; |
| 1712 | } |
| 1713 | |
| 1714 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1715 | if (!cmd) { |
| 1716 | err = -ENOMEM; |
| 1717 | goto failed; |
| 1718 | } |
| 1719 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1720 | 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] | 1721 | if (err < 0) { |
| 1722 | mgmt_pending_remove(cmd); |
| 1723 | goto failed; |
| 1724 | } |
| 1725 | |
| 1726 | failed: |
| 1727 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1728 | return err; |
| 1729 | } |
| 1730 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1731 | 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] | 1732 | { |
| 1733 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1734 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1735 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1736 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1737 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1738 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1739 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1740 | status = mgmt_bredr_support(hdev); |
| 1741 | if (status) |
| 1742 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1743 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1744 | if (!lmp_ssp_capable(hdev)) |
| 1745 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1746 | MGMT_STATUS_NOT_SUPPORTED); |
| 1747 | |
| 1748 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1749 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1750 | MGMT_STATUS_REJECTED); |
| 1751 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1752 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1753 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1754 | MGMT_STATUS_INVALID_PARAMS); |
| 1755 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1756 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1757 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1758 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1759 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1760 | } else { |
| 1761 | if (hdev_is_powered(hdev)) { |
| 1762 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1763 | MGMT_STATUS_REJECTED); |
| 1764 | goto unlock; |
| 1765 | } |
| 1766 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1767 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1768 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1769 | |
| 1770 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1771 | if (err < 0) |
| 1772 | goto unlock; |
| 1773 | |
| 1774 | if (changed) |
| 1775 | err = new_settings(hdev, sk); |
| 1776 | |
| 1777 | unlock: |
| 1778 | hci_dev_unlock(hdev); |
| 1779 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1780 | } |
| 1781 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1782 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1783 | { |
| 1784 | struct cmd_lookup match = { NULL, hdev }; |
| 1785 | |
| 1786 | if (status) { |
| 1787 | u8 mgmt_err = mgmt_status(status); |
| 1788 | |
| 1789 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1790 | &mgmt_err); |
| 1791 | return; |
| 1792 | } |
| 1793 | |
| 1794 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1795 | |
| 1796 | new_settings(hdev, match.sk); |
| 1797 | |
| 1798 | if (match.sk) |
| 1799 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1800 | |
| 1801 | /* Make sure the controller has a good default for |
| 1802 | * advertising data. Restrict the update to when LE |
| 1803 | * has actually been enabled. During power on, the |
| 1804 | * update in powered_update_hci will take care of it. |
| 1805 | */ |
| 1806 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1807 | struct hci_request req; |
| 1808 | |
| 1809 | hci_dev_lock(hdev); |
| 1810 | |
| 1811 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 1812 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 1813 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1814 | hci_req_run(&req, NULL); |
| 1815 | |
| 1816 | hci_dev_unlock(hdev); |
| 1817 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1818 | } |
| 1819 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1820 | 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] | 1821 | { |
| 1822 | struct mgmt_mode *cp = data; |
| 1823 | struct hci_cp_write_le_host_supported hci_cp; |
| 1824 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1825 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1826 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1827 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1828 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1829 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1830 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1831 | if (!lmp_le_capable(hdev)) |
| 1832 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1833 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1834 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1835 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1836 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1837 | MGMT_STATUS_INVALID_PARAMS); |
| 1838 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1839 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 1840 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 1841 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1842 | MGMT_STATUS_REJECTED); |
| 1843 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1844 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1845 | |
| 1846 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1847 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1848 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1849 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1850 | bool changed = false; |
| 1851 | |
| 1852 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1853 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 1854 | changed = true; |
| 1855 | } |
| 1856 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 1857 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 1858 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 1859 | changed = true; |
| 1860 | } |
| 1861 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1862 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 1863 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1864 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1865 | |
| 1866 | if (changed) |
| 1867 | err = new_settings(hdev, sk); |
| 1868 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1869 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1870 | } |
| 1871 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 1872 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 1873 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1874 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1875 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1876 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 1880 | if (!cmd) { |
| 1881 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1882 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1883 | } |
| 1884 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1885 | hci_req_init(&req, hdev); |
| 1886 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1887 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 1888 | |
| 1889 | if (val) { |
| 1890 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 1891 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1892 | } else { |
| 1893 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1894 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1895 | } |
| 1896 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1897 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 1898 | &hci_cp); |
| 1899 | |
| 1900 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 1901 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1902 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1903 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1904 | unlock: |
| 1905 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1906 | return err; |
| 1907 | } |
| 1908 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1909 | /* This is a helper function to test for pending mgmt commands that can |
| 1910 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 1911 | * mgmt command at a time since otherwise we cannot easily track what |
| 1912 | * the current values are, will be, and based on that calculate if a new |
| 1913 | * HCI command needs to be sent and if yes with what value. |
| 1914 | */ |
| 1915 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 1916 | { |
| 1917 | struct pending_cmd *cmd; |
| 1918 | |
| 1919 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 1920 | switch (cmd->opcode) { |
| 1921 | case MGMT_OP_ADD_UUID: |
| 1922 | case MGMT_OP_REMOVE_UUID: |
| 1923 | case MGMT_OP_SET_DEV_CLASS: |
| 1924 | case MGMT_OP_SET_POWERED: |
| 1925 | return true; |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | return false; |
| 1930 | } |
| 1931 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 1932 | static const u8 bluetooth_base_uuid[] = { |
| 1933 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 1934 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 1935 | }; |
| 1936 | |
| 1937 | static u8 get_uuid_size(const u8 *uuid) |
| 1938 | { |
| 1939 | u32 val; |
| 1940 | |
| 1941 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 1942 | return 128; |
| 1943 | |
| 1944 | val = get_unaligned_le32(&uuid[12]); |
| 1945 | if (val > 0xffff) |
| 1946 | return 32; |
| 1947 | |
| 1948 | return 16; |
| 1949 | } |
| 1950 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 1951 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 1952 | { |
| 1953 | struct pending_cmd *cmd; |
| 1954 | |
| 1955 | hci_dev_lock(hdev); |
| 1956 | |
| 1957 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 1958 | if (!cmd) |
| 1959 | goto unlock; |
| 1960 | |
| 1961 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 1962 | hdev->dev_class, 3); |
| 1963 | |
| 1964 | mgmt_pending_remove(cmd); |
| 1965 | |
| 1966 | unlock: |
| 1967 | hci_dev_unlock(hdev); |
| 1968 | } |
| 1969 | |
| 1970 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 1971 | { |
| 1972 | BT_DBG("status 0x%02x", status); |
| 1973 | |
| 1974 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 1975 | } |
| 1976 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1977 | 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] | 1978 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1979 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 1980 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 1981 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1982 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1983 | int err; |
| 1984 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1985 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1986 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1987 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1988 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 1989 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1990 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1991 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 1992 | goto failed; |
| 1993 | } |
| 1994 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 1995 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 1996 | if (!uuid) { |
| 1997 | err = -ENOMEM; |
| 1998 | goto failed; |
| 1999 | } |
| 2000 | |
| 2001 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2002 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2003 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2004 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2005 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2006 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2007 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2008 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2009 | update_class(&req); |
| 2010 | update_eir(&req); |
| 2011 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2012 | err = hci_req_run(&req, add_uuid_complete); |
| 2013 | if (err < 0) { |
| 2014 | if (err != -ENODATA) |
| 2015 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2016 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2017 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2018 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2019 | goto failed; |
| 2020 | } |
| 2021 | |
| 2022 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2023 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2024 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2025 | goto failed; |
| 2026 | } |
| 2027 | |
| 2028 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2029 | |
| 2030 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2031 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2032 | return err; |
| 2033 | } |
| 2034 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2035 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2036 | { |
| 2037 | if (!hdev_is_powered(hdev)) |
| 2038 | return false; |
| 2039 | |
| 2040 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2041 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2042 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2043 | return true; |
| 2044 | } |
| 2045 | |
| 2046 | return false; |
| 2047 | } |
| 2048 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2049 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2050 | { |
| 2051 | BT_DBG("status 0x%02x", status); |
| 2052 | |
| 2053 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2054 | } |
| 2055 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2056 | 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] | 2057 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2058 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2059 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2060 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2061 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2062 | 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] | 2063 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2064 | int err, found; |
| 2065 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2066 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2067 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2068 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2069 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2070 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2071 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2072 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2073 | goto unlock; |
| 2074 | } |
| 2075 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2076 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2077 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2078 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2079 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2080 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2081 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2082 | goto unlock; |
| 2083 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2084 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2085 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | found = 0; |
| 2089 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2090 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2091 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2092 | continue; |
| 2093 | |
| 2094 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2095 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2096 | found++; |
| 2097 | } |
| 2098 | |
| 2099 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2100 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2101 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2102 | goto unlock; |
| 2103 | } |
| 2104 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2105 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2106 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2107 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2108 | update_class(&req); |
| 2109 | update_eir(&req); |
| 2110 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2111 | err = hci_req_run(&req, remove_uuid_complete); |
| 2112 | if (err < 0) { |
| 2113 | if (err != -ENODATA) |
| 2114 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2115 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2116 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2117 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2118 | goto unlock; |
| 2119 | } |
| 2120 | |
| 2121 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2122 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2123 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2124 | goto unlock; |
| 2125 | } |
| 2126 | |
| 2127 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2128 | |
| 2129 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2130 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2131 | return err; |
| 2132 | } |
| 2133 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2134 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2135 | { |
| 2136 | BT_DBG("status 0x%02x", status); |
| 2137 | |
| 2138 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2139 | } |
| 2140 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2141 | 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] | 2142 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2143 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2144 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2145 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2146 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2147 | int err; |
| 2148 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2149 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2150 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2151 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2152 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2153 | MGMT_STATUS_NOT_SUPPORTED); |
| 2154 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2155 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2156 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2157 | if (pending_eir_or_class(hdev)) { |
| 2158 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2159 | MGMT_STATUS_BUSY); |
| 2160 | goto unlock; |
| 2161 | } |
| 2162 | |
| 2163 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2164 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2165 | MGMT_STATUS_INVALID_PARAMS); |
| 2166 | goto unlock; |
| 2167 | } |
| 2168 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2169 | hdev->major_class = cp->major; |
| 2170 | hdev->minor_class = cp->minor; |
| 2171 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2172 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2173 | 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] | 2174 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2175 | goto unlock; |
| 2176 | } |
| 2177 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2178 | hci_req_init(&req, hdev); |
| 2179 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2180 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2181 | hci_dev_unlock(hdev); |
| 2182 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2183 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2184 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2185 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2186 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2187 | update_class(&req); |
| 2188 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2189 | err = hci_req_run(&req, set_class_complete); |
| 2190 | if (err < 0) { |
| 2191 | if (err != -ENODATA) |
| 2192 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2193 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2194 | 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] | 2195 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2196 | goto unlock; |
| 2197 | } |
| 2198 | |
| 2199 | 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] | 2200 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2201 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2202 | goto unlock; |
| 2203 | } |
| 2204 | |
| 2205 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2206 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2207 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2208 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2209 | return err; |
| 2210 | } |
| 2211 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2212 | 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] | 2213 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2214 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2215 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2216 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2217 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2218 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2219 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2220 | BT_DBG("request for %s", hdev->name); |
| 2221 | |
| 2222 | if (!lmp_bredr_capable(hdev)) |
| 2223 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2224 | MGMT_STATUS_NOT_SUPPORTED); |
| 2225 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2226 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2227 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2228 | expected_len = sizeof(*cp) + key_count * |
| 2229 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2230 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2231 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2232 | len, expected_len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2233 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2234 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2235 | } |
| 2236 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2237 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2238 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2239 | MGMT_STATUS_INVALID_PARAMS); |
| 2240 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2241 | 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] | 2242 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2243 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2244 | for (i = 0; i < key_count; i++) { |
| 2245 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2246 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2247 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2248 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2249 | MGMT_STATUS_INVALID_PARAMS); |
| 2250 | } |
| 2251 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2252 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2253 | |
| 2254 | hci_link_keys_clear(hdev); |
| 2255 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2256 | if (cp->debug_keys) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2257 | changed = !test_and_set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2258 | else |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2259 | changed = test_and_clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 2260 | |
| 2261 | if (changed) |
| 2262 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2263 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2264 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2265 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2266 | |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 2267 | 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] | 2268 | key->type, key->pin_len); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2269 | } |
| 2270 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2271 | 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] | 2272 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2273 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2274 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2275 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2278 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2279 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2280 | { |
| 2281 | struct mgmt_ev_device_unpaired ev; |
| 2282 | |
| 2283 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2284 | ev.addr.type = addr_type; |
| 2285 | |
| 2286 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2287 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2288 | } |
| 2289 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2290 | 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] | 2291 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2292 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2293 | struct mgmt_cp_unpair_device *cp = data; |
| 2294 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2295 | struct hci_cp_disconnect dc; |
| 2296 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2297 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2298 | int err; |
| 2299 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2300 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2301 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2302 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2303 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2304 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2305 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2306 | MGMT_STATUS_INVALID_PARAMS, |
| 2307 | &rp, sizeof(rp)); |
| 2308 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2309 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2310 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2311 | MGMT_STATUS_INVALID_PARAMS, |
| 2312 | &rp, sizeof(rp)); |
| 2313 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2314 | hci_dev_lock(hdev); |
| 2315 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2316 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2317 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2318 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2319 | goto unlock; |
| 2320 | } |
| 2321 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2322 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2323 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2324 | } else { |
| 2325 | u8 addr_type; |
| 2326 | |
| 2327 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2328 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2329 | else |
| 2330 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2331 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2332 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2333 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2334 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2335 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2336 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2337 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2338 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2339 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2340 | goto unlock; |
| 2341 | } |
| 2342 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2343 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2344 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2345 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2346 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2347 | else |
| 2348 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2349 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2350 | } else { |
| 2351 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2352 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2353 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2354 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2355 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2356 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2357 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2358 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2361 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2362 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2363 | if (!cmd) { |
| 2364 | err = -ENOMEM; |
| 2365 | goto unlock; |
| 2366 | } |
| 2367 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2368 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2369 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2370 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2371 | if (err < 0) |
| 2372 | mgmt_pending_remove(cmd); |
| 2373 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2374 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2375 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2376 | return err; |
| 2377 | } |
| 2378 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2379 | 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] | 2380 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2381 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2382 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2383 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2384 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2385 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2386 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2387 | int err; |
| 2388 | |
| 2389 | BT_DBG(""); |
| 2390 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2391 | memset(&rp, 0, sizeof(rp)); |
| 2392 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2393 | rp.addr.type = cp->addr.type; |
| 2394 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2395 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2396 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2397 | MGMT_STATUS_INVALID_PARAMS, |
| 2398 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2399 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2400 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2401 | |
| 2402 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2403 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2404 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2405 | goto failed; |
| 2406 | } |
| 2407 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2408 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2409 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2410 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2411 | goto failed; |
| 2412 | } |
| 2413 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2414 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2415 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2416 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2417 | else |
| 2418 | 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] | 2419 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2420 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2421 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2422 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2423 | goto failed; |
| 2424 | } |
| 2425 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2426 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2427 | if (!cmd) { |
| 2428 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2429 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2430 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2431 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2432 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2433 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2434 | |
| 2435 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2436 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2437 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2438 | |
| 2439 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2440 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2441 | return err; |
| 2442 | } |
| 2443 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2444 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2445 | { |
| 2446 | switch (link_type) { |
| 2447 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2448 | switch (addr_type) { |
| 2449 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2450 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2451 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2452 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2453 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2454 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2455 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2456 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2457 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2458 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2459 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2463 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2464 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2465 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2466 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2467 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2468 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2469 | int err; |
| 2470 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2471 | |
| 2472 | BT_DBG(""); |
| 2473 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2474 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2475 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2476 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2477 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2478 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2479 | goto unlock; |
| 2480 | } |
| 2481 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2482 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2483 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2484 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2485 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2486 | } |
| 2487 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2488 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2489 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2490 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2491 | err = -ENOMEM; |
| 2492 | goto unlock; |
| 2493 | } |
| 2494 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2495 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2496 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2497 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2498 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2499 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2500 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2501 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2502 | continue; |
| 2503 | i++; |
| 2504 | } |
| 2505 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2506 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2507 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2508 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2509 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2510 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2511 | 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] | 2512 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2513 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2514 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2515 | |
| 2516 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2517 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2518 | return err; |
| 2519 | } |
| 2520 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2521 | 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] | 2522 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2523 | { |
| 2524 | struct pending_cmd *cmd; |
| 2525 | int err; |
| 2526 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2527 | 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] | 2528 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2529 | if (!cmd) |
| 2530 | return -ENOMEM; |
| 2531 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2532 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2533 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2534 | if (err < 0) |
| 2535 | mgmt_pending_remove(cmd); |
| 2536 | |
| 2537 | return err; |
| 2538 | } |
| 2539 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2540 | 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] | 2541 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2542 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2543 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2544 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2545 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2546 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2547 | int err; |
| 2548 | |
| 2549 | BT_DBG(""); |
| 2550 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2551 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2552 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2553 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2554 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2555 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2556 | goto failed; |
| 2557 | } |
| 2558 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2559 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2560 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2561 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2562 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2563 | goto failed; |
| 2564 | } |
| 2565 | |
| 2566 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2567 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2568 | |
| 2569 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2570 | |
| 2571 | BT_ERR("PIN code is not 16 bytes long"); |
| 2572 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2573 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2574 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2575 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2576 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2577 | |
| 2578 | goto failed; |
| 2579 | } |
| 2580 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2581 | 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] | 2582 | if (!cmd) { |
| 2583 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2584 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2585 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2586 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2587 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2588 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2589 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2590 | |
| 2591 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2592 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2593 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2594 | |
| 2595 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2596 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2597 | return err; |
| 2598 | } |
| 2599 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2600 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2601 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2602 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2603 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2604 | |
| 2605 | BT_DBG(""); |
| 2606 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2607 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2608 | |
| 2609 | hdev->io_capability = cp->io_capability; |
| 2610 | |
| 2611 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2612 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2613 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2614 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2615 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2616 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2617 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2618 | } |
| 2619 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2620 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2621 | { |
| 2622 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2623 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2624 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2625 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2626 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2627 | continue; |
| 2628 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2629 | if (cmd->user_data != conn) |
| 2630 | continue; |
| 2631 | |
| 2632 | return cmd; |
| 2633 | } |
| 2634 | |
| 2635 | return NULL; |
| 2636 | } |
| 2637 | |
| 2638 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2639 | { |
| 2640 | struct mgmt_rp_pair_device rp; |
| 2641 | struct hci_conn *conn = cmd->user_data; |
| 2642 | |
Johan Hedberg | ba4e564 | 2011-11-11 00:07:34 +0200 | [diff] [blame] | 2643 | bacpy(&rp.addr.bdaddr, &conn->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2644 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2645 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2646 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2647 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2648 | |
| 2649 | /* So we don't get further callbacks for this connection */ |
| 2650 | conn->connect_cfm_cb = NULL; |
| 2651 | conn->security_cfm_cb = NULL; |
| 2652 | conn->disconn_cfm_cb = NULL; |
| 2653 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2654 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2655 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2656 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2657 | } |
| 2658 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2659 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2660 | { |
| 2661 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2662 | struct pending_cmd *cmd; |
| 2663 | |
| 2664 | cmd = find_pairing(conn); |
| 2665 | if (cmd) |
| 2666 | pairing_complete(cmd, status); |
| 2667 | } |
| 2668 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2669 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2670 | { |
| 2671 | struct pending_cmd *cmd; |
| 2672 | |
| 2673 | BT_DBG("status %u", status); |
| 2674 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2675 | cmd = find_pairing(conn); |
| 2676 | if (!cmd) |
| 2677 | BT_DBG("Unable to find a pending command"); |
| 2678 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2679 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2680 | } |
| 2681 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2682 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2683 | { |
| 2684 | struct pending_cmd *cmd; |
| 2685 | |
| 2686 | BT_DBG("status %u", status); |
| 2687 | |
| 2688 | if (!status) |
| 2689 | return; |
| 2690 | |
| 2691 | cmd = find_pairing(conn); |
| 2692 | if (!cmd) |
| 2693 | BT_DBG("Unable to find a pending command"); |
| 2694 | else |
| 2695 | pairing_complete(cmd, mgmt_status(status)); |
| 2696 | } |
| 2697 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2698 | 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] | 2699 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2700 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2701 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2702 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2703 | struct pending_cmd *cmd; |
| 2704 | u8 sec_level, auth_type; |
| 2705 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2706 | int err; |
| 2707 | |
| 2708 | BT_DBG(""); |
| 2709 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2710 | memset(&rp, 0, sizeof(rp)); |
| 2711 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2712 | rp.addr.type = cp->addr.type; |
| 2713 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2714 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2715 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2716 | MGMT_STATUS_INVALID_PARAMS, |
| 2717 | &rp, sizeof(rp)); |
| 2718 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2719 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2720 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2721 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2722 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2723 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2724 | goto unlock; |
| 2725 | } |
| 2726 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2727 | sec_level = BT_SECURITY_MEDIUM; |
| 2728 | if (cp->io_cap == 0x03) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2729 | auth_type = HCI_AT_DEDICATED_BONDING; |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2730 | else |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2731 | auth_type = HCI_AT_DEDICATED_BONDING_MITM; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2732 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2733 | if (cp->addr.type == BDADDR_BREDR) |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 2734 | conn = hci_connect(hdev, ACL_LINK, &cp->addr.bdaddr, |
| 2735 | cp->addr.type, sec_level, auth_type); |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2736 | else |
Andre Guedes | b12f62c | 2012-04-24 21:02:54 -0300 | [diff] [blame] | 2737 | conn = hci_connect(hdev, LE_LINK, &cp->addr.bdaddr, |
| 2738 | cp->addr.type, sec_level, auth_type); |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2739 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2740 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2741 | int status; |
| 2742 | |
| 2743 | if (PTR_ERR(conn) == -EBUSY) |
| 2744 | status = MGMT_STATUS_BUSY; |
| 2745 | else |
| 2746 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2747 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2748 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2749 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2750 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2751 | goto unlock; |
| 2752 | } |
| 2753 | |
| 2754 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2755 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2756 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2757 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2758 | goto unlock; |
| 2759 | } |
| 2760 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2761 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2762 | if (!cmd) { |
| 2763 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2764 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2765 | goto unlock; |
| 2766 | } |
| 2767 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2768 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2769 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2770 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2771 | conn->security_cfm_cb = pairing_complete_cb; |
| 2772 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2773 | } else { |
| 2774 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2775 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2776 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2777 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2778 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2779 | conn->io_capability = cp->io_cap; |
| 2780 | cmd->user_data = conn; |
| 2781 | |
| 2782 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2783 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2784 | pairing_complete(cmd, 0); |
| 2785 | |
| 2786 | err = 0; |
| 2787 | |
| 2788 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2789 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2790 | return err; |
| 2791 | } |
| 2792 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2793 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2794 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2795 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2796 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2797 | struct pending_cmd *cmd; |
| 2798 | struct hci_conn *conn; |
| 2799 | int err; |
| 2800 | |
| 2801 | BT_DBG(""); |
| 2802 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2803 | hci_dev_lock(hdev); |
| 2804 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2805 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2806 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2807 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2808 | goto unlock; |
| 2809 | } |
| 2810 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2811 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 2812 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2813 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2814 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2815 | goto unlock; |
| 2816 | } |
| 2817 | |
| 2818 | conn = cmd->user_data; |
| 2819 | |
| 2820 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2821 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2822 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2823 | goto unlock; |
| 2824 | } |
| 2825 | |
| 2826 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 2827 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2828 | 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] | 2829 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2830 | unlock: |
| 2831 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2832 | return err; |
| 2833 | } |
| 2834 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2835 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2836 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2837 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2838 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2839 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2840 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2841 | int err; |
| 2842 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2843 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 2844 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2845 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2846 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2847 | MGMT_STATUS_NOT_POWERED, addr, |
| 2848 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2849 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2850 | } |
| 2851 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2852 | if (addr->type == BDADDR_BREDR) |
| 2853 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2854 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2855 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2856 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2857 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2858 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2859 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 2860 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 2861 | goto done; |
| 2862 | } |
| 2863 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2864 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2865 | /* Continue with pairing via SMP */ |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2866 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2867 | |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2868 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2869 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2870 | MGMT_STATUS_SUCCESS, addr, |
| 2871 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2872 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 2873 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 2874 | MGMT_STATUS_FAILED, addr, |
| 2875 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 2876 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 2877 | goto done; |
| 2878 | } |
| 2879 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2880 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2881 | if (!cmd) { |
| 2882 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2883 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2884 | } |
| 2885 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2886 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2887 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 2888 | struct hci_cp_user_passkey_reply cp; |
| 2889 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2890 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2891 | cp.passkey = passkey; |
| 2892 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 2893 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2894 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 2895 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2896 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2897 | if (err < 0) |
| 2898 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2899 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2900 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2901 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 2902 | return err; |
| 2903 | } |
| 2904 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 2905 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 2906 | void *data, u16 len) |
| 2907 | { |
| 2908 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 2909 | |
| 2910 | BT_DBG(""); |
| 2911 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2912 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 2913 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 2914 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 2915 | } |
| 2916 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2917 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2918 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2919 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2920 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2921 | |
| 2922 | BT_DBG(""); |
| 2923 | |
| 2924 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2925 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2926 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2927 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2928 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2929 | MGMT_OP_USER_CONFIRM_REPLY, |
| 2930 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2931 | } |
| 2932 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2933 | 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] | 2934 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2935 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 2936 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2937 | |
| 2938 | BT_DBG(""); |
| 2939 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2940 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2941 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 2942 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 2943 | } |
| 2944 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2945 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2946 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2947 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2948 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2949 | |
| 2950 | BT_DBG(""); |
| 2951 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2952 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2953 | MGMT_OP_USER_PASSKEY_REPLY, |
| 2954 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2955 | } |
| 2956 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2957 | 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] | 2958 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2959 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2960 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2961 | |
| 2962 | BT_DBG(""); |
| 2963 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 2964 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2965 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 2966 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 2967 | } |
| 2968 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2969 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2970 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2971 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2972 | struct hci_cp_write_local_name cp; |
| 2973 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2974 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2975 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2976 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 2977 | } |
| 2978 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 2979 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 2980 | { |
| 2981 | struct mgmt_cp_set_local_name *cp; |
| 2982 | struct pending_cmd *cmd; |
| 2983 | |
| 2984 | BT_DBG("status 0x%02x", status); |
| 2985 | |
| 2986 | hci_dev_lock(hdev); |
| 2987 | |
| 2988 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 2989 | if (!cmd) |
| 2990 | goto unlock; |
| 2991 | |
| 2992 | cp = cmd->param; |
| 2993 | |
| 2994 | if (status) |
| 2995 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 2996 | mgmt_status(status)); |
| 2997 | else |
| 2998 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 2999 | cp, sizeof(*cp)); |
| 3000 | |
| 3001 | mgmt_pending_remove(cmd); |
| 3002 | |
| 3003 | unlock: |
| 3004 | hci_dev_unlock(hdev); |
| 3005 | } |
| 3006 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3007 | 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] | 3008 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3009 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3010 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3011 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3012 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3013 | int err; |
| 3014 | |
| 3015 | BT_DBG(""); |
| 3016 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3017 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3018 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3019 | /* If the old values are the same as the new ones just return a |
| 3020 | * direct command complete event. |
| 3021 | */ |
| 3022 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3023 | !memcmp(hdev->short_name, cp->short_name, |
| 3024 | sizeof(hdev->short_name))) { |
| 3025 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3026 | data, len); |
| 3027 | goto failed; |
| 3028 | } |
| 3029 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3030 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3031 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3032 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3033 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3034 | |
| 3035 | 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] | 3036 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3037 | if (err < 0) |
| 3038 | goto failed; |
| 3039 | |
| 3040 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3041 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3042 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3043 | goto failed; |
| 3044 | } |
| 3045 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3046 | 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] | 3047 | if (!cmd) { |
| 3048 | err = -ENOMEM; |
| 3049 | goto failed; |
| 3050 | } |
| 3051 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3052 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3053 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3054 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3055 | |
| 3056 | if (lmp_bredr_capable(hdev)) { |
| 3057 | update_name(&req); |
| 3058 | update_eir(&req); |
| 3059 | } |
| 3060 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3061 | /* The name is stored in the scan response data and so |
| 3062 | * no need to udpate the advertising data here. |
| 3063 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3064 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3065 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3066 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3067 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3068 | if (err < 0) |
| 3069 | mgmt_pending_remove(cmd); |
| 3070 | |
| 3071 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3072 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3073 | return err; |
| 3074 | } |
| 3075 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3076 | 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] | 3077 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3078 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3079 | struct pending_cmd *cmd; |
| 3080 | int err; |
| 3081 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3082 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3083 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3084 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3085 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3086 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3087 | 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] | 3088 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3089 | goto unlock; |
| 3090 | } |
| 3091 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3092 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3093 | 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] | 3094 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3095 | goto unlock; |
| 3096 | } |
| 3097 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3098 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3099 | 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] | 3100 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3101 | goto unlock; |
| 3102 | } |
| 3103 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3104 | 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] | 3105 | if (!cmd) { |
| 3106 | err = -ENOMEM; |
| 3107 | goto unlock; |
| 3108 | } |
| 3109 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3110 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3111 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3112 | 0, NULL); |
| 3113 | else |
| 3114 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3115 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3116 | if (err < 0) |
| 3117 | mgmt_pending_remove(cmd); |
| 3118 | |
| 3119 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3120 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3121 | return err; |
| 3122 | } |
| 3123 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3124 | 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] | 3125 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3126 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3127 | int err; |
| 3128 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3129 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3130 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3131 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3132 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3133 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3134 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3135 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3136 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3137 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3138 | cp->hash, cp->randomizer); |
| 3139 | if (err < 0) |
| 3140 | status = MGMT_STATUS_FAILED; |
| 3141 | else |
| 3142 | status = MGMT_STATUS_SUCCESS; |
| 3143 | |
| 3144 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3145 | status, &cp->addr, sizeof(cp->addr)); |
| 3146 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3147 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3148 | u8 status; |
| 3149 | |
| 3150 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3151 | cp->hash192, |
| 3152 | cp->randomizer192, |
| 3153 | cp->hash256, |
| 3154 | cp->randomizer256); |
| 3155 | if (err < 0) |
| 3156 | status = MGMT_STATUS_FAILED; |
| 3157 | else |
| 3158 | status = MGMT_STATUS_SUCCESS; |
| 3159 | |
| 3160 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3161 | status, &cp->addr, sizeof(cp->addr)); |
| 3162 | } else { |
| 3163 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3164 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3165 | MGMT_STATUS_INVALID_PARAMS); |
| 3166 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3167 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3168 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3169 | return err; |
| 3170 | } |
| 3171 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3172 | 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] | 3173 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3174 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3175 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3176 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3177 | int err; |
| 3178 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3179 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3180 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3181 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3182 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3183 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3184 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3185 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3186 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3187 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3188 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3189 | 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] | 3190 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3191 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3192 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3193 | return err; |
| 3194 | } |
| 3195 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3196 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3197 | { |
| 3198 | struct pending_cmd *cmd; |
| 3199 | u8 type; |
| 3200 | int err; |
| 3201 | |
| 3202 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3203 | |
| 3204 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3205 | if (!cmd) |
| 3206 | return -ENOENT; |
| 3207 | |
| 3208 | type = hdev->discovery.type; |
| 3209 | |
| 3210 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3211 | &type, sizeof(type)); |
| 3212 | mgmt_pending_remove(cmd); |
| 3213 | |
| 3214 | return err; |
| 3215 | } |
| 3216 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3217 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3218 | { |
| 3219 | BT_DBG("status %d", status); |
| 3220 | |
| 3221 | if (status) { |
| 3222 | hci_dev_lock(hdev); |
| 3223 | mgmt_start_discovery_failed(hdev, status); |
| 3224 | hci_dev_unlock(hdev); |
| 3225 | return; |
| 3226 | } |
| 3227 | |
| 3228 | hci_dev_lock(hdev); |
| 3229 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3230 | hci_dev_unlock(hdev); |
| 3231 | |
| 3232 | switch (hdev->discovery.type) { |
| 3233 | case DISCOV_TYPE_LE: |
| 3234 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3235 | DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3236 | break; |
| 3237 | |
| 3238 | case DISCOV_TYPE_INTERLEAVED: |
| 3239 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3240 | DISCOV_INTERLEAVED_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3241 | break; |
| 3242 | |
| 3243 | case DISCOV_TYPE_BREDR: |
| 3244 | break; |
| 3245 | |
| 3246 | default: |
| 3247 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3248 | } |
| 3249 | } |
| 3250 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3251 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3252 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3253 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3254 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3255 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3256 | struct hci_cp_le_set_scan_param param_cp; |
| 3257 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3258 | struct hci_cp_inquiry inq_cp; |
| 3259 | struct hci_request req; |
| 3260 | /* General inquiry access code (GIAC) */ |
| 3261 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3262 | u8 status; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3263 | int err; |
| 3264 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3265 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3266 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3267 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3268 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3269 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3270 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3271 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3272 | goto failed; |
| 3273 | } |
| 3274 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3275 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3276 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3277 | MGMT_STATUS_BUSY); |
| 3278 | goto failed; |
| 3279 | } |
| 3280 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3281 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3282 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3283 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3284 | goto failed; |
| 3285 | } |
| 3286 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3287 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3288 | if (!cmd) { |
| 3289 | err = -ENOMEM; |
| 3290 | goto failed; |
| 3291 | } |
| 3292 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3293 | hdev->discovery.type = cp->type; |
| 3294 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3295 | hci_req_init(&req, hdev); |
| 3296 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3297 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3298 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3299 | status = mgmt_bredr_support(hdev); |
| 3300 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3301 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3302 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3303 | mgmt_pending_remove(cmd); |
| 3304 | goto failed; |
| 3305 | } |
| 3306 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3307 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3308 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3309 | MGMT_STATUS_BUSY); |
| 3310 | mgmt_pending_remove(cmd); |
| 3311 | goto failed; |
| 3312 | } |
| 3313 | |
| 3314 | hci_inquiry_cache_flush(hdev); |
| 3315 | |
| 3316 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3317 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3318 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3319 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3320 | break; |
| 3321 | |
| 3322 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3323 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3324 | status = mgmt_le_support(hdev); |
| 3325 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3326 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3327 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3328 | mgmt_pending_remove(cmd); |
| 3329 | goto failed; |
| 3330 | } |
| 3331 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3332 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3333 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3334 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3335 | MGMT_STATUS_NOT_SUPPORTED); |
| 3336 | mgmt_pending_remove(cmd); |
| 3337 | goto failed; |
| 3338 | } |
| 3339 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3340 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3341 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3342 | MGMT_STATUS_REJECTED); |
| 3343 | mgmt_pending_remove(cmd); |
| 3344 | goto failed; |
| 3345 | } |
| 3346 | |
| 3347 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { |
| 3348 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3349 | MGMT_STATUS_BUSY); |
| 3350 | mgmt_pending_remove(cmd); |
| 3351 | goto failed; |
| 3352 | } |
| 3353 | |
| 3354 | memset(¶m_cp, 0, sizeof(param_cp)); |
| 3355 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3356 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3357 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Marcel Holtmann | 79830f6 | 2013-10-18 16:38:09 -0700 | [diff] [blame] | 3358 | param_cp.own_address_type = hdev->own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3359 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3360 | ¶m_cp); |
| 3361 | |
| 3362 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3363 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3364 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3365 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3366 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3367 | break; |
| 3368 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3369 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3370 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3371 | MGMT_STATUS_INVALID_PARAMS); |
| 3372 | mgmt_pending_remove(cmd); |
| 3373 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3374 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3375 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3376 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3377 | if (err < 0) |
| 3378 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3379 | else |
| 3380 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3381 | |
| 3382 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3383 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3384 | return err; |
| 3385 | } |
| 3386 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3387 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3388 | { |
| 3389 | struct pending_cmd *cmd; |
| 3390 | int err; |
| 3391 | |
| 3392 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3393 | if (!cmd) |
| 3394 | return -ENOENT; |
| 3395 | |
| 3396 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3397 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3398 | mgmt_pending_remove(cmd); |
| 3399 | |
| 3400 | return err; |
| 3401 | } |
| 3402 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3403 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3404 | { |
| 3405 | BT_DBG("status %d", status); |
| 3406 | |
| 3407 | hci_dev_lock(hdev); |
| 3408 | |
| 3409 | if (status) { |
| 3410 | mgmt_stop_discovery_failed(hdev, status); |
| 3411 | goto unlock; |
| 3412 | } |
| 3413 | |
| 3414 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3415 | |
| 3416 | unlock: |
| 3417 | hci_dev_unlock(hdev); |
| 3418 | } |
| 3419 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3420 | 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] | 3421 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3422 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3423 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3424 | struct pending_cmd *cmd; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3425 | struct hci_cp_remote_name_req_cancel cp; |
| 3426 | struct inquiry_entry *e; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3427 | struct hci_request req; |
| 3428 | struct hci_cp_le_set_scan_enable enable_cp; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3429 | int err; |
| 3430 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3431 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3432 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3433 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3434 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3435 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3436 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3437 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3438 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3439 | goto unlock; |
| 3440 | } |
| 3441 | |
| 3442 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3443 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3444 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3445 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3446 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3447 | } |
| 3448 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3449 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3450 | if (!cmd) { |
| 3451 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3452 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3453 | } |
| 3454 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3455 | hci_req_init(&req, hdev); |
| 3456 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3457 | switch (hdev->discovery.state) { |
| 3458 | case DISCOVERY_FINDING: |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3459 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3460 | hci_req_add(&req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 3461 | } else { |
| 3462 | cancel_delayed_work(&hdev->le_scan_disable); |
| 3463 | |
| 3464 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3465 | enable_cp.enable = LE_SCAN_DISABLE; |
| 3466 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, |
| 3467 | sizeof(enable_cp), &enable_cp); |
| 3468 | } |
Andre Guedes | c9ecc48 | 2012-03-15 16:52:08 -0300 | [diff] [blame] | 3469 | |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3470 | break; |
| 3471 | |
| 3472 | case DISCOVERY_RESOLVING: |
| 3473 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 3474 | NAME_PENDING); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3475 | if (!e) { |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3476 | mgmt_pending_remove(cmd); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3477 | err = cmd_complete(sk, hdev->id, |
| 3478 | MGMT_OP_STOP_DISCOVERY, 0, |
| 3479 | &mgmt_cp->type, |
| 3480 | sizeof(mgmt_cp->type)); |
| 3481 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3482 | goto unlock; |
| 3483 | } |
| 3484 | |
| 3485 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3486 | hci_req_add(&req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 3487 | &cp); |
Andre Guedes | e0d9727e | 2012-03-20 15:15:36 -0300 | [diff] [blame] | 3488 | |
| 3489 | break; |
| 3490 | |
| 3491 | default: |
| 3492 | BT_DBG("unknown discovery state %u", hdev->discovery.state); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3493 | |
| 3494 | mgmt_pending_remove(cmd); |
| 3495 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
| 3496 | MGMT_STATUS_FAILED, &mgmt_cp->type, |
| 3497 | sizeof(mgmt_cp->type)); |
| 3498 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3499 | } |
| 3500 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3501 | err = hci_req_run(&req, stop_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3502 | if (err < 0) |
| 3503 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3504 | else |
| 3505 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3506 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3507 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3508 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3509 | return err; |
| 3510 | } |
| 3511 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3512 | 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] | 3513 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3514 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3515 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3516 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3517 | int err; |
| 3518 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3519 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3520 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3521 | hci_dev_lock(hdev); |
| 3522 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3523 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3524 | err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3525 | MGMT_STATUS_FAILED); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3526 | goto failed; |
| 3527 | } |
| 3528 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3529 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3530 | if (!e) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3531 | err = cmd_status(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3532 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3533 | goto failed; |
| 3534 | } |
| 3535 | |
| 3536 | if (cp->name_known) { |
| 3537 | e->name_state = NAME_KNOWN; |
| 3538 | list_del(&e->list); |
| 3539 | } else { |
| 3540 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3541 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3542 | } |
| 3543 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3544 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3545 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3546 | |
| 3547 | failed: |
| 3548 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3549 | return err; |
| 3550 | } |
| 3551 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3552 | 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] | 3553 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3554 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3555 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3556 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3557 | int err; |
| 3558 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3559 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3560 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3561 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3562 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3563 | MGMT_STATUS_INVALID_PARAMS, |
| 3564 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3565 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3566 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3567 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3568 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3569 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3570 | status = MGMT_STATUS_FAILED; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3571 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3572 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3573 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3574 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3575 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3576 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3577 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3578 | |
| 3579 | return err; |
| 3580 | } |
| 3581 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3582 | 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] | 3583 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3584 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3585 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3586 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3587 | int err; |
| 3588 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3589 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3590 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3591 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3592 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3593 | MGMT_STATUS_INVALID_PARAMS, |
| 3594 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3595 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3596 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3597 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3598 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3599 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3600 | status = MGMT_STATUS_INVALID_PARAMS; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3601 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3602 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3603 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3604 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3605 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3606 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3607 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3608 | |
| 3609 | return err; |
| 3610 | } |
| 3611 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3612 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3613 | u16 len) |
| 3614 | { |
| 3615 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3616 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3617 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3618 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3619 | |
| 3620 | BT_DBG("%s", hdev->name); |
| 3621 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3622 | source = __le16_to_cpu(cp->source); |
| 3623 | |
| 3624 | if (source > 0x0002) |
| 3625 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3626 | MGMT_STATUS_INVALID_PARAMS); |
| 3627 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3628 | hci_dev_lock(hdev); |
| 3629 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3630 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3631 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3632 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3633 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3634 | |
| 3635 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3636 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3637 | hci_req_init(&req, hdev); |
| 3638 | update_eir(&req); |
| 3639 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3640 | |
| 3641 | hci_dev_unlock(hdev); |
| 3642 | |
| 3643 | return err; |
| 3644 | } |
| 3645 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3646 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3647 | { |
| 3648 | struct cmd_lookup match = { NULL, hdev }; |
| 3649 | |
| 3650 | if (status) { |
| 3651 | u8 mgmt_err = mgmt_status(status); |
| 3652 | |
| 3653 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3654 | cmd_status_rsp, &mgmt_err); |
| 3655 | return; |
| 3656 | } |
| 3657 | |
| 3658 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3659 | &match); |
| 3660 | |
| 3661 | new_settings(hdev, match.sk); |
| 3662 | |
| 3663 | if (match.sk) |
| 3664 | sock_put(match.sk); |
| 3665 | } |
| 3666 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3667 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3668 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3669 | { |
| 3670 | struct mgmt_mode *cp = data; |
| 3671 | struct pending_cmd *cmd; |
| 3672 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3673 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3674 | int err; |
| 3675 | |
| 3676 | BT_DBG("request for %s", hdev->name); |
| 3677 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3678 | status = mgmt_le_support(hdev); |
| 3679 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3680 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3681 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3682 | |
| 3683 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3684 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3685 | MGMT_STATUS_INVALID_PARAMS); |
| 3686 | |
| 3687 | hci_dev_lock(hdev); |
| 3688 | |
| 3689 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3690 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3691 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3692 | /* The following conditions are ones which mean that we should |
| 3693 | * not do any HCI communication but directly send a mgmt |
| 3694 | * response to user space (after toggling the flag if |
| 3695 | * necessary). |
| 3696 | */ |
| 3697 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3698 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3699 | bool changed = false; |
| 3700 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3701 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3702 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3703 | changed = true; |
| 3704 | } |
| 3705 | |
| 3706 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3707 | if (err < 0) |
| 3708 | goto unlock; |
| 3709 | |
| 3710 | if (changed) |
| 3711 | err = new_settings(hdev, sk); |
| 3712 | |
| 3713 | goto unlock; |
| 3714 | } |
| 3715 | |
| 3716 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3717 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3718 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3719 | MGMT_STATUS_BUSY); |
| 3720 | goto unlock; |
| 3721 | } |
| 3722 | |
| 3723 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3724 | if (!cmd) { |
| 3725 | err = -ENOMEM; |
| 3726 | goto unlock; |
| 3727 | } |
| 3728 | |
| 3729 | hci_req_init(&req, hdev); |
| 3730 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3731 | if (val) |
| 3732 | enable_advertising(&req); |
| 3733 | else |
| 3734 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3735 | |
| 3736 | err = hci_req_run(&req, set_advertising_complete); |
| 3737 | if (err < 0) |
| 3738 | mgmt_pending_remove(cmd); |
| 3739 | |
| 3740 | unlock: |
| 3741 | hci_dev_unlock(hdev); |
| 3742 | return err; |
| 3743 | } |
| 3744 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3745 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3746 | void *data, u16 len) |
| 3747 | { |
| 3748 | struct mgmt_cp_set_static_address *cp = data; |
| 3749 | int err; |
| 3750 | |
| 3751 | BT_DBG("%s", hdev->name); |
| 3752 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3753 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3754 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3755 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3756 | |
| 3757 | if (hdev_is_powered(hdev)) |
| 3758 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3759 | MGMT_STATUS_REJECTED); |
| 3760 | |
| 3761 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3762 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3763 | return cmd_status(sk, hdev->id, |
| 3764 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3765 | MGMT_STATUS_INVALID_PARAMS); |
| 3766 | |
| 3767 | /* Two most significant bits shall be set */ |
| 3768 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3769 | return cmd_status(sk, hdev->id, |
| 3770 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3771 | MGMT_STATUS_INVALID_PARAMS); |
| 3772 | } |
| 3773 | |
| 3774 | hci_dev_lock(hdev); |
| 3775 | |
| 3776 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3777 | |
| 3778 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3779 | |
| 3780 | hci_dev_unlock(hdev); |
| 3781 | |
| 3782 | return err; |
| 3783 | } |
| 3784 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3785 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 3786 | void *data, u16 len) |
| 3787 | { |
| 3788 | struct mgmt_cp_set_scan_params *cp = data; |
| 3789 | __u16 interval, window; |
| 3790 | int err; |
| 3791 | |
| 3792 | BT_DBG("%s", hdev->name); |
| 3793 | |
| 3794 | if (!lmp_le_capable(hdev)) |
| 3795 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3796 | MGMT_STATUS_NOT_SUPPORTED); |
| 3797 | |
| 3798 | interval = __le16_to_cpu(cp->interval); |
| 3799 | |
| 3800 | if (interval < 0x0004 || interval > 0x4000) |
| 3801 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3802 | MGMT_STATUS_INVALID_PARAMS); |
| 3803 | |
| 3804 | window = __le16_to_cpu(cp->window); |
| 3805 | |
| 3806 | if (window < 0x0004 || window > 0x4000) |
| 3807 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3808 | MGMT_STATUS_INVALID_PARAMS); |
| 3809 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 3810 | if (window > interval) |
| 3811 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3812 | MGMT_STATUS_INVALID_PARAMS); |
| 3813 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3814 | hci_dev_lock(hdev); |
| 3815 | |
| 3816 | hdev->le_scan_interval = interval; |
| 3817 | hdev->le_scan_window = window; |
| 3818 | |
| 3819 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 3820 | |
| 3821 | hci_dev_unlock(hdev); |
| 3822 | |
| 3823 | return err; |
| 3824 | } |
| 3825 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3826 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 3827 | { |
| 3828 | struct pending_cmd *cmd; |
| 3829 | |
| 3830 | BT_DBG("status 0x%02x", status); |
| 3831 | |
| 3832 | hci_dev_lock(hdev); |
| 3833 | |
| 3834 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 3835 | if (!cmd) |
| 3836 | goto unlock; |
| 3837 | |
| 3838 | if (status) { |
| 3839 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3840 | mgmt_status(status)); |
| 3841 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 3842 | struct mgmt_mode *cp = cmd->param; |
| 3843 | |
| 3844 | if (cp->val) |
| 3845 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 3846 | else |
| 3847 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 3848 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3849 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 3850 | new_settings(hdev, cmd->sk); |
| 3851 | } |
| 3852 | |
| 3853 | mgmt_pending_remove(cmd); |
| 3854 | |
| 3855 | unlock: |
| 3856 | hci_dev_unlock(hdev); |
| 3857 | } |
| 3858 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3859 | 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] | 3860 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3861 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3862 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3863 | struct pending_cmd *cmd; |
| 3864 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3865 | int err; |
| 3866 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3867 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3868 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3869 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 3870 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 3871 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3872 | MGMT_STATUS_NOT_SUPPORTED); |
| 3873 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 3874 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3875 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3876 | MGMT_STATUS_INVALID_PARAMS); |
| 3877 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 3878 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3879 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3880 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 3881 | |
| 3882 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
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_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3885 | |
| 3886 | hci_dev_lock(hdev); |
| 3887 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 3888 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 3889 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3890 | MGMT_STATUS_BUSY); |
| 3891 | goto unlock; |
| 3892 | } |
| 3893 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 3894 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 3895 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 3896 | hdev); |
| 3897 | goto unlock; |
| 3898 | } |
| 3899 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3900 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 3901 | data, len); |
| 3902 | if (!cmd) { |
| 3903 | err = -ENOMEM; |
| 3904 | goto unlock; |
| 3905 | } |
| 3906 | |
| 3907 | hci_req_init(&req, hdev); |
| 3908 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 3909 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3910 | |
| 3911 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3912 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3913 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3914 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3915 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3916 | } |
| 3917 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3918 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3919 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 3920 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 3921 | return err; |
| 3922 | } |
| 3923 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 3924 | static void set_bredr_scan(struct hci_request *req) |
| 3925 | { |
| 3926 | struct hci_dev *hdev = req->hdev; |
| 3927 | u8 scan = 0; |
| 3928 | |
| 3929 | /* Ensure that fast connectable is disabled. This function will |
| 3930 | * not do anything if the page scan parameters are already what |
| 3931 | * they should be. |
| 3932 | */ |
| 3933 | write_fast_connectable(req, false); |
| 3934 | |
| 3935 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 3936 | scan |= SCAN_PAGE; |
| 3937 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 3938 | scan |= SCAN_INQUIRY; |
| 3939 | |
| 3940 | if (scan) |
| 3941 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 3942 | } |
| 3943 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 3944 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 3945 | { |
| 3946 | struct pending_cmd *cmd; |
| 3947 | |
| 3948 | BT_DBG("status 0x%02x", status); |
| 3949 | |
| 3950 | hci_dev_lock(hdev); |
| 3951 | |
| 3952 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 3953 | if (!cmd) |
| 3954 | goto unlock; |
| 3955 | |
| 3956 | if (status) { |
| 3957 | u8 mgmt_err = mgmt_status(status); |
| 3958 | |
| 3959 | /* We need to restore the flag if related HCI commands |
| 3960 | * failed. |
| 3961 | */ |
| 3962 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 3963 | |
| 3964 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 3965 | } else { |
| 3966 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 3967 | new_settings(hdev, cmd->sk); |
| 3968 | } |
| 3969 | |
| 3970 | mgmt_pending_remove(cmd); |
| 3971 | |
| 3972 | unlock: |
| 3973 | hci_dev_unlock(hdev); |
| 3974 | } |
| 3975 | |
| 3976 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 3977 | { |
| 3978 | struct mgmt_mode *cp = data; |
| 3979 | struct pending_cmd *cmd; |
| 3980 | struct hci_request req; |
| 3981 | int err; |
| 3982 | |
| 3983 | BT_DBG("request for %s", hdev->name); |
| 3984 | |
| 3985 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 3986 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 3987 | MGMT_STATUS_NOT_SUPPORTED); |
| 3988 | |
| 3989 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 3990 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 3991 | MGMT_STATUS_REJECTED); |
| 3992 | |
| 3993 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3994 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 3995 | MGMT_STATUS_INVALID_PARAMS); |
| 3996 | |
| 3997 | hci_dev_lock(hdev); |
| 3998 | |
| 3999 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4000 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4001 | goto unlock; |
| 4002 | } |
| 4003 | |
| 4004 | if (!hdev_is_powered(hdev)) { |
| 4005 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4006 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4007 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4008 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4009 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4010 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4011 | } |
| 4012 | |
| 4013 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4014 | |
| 4015 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4016 | if (err < 0) |
| 4017 | goto unlock; |
| 4018 | |
| 4019 | err = new_settings(hdev, sk); |
| 4020 | goto unlock; |
| 4021 | } |
| 4022 | |
| 4023 | /* Reject disabling when powered on */ |
| 4024 | if (!cp->val) { |
| 4025 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4026 | MGMT_STATUS_REJECTED); |
| 4027 | goto unlock; |
| 4028 | } |
| 4029 | |
| 4030 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4031 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4032 | MGMT_STATUS_BUSY); |
| 4033 | goto unlock; |
| 4034 | } |
| 4035 | |
| 4036 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4037 | if (!cmd) { |
| 4038 | err = -ENOMEM; |
| 4039 | goto unlock; |
| 4040 | } |
| 4041 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4042 | /* 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] | 4043 | * generates the correct flags. |
| 4044 | */ |
| 4045 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4046 | |
| 4047 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4048 | |
| 4049 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4050 | set_bredr_scan(&req); |
| 4051 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4052 | /* Since only the advertising data flags will change, there |
| 4053 | * is no need to update the scan response data. |
| 4054 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4055 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4056 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4057 | err = hci_req_run(&req, set_bredr_complete); |
| 4058 | if (err < 0) |
| 4059 | mgmt_pending_remove(cmd); |
| 4060 | |
| 4061 | unlock: |
| 4062 | hci_dev_unlock(hdev); |
| 4063 | return err; |
| 4064 | } |
| 4065 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4066 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4067 | void *data, u16 len) |
| 4068 | { |
| 4069 | struct mgmt_mode *cp = data; |
| 4070 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4071 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4072 | int err; |
| 4073 | |
| 4074 | BT_DBG("request for %s", hdev->name); |
| 4075 | |
| 4076 | status = mgmt_bredr_support(hdev); |
| 4077 | if (status) |
| 4078 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4079 | status); |
| 4080 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4081 | if (!lmp_sc_capable(hdev) && |
| 4082 | !test_bit(HCI_FORCE_SC, &hdev->dev_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4083 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4084 | MGMT_STATUS_NOT_SUPPORTED); |
| 4085 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4086 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
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_INVALID_PARAMS); |
| 4089 | |
| 4090 | hci_dev_lock(hdev); |
| 4091 | |
| 4092 | if (!hdev_is_powered(hdev)) { |
| 4093 | bool changed; |
| 4094 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4095 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4096 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4097 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4098 | if (cp->val == 0x02) |
| 4099 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4100 | else |
| 4101 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4102 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4103 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4104 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4105 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4106 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4107 | |
| 4108 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4109 | if (err < 0) |
| 4110 | goto failed; |
| 4111 | |
| 4112 | if (changed) |
| 4113 | err = new_settings(hdev, sk); |
| 4114 | |
| 4115 | goto failed; |
| 4116 | } |
| 4117 | |
| 4118 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4119 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4120 | MGMT_STATUS_BUSY); |
| 4121 | goto failed; |
| 4122 | } |
| 4123 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4124 | val = !!cp->val; |
| 4125 | |
| 4126 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4127 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4128 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4129 | goto failed; |
| 4130 | } |
| 4131 | |
| 4132 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4133 | if (!cmd) { |
| 4134 | err = -ENOMEM; |
| 4135 | goto failed; |
| 4136 | } |
| 4137 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4138 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4139 | if (err < 0) { |
| 4140 | mgmt_pending_remove(cmd); |
| 4141 | goto failed; |
| 4142 | } |
| 4143 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4144 | if (cp->val == 0x02) |
| 4145 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4146 | else |
| 4147 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4148 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4149 | failed: |
| 4150 | hci_dev_unlock(hdev); |
| 4151 | return err; |
| 4152 | } |
| 4153 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4154 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4155 | void *data, u16 len) |
| 4156 | { |
| 4157 | struct mgmt_mode *cp = data; |
| 4158 | bool changed; |
| 4159 | int err; |
| 4160 | |
| 4161 | BT_DBG("request for %s", hdev->name); |
| 4162 | |
| 4163 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4164 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4165 | MGMT_STATUS_INVALID_PARAMS); |
| 4166 | |
| 4167 | hci_dev_lock(hdev); |
| 4168 | |
| 4169 | if (cp->val) |
| 4170 | changed = !test_and_set_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 4171 | else |
| 4172 | changed = test_and_clear_bit(HCI_DEBUG_KEYS, &hdev->dev_flags); |
| 4173 | |
| 4174 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4175 | if (err < 0) |
| 4176 | goto unlock; |
| 4177 | |
| 4178 | if (changed) |
| 4179 | err = new_settings(hdev, sk); |
| 4180 | |
| 4181 | unlock: |
| 4182 | hci_dev_unlock(hdev); |
| 4183 | return err; |
| 4184 | } |
| 4185 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4186 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4187 | { |
| 4188 | switch (irk->addr.type) { |
| 4189 | case BDADDR_LE_PUBLIC: |
| 4190 | return true; |
| 4191 | |
| 4192 | case BDADDR_LE_RANDOM: |
| 4193 | /* Two most significant bits shall be set */ |
| 4194 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4195 | return false; |
| 4196 | return true; |
| 4197 | } |
| 4198 | |
| 4199 | return false; |
| 4200 | } |
| 4201 | |
| 4202 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4203 | u16 len) |
| 4204 | { |
| 4205 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4206 | u16 irk_count, expected_len; |
| 4207 | int i, err; |
| 4208 | |
| 4209 | BT_DBG("request for %s", hdev->name); |
| 4210 | |
| 4211 | if (!lmp_le_capable(hdev)) |
| 4212 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4213 | MGMT_STATUS_NOT_SUPPORTED); |
| 4214 | |
| 4215 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4216 | |
| 4217 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4218 | if (expected_len != len) { |
| 4219 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
| 4220 | len, expected_len); |
| 4221 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4222 | MGMT_STATUS_INVALID_PARAMS); |
| 4223 | } |
| 4224 | |
| 4225 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4226 | |
| 4227 | for (i = 0; i < irk_count; i++) { |
| 4228 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4229 | |
| 4230 | if (!irk_is_valid(key)) |
| 4231 | return cmd_status(sk, hdev->id, |
| 4232 | MGMT_OP_LOAD_IRKS, |
| 4233 | MGMT_STATUS_INVALID_PARAMS); |
| 4234 | } |
| 4235 | |
| 4236 | hci_dev_lock(hdev); |
| 4237 | |
| 4238 | hci_smp_irks_clear(hdev); |
| 4239 | |
| 4240 | for (i = 0; i < irk_count; i++) { |
| 4241 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4242 | u8 addr_type; |
| 4243 | |
| 4244 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4245 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4246 | else |
| 4247 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4248 | |
| 4249 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4250 | BDADDR_ANY); |
| 4251 | } |
| 4252 | |
| 4253 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4254 | |
| 4255 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4256 | |
| 4257 | hci_dev_unlock(hdev); |
| 4258 | |
| 4259 | return err; |
| 4260 | } |
| 4261 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4262 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4263 | { |
| 4264 | if (key->master != 0x00 && key->master != 0x01) |
| 4265 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4266 | |
| 4267 | switch (key->addr.type) { |
| 4268 | case BDADDR_LE_PUBLIC: |
| 4269 | return true; |
| 4270 | |
| 4271 | case BDADDR_LE_RANDOM: |
| 4272 | /* Two most significant bits shall be set */ |
| 4273 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4274 | return false; |
| 4275 | return true; |
| 4276 | } |
| 4277 | |
| 4278 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4279 | } |
| 4280 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4281 | 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] | 4282 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4283 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4284 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4285 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4286 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4287 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4288 | BT_DBG("request for %s", hdev->name); |
| 4289 | |
| 4290 | if (!lmp_le_capable(hdev)) |
| 4291 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4292 | MGMT_STATUS_NOT_SUPPORTED); |
| 4293 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4294 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4295 | |
| 4296 | expected_len = sizeof(*cp) + key_count * |
| 4297 | sizeof(struct mgmt_ltk_info); |
| 4298 | if (expected_len != len) { |
| 4299 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4300 | len, expected_len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4301 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4302 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4303 | } |
| 4304 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4305 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4306 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4307 | for (i = 0; i < key_count; i++) { |
| 4308 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4309 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4310 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4311 | return cmd_status(sk, hdev->id, |
| 4312 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4313 | MGMT_STATUS_INVALID_PARAMS); |
| 4314 | } |
| 4315 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4316 | hci_dev_lock(hdev); |
| 4317 | |
| 4318 | hci_smp_ltks_clear(hdev); |
| 4319 | |
| 4320 | for (i = 0; i < key_count; i++) { |
| 4321 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4322 | u8 type, addr_type; |
| 4323 | |
| 4324 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4325 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4326 | else |
| 4327 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4328 | |
| 4329 | if (key->master) |
| 4330 | type = HCI_SMP_LTK; |
| 4331 | else |
| 4332 | type = HCI_SMP_LTK_SLAVE; |
| 4333 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4334 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
| 4335 | key->type, key->val, key->enc_size, key->ediv, |
| 4336 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4337 | } |
| 4338 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4339 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4340 | NULL, 0); |
| 4341 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4342 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4343 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4344 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4345 | } |
| 4346 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4347 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4348 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 4349 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4350 | bool var_len; |
| 4351 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4352 | } mgmt_handlers[] = { |
| 4353 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4354 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 4355 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 4356 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 4357 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 4358 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 4359 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 4360 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 4361 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 4362 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 4363 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 4364 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 4365 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 4366 | { set_le, false, MGMT_SETTING_SIZE }, |
| 4367 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 4368 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 4369 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 4370 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 4371 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 4372 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 4373 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 4374 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 4375 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 4376 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 4377 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 4378 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 4379 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 4380 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 4381 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 4382 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 4383 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 4384 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 4385 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4386 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4387 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 4388 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 4389 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 4390 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 4391 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 4392 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4393 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4394 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4395 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4396 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4397 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4398 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4399 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4400 | { }, |
| 4401 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4402 | }; |
| 4403 | |
| 4404 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4405 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 4406 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4407 | void *buf; |
| 4408 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4409 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 4410 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4411 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4412 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4413 | int err; |
| 4414 | |
| 4415 | BT_DBG("got %zu bytes", msglen); |
| 4416 | |
| 4417 | if (msglen < sizeof(*hdr)) |
| 4418 | return -EINVAL; |
| 4419 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 4420 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4421 | if (!buf) |
| 4422 | return -ENOMEM; |
| 4423 | |
| 4424 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 4425 | err = -EFAULT; |
| 4426 | goto done; |
| 4427 | } |
| 4428 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4429 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4430 | opcode = __le16_to_cpu(hdr->opcode); |
| 4431 | index = __le16_to_cpu(hdr->index); |
| 4432 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4433 | |
| 4434 | if (len != msglen - sizeof(*hdr)) { |
| 4435 | err = -EINVAL; |
| 4436 | goto done; |
| 4437 | } |
| 4438 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4439 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4440 | hdev = hci_dev_get(index); |
| 4441 | if (!hdev) { |
| 4442 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4443 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4444 | goto done; |
| 4445 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4446 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 4447 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
| 4448 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4449 | err = cmd_status(sk, index, opcode, |
| 4450 | MGMT_STATUS_INVALID_INDEX); |
| 4451 | goto done; |
| 4452 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4453 | } |
| 4454 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4455 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4456 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4457 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4458 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4459 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4460 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4461 | } |
| 4462 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4463 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4464 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4465 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4466 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4467 | goto done; |
| 4468 | } |
| 4469 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4470 | handler = &mgmt_handlers[opcode]; |
| 4471 | |
| 4472 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4473 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4474 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4475 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4476 | goto done; |
| 4477 | } |
| 4478 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4479 | if (hdev) |
| 4480 | mgmt_init_hdev(sk, hdev); |
| 4481 | |
| 4482 | cp = buf + sizeof(*hdr); |
| 4483 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4484 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 4485 | if (err < 0) |
| 4486 | goto done; |
| 4487 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4488 | err = msglen; |
| 4489 | |
| 4490 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4491 | if (hdev) |
| 4492 | hci_dev_put(hdev); |
| 4493 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4494 | kfree(buf); |
| 4495 | return err; |
| 4496 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4497 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4498 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4499 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4500 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4501 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4502 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4503 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4504 | } |
| 4505 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4506 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4507 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 4508 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4509 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4510 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4511 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4512 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4513 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4514 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4515 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 4516 | } |
| 4517 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4518 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 4519 | { |
| 4520 | struct cmd_lookup match = { NULL, hdev }; |
| 4521 | |
| 4522 | BT_DBG("status 0x%02x", status); |
| 4523 | |
| 4524 | hci_dev_lock(hdev); |
| 4525 | |
| 4526 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 4527 | |
| 4528 | new_settings(hdev, match.sk); |
| 4529 | |
| 4530 | hci_dev_unlock(hdev); |
| 4531 | |
| 4532 | if (match.sk) |
| 4533 | sock_put(match.sk); |
| 4534 | } |
| 4535 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4536 | static int powered_update_hci(struct hci_dev *hdev) |
| 4537 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4538 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4539 | u8 link_sec; |
| 4540 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4541 | hci_req_init(&req, hdev); |
| 4542 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4543 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 4544 | !lmp_host_ssp_capable(hdev)) { |
| 4545 | u8 ssp = 1; |
| 4546 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4547 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4548 | } |
| 4549 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 4550 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 4551 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4552 | struct hci_cp_write_le_host_supported cp; |
| 4553 | |
| 4554 | cp.le = 1; |
| 4555 | cp.simul = lmp_le_br_capable(hdev); |
| 4556 | |
| 4557 | /* Check first if we already have the right |
| 4558 | * host state (host features set) |
| 4559 | */ |
| 4560 | if (cp.le != lmp_host_le_capable(hdev) || |
| 4561 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4562 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 4563 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4564 | } |
| 4565 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4566 | if (lmp_le_capable(hdev)) { |
| 4567 | /* Set random address to static address if configured */ |
| 4568 | if (bacmp(&hdev->static_addr, BDADDR_ANY)) |
| 4569 | hci_req_add(&req, HCI_OP_LE_SET_RANDOM_ADDR, 6, |
| 4570 | &hdev->static_addr); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4571 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 4572 | /* Make sure the controller has a good default for |
| 4573 | * advertising data. This also applies to the case |
| 4574 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 4575 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4576 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4577 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4578 | update_scan_rsp_data(&req); |
| 4579 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 4580 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 4581 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 4582 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 4583 | } |
| 4584 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4585 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4586 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4587 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 4588 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4589 | |
| 4590 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4591 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 4592 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4593 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 4594 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 4595 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4596 | } |
| 4597 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4598 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 4599 | } |
| 4600 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4601 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 4602 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 4603 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4604 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 4605 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 4606 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 4607 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 4608 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 4609 | return 0; |
| 4610 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 4611 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4612 | if (powered_update_hci(hdev) == 0) |
| 4613 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 4614 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4615 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 4616 | &match); |
| 4617 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4618 | } |
| 4619 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 4620 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 4621 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 4622 | |
| 4623 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 4624 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 4625 | zero_cod, sizeof(zero_cod), NULL); |
| 4626 | |
| 4627 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 4628 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 4629 | |
| 4630 | if (match.sk) |
| 4631 | sock_put(match.sk); |
| 4632 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 4633 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 4634 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4635 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 4636 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4637 | { |
| 4638 | struct pending_cmd *cmd; |
| 4639 | u8 status; |
| 4640 | |
| 4641 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 4642 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 4643 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4644 | |
| 4645 | if (err == -ERFKILL) |
| 4646 | status = MGMT_STATUS_RFKILLED; |
| 4647 | else |
| 4648 | status = MGMT_STATUS_FAILED; |
| 4649 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 4650 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4651 | |
| 4652 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 4653 | } |
| 4654 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4655 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 4656 | { |
| 4657 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4658 | |
| 4659 | hci_dev_lock(hdev); |
| 4660 | |
| 4661 | /* When discoverable timeout triggers, then just make sure |
| 4662 | * the limited discoverable flag is cleared. Even in the case |
| 4663 | * of a timeout triggered from general discoverable, it is |
| 4664 | * safe to unconditionally clear the flag. |
| 4665 | */ |
| 4666 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4667 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4668 | |
| 4669 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 4670 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4671 | u8 scan = SCAN_PAGE; |
| 4672 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 4673 | sizeof(scan), &scan); |
| 4674 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4675 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4676 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4677 | hci_req_run(&req, NULL); |
| 4678 | |
| 4679 | hdev->discov_timeout = 0; |
| 4680 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4681 | new_settings(hdev, NULL); |
| 4682 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 4683 | hci_dev_unlock(hdev); |
| 4684 | } |
| 4685 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4686 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4687 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4688 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4689 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 4690 | /* Nothing needed here if there's a pending command since that |
| 4691 | * commands request completion callback takes care of everything |
| 4692 | * necessary. |
| 4693 | */ |
| 4694 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4695 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 4696 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4697 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4698 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4699 | } else { |
| 4700 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4701 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4702 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4703 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4704 | if (changed) { |
| 4705 | struct hci_request req; |
| 4706 | |
| 4707 | /* In case this change in discoverable was triggered by |
| 4708 | * a disabling of connectable there could be a need to |
| 4709 | * update the advertising flags. |
| 4710 | */ |
| 4711 | hci_req_init(&req, hdev); |
| 4712 | update_adv_data(&req); |
| 4713 | hci_req_run(&req, NULL); |
| 4714 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 4715 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 4716 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 4717 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4718 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4719 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4720 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4721 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4722 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 4723 | /* Nothing needed here if there's a pending command since that |
| 4724 | * commands request completion callback takes care of everything |
| 4725 | * necessary. |
| 4726 | */ |
| 4727 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4728 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 4729 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4730 | if (connectable) |
| 4731 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 4732 | else |
| 4733 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4734 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 4735 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 4736 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 4737 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4738 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 4739 | 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] | 4740 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4741 | u8 mgmt_err = mgmt_status(status); |
| 4742 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 4743 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4744 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4745 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 4746 | |
| 4747 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4748 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, 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 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 4752 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 4753 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4754 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 4755 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4756 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4757 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4758 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4759 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 4760 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 4761 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4762 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 4763 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 4764 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4765 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 4766 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 4767 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4768 | |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 4769 | 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] | 4770 | { |
| 4771 | struct mgmt_ev_new_long_term_key ev; |
| 4772 | |
| 4773 | memset(&ev, 0, sizeof(ev)); |
| 4774 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 4775 | /* Devices using resolvable or non-resolvable random addresses |
| 4776 | * without providing an indentity resolving key don't require |
| 4777 | * to store long term keys. Their addresses will change the |
| 4778 | * next time around. |
| 4779 | * |
| 4780 | * Only when a remote device provides an identity address |
| 4781 | * make sure the long term key is stored. If the remote |
| 4782 | * identity is known, the long term keys are internally |
| 4783 | * mapped to the identity address. So allow static random |
| 4784 | * and public addresses here. |
| 4785 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 4786 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 4787 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 4788 | ev.store_hint = 0x00; |
| 4789 | else |
| 4790 | ev.store_hint = 0x01; |
| 4791 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4792 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4793 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Marcel Holtmann | d40f3ee | 2014-01-31 18:42:17 -0800 | [diff] [blame] | 4794 | ev.key.type = key->authenticated; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4795 | ev.key.enc_size = key->enc_size; |
| 4796 | ev.key.ediv = key->ediv; |
| 4797 | |
| 4798 | if (key->type == HCI_SMP_LTK) |
| 4799 | ev.key.master = 1; |
| 4800 | |
| 4801 | memcpy(ev.key.rand, key->rand, sizeof(key->rand)); |
| 4802 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 4803 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 4804 | 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] | 4805 | } |
| 4806 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 4807 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 4808 | { |
| 4809 | struct mgmt_ev_new_irk ev; |
| 4810 | |
| 4811 | memset(&ev, 0, sizeof(ev)); |
| 4812 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 4813 | /* For identity resolving keys from devices that are already |
| 4814 | * using a public address or static random address, do not |
| 4815 | * ask for storing this key. The identity resolving key really |
| 4816 | * is only mandatory for devices using resovlable random |
| 4817 | * addresses. |
| 4818 | * |
| 4819 | * Storing all identity resolving keys has the downside that |
| 4820 | * they will be also loaded on next boot of they system. More |
| 4821 | * identity resolving keys, means more time during scanning is |
| 4822 | * needed to actually resolve these addresses. |
| 4823 | */ |
| 4824 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 4825 | ev.store_hint = 0x01; |
| 4826 | else |
| 4827 | ev.store_hint = 0x00; |
| 4828 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 4829 | bacpy(&ev.rpa, &irk->rpa); |
| 4830 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 4831 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 4832 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 4833 | |
| 4834 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 4835 | } |
| 4836 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 4837 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 4838 | u8 data_len) |
| 4839 | { |
| 4840 | eir[eir_len++] = sizeof(type) + data_len; |
| 4841 | eir[eir_len++] = type; |
| 4842 | memcpy(&eir[eir_len], data, data_len); |
| 4843 | eir_len += data_len; |
| 4844 | |
| 4845 | return eir_len; |
| 4846 | } |
| 4847 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 4848 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 4849 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 4850 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4851 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4852 | char buf[512]; |
| 4853 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 4854 | u16 eir_len = 0; |
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 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4857 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4858 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 4859 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 4860 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4861 | if (name_len > 0) |
| 4862 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4863 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4864 | |
| 4865 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 4866 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4867 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4868 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 4869 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 4870 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 4871 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 4872 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4873 | } |
| 4874 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4875 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 4876 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 4877 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4878 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 4879 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4880 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 4881 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4882 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4883 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 4884 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4885 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4886 | |
| 4887 | *sk = cmd->sk; |
| 4888 | sock_hold(*sk); |
| 4889 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 4890 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4891 | } |
| 4892 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4893 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4894 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 4895 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4896 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 4897 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4898 | |
| 4899 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4900 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4901 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4902 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 4903 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 4904 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 4905 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 4906 | |
| 4907 | mgmt_pending_remove(cmd); |
| 4908 | } |
| 4909 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 4910 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4911 | u8 link_type, u8 addr_type, u8 reason) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4912 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 4913 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4914 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4915 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 4916 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 4917 | return; |
| 4918 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4919 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4920 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 4921 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 4922 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 4923 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4924 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 4925 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4926 | |
| 4927 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 4928 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4929 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 4930 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4931 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4932 | } |
| 4933 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 4934 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4935 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4936 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 4937 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 4938 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 4939 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4940 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4941 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 4942 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 4943 | hdev); |
| 4944 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4945 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4946 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 4947 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4948 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 4949 | cp = cmd->param; |
| 4950 | |
| 4951 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 4952 | return; |
| 4953 | |
| 4954 | if (cp->addr.type != bdaddr_type) |
| 4955 | return; |
| 4956 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 4957 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 4958 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 4959 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 4960 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 4961 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 4962 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 4963 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 4964 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4965 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 4966 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 4967 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4968 | { |
| 4969 | struct mgmt_ev_connect_failed ev; |
| 4970 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 4971 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 4972 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4973 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4974 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 4975 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 4976 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4977 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 4978 | 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] | 4979 | { |
| 4980 | struct mgmt_ev_pin_code_request ev; |
| 4981 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 4982 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 4983 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 4984 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4985 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 4986 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4987 | } |
| 4988 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 4989 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 4990 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4991 | { |
| 4992 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 4993 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4994 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 4995 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4996 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 4997 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 4998 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 4999 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5000 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5001 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5002 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 5003 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5004 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5005 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5006 | } |
| 5007 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5008 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5009 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5010 | { |
| 5011 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5012 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5013 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5014 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5015 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5016 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5017 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5018 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5019 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5020 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5021 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 5022 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5023 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5024 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5025 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5026 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5027 | 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] | 5028 | u8 link_type, u8 addr_type, __le32 value, |
| 5029 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5030 | { |
| 5031 | struct mgmt_ev_user_confirm_request ev; |
| 5032 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5033 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5034 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5035 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5036 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 5037 | ev.confirm_hint = confirm_hint; |
Andrei Emeltchenko | 78e8098 | 2012-03-09 13:00:50 +0200 | [diff] [blame] | 5038 | ev.value = value; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5039 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5040 | 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] | 5041 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5042 | } |
| 5043 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5044 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5045 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5046 | { |
| 5047 | struct mgmt_ev_user_passkey_request ev; |
| 5048 | |
| 5049 | BT_DBG("%s", hdev->name); |
| 5050 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5051 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5052 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5053 | |
| 5054 | 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] | 5055 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5056 | } |
| 5057 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 5058 | 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] | 5059 | u8 link_type, u8 addr_type, u8 status, |
| 5060 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5061 | { |
| 5062 | struct pending_cmd *cmd; |
| 5063 | struct mgmt_rp_user_confirm_reply rp; |
| 5064 | int err; |
| 5065 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5066 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5067 | if (!cmd) |
| 5068 | return -ENOENT; |
| 5069 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5070 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5071 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5072 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5073 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5074 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5075 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5076 | |
| 5077 | return err; |
| 5078 | } |
| 5079 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5080 | 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] | 5081 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5082 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5083 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5084 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5085 | } |
| 5086 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5087 | 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] | 5088 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5089 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5090 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5091 | status, |
| 5092 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5093 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5094 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5095 | 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] | 5096 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5097 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5098 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5099 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5100 | } |
| 5101 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5102 | 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] | 5103 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5104 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5105 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5106 | status, |
| 5107 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5108 | } |
| 5109 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 5110 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5111 | u8 link_type, u8 addr_type, u32 passkey, |
| 5112 | u8 entered) |
| 5113 | { |
| 5114 | struct mgmt_ev_passkey_notify ev; |
| 5115 | |
| 5116 | BT_DBG("%s", hdev->name); |
| 5117 | |
| 5118 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5119 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5120 | ev.passkey = __cpu_to_le32(passkey); |
| 5121 | ev.entered = entered; |
| 5122 | |
| 5123 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 5124 | } |
| 5125 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5126 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5127 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5128 | { |
| 5129 | struct mgmt_ev_auth_failed ev; |
| 5130 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 5131 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5132 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5133 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5134 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5135 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5136 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5137 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5138 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5139 | { |
| 5140 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5141 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5142 | |
| 5143 | if (status) { |
| 5144 | u8 mgmt_err = mgmt_status(status); |
| 5145 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5146 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5147 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5148 | } |
| 5149 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5150 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 5151 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 5152 | &hdev->dev_flags); |
| 5153 | else |
| 5154 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 5155 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5156 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5157 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5158 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5159 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5160 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5161 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5162 | |
| 5163 | if (match.sk) |
| 5164 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5165 | } |
| 5166 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5167 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5168 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5169 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5170 | struct hci_cp_write_eir cp; |
| 5171 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 5172 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5173 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5174 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 5175 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 5176 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5177 | memset(&cp, 0, sizeof(cp)); |
| 5178 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5179 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5180 | } |
| 5181 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5182 | 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] | 5183 | { |
| 5184 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5185 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5186 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5187 | |
| 5188 | if (status) { |
| 5189 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5190 | |
| 5191 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5192 | &hdev->dev_flags)) { |
| 5193 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5194 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5195 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5196 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5197 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 5198 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5199 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5203 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5204 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5205 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 5206 | if (!changed) |
| 5207 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 5208 | &hdev->dev_flags); |
| 5209 | else |
| 5210 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5211 | } |
| 5212 | |
| 5213 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 5214 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5215 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5216 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5217 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5218 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5219 | sock_put(match.sk); |
| 5220 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5221 | hci_req_init(&req, hdev); |
| 5222 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5223 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5224 | update_eir(&req); |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5225 | else |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5226 | clear_eir(&req); |
| 5227 | |
| 5228 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5229 | } |
| 5230 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5231 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 5232 | { |
| 5233 | struct cmd_lookup match = { NULL, hdev }; |
| 5234 | bool changed = false; |
| 5235 | |
| 5236 | if (status) { |
| 5237 | u8 mgmt_err = mgmt_status(status); |
| 5238 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5239 | if (enable) { |
| 5240 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 5241 | &hdev->dev_flags)) |
| 5242 | new_settings(hdev, NULL); |
| 5243 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5244 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5245 | |
| 5246 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5247 | cmd_status_rsp, &mgmt_err); |
| 5248 | return; |
| 5249 | } |
| 5250 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5251 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5252 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5253 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5254 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5255 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5256 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5257 | |
| 5258 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5259 | settings_rsp, &match); |
| 5260 | |
| 5261 | if (changed) |
| 5262 | new_settings(hdev, match.sk); |
| 5263 | |
| 5264 | if (match.sk) |
| 5265 | sock_put(match.sk); |
| 5266 | } |
| 5267 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5268 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5269 | { |
| 5270 | struct cmd_lookup *match = data; |
| 5271 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5272 | if (match->sk == NULL) { |
| 5273 | match->sk = cmd->sk; |
| 5274 | sock_hold(match->sk); |
| 5275 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5276 | } |
| 5277 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5278 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 5279 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5280 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5281 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5282 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5283 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 5284 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 5285 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5286 | |
| 5287 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5288 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 5289 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5290 | |
| 5291 | if (match.sk) |
| 5292 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5293 | } |
| 5294 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5295 | 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] | 5296 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5297 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5298 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5299 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5300 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5301 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5302 | |
| 5303 | memset(&ev, 0, sizeof(ev)); |
| 5304 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5305 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5306 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5307 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5308 | if (!cmd) { |
| 5309 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5310 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5311 | /* If this is a HCI command related to powering on the |
| 5312 | * HCI dev don't send any mgmt signals. |
| 5313 | */ |
| 5314 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5315 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5316 | } |
| 5317 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5318 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 5319 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5320 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5321 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5322 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 5323 | u8 *randomizer192, u8 *hash256, |
| 5324 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5325 | { |
| 5326 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5327 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5328 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5329 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5330 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5331 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5332 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5333 | |
| 5334 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5335 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 5336 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5337 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5338 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 5339 | hash256 && randomizer256) { |
| 5340 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5341 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5342 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 5343 | memcpy(rp.randomizer192, randomizer192, |
| 5344 | sizeof(rp.randomizer192)); |
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.hash256, hash256, sizeof(rp.hash256)); |
| 5347 | memcpy(rp.randomizer256, randomizer256, |
| 5348 | sizeof(rp.randomizer256)); |
| 5349 | |
| 5350 | cmd_complete(cmd->sk, hdev->id, |
| 5351 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5352 | &rp, sizeof(rp)); |
| 5353 | } else { |
| 5354 | struct mgmt_rp_read_local_oob_data rp; |
| 5355 | |
| 5356 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 5357 | memcpy(rp.randomizer, randomizer192, |
| 5358 | sizeof(rp.randomizer)); |
| 5359 | |
| 5360 | cmd_complete(cmd->sk, hdev->id, |
| 5361 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5362 | &rp, sizeof(rp)); |
| 5363 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5364 | } |
| 5365 | |
| 5366 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5367 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5368 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5369 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5370 | u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, u8 |
| 5371 | ssp, u8 *eir, u16 eir_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5372 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5373 | char buf[512]; |
| 5374 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 5375 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5376 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5377 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5378 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5379 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5380 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5381 | /* Leave 5 bytes for a potential CoD field */ |
| 5382 | if (sizeof(*ev) + eir_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5383 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 5384 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5385 | memset(buf, 0, sizeof(buf)); |
| 5386 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 5387 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 5388 | if (irk) { |
| 5389 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 5390 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 5391 | } else { |
| 5392 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 5393 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 5394 | } |
| 5395 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5396 | ev->rssi = rssi; |
Johan Hedberg | 9a395a8 | 2012-02-23 00:00:32 +0200 | [diff] [blame] | 5397 | if (cfm_name) |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 5398 | ev->flags |= __constant_cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME); |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 5399 | if (!ssp) |
Syam Sidhardhan | 612dfce | 2012-10-29 22:37:36 +0530 | [diff] [blame] | 5400 | ev->flags |= __constant_cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5401 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5402 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5403 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5404 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5405 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 5406 | 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] | 5407 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5408 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5409 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5410 | ev_size = sizeof(*ev) + eir_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 5411 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5412 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5413 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5414 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 5415 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5416 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5417 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5418 | struct mgmt_ev_device_found *ev; |
| 5419 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 5420 | u16 eir_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 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5423 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5424 | memset(buf, 0, sizeof(buf)); |
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 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5427 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5428 | ev->rssi = rssi; |
| 5429 | |
| 5430 | 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] | 5431 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5432 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5433 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5434 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 5435 | 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] | 5436 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5437 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 5438 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5439 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 5440 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5441 | struct pending_cmd *cmd; |
| 5442 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 5443 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 5444 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5445 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5446 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5447 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5448 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5449 | |
| 5450 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 5451 | u8 type = hdev->discovery.type; |
| 5452 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5453 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 5454 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 5455 | mgmt_pending_remove(cmd); |
| 5456 | } |
| 5457 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 5458 | memset(&ev, 0, sizeof(ev)); |
| 5459 | ev.type = hdev->discovery.type; |
| 5460 | ev.discovering = discovering; |
| 5461 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 5462 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 5463 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5464 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5465 | 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] | 5466 | { |
| 5467 | struct pending_cmd *cmd; |
| 5468 | struct mgmt_ev_device_blocked ev; |
| 5469 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5470 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5471 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5472 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5473 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5474 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5475 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5476 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5477 | } |
| 5478 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5479 | 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] | 5480 | { |
| 5481 | struct pending_cmd *cmd; |
| 5482 | struct mgmt_ev_device_unblocked ev; |
| 5483 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5484 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5485 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 5486 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5487 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5488 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5489 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5490 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 5491 | } |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5492 | |
| 5493 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 5494 | { |
| 5495 | BT_DBG("%s status %u", hdev->name, status); |
| 5496 | |
| 5497 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 5498 | if (status) { |
| 5499 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 5500 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5501 | } |
| 5502 | } |
| 5503 | |
| 5504 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 5505 | { |
| 5506 | struct hci_request req; |
| 5507 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 5508 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5509 | return; |
| 5510 | |
| 5511 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5512 | return; |
| 5513 | |
| 5514 | hci_req_init(&req, hdev); |
| 5515 | enable_advertising(&req); |
| 5516 | |
| 5517 | /* If this fails we have no option but to let user space know |
| 5518 | * that we've disabled advertising. |
| 5519 | */ |
| 5520 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 5521 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 5522 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 5523 | } |
| 5524 | } |