Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 3 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4 | Copyright (C) 2010 Nokia Corporation |
Johan Hedberg | ea585ab | 2012-02-17 14:50:39 +0200 | [diff] [blame] | 5 | Copyright (C) 2011-2012 Intel Corporation |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License version 2 as |
| 9 | published by the Free Software Foundation; |
| 10 | |
| 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 12 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 13 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 14 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
| 15 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 16 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | |
| 20 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 21 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
| 22 | SOFTWARE IS DISCLAIMED. |
| 23 | */ |
| 24 | |
| 25 | /* Bluetooth HCI Management interface */ |
| 26 | |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 27 | #include <linux/module.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
| 29 | |
| 30 | #include <net/bluetooth/bluetooth.h> |
| 31 | #include <net/bluetooth/hci_core.h> |
Johan Hedberg | 4bc58f5 | 2014-05-20 09:45:47 +0300 | [diff] [blame] | 32 | #include <net/bluetooth/l2cap.h> |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 33 | #include <net/bluetooth/mgmt.h> |
Marcel Holtmann | ac4b723 | 2013-10-10 14:54:16 -0700 | [diff] [blame] | 34 | |
| 35 | #include "smp.h" |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 36 | |
Johan Hedberg | 2da9c55 | 2012-02-17 14:39:28 +0200 | [diff] [blame] | 37 | #define MGMT_VERSION 1 |
Marcel Holtmann | 730f091 | 2014-06-28 12:36:10 +0200 | [diff] [blame] | 38 | #define MGMT_REVISION 7 |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 39 | |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 40 | static const u16 mgmt_commands[] = { |
| 41 | MGMT_OP_READ_INDEX_LIST, |
| 42 | MGMT_OP_READ_INFO, |
| 43 | MGMT_OP_SET_POWERED, |
| 44 | MGMT_OP_SET_DISCOVERABLE, |
| 45 | MGMT_OP_SET_CONNECTABLE, |
| 46 | MGMT_OP_SET_FAST_CONNECTABLE, |
| 47 | MGMT_OP_SET_PAIRABLE, |
| 48 | MGMT_OP_SET_LINK_SECURITY, |
| 49 | MGMT_OP_SET_SSP, |
| 50 | MGMT_OP_SET_HS, |
| 51 | MGMT_OP_SET_LE, |
| 52 | MGMT_OP_SET_DEV_CLASS, |
| 53 | MGMT_OP_SET_LOCAL_NAME, |
| 54 | MGMT_OP_ADD_UUID, |
| 55 | MGMT_OP_REMOVE_UUID, |
| 56 | MGMT_OP_LOAD_LINK_KEYS, |
| 57 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 58 | MGMT_OP_DISCONNECT, |
| 59 | MGMT_OP_GET_CONNECTIONS, |
| 60 | MGMT_OP_PIN_CODE_REPLY, |
| 61 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 62 | MGMT_OP_SET_IO_CAPABILITY, |
| 63 | MGMT_OP_PAIR_DEVICE, |
| 64 | MGMT_OP_CANCEL_PAIR_DEVICE, |
| 65 | MGMT_OP_UNPAIR_DEVICE, |
| 66 | MGMT_OP_USER_CONFIRM_REPLY, |
| 67 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 68 | MGMT_OP_USER_PASSKEY_REPLY, |
| 69 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 70 | MGMT_OP_READ_LOCAL_OOB_DATA, |
| 71 | MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 72 | MGMT_OP_REMOVE_REMOTE_OOB_DATA, |
| 73 | MGMT_OP_START_DISCOVERY, |
| 74 | MGMT_OP_STOP_DISCOVERY, |
| 75 | MGMT_OP_CONFIRM_NAME, |
| 76 | MGMT_OP_BLOCK_DEVICE, |
| 77 | MGMT_OP_UNBLOCK_DEVICE, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 78 | MGMT_OP_SET_DEVICE_ID, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 79 | MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 80 | MGMT_OP_SET_BREDR, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 81 | MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 7f72134e | 2013-10-11 14:44:58 -0700 | [diff] [blame] | 82 | MGMT_OP_SET_SCAN_PARAMS, |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 83 | MGMT_OP_SET_SECURE_CONN, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 84 | MGMT_OP_SET_DEBUG_KEYS, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 85 | MGMT_OP_SET_PRIVACY, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 86 | MGMT_OP_LOAD_IRKS, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 87 | MGMT_OP_GET_CONN_INFO, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame^] | 88 | MGMT_OP_GET_CLOCK_INFO, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | static const u16 mgmt_events[] = { |
| 92 | MGMT_EV_CONTROLLER_ERROR, |
| 93 | MGMT_EV_INDEX_ADDED, |
| 94 | MGMT_EV_INDEX_REMOVED, |
| 95 | MGMT_EV_NEW_SETTINGS, |
| 96 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 97 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 98 | MGMT_EV_NEW_LINK_KEY, |
| 99 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 100 | MGMT_EV_DEVICE_CONNECTED, |
| 101 | MGMT_EV_DEVICE_DISCONNECTED, |
| 102 | MGMT_EV_CONNECT_FAILED, |
| 103 | MGMT_EV_PIN_CODE_REQUEST, |
| 104 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 105 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 106 | MGMT_EV_AUTH_FAILED, |
| 107 | MGMT_EV_DEVICE_FOUND, |
| 108 | MGMT_EV_DISCOVERING, |
| 109 | MGMT_EV_DEVICE_BLOCKED, |
| 110 | MGMT_EV_DEVICE_UNBLOCKED, |
| 111 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 112 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 113 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 114 | MGMT_EV_NEW_CSRK, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 117 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 118 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 119 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 120 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 121 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 122 | struct pending_cmd { |
| 123 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 124 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 125 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 126 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 127 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 128 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 131 | /* HCI to MGMT error code conversion table */ |
| 132 | static u8 mgmt_status_table[] = { |
| 133 | MGMT_STATUS_SUCCESS, |
| 134 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 135 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 136 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 137 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 138 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 139 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 140 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 141 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 142 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 143 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 144 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 145 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 146 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 147 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 148 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 149 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 150 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 151 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 152 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 153 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 154 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 155 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 156 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 157 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 158 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 159 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 160 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 161 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 162 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 163 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 164 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 165 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 166 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 167 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 168 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 169 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 170 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 171 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 172 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 173 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 174 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 175 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 176 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 177 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 178 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 179 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 180 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 181 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 182 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 183 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 184 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 185 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 186 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 187 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 188 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 189 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 190 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 191 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 192 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 193 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 194 | }; |
| 195 | |
| 196 | static u8 mgmt_status(u8 hci_status) |
| 197 | { |
| 198 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 199 | return mgmt_status_table[hci_status]; |
| 200 | |
| 201 | return MGMT_STATUS_FAILED; |
| 202 | } |
| 203 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 204 | 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] | 205 | { |
| 206 | struct sk_buff *skb; |
| 207 | struct mgmt_hdr *hdr; |
| 208 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 209 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 210 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 211 | 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] | 212 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 213 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 214 | if (!skb) |
| 215 | return -ENOMEM; |
| 216 | |
| 217 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 218 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 219 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 220 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 221 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 222 | |
| 223 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 224 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 225 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 226 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 227 | err = sock_queue_rcv_skb(sk, skb); |
| 228 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 229 | kfree_skb(skb); |
| 230 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 231 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 232 | } |
| 233 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 234 | 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] | 235 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 236 | { |
| 237 | struct sk_buff *skb; |
| 238 | struct mgmt_hdr *hdr; |
| 239 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 240 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 241 | |
| 242 | BT_DBG("sock %p", sk); |
| 243 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 244 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 245 | if (!skb) |
| 246 | return -ENOMEM; |
| 247 | |
| 248 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 249 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 250 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 251 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 252 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 253 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 254 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 255 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 256 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 257 | |
| 258 | if (rp) |
| 259 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 260 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 261 | err = sock_queue_rcv_skb(sk, skb); |
| 262 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 263 | kfree_skb(skb); |
| 264 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 265 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 266 | } |
| 267 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 268 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 269 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 270 | { |
| 271 | struct mgmt_rp_read_version rp; |
| 272 | |
| 273 | BT_DBG("sock %p", sk); |
| 274 | |
| 275 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 276 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 277 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 278 | 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] | 279 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 280 | } |
| 281 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 282 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 283 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 284 | { |
| 285 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 286 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 287 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 288 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 289 | size_t rp_size; |
| 290 | int i, err; |
| 291 | |
| 292 | BT_DBG("sock %p", sk); |
| 293 | |
| 294 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 295 | |
| 296 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 297 | if (!rp) |
| 298 | return -ENOMEM; |
| 299 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 300 | rp->num_commands = cpu_to_le16(num_commands); |
| 301 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 302 | |
| 303 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 304 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 305 | |
| 306 | for (i = 0; i < num_events; i++, opcode++) |
| 307 | put_unaligned_le16(mgmt_events[i], opcode); |
| 308 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 309 | 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] | 310 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 311 | kfree(rp); |
| 312 | |
| 313 | return err; |
| 314 | } |
| 315 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 316 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 317 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 318 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 319 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 320 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 321 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 322 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 323 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 324 | |
| 325 | BT_DBG("sock %p", sk); |
| 326 | |
| 327 | read_lock(&hci_dev_list_lock); |
| 328 | |
| 329 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 330 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 331 | if (d->dev_type == HCI_BREDR) |
| 332 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 333 | } |
| 334 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 335 | rp_len = sizeof(*rp) + (2 * count); |
| 336 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 337 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 338 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 339 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 340 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 341 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 342 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 343 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 344 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 345 | continue; |
| 346 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 347 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 348 | continue; |
| 349 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 350 | if (d->dev_type == HCI_BREDR) { |
| 351 | rp->index[count++] = cpu_to_le16(d->id); |
| 352 | BT_DBG("Added hci%u", d->id); |
| 353 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 356 | rp->num_controllers = cpu_to_le16(count); |
| 357 | rp_len = sizeof(*rp) + (2 * count); |
| 358 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 359 | read_unlock(&hci_dev_list_lock); |
| 360 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 361 | 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] | 362 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 363 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 364 | kfree(rp); |
| 365 | |
| 366 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 369 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 370 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 371 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 372 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 373 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 374 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 375 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 376 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 377 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 378 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 379 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 380 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 381 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 382 | settings |= MGMT_SETTING_BREDR; |
| 383 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 384 | |
| 385 | if (lmp_ssp_capable(hdev)) { |
| 386 | settings |= MGMT_SETTING_SSP; |
| 387 | settings |= MGMT_SETTING_HS; |
| 388 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 389 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 390 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 391 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 392 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 393 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 394 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 395 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 396 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 397 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 398 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 399 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 400 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 401 | return settings; |
| 402 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 403 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 404 | static u32 get_current_settings(struct hci_dev *hdev) |
| 405 | { |
| 406 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 407 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 408 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 409 | settings |= MGMT_SETTING_POWERED; |
| 410 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 411 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 412 | settings |= MGMT_SETTING_CONNECTABLE; |
| 413 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 414 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 415 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 416 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 417 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 418 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 419 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 420 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 421 | settings |= MGMT_SETTING_PAIRABLE; |
| 422 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 423 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 424 | settings |= MGMT_SETTING_BREDR; |
| 425 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 426 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 427 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 428 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 429 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 430 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 431 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 432 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 433 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 434 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 435 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 436 | settings |= MGMT_SETTING_HS; |
| 437 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 438 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 439 | settings |= MGMT_SETTING_ADVERTISING; |
| 440 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 441 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 442 | settings |= MGMT_SETTING_SECURE_CONN; |
| 443 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 444 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 445 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 446 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 447 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 448 | settings |= MGMT_SETTING_PRIVACY; |
| 449 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 450 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 451 | } |
| 452 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 453 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 454 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 455 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 456 | { |
| 457 | u8 *ptr = data, *uuids_start = NULL; |
| 458 | struct bt_uuid *uuid; |
| 459 | |
| 460 | if (len < 4) |
| 461 | return ptr; |
| 462 | |
| 463 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 464 | u16 uuid16; |
| 465 | |
| 466 | if (uuid->size != 16) |
| 467 | continue; |
| 468 | |
| 469 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 470 | if (uuid16 < 0x1100) |
| 471 | continue; |
| 472 | |
| 473 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 474 | continue; |
| 475 | |
| 476 | if (!uuids_start) { |
| 477 | uuids_start = ptr; |
| 478 | uuids_start[0] = 1; |
| 479 | uuids_start[1] = EIR_UUID16_ALL; |
| 480 | ptr += 2; |
| 481 | } |
| 482 | |
| 483 | /* Stop if not enough space to put next UUID */ |
| 484 | if ((ptr - data) + sizeof(u16) > len) { |
| 485 | uuids_start[1] = EIR_UUID16_SOME; |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | *ptr++ = (uuid16 & 0x00ff); |
| 490 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 491 | uuids_start[0] += sizeof(uuid16); |
| 492 | } |
| 493 | |
| 494 | return ptr; |
| 495 | } |
| 496 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 497 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 498 | { |
| 499 | u8 *ptr = data, *uuids_start = NULL; |
| 500 | struct bt_uuid *uuid; |
| 501 | |
| 502 | if (len < 6) |
| 503 | return ptr; |
| 504 | |
| 505 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 506 | if (uuid->size != 32) |
| 507 | continue; |
| 508 | |
| 509 | if (!uuids_start) { |
| 510 | uuids_start = ptr; |
| 511 | uuids_start[0] = 1; |
| 512 | uuids_start[1] = EIR_UUID32_ALL; |
| 513 | ptr += 2; |
| 514 | } |
| 515 | |
| 516 | /* Stop if not enough space to put next UUID */ |
| 517 | if ((ptr - data) + sizeof(u32) > len) { |
| 518 | uuids_start[1] = EIR_UUID32_SOME; |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 523 | ptr += sizeof(u32); |
| 524 | uuids_start[0] += sizeof(u32); |
| 525 | } |
| 526 | |
| 527 | return ptr; |
| 528 | } |
| 529 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 530 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 531 | { |
| 532 | u8 *ptr = data, *uuids_start = NULL; |
| 533 | struct bt_uuid *uuid; |
| 534 | |
| 535 | if (len < 18) |
| 536 | return ptr; |
| 537 | |
| 538 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 539 | if (uuid->size != 128) |
| 540 | continue; |
| 541 | |
| 542 | if (!uuids_start) { |
| 543 | uuids_start = ptr; |
| 544 | uuids_start[0] = 1; |
| 545 | uuids_start[1] = EIR_UUID128_ALL; |
| 546 | ptr += 2; |
| 547 | } |
| 548 | |
| 549 | /* Stop if not enough space to put next UUID */ |
| 550 | if ((ptr - data) + 16 > len) { |
| 551 | uuids_start[1] = EIR_UUID128_SOME; |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | memcpy(ptr, uuid->uuid, 16); |
| 556 | ptr += 16; |
| 557 | uuids_start[0] += 16; |
| 558 | } |
| 559 | |
| 560 | return ptr; |
| 561 | } |
| 562 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 563 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 564 | { |
| 565 | struct pending_cmd *cmd; |
| 566 | |
| 567 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 568 | if (cmd->opcode == opcode) |
| 569 | return cmd; |
| 570 | } |
| 571 | |
| 572 | return NULL; |
| 573 | } |
| 574 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame^] | 575 | static struct pending_cmd *mgmt_pending_find_data(u16 opcode, |
| 576 | struct hci_dev *hdev, |
| 577 | const void *data) |
| 578 | { |
| 579 | struct pending_cmd *cmd; |
| 580 | |
| 581 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 582 | if (cmd->user_data != data) |
| 583 | continue; |
| 584 | if (cmd->opcode == opcode) |
| 585 | return cmd; |
| 586 | } |
| 587 | |
| 588 | return NULL; |
| 589 | } |
| 590 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 591 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 592 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 593 | u8 ad_len = 0; |
| 594 | size_t name_len; |
| 595 | |
| 596 | name_len = strlen(hdev->dev_name); |
| 597 | if (name_len > 0) { |
| 598 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 599 | |
| 600 | if (name_len > max_len) { |
| 601 | name_len = max_len; |
| 602 | ptr[1] = EIR_NAME_SHORT; |
| 603 | } else |
| 604 | ptr[1] = EIR_NAME_COMPLETE; |
| 605 | |
| 606 | ptr[0] = name_len + 1; |
| 607 | |
| 608 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 609 | |
| 610 | ad_len += (name_len + 2); |
| 611 | ptr += (name_len + 2); |
| 612 | } |
| 613 | |
| 614 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static void update_scan_rsp_data(struct hci_request *req) |
| 618 | { |
| 619 | struct hci_dev *hdev = req->hdev; |
| 620 | struct hci_cp_le_set_scan_rsp_data cp; |
| 621 | u8 len; |
| 622 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 623 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 624 | return; |
| 625 | |
| 626 | memset(&cp, 0, sizeof(cp)); |
| 627 | |
| 628 | len = create_scan_rsp_data(hdev, cp.data); |
| 629 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 630 | if (hdev->scan_rsp_data_len == len && |
| 631 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 632 | return; |
| 633 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 634 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 635 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 636 | |
| 637 | cp.length = len; |
| 638 | |
| 639 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 640 | } |
| 641 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 642 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 643 | { |
| 644 | struct pending_cmd *cmd; |
| 645 | |
| 646 | /* If there's a pending mgmt command the flags will not yet have |
| 647 | * their final values, so check for this first. |
| 648 | */ |
| 649 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 650 | if (cmd) { |
| 651 | struct mgmt_mode *cp = cmd->param; |
| 652 | if (cp->val == 0x01) |
| 653 | return LE_AD_GENERAL; |
| 654 | else if (cp->val == 0x02) |
| 655 | return LE_AD_LIMITED; |
| 656 | } else { |
| 657 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 658 | return LE_AD_LIMITED; |
| 659 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 660 | return LE_AD_GENERAL; |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 666 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 667 | { |
| 668 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 669 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 670 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 671 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 672 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 673 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 674 | |
| 675 | if (flags) { |
| 676 | BT_DBG("adv flags 0x%02x", flags); |
| 677 | |
| 678 | ptr[0] = 2; |
| 679 | ptr[1] = EIR_FLAGS; |
| 680 | ptr[2] = flags; |
| 681 | |
| 682 | ad_len += 3; |
| 683 | ptr += 3; |
| 684 | } |
| 685 | |
| 686 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 687 | ptr[0] = 2; |
| 688 | ptr[1] = EIR_TX_POWER; |
| 689 | ptr[2] = (u8) hdev->adv_tx_power; |
| 690 | |
| 691 | ad_len += 3; |
| 692 | ptr += 3; |
| 693 | } |
| 694 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 695 | return ad_len; |
| 696 | } |
| 697 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 698 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 699 | { |
| 700 | struct hci_dev *hdev = req->hdev; |
| 701 | struct hci_cp_le_set_adv_data cp; |
| 702 | u8 len; |
| 703 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 704 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 705 | return; |
| 706 | |
| 707 | memset(&cp, 0, sizeof(cp)); |
| 708 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 709 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 710 | |
| 711 | if (hdev->adv_data_len == len && |
| 712 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 713 | return; |
| 714 | |
| 715 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 716 | hdev->adv_data_len = len; |
| 717 | |
| 718 | cp.length = len; |
| 719 | |
| 720 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 721 | } |
| 722 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 723 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 724 | { |
| 725 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 726 | size_t name_len; |
| 727 | |
| 728 | name_len = strlen(hdev->dev_name); |
| 729 | |
| 730 | if (name_len > 0) { |
| 731 | /* EIR Data type */ |
| 732 | if (name_len > 48) { |
| 733 | name_len = 48; |
| 734 | ptr[1] = EIR_NAME_SHORT; |
| 735 | } else |
| 736 | ptr[1] = EIR_NAME_COMPLETE; |
| 737 | |
| 738 | /* EIR Data length */ |
| 739 | ptr[0] = name_len + 1; |
| 740 | |
| 741 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 742 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 743 | ptr += (name_len + 2); |
| 744 | } |
| 745 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 746 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 747 | ptr[0] = 2; |
| 748 | ptr[1] = EIR_TX_POWER; |
| 749 | ptr[2] = (u8) hdev->inq_tx_power; |
| 750 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 751 | ptr += 3; |
| 752 | } |
| 753 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 754 | if (hdev->devid_source > 0) { |
| 755 | ptr[0] = 9; |
| 756 | ptr[1] = EIR_DEVICE_ID; |
| 757 | |
| 758 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 759 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 760 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 761 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 762 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 763 | ptr += 10; |
| 764 | } |
| 765 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 766 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 767 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 768 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 769 | } |
| 770 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 771 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 772 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 773 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 774 | struct hci_cp_write_eir cp; |
| 775 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 776 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 777 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 778 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 779 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 780 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 781 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 782 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 783 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 784 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 785 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 786 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 787 | |
| 788 | memset(&cp, 0, sizeof(cp)); |
| 789 | |
| 790 | create_eir(hdev, cp.data); |
| 791 | |
| 792 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 793 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 794 | |
| 795 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 796 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 797 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 798 | } |
| 799 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 800 | static u8 get_service_classes(struct hci_dev *hdev) |
| 801 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 802 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 803 | u8 val = 0; |
| 804 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 805 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 806 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 807 | |
| 808 | return val; |
| 809 | } |
| 810 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 811 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 812 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 813 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 814 | u8 cod[3]; |
| 815 | |
| 816 | BT_DBG("%s", hdev->name); |
| 817 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 818 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 819 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 820 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 821 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 822 | return; |
| 823 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 824 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 825 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 826 | |
| 827 | cod[0] = hdev->minor_class; |
| 828 | cod[1] = hdev->major_class; |
| 829 | cod[2] = get_service_classes(hdev); |
| 830 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 831 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 832 | cod[1] |= 0x20; |
| 833 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 834 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 835 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 836 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 837 | 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] | 838 | } |
| 839 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 840 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 841 | { |
| 842 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 843 | |
| 844 | /* If there's a pending mgmt command the flag will not yet have |
| 845 | * it's final value, so check for this first. |
| 846 | */ |
| 847 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 848 | if (cmd) { |
| 849 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 850 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 851 | } |
| 852 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 853 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static void enable_advertising(struct hci_request *req) |
| 857 | { |
| 858 | struct hci_dev *hdev = req->hdev; |
| 859 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 860 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 861 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 862 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 863 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 864 | * hci_update_random_address knows that it's safe to go ahead |
| 865 | * and write a new random address. The flag will be set back on |
| 866 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 867 | */ |
| 868 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 869 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 870 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 871 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 872 | /* Set require_privacy to true only when non-connectable |
| 873 | * advertising is used. In that case it is fine to use a |
| 874 | * non-resolvable private address. |
| 875 | */ |
| 876 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 877 | return; |
| 878 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 879 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 880 | cp.min_interval = cpu_to_le16(0x0800); |
| 881 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 882 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 883 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 884 | cp.channel_map = hdev->le_adv_channel_map; |
| 885 | |
| 886 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 887 | |
| 888 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 889 | } |
| 890 | |
| 891 | static void disable_advertising(struct hci_request *req) |
| 892 | { |
| 893 | u8 enable = 0x00; |
| 894 | |
| 895 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 896 | } |
| 897 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 898 | static void service_cache_off(struct work_struct *work) |
| 899 | { |
| 900 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 901 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 902 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 903 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 904 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 905 | return; |
| 906 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 907 | hci_req_init(&req, hdev); |
| 908 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 909 | hci_dev_lock(hdev); |
| 910 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 911 | update_eir(&req); |
| 912 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 913 | |
| 914 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 915 | |
| 916 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 917 | } |
| 918 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 919 | static void rpa_expired(struct work_struct *work) |
| 920 | { |
| 921 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 922 | rpa_expired.work); |
| 923 | struct hci_request req; |
| 924 | |
| 925 | BT_DBG(""); |
| 926 | |
| 927 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 928 | |
| 929 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 930 | hci_conn_num(hdev, LE_LINK) > 0) |
| 931 | return; |
| 932 | |
| 933 | /* The generation of a new RPA and programming it into the |
| 934 | * controller happens in the enable_advertising() function. |
| 935 | */ |
| 936 | |
| 937 | hci_req_init(&req, hdev); |
| 938 | |
| 939 | disable_advertising(&req); |
| 940 | enable_advertising(&req); |
| 941 | |
| 942 | hci_req_run(&req, NULL); |
| 943 | } |
| 944 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 945 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 946 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 947 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 948 | return; |
| 949 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 950 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 951 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 952 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 953 | /* Non-mgmt controlled devices get this bit set |
| 954 | * implicitly so that pairing works for them, however |
| 955 | * for mgmt we require user-space to explicitly enable |
| 956 | * it |
| 957 | */ |
| 958 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 959 | } |
| 960 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 961 | 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] | 962 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 963 | { |
| 964 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 965 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 966 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 967 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 968 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 969 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 970 | memset(&rp, 0, sizeof(rp)); |
| 971 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 972 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 973 | |
| 974 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 975 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 976 | |
| 977 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 978 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 979 | |
| 980 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 981 | |
| 982 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 983 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 984 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 985 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 986 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 987 | 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] | 988 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 992 | { |
| 993 | sock_put(cmd->sk); |
| 994 | kfree(cmd->param); |
| 995 | kfree(cmd); |
| 996 | } |
| 997 | |
| 998 | 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] | 999 | struct hci_dev *hdev, void *data, |
| 1000 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1001 | { |
| 1002 | struct pending_cmd *cmd; |
| 1003 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 1004 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1005 | if (!cmd) |
| 1006 | return NULL; |
| 1007 | |
| 1008 | cmd->opcode = opcode; |
| 1009 | cmd->index = hdev->id; |
| 1010 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 1011 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1012 | if (!cmd->param) { |
| 1013 | kfree(cmd); |
| 1014 | return NULL; |
| 1015 | } |
| 1016 | |
| 1017 | if (data) |
| 1018 | memcpy(cmd->param, data, len); |
| 1019 | |
| 1020 | cmd->sk = sk; |
| 1021 | sock_hold(sk); |
| 1022 | |
| 1023 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1024 | |
| 1025 | return cmd; |
| 1026 | } |
| 1027 | |
| 1028 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1029 | void (*cb)(struct pending_cmd *cmd, |
| 1030 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1031 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1032 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1033 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1034 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1035 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1036 | if (opcode > 0 && cmd->opcode != opcode) |
| 1037 | continue; |
| 1038 | |
| 1039 | cb(cmd, data); |
| 1040 | } |
| 1041 | } |
| 1042 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1043 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1044 | { |
| 1045 | list_del(&cmd->list); |
| 1046 | mgmt_pending_free(cmd); |
| 1047 | } |
| 1048 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1049 | 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] | 1050 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1051 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1052 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1053 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1054 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1057 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1058 | { |
| 1059 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1060 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1061 | if (hci_conn_count(hdev) == 0) { |
| 1062 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1063 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1064 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1065 | } |
| 1066 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1067 | static void hci_stop_discovery(struct hci_request *req) |
| 1068 | { |
| 1069 | struct hci_dev *hdev = req->hdev; |
| 1070 | struct hci_cp_remote_name_req_cancel cp; |
| 1071 | struct inquiry_entry *e; |
| 1072 | |
| 1073 | switch (hdev->discovery.state) { |
| 1074 | case DISCOVERY_FINDING: |
| 1075 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1076 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1077 | } else { |
| 1078 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1079 | hci_req_add_le_scan_disable(req); |
| 1080 | } |
| 1081 | |
| 1082 | break; |
| 1083 | |
| 1084 | case DISCOVERY_RESOLVING: |
| 1085 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1086 | NAME_PENDING); |
| 1087 | if (!e) |
| 1088 | return; |
| 1089 | |
| 1090 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1091 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1092 | &cp); |
| 1093 | |
| 1094 | break; |
| 1095 | |
| 1096 | default: |
| 1097 | /* Passive scanning */ |
| 1098 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1099 | hci_req_add_le_scan_disable(req); |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
| 1103 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1104 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1105 | { |
| 1106 | struct hci_request req; |
| 1107 | struct hci_conn *conn; |
| 1108 | |
| 1109 | hci_req_init(&req, hdev); |
| 1110 | |
| 1111 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1112 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1113 | u8 scan = 0x00; |
| 1114 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1115 | } |
| 1116 | |
| 1117 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1118 | disable_advertising(&req); |
| 1119 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1120 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1121 | |
| 1122 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1123 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1124 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1125 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1126 | switch (conn->state) { |
| 1127 | case BT_CONNECTED: |
| 1128 | case BT_CONFIG: |
| 1129 | dc.handle = cpu_to_le16(conn->handle); |
| 1130 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1131 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1132 | break; |
| 1133 | case BT_CONNECT: |
| 1134 | if (conn->type == LE_LINK) |
| 1135 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1136 | 0, NULL); |
| 1137 | else if (conn->type == ACL_LINK) |
| 1138 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1139 | 6, &conn->dst); |
| 1140 | break; |
| 1141 | case BT_CONNECT2: |
| 1142 | bacpy(&rej.bdaddr, &conn->dst); |
| 1143 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1144 | if (conn->type == ACL_LINK) |
| 1145 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1146 | sizeof(rej), &rej); |
| 1147 | else if (conn->type == SCO_LINK) |
| 1148 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1149 | sizeof(rej), &rej); |
| 1150 | break; |
| 1151 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | return hci_req_run(&req, clean_up_hci_complete); |
| 1155 | } |
| 1156 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1157 | 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] | 1158 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1159 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1160 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1161 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1162 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1163 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1164 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1165 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1166 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1167 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1168 | MGMT_STATUS_INVALID_PARAMS); |
| 1169 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1170 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1171 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1172 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1173 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1174 | MGMT_STATUS_BUSY); |
| 1175 | goto failed; |
| 1176 | } |
| 1177 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1178 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1179 | cancel_delayed_work(&hdev->power_off); |
| 1180 | |
| 1181 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1182 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1183 | data, len); |
| 1184 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1185 | goto failed; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1189 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1190 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1191 | goto failed; |
| 1192 | } |
| 1193 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1194 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1195 | if (!cmd) { |
| 1196 | err = -ENOMEM; |
| 1197 | goto failed; |
| 1198 | } |
| 1199 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1200 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1201 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1202 | err = 0; |
| 1203 | } else { |
| 1204 | /* Disconnect connections, stop scans, etc */ |
| 1205 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1206 | if (!err) |
| 1207 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1208 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1209 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1210 | /* ENODATA means there were no HCI commands queued */ |
| 1211 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1212 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1213 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1214 | err = 0; |
| 1215 | } |
| 1216 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1217 | |
| 1218 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1219 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1220 | return err; |
| 1221 | } |
| 1222 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1223 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1224 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1225 | { |
| 1226 | struct sk_buff *skb; |
| 1227 | struct mgmt_hdr *hdr; |
| 1228 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1229 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1230 | if (!skb) |
| 1231 | return -ENOMEM; |
| 1232 | |
| 1233 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1234 | hdr->opcode = cpu_to_le16(event); |
| 1235 | if (hdev) |
| 1236 | hdr->index = cpu_to_le16(hdev->id); |
| 1237 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1238 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1239 | hdr->len = cpu_to_le16(data_len); |
| 1240 | |
| 1241 | if (data) |
| 1242 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1243 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1244 | /* Time stamp */ |
| 1245 | __net_timestamp(skb); |
| 1246 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1247 | hci_send_to_control(skb, skip_sk); |
| 1248 | kfree_skb(skb); |
| 1249 | |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1254 | { |
| 1255 | __le32 ev; |
| 1256 | |
| 1257 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1258 | |
| 1259 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1260 | } |
| 1261 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1262 | struct cmd_lookup { |
| 1263 | struct sock *sk; |
| 1264 | struct hci_dev *hdev; |
| 1265 | u8 mgmt_status; |
| 1266 | }; |
| 1267 | |
| 1268 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1269 | { |
| 1270 | struct cmd_lookup *match = data; |
| 1271 | |
| 1272 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1273 | |
| 1274 | list_del(&cmd->list); |
| 1275 | |
| 1276 | if (match->sk == NULL) { |
| 1277 | match->sk = cmd->sk; |
| 1278 | sock_hold(match->sk); |
| 1279 | } |
| 1280 | |
| 1281 | mgmt_pending_free(cmd); |
| 1282 | } |
| 1283 | |
| 1284 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1285 | { |
| 1286 | u8 *status = data; |
| 1287 | |
| 1288 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1289 | mgmt_pending_remove(cmd); |
| 1290 | } |
| 1291 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1292 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1293 | { |
| 1294 | if (!lmp_bredr_capable(hdev)) |
| 1295 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1296 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1297 | return MGMT_STATUS_REJECTED; |
| 1298 | else |
| 1299 | return MGMT_STATUS_SUCCESS; |
| 1300 | } |
| 1301 | |
| 1302 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1303 | { |
| 1304 | if (!lmp_le_capable(hdev)) |
| 1305 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1306 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1307 | return MGMT_STATUS_REJECTED; |
| 1308 | else |
| 1309 | return MGMT_STATUS_SUCCESS; |
| 1310 | } |
| 1311 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1312 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1313 | { |
| 1314 | struct pending_cmd *cmd; |
| 1315 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1316 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1317 | bool changed; |
| 1318 | |
| 1319 | BT_DBG("status 0x%02x", status); |
| 1320 | |
| 1321 | hci_dev_lock(hdev); |
| 1322 | |
| 1323 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1324 | if (!cmd) |
| 1325 | goto unlock; |
| 1326 | |
| 1327 | if (status) { |
| 1328 | u8 mgmt_err = mgmt_status(status); |
| 1329 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1330 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1331 | goto remove_cmd; |
| 1332 | } |
| 1333 | |
| 1334 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1335 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1336 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1337 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1338 | |
| 1339 | if (hdev->discov_timeout > 0) { |
| 1340 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1341 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1342 | to); |
| 1343 | } |
| 1344 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1345 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1346 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1347 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1348 | |
| 1349 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1350 | |
| 1351 | if (changed) |
| 1352 | new_settings(hdev, cmd->sk); |
| 1353 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1354 | /* When the discoverable mode gets changed, make sure |
| 1355 | * that class of device has the limited discoverable |
| 1356 | * bit correctly set. |
| 1357 | */ |
| 1358 | hci_req_init(&req, hdev); |
| 1359 | update_class(&req); |
| 1360 | hci_req_run(&req, NULL); |
| 1361 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1362 | remove_cmd: |
| 1363 | mgmt_pending_remove(cmd); |
| 1364 | |
| 1365 | unlock: |
| 1366 | hci_dev_unlock(hdev); |
| 1367 | } |
| 1368 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1369 | 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] | 1370 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1371 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1372 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1373 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1374 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1375 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1376 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1377 | int err; |
| 1378 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1379 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1380 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1381 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1382 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1383 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1384 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1385 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1386 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1387 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1388 | MGMT_STATUS_INVALID_PARAMS); |
| 1389 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1390 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1391 | |
| 1392 | /* Disabling discoverable requires that no timeout is set, |
| 1393 | * and enabling limited discoverable requires a timeout. |
| 1394 | */ |
| 1395 | if ((cp->val == 0x00 && timeout > 0) || |
| 1396 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1397 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1398 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1399 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1400 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1401 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1402 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1403 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1404 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1405 | goto failed; |
| 1406 | } |
| 1407 | |
| 1408 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1409 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1410 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1411 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1412 | goto failed; |
| 1413 | } |
| 1414 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1415 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1416 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1417 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1418 | goto failed; |
| 1419 | } |
| 1420 | |
| 1421 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1422 | bool changed = false; |
| 1423 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1424 | /* Setting limited discoverable when powered off is |
| 1425 | * not a valid operation since it requires a timeout |
| 1426 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1427 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1428 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1429 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1430 | changed = true; |
| 1431 | } |
| 1432 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1433 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1434 | if (err < 0) |
| 1435 | goto failed; |
| 1436 | |
| 1437 | if (changed) |
| 1438 | err = new_settings(hdev, sk); |
| 1439 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1440 | goto failed; |
| 1441 | } |
| 1442 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1443 | /* If the current mode is the same, then just update the timeout |
| 1444 | * value with the new value. And if only the timeout gets updated, |
| 1445 | * then no need for any HCI transactions. |
| 1446 | */ |
| 1447 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1448 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1449 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1450 | cancel_delayed_work(&hdev->discov_off); |
| 1451 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1452 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1453 | if (cp->val && hdev->discov_timeout > 0) { |
| 1454 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1455 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1456 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1459 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1460 | goto failed; |
| 1461 | } |
| 1462 | |
| 1463 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1464 | if (!cmd) { |
| 1465 | err = -ENOMEM; |
| 1466 | goto failed; |
| 1467 | } |
| 1468 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1469 | /* Cancel any potential discoverable timeout that might be |
| 1470 | * still active and store new timeout value. The arming of |
| 1471 | * the timeout happens in the complete handler. |
| 1472 | */ |
| 1473 | cancel_delayed_work(&hdev->discov_off); |
| 1474 | hdev->discov_timeout = timeout; |
| 1475 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1476 | /* Limited discoverable mode */ |
| 1477 | if (cp->val == 0x02) |
| 1478 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1479 | else |
| 1480 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1481 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1482 | hci_req_init(&req, hdev); |
| 1483 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1484 | /* The procedure for LE-only controllers is much simpler - just |
| 1485 | * update the advertising data. |
| 1486 | */ |
| 1487 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1488 | goto update_ad; |
| 1489 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1490 | scan = SCAN_PAGE; |
| 1491 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1492 | if (cp->val) { |
| 1493 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1494 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1495 | if (cp->val == 0x02) { |
| 1496 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1497 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1498 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1499 | hci_cp.iac_lap[1] = 0x8b; |
| 1500 | hci_cp.iac_lap[2] = 0x9e; |
| 1501 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1502 | hci_cp.iac_lap[4] = 0x8b; |
| 1503 | hci_cp.iac_lap[5] = 0x9e; |
| 1504 | } else { |
| 1505 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1506 | hci_cp.num_iac = 1; |
| 1507 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1508 | hci_cp.iac_lap[1] = 0x8b; |
| 1509 | hci_cp.iac_lap[2] = 0x9e; |
| 1510 | } |
| 1511 | |
| 1512 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1513 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1514 | |
| 1515 | scan |= SCAN_INQUIRY; |
| 1516 | } else { |
| 1517 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1518 | } |
| 1519 | |
| 1520 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1521 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1522 | update_ad: |
| 1523 | update_adv_data(&req); |
| 1524 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1525 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1526 | if (err < 0) |
| 1527 | mgmt_pending_remove(cmd); |
| 1528 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1529 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1530 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1531 | return err; |
| 1532 | } |
| 1533 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1534 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1535 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1536 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1537 | struct hci_cp_write_page_scan_activity acp; |
| 1538 | u8 type; |
| 1539 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1540 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1541 | return; |
| 1542 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1543 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1544 | return; |
| 1545 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1546 | if (enable) { |
| 1547 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1548 | |
| 1549 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1550 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1551 | } else { |
| 1552 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1553 | |
| 1554 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1555 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1556 | } |
| 1557 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1558 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1559 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1560 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1561 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1562 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1563 | sizeof(acp), &acp); |
| 1564 | |
| 1565 | if (hdev->page_scan_type != type) |
| 1566 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1567 | } |
| 1568 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1569 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1570 | { |
| 1571 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1572 | struct mgmt_mode *cp; |
| 1573 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1574 | |
| 1575 | BT_DBG("status 0x%02x", status); |
| 1576 | |
| 1577 | hci_dev_lock(hdev); |
| 1578 | |
| 1579 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1580 | if (!cmd) |
| 1581 | goto unlock; |
| 1582 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1583 | if (status) { |
| 1584 | u8 mgmt_err = mgmt_status(status); |
| 1585 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1586 | goto remove_cmd; |
| 1587 | } |
| 1588 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1589 | cp = cmd->param; |
| 1590 | if (cp->val) |
| 1591 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1592 | else |
| 1593 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1594 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1595 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1596 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1597 | if (changed) |
| 1598 | new_settings(hdev, cmd->sk); |
| 1599 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1600 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1601 | mgmt_pending_remove(cmd); |
| 1602 | |
| 1603 | unlock: |
| 1604 | hci_dev_unlock(hdev); |
| 1605 | } |
| 1606 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1607 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1608 | struct sock *sk, u8 val) |
| 1609 | { |
| 1610 | bool changed = false; |
| 1611 | int err; |
| 1612 | |
| 1613 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1614 | changed = true; |
| 1615 | |
| 1616 | if (val) { |
| 1617 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1618 | } else { |
| 1619 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1620 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1621 | } |
| 1622 | |
| 1623 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1624 | if (err < 0) |
| 1625 | return err; |
| 1626 | |
| 1627 | if (changed) |
| 1628 | return new_settings(hdev, sk); |
| 1629 | |
| 1630 | return 0; |
| 1631 | } |
| 1632 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1633 | 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] | 1634 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1635 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1636 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1637 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1638 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1639 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1640 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1641 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1642 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1643 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1644 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1645 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1646 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1647 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1648 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1649 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1650 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1651 | MGMT_STATUS_INVALID_PARAMS); |
| 1652 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1653 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1654 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1655 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1656 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1657 | goto failed; |
| 1658 | } |
| 1659 | |
| 1660 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1661 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1662 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1663 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1664 | goto failed; |
| 1665 | } |
| 1666 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1667 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1668 | if (!cmd) { |
| 1669 | err = -ENOMEM; |
| 1670 | goto failed; |
| 1671 | } |
| 1672 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1673 | hci_req_init(&req, hdev); |
| 1674 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1675 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1676 | * by-product of disabling connectable, we need to update the |
| 1677 | * advertising flags. |
| 1678 | */ |
| 1679 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1680 | if (!cp->val) { |
| 1681 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1682 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1683 | } |
| 1684 | update_adv_data(&req); |
| 1685 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1686 | if (cp->val) { |
| 1687 | scan = SCAN_PAGE; |
| 1688 | } else { |
| 1689 | scan = 0; |
| 1690 | |
| 1691 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1692 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1693 | cancel_delayed_work(&hdev->discov_off); |
| 1694 | } |
| 1695 | |
| 1696 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1697 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1698 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1699 | /* If we're going from non-connectable to connectable or |
| 1700 | * vice-versa when fast connectable is enabled ensure that fast |
| 1701 | * connectable gets disabled. write_fast_connectable won't do |
| 1702 | * anything if the page scan parameters are already what they |
| 1703 | * should be. |
| 1704 | */ |
| 1705 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1706 | write_fast_connectable(&req, false); |
| 1707 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1708 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1709 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1710 | disable_advertising(&req); |
| 1711 | enable_advertising(&req); |
| 1712 | } |
| 1713 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1714 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1715 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1716 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1717 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1718 | err = set_connectable_update_settings(hdev, sk, |
| 1719 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1720 | goto failed; |
| 1721 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1722 | |
| 1723 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1724 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1725 | return err; |
| 1726 | } |
| 1727 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1728 | 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] | 1729 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1730 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1731 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1732 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1733 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1734 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1735 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1736 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1737 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1738 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1739 | MGMT_STATUS_INVALID_PARAMS); |
| 1740 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1741 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1742 | |
| 1743 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1744 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1745 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1746 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1747 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1748 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1749 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1750 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1751 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1752 | if (changed) |
| 1753 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1754 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1755 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1756 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1757 | return err; |
| 1758 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1759 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1760 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1761 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1762 | { |
| 1763 | struct mgmt_mode *cp = data; |
| 1764 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1765 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1766 | int err; |
| 1767 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1768 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1769 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1770 | status = mgmt_bredr_support(hdev); |
| 1771 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1772 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1773 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1774 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1775 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1776 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1777 | MGMT_STATUS_INVALID_PARAMS); |
| 1778 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1779 | hci_dev_lock(hdev); |
| 1780 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1781 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1782 | bool changed = false; |
| 1783 | |
| 1784 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1785 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1786 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1787 | changed = true; |
| 1788 | } |
| 1789 | |
| 1790 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1791 | if (err < 0) |
| 1792 | goto failed; |
| 1793 | |
| 1794 | if (changed) |
| 1795 | err = new_settings(hdev, sk); |
| 1796 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1797 | goto failed; |
| 1798 | } |
| 1799 | |
| 1800 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1801 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1802 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1803 | goto failed; |
| 1804 | } |
| 1805 | |
| 1806 | val = !!cp->val; |
| 1807 | |
| 1808 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1809 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1810 | goto failed; |
| 1811 | } |
| 1812 | |
| 1813 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1814 | if (!cmd) { |
| 1815 | err = -ENOMEM; |
| 1816 | goto failed; |
| 1817 | } |
| 1818 | |
| 1819 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1820 | if (err < 0) { |
| 1821 | mgmt_pending_remove(cmd); |
| 1822 | goto failed; |
| 1823 | } |
| 1824 | |
| 1825 | failed: |
| 1826 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1827 | return err; |
| 1828 | } |
| 1829 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1830 | 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] | 1831 | { |
| 1832 | struct mgmt_mode *cp = data; |
| 1833 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1834 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1835 | int err; |
| 1836 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1837 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1838 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1839 | status = mgmt_bredr_support(hdev); |
| 1840 | if (status) |
| 1841 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1842 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1843 | if (!lmp_ssp_capable(hdev)) |
| 1844 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1845 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1846 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1847 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1848 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1849 | MGMT_STATUS_INVALID_PARAMS); |
| 1850 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1851 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1852 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1853 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1854 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1855 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1856 | if (cp->val) { |
| 1857 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1858 | &hdev->dev_flags); |
| 1859 | } else { |
| 1860 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1861 | &hdev->dev_flags); |
| 1862 | if (!changed) |
| 1863 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1864 | &hdev->dev_flags); |
| 1865 | else |
| 1866 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1870 | if (err < 0) |
| 1871 | goto failed; |
| 1872 | |
| 1873 | if (changed) |
| 1874 | err = new_settings(hdev, sk); |
| 1875 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1876 | goto failed; |
| 1877 | } |
| 1878 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1879 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1880 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1881 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1882 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1883 | goto failed; |
| 1884 | } |
| 1885 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1886 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1887 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1888 | goto failed; |
| 1889 | } |
| 1890 | |
| 1891 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1892 | if (!cmd) { |
| 1893 | err = -ENOMEM; |
| 1894 | goto failed; |
| 1895 | } |
| 1896 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1897 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 1898 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1899 | sizeof(cp->val), &cp->val); |
| 1900 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1901 | 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] | 1902 | if (err < 0) { |
| 1903 | mgmt_pending_remove(cmd); |
| 1904 | goto failed; |
| 1905 | } |
| 1906 | |
| 1907 | failed: |
| 1908 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1909 | return err; |
| 1910 | } |
| 1911 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1912 | 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] | 1913 | { |
| 1914 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1915 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1916 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1917 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1918 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1919 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1920 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1921 | status = mgmt_bredr_support(hdev); |
| 1922 | if (status) |
| 1923 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1924 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1925 | if (!lmp_ssp_capable(hdev)) |
| 1926 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1927 | MGMT_STATUS_NOT_SUPPORTED); |
| 1928 | |
| 1929 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1930 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1931 | MGMT_STATUS_REJECTED); |
| 1932 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1933 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1934 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1935 | MGMT_STATUS_INVALID_PARAMS); |
| 1936 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1937 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1938 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1939 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1940 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1941 | } else { |
| 1942 | if (hdev_is_powered(hdev)) { |
| 1943 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1944 | MGMT_STATUS_REJECTED); |
| 1945 | goto unlock; |
| 1946 | } |
| 1947 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1948 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1949 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1950 | |
| 1951 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1952 | if (err < 0) |
| 1953 | goto unlock; |
| 1954 | |
| 1955 | if (changed) |
| 1956 | err = new_settings(hdev, sk); |
| 1957 | |
| 1958 | unlock: |
| 1959 | hci_dev_unlock(hdev); |
| 1960 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1961 | } |
| 1962 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1963 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1964 | { |
| 1965 | struct cmd_lookup match = { NULL, hdev }; |
| 1966 | |
| 1967 | if (status) { |
| 1968 | u8 mgmt_err = mgmt_status(status); |
| 1969 | |
| 1970 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1971 | &mgmt_err); |
| 1972 | return; |
| 1973 | } |
| 1974 | |
| 1975 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1976 | |
| 1977 | new_settings(hdev, match.sk); |
| 1978 | |
| 1979 | if (match.sk) |
| 1980 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1981 | |
| 1982 | /* Make sure the controller has a good default for |
| 1983 | * advertising data. Restrict the update to when LE |
| 1984 | * has actually been enabled. During power on, the |
| 1985 | * update in powered_update_hci will take care of it. |
| 1986 | */ |
| 1987 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1988 | struct hci_request req; |
| 1989 | |
| 1990 | hci_dev_lock(hdev); |
| 1991 | |
| 1992 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 1993 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 1994 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1995 | hci_req_run(&req, NULL); |
| 1996 | |
| 1997 | hci_dev_unlock(hdev); |
| 1998 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1999 | } |
| 2000 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2001 | 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] | 2002 | { |
| 2003 | struct mgmt_mode *cp = data; |
| 2004 | struct hci_cp_write_le_host_supported hci_cp; |
| 2005 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2006 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2007 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2008 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2009 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2010 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2011 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2012 | if (!lmp_le_capable(hdev)) |
| 2013 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2014 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2015 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 2016 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2017 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2018 | MGMT_STATUS_INVALID_PARAMS); |
| 2019 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2020 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2021 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2022 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2023 | MGMT_STATUS_REJECTED); |
| 2024 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2025 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2026 | |
| 2027 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2028 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2029 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2030 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2031 | bool changed = false; |
| 2032 | |
| 2033 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2034 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2035 | changed = true; |
| 2036 | } |
| 2037 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2038 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2039 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2040 | changed = true; |
| 2041 | } |
| 2042 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2043 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2044 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2045 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2046 | |
| 2047 | if (changed) |
| 2048 | err = new_settings(hdev, sk); |
| 2049 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2050 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2051 | } |
| 2052 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2053 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2054 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2055 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2056 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2057 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2061 | if (!cmd) { |
| 2062 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2063 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2064 | } |
| 2065 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2066 | hci_req_init(&req, hdev); |
| 2067 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2068 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2069 | |
| 2070 | if (val) { |
| 2071 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2072 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2073 | } else { |
| 2074 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2075 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2076 | } |
| 2077 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2078 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2079 | &hci_cp); |
| 2080 | |
| 2081 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2082 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2083 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2084 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2085 | unlock: |
| 2086 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2087 | return err; |
| 2088 | } |
| 2089 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2090 | /* This is a helper function to test for pending mgmt commands that can |
| 2091 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2092 | * mgmt command at a time since otherwise we cannot easily track what |
| 2093 | * the current values are, will be, and based on that calculate if a new |
| 2094 | * HCI command needs to be sent and if yes with what value. |
| 2095 | */ |
| 2096 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2097 | { |
| 2098 | struct pending_cmd *cmd; |
| 2099 | |
| 2100 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2101 | switch (cmd->opcode) { |
| 2102 | case MGMT_OP_ADD_UUID: |
| 2103 | case MGMT_OP_REMOVE_UUID: |
| 2104 | case MGMT_OP_SET_DEV_CLASS: |
| 2105 | case MGMT_OP_SET_POWERED: |
| 2106 | return true; |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | return false; |
| 2111 | } |
| 2112 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2113 | static const u8 bluetooth_base_uuid[] = { |
| 2114 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2115 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2116 | }; |
| 2117 | |
| 2118 | static u8 get_uuid_size(const u8 *uuid) |
| 2119 | { |
| 2120 | u32 val; |
| 2121 | |
| 2122 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2123 | return 128; |
| 2124 | |
| 2125 | val = get_unaligned_le32(&uuid[12]); |
| 2126 | if (val > 0xffff) |
| 2127 | return 32; |
| 2128 | |
| 2129 | return 16; |
| 2130 | } |
| 2131 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2132 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2133 | { |
| 2134 | struct pending_cmd *cmd; |
| 2135 | |
| 2136 | hci_dev_lock(hdev); |
| 2137 | |
| 2138 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2139 | if (!cmd) |
| 2140 | goto unlock; |
| 2141 | |
| 2142 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2143 | hdev->dev_class, 3); |
| 2144 | |
| 2145 | mgmt_pending_remove(cmd); |
| 2146 | |
| 2147 | unlock: |
| 2148 | hci_dev_unlock(hdev); |
| 2149 | } |
| 2150 | |
| 2151 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2152 | { |
| 2153 | BT_DBG("status 0x%02x", status); |
| 2154 | |
| 2155 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2156 | } |
| 2157 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2158 | 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] | 2159 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2160 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2161 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2162 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2163 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2164 | int err; |
| 2165 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2166 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2167 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2168 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2169 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2170 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2171 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2172 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2173 | goto failed; |
| 2174 | } |
| 2175 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2176 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2177 | if (!uuid) { |
| 2178 | err = -ENOMEM; |
| 2179 | goto failed; |
| 2180 | } |
| 2181 | |
| 2182 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2183 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2184 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2185 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2186 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2187 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2188 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2189 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2190 | update_class(&req); |
| 2191 | update_eir(&req); |
| 2192 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2193 | err = hci_req_run(&req, add_uuid_complete); |
| 2194 | if (err < 0) { |
| 2195 | if (err != -ENODATA) |
| 2196 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2197 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2198 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2199 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2200 | goto failed; |
| 2201 | } |
| 2202 | |
| 2203 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2204 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2205 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2206 | goto failed; |
| 2207 | } |
| 2208 | |
| 2209 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2210 | |
| 2211 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2212 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2213 | return err; |
| 2214 | } |
| 2215 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2216 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2217 | { |
| 2218 | if (!hdev_is_powered(hdev)) |
| 2219 | return false; |
| 2220 | |
| 2221 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2222 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2223 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2224 | return true; |
| 2225 | } |
| 2226 | |
| 2227 | return false; |
| 2228 | } |
| 2229 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2230 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2231 | { |
| 2232 | BT_DBG("status 0x%02x", status); |
| 2233 | |
| 2234 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2235 | } |
| 2236 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2237 | 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] | 2238 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2239 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2240 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2241 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2242 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2243 | 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] | 2244 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2245 | int err, found; |
| 2246 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2247 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2248 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2249 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2250 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2251 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2252 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2253 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2254 | goto unlock; |
| 2255 | } |
| 2256 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2257 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2258 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2259 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2260 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2261 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2262 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2263 | goto unlock; |
| 2264 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2265 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2266 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | found = 0; |
| 2270 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2271 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2272 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2273 | continue; |
| 2274 | |
| 2275 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2276 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2277 | found++; |
| 2278 | } |
| 2279 | |
| 2280 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2281 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2282 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2283 | goto unlock; |
| 2284 | } |
| 2285 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2286 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2287 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2288 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2289 | update_class(&req); |
| 2290 | update_eir(&req); |
| 2291 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2292 | err = hci_req_run(&req, remove_uuid_complete); |
| 2293 | if (err < 0) { |
| 2294 | if (err != -ENODATA) |
| 2295 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2296 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2297 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2298 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2299 | goto unlock; |
| 2300 | } |
| 2301 | |
| 2302 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2303 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2304 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2305 | goto unlock; |
| 2306 | } |
| 2307 | |
| 2308 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2309 | |
| 2310 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2311 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2312 | return err; |
| 2313 | } |
| 2314 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2315 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2316 | { |
| 2317 | BT_DBG("status 0x%02x", status); |
| 2318 | |
| 2319 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2320 | } |
| 2321 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2322 | 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] | 2323 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2324 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2325 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2326 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2327 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2328 | int err; |
| 2329 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2330 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2331 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2332 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2333 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2334 | MGMT_STATUS_NOT_SUPPORTED); |
| 2335 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2336 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2337 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2338 | if (pending_eir_or_class(hdev)) { |
| 2339 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2340 | MGMT_STATUS_BUSY); |
| 2341 | goto unlock; |
| 2342 | } |
| 2343 | |
| 2344 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2345 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2346 | MGMT_STATUS_INVALID_PARAMS); |
| 2347 | goto unlock; |
| 2348 | } |
| 2349 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2350 | hdev->major_class = cp->major; |
| 2351 | hdev->minor_class = cp->minor; |
| 2352 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2353 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2354 | 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] | 2355 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2356 | goto unlock; |
| 2357 | } |
| 2358 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2359 | hci_req_init(&req, hdev); |
| 2360 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2361 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2362 | hci_dev_unlock(hdev); |
| 2363 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2364 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2365 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2366 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2367 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2368 | update_class(&req); |
| 2369 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2370 | err = hci_req_run(&req, set_class_complete); |
| 2371 | if (err < 0) { |
| 2372 | if (err != -ENODATA) |
| 2373 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2374 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2375 | 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] | 2376 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2377 | goto unlock; |
| 2378 | } |
| 2379 | |
| 2380 | 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] | 2381 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2382 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2383 | goto unlock; |
| 2384 | } |
| 2385 | |
| 2386 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2387 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2388 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2389 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2390 | return err; |
| 2391 | } |
| 2392 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2393 | 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] | 2394 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2395 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2396 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2397 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2398 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2399 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2400 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2401 | BT_DBG("request for %s", hdev->name); |
| 2402 | |
| 2403 | if (!lmp_bredr_capable(hdev)) |
| 2404 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2405 | MGMT_STATUS_NOT_SUPPORTED); |
| 2406 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2407 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2408 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2409 | expected_len = sizeof(*cp) + key_count * |
| 2410 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2411 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2412 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2413 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2414 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2415 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2416 | } |
| 2417 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2418 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2419 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2420 | MGMT_STATUS_INVALID_PARAMS); |
| 2421 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2422 | 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] | 2423 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2424 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2425 | for (i = 0; i < key_count; i++) { |
| 2426 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2427 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2428 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2429 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2430 | MGMT_STATUS_INVALID_PARAMS); |
| 2431 | } |
| 2432 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2433 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2434 | |
| 2435 | hci_link_keys_clear(hdev); |
| 2436 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2437 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2438 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2439 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2440 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2441 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2442 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2443 | |
| 2444 | if (changed) |
| 2445 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2446 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2447 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2448 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2449 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2450 | /* Always ignore debug keys and require a new pairing if |
| 2451 | * the user wants to use them. |
| 2452 | */ |
| 2453 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2454 | continue; |
| 2455 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2456 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2457 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2458 | } |
| 2459 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2460 | 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] | 2461 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2462 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2463 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2464 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2465 | } |
| 2466 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2467 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2468 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2469 | { |
| 2470 | struct mgmt_ev_device_unpaired ev; |
| 2471 | |
| 2472 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2473 | ev.addr.type = addr_type; |
| 2474 | |
| 2475 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2476 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2479 | 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] | 2480 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2481 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2482 | struct mgmt_cp_unpair_device *cp = data; |
| 2483 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2484 | struct hci_cp_disconnect dc; |
| 2485 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2486 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2487 | int err; |
| 2488 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2489 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2490 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2491 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2492 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2493 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2494 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2495 | MGMT_STATUS_INVALID_PARAMS, |
| 2496 | &rp, sizeof(rp)); |
| 2497 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2498 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2499 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2500 | MGMT_STATUS_INVALID_PARAMS, |
| 2501 | &rp, sizeof(rp)); |
| 2502 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2503 | hci_dev_lock(hdev); |
| 2504 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2505 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2506 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2507 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2508 | goto unlock; |
| 2509 | } |
| 2510 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2511 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2512 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2513 | } else { |
| 2514 | u8 addr_type; |
| 2515 | |
| 2516 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2517 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2518 | else |
| 2519 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2520 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2521 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2522 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2523 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2524 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2525 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2526 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2527 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2528 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2529 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2530 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2531 | goto unlock; |
| 2532 | } |
| 2533 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2534 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2535 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2536 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2537 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2538 | else |
| 2539 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2540 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2541 | } else { |
| 2542 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2543 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2544 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2545 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2546 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2547 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2548 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2549 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2550 | } |
| 2551 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2552 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2553 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2554 | if (!cmd) { |
| 2555 | err = -ENOMEM; |
| 2556 | goto unlock; |
| 2557 | } |
| 2558 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2559 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2560 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2561 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2562 | if (err < 0) |
| 2563 | mgmt_pending_remove(cmd); |
| 2564 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2565 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2566 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2567 | return err; |
| 2568 | } |
| 2569 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2570 | 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] | 2571 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2572 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2573 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2574 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2575 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2576 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2577 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2578 | int err; |
| 2579 | |
| 2580 | BT_DBG(""); |
| 2581 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2582 | memset(&rp, 0, sizeof(rp)); |
| 2583 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2584 | rp.addr.type = cp->addr.type; |
| 2585 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2586 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2587 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2588 | MGMT_STATUS_INVALID_PARAMS, |
| 2589 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2590 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2591 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2592 | |
| 2593 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2594 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2595 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2596 | goto failed; |
| 2597 | } |
| 2598 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2599 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2600 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2601 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2602 | goto failed; |
| 2603 | } |
| 2604 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2605 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2606 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2607 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2608 | else |
| 2609 | 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] | 2610 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2611 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2612 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2613 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2614 | goto failed; |
| 2615 | } |
| 2616 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2617 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2618 | if (!cmd) { |
| 2619 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2620 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2621 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2622 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2623 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2624 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2625 | |
| 2626 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2627 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2628 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2629 | |
| 2630 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2631 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2632 | return err; |
| 2633 | } |
| 2634 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2635 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2636 | { |
| 2637 | switch (link_type) { |
| 2638 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2639 | switch (addr_type) { |
| 2640 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2641 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2642 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2643 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2644 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2645 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2646 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2647 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2648 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2649 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2650 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2651 | } |
| 2652 | } |
| 2653 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2654 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2655 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2656 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2657 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2658 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2659 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2660 | int err; |
| 2661 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2662 | |
| 2663 | BT_DBG(""); |
| 2664 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2665 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2666 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2667 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2668 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2669 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2670 | goto unlock; |
| 2671 | } |
| 2672 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2673 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2674 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2675 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2676 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2677 | } |
| 2678 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2679 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2680 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2681 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2682 | err = -ENOMEM; |
| 2683 | goto unlock; |
| 2684 | } |
| 2685 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2686 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2687 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2688 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2689 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2690 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2691 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2692 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2693 | continue; |
| 2694 | i++; |
| 2695 | } |
| 2696 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2697 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2698 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2699 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2700 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2701 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2702 | 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] | 2703 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2704 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2705 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2706 | |
| 2707 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2708 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2709 | return err; |
| 2710 | } |
| 2711 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2712 | 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] | 2713 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2714 | { |
| 2715 | struct pending_cmd *cmd; |
| 2716 | int err; |
| 2717 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2718 | 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] | 2719 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2720 | if (!cmd) |
| 2721 | return -ENOMEM; |
| 2722 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2723 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2724 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2725 | if (err < 0) |
| 2726 | mgmt_pending_remove(cmd); |
| 2727 | |
| 2728 | return err; |
| 2729 | } |
| 2730 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2731 | 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] | 2732 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2733 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2734 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2735 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2736 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2737 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2738 | int err; |
| 2739 | |
| 2740 | BT_DBG(""); |
| 2741 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2742 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2743 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2744 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2745 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2746 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2747 | goto failed; |
| 2748 | } |
| 2749 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2750 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2751 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2752 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2753 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2754 | goto failed; |
| 2755 | } |
| 2756 | |
| 2757 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2758 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2759 | |
| 2760 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2761 | |
| 2762 | BT_ERR("PIN code is not 16 bytes long"); |
| 2763 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2764 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2765 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2766 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2767 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2768 | |
| 2769 | goto failed; |
| 2770 | } |
| 2771 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2772 | 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] | 2773 | if (!cmd) { |
| 2774 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2775 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2776 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2777 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2778 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2779 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2780 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2781 | |
| 2782 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2783 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2784 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2785 | |
| 2786 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2787 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2788 | return err; |
| 2789 | } |
| 2790 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2791 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2792 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2793 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2794 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2795 | |
| 2796 | BT_DBG(""); |
| 2797 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2798 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2799 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2800 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2801 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2802 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2803 | |
| 2804 | hdev->io_capability = cp->io_capability; |
| 2805 | |
| 2806 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2807 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2808 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2809 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2810 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2811 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2812 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2813 | } |
| 2814 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2815 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2816 | { |
| 2817 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2818 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2819 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2820 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2821 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2822 | continue; |
| 2823 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2824 | if (cmd->user_data != conn) |
| 2825 | continue; |
| 2826 | |
| 2827 | return cmd; |
| 2828 | } |
| 2829 | |
| 2830 | return NULL; |
| 2831 | } |
| 2832 | |
| 2833 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2834 | { |
| 2835 | struct mgmt_rp_pair_device rp; |
| 2836 | struct hci_conn *conn = cmd->user_data; |
| 2837 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2838 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2839 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2840 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2841 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2842 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2843 | |
| 2844 | /* So we don't get further callbacks for this connection */ |
| 2845 | conn->connect_cfm_cb = NULL; |
| 2846 | conn->security_cfm_cb = NULL; |
| 2847 | conn->disconn_cfm_cb = NULL; |
| 2848 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2849 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2850 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2851 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2852 | } |
| 2853 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2854 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2855 | { |
| 2856 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2857 | struct pending_cmd *cmd; |
| 2858 | |
| 2859 | cmd = find_pairing(conn); |
| 2860 | if (cmd) |
| 2861 | pairing_complete(cmd, status); |
| 2862 | } |
| 2863 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2864 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2865 | { |
| 2866 | struct pending_cmd *cmd; |
| 2867 | |
| 2868 | BT_DBG("status %u", status); |
| 2869 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2870 | cmd = find_pairing(conn); |
| 2871 | if (!cmd) |
| 2872 | BT_DBG("Unable to find a pending command"); |
| 2873 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2874 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2875 | } |
| 2876 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2877 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2878 | { |
| 2879 | struct pending_cmd *cmd; |
| 2880 | |
| 2881 | BT_DBG("status %u", status); |
| 2882 | |
| 2883 | if (!status) |
| 2884 | return; |
| 2885 | |
| 2886 | cmd = find_pairing(conn); |
| 2887 | if (!cmd) |
| 2888 | BT_DBG("Unable to find a pending command"); |
| 2889 | else |
| 2890 | pairing_complete(cmd, mgmt_status(status)); |
| 2891 | } |
| 2892 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2893 | 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] | 2894 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2895 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2896 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2897 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2898 | struct pending_cmd *cmd; |
| 2899 | u8 sec_level, auth_type; |
| 2900 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2901 | int err; |
| 2902 | |
| 2903 | BT_DBG(""); |
| 2904 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2905 | memset(&rp, 0, sizeof(rp)); |
| 2906 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2907 | rp.addr.type = cp->addr.type; |
| 2908 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2909 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2910 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2911 | MGMT_STATUS_INVALID_PARAMS, |
| 2912 | &rp, sizeof(rp)); |
| 2913 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2914 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 2915 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2916 | MGMT_STATUS_INVALID_PARAMS, |
| 2917 | &rp, sizeof(rp)); |
| 2918 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2919 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2920 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2921 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2922 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2923 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2924 | goto unlock; |
| 2925 | } |
| 2926 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2927 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2928 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2929 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2930 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2931 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2932 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2933 | } else { |
| 2934 | u8 addr_type; |
| 2935 | |
| 2936 | /* Convert from L2CAP channel address type to HCI address type |
| 2937 | */ |
| 2938 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2939 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2940 | else |
| 2941 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2942 | |
| 2943 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2944 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2945 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2946 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2947 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2948 | int status; |
| 2949 | |
| 2950 | if (PTR_ERR(conn) == -EBUSY) |
| 2951 | status = MGMT_STATUS_BUSY; |
| 2952 | else |
| 2953 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2954 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2955 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2956 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2957 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2958 | goto unlock; |
| 2959 | } |
| 2960 | |
| 2961 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2962 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2963 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2964 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2965 | goto unlock; |
| 2966 | } |
| 2967 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2968 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2969 | if (!cmd) { |
| 2970 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2971 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2972 | goto unlock; |
| 2973 | } |
| 2974 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2975 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2976 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2977 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2978 | conn->security_cfm_cb = pairing_complete_cb; |
| 2979 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2980 | } else { |
| 2981 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2982 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2983 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2984 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2985 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2986 | conn->io_capability = cp->io_cap; |
| 2987 | cmd->user_data = conn; |
| 2988 | |
| 2989 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2990 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2991 | pairing_complete(cmd, 0); |
| 2992 | |
| 2993 | err = 0; |
| 2994 | |
| 2995 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2996 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2997 | return err; |
| 2998 | } |
| 2999 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3000 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3001 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3002 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3003 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3004 | struct pending_cmd *cmd; |
| 3005 | struct hci_conn *conn; |
| 3006 | int err; |
| 3007 | |
| 3008 | BT_DBG(""); |
| 3009 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3010 | hci_dev_lock(hdev); |
| 3011 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3012 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3013 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3014 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 3015 | goto unlock; |
| 3016 | } |
| 3017 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3018 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3019 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3020 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3021 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3022 | goto unlock; |
| 3023 | } |
| 3024 | |
| 3025 | conn = cmd->user_data; |
| 3026 | |
| 3027 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3028 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3029 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3030 | goto unlock; |
| 3031 | } |
| 3032 | |
| 3033 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3034 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3035 | 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] | 3036 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3037 | unlock: |
| 3038 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3039 | return err; |
| 3040 | } |
| 3041 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3042 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3043 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3044 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3045 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3046 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3047 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3048 | int err; |
| 3049 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3050 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3051 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3052 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3053 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3054 | MGMT_STATUS_NOT_POWERED, addr, |
| 3055 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3056 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3057 | } |
| 3058 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3059 | if (addr->type == BDADDR_BREDR) |
| 3060 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3061 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3062 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3063 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3064 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3065 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3066 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3067 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3068 | goto done; |
| 3069 | } |
| 3070 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3071 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3072 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3073 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3074 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3075 | MGMT_STATUS_SUCCESS, addr, |
| 3076 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3077 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3078 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3079 | MGMT_STATUS_FAILED, addr, |
| 3080 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3081 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3082 | goto done; |
| 3083 | } |
| 3084 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3085 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3086 | if (!cmd) { |
| 3087 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3088 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3089 | } |
| 3090 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3091 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3092 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3093 | struct hci_cp_user_passkey_reply cp; |
| 3094 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3095 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3096 | cp.passkey = passkey; |
| 3097 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3098 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3099 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3100 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3101 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3102 | if (err < 0) |
| 3103 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3104 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3105 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3106 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3107 | return err; |
| 3108 | } |
| 3109 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3110 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3111 | void *data, u16 len) |
| 3112 | { |
| 3113 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3114 | |
| 3115 | BT_DBG(""); |
| 3116 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3117 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3118 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3119 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3120 | } |
| 3121 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3122 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3123 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3124 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3125 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3126 | |
| 3127 | BT_DBG(""); |
| 3128 | |
| 3129 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3130 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3131 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3132 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3133 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3134 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3135 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3136 | } |
| 3137 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3138 | 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] | 3139 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3140 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3141 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3142 | |
| 3143 | BT_DBG(""); |
| 3144 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3145 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3146 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3147 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3148 | } |
| 3149 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3150 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3151 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3152 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3153 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3154 | |
| 3155 | BT_DBG(""); |
| 3156 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3157 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3158 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3159 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3160 | } |
| 3161 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3162 | 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] | 3163 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3164 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3165 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3166 | |
| 3167 | BT_DBG(""); |
| 3168 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3169 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3170 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3171 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3172 | } |
| 3173 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3174 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3175 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3176 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3177 | struct hci_cp_write_local_name cp; |
| 3178 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3179 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3180 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3181 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3182 | } |
| 3183 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3184 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3185 | { |
| 3186 | struct mgmt_cp_set_local_name *cp; |
| 3187 | struct pending_cmd *cmd; |
| 3188 | |
| 3189 | BT_DBG("status 0x%02x", status); |
| 3190 | |
| 3191 | hci_dev_lock(hdev); |
| 3192 | |
| 3193 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3194 | if (!cmd) |
| 3195 | goto unlock; |
| 3196 | |
| 3197 | cp = cmd->param; |
| 3198 | |
| 3199 | if (status) |
| 3200 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3201 | mgmt_status(status)); |
| 3202 | else |
| 3203 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3204 | cp, sizeof(*cp)); |
| 3205 | |
| 3206 | mgmt_pending_remove(cmd); |
| 3207 | |
| 3208 | unlock: |
| 3209 | hci_dev_unlock(hdev); |
| 3210 | } |
| 3211 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3212 | 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] | 3213 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3214 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3215 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3216 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3217 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3218 | int err; |
| 3219 | |
| 3220 | BT_DBG(""); |
| 3221 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3222 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3223 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3224 | /* If the old values are the same as the new ones just return a |
| 3225 | * direct command complete event. |
| 3226 | */ |
| 3227 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3228 | !memcmp(hdev->short_name, cp->short_name, |
| 3229 | sizeof(hdev->short_name))) { |
| 3230 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3231 | data, len); |
| 3232 | goto failed; |
| 3233 | } |
| 3234 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3235 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3236 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3237 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3238 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3239 | |
| 3240 | 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] | 3241 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3242 | if (err < 0) |
| 3243 | goto failed; |
| 3244 | |
| 3245 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3246 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3247 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3248 | goto failed; |
| 3249 | } |
| 3250 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3251 | 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] | 3252 | if (!cmd) { |
| 3253 | err = -ENOMEM; |
| 3254 | goto failed; |
| 3255 | } |
| 3256 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3257 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3258 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3259 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3260 | |
| 3261 | if (lmp_bredr_capable(hdev)) { |
| 3262 | update_name(&req); |
| 3263 | update_eir(&req); |
| 3264 | } |
| 3265 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3266 | /* The name is stored in the scan response data and so |
| 3267 | * no need to udpate the advertising data here. |
| 3268 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3269 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3270 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3271 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3272 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3273 | if (err < 0) |
| 3274 | mgmt_pending_remove(cmd); |
| 3275 | |
| 3276 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3277 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3278 | return err; |
| 3279 | } |
| 3280 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3281 | 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] | 3282 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3283 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3284 | struct pending_cmd *cmd; |
| 3285 | int err; |
| 3286 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3287 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3288 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3289 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3290 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3291 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3292 | 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] | 3293 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3294 | goto unlock; |
| 3295 | } |
| 3296 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3297 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3298 | 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] | 3299 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3300 | goto unlock; |
| 3301 | } |
| 3302 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3303 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3304 | 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] | 3305 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3306 | goto unlock; |
| 3307 | } |
| 3308 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3309 | 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] | 3310 | if (!cmd) { |
| 3311 | err = -ENOMEM; |
| 3312 | goto unlock; |
| 3313 | } |
| 3314 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3315 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3316 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3317 | 0, NULL); |
| 3318 | else |
| 3319 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3320 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3321 | if (err < 0) |
| 3322 | mgmt_pending_remove(cmd); |
| 3323 | |
| 3324 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3325 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3326 | return err; |
| 3327 | } |
| 3328 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3329 | 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] | 3330 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3331 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3332 | int err; |
| 3333 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3334 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3335 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3336 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3337 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3338 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3339 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3340 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3341 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3342 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3343 | cp->hash, cp->randomizer); |
| 3344 | if (err < 0) |
| 3345 | status = MGMT_STATUS_FAILED; |
| 3346 | else |
| 3347 | status = MGMT_STATUS_SUCCESS; |
| 3348 | |
| 3349 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3350 | status, &cp->addr, sizeof(cp->addr)); |
| 3351 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3352 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3353 | u8 status; |
| 3354 | |
| 3355 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3356 | cp->hash192, |
| 3357 | cp->randomizer192, |
| 3358 | cp->hash256, |
| 3359 | cp->randomizer256); |
| 3360 | if (err < 0) |
| 3361 | status = MGMT_STATUS_FAILED; |
| 3362 | else |
| 3363 | status = MGMT_STATUS_SUCCESS; |
| 3364 | |
| 3365 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3366 | status, &cp->addr, sizeof(cp->addr)); |
| 3367 | } else { |
| 3368 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3369 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3370 | MGMT_STATUS_INVALID_PARAMS); |
| 3371 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3372 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3373 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3374 | return err; |
| 3375 | } |
| 3376 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3377 | 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] | 3378 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3379 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3380 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3381 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3382 | int err; |
| 3383 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3384 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3385 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3386 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3387 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3388 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3389 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3390 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3391 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3392 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3393 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3394 | 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] | 3395 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3396 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3397 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3398 | return err; |
| 3399 | } |
| 3400 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3401 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3402 | { |
| 3403 | struct pending_cmd *cmd; |
| 3404 | u8 type; |
| 3405 | int err; |
| 3406 | |
| 3407 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3408 | |
| 3409 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3410 | if (!cmd) |
| 3411 | return -ENOENT; |
| 3412 | |
| 3413 | type = hdev->discovery.type; |
| 3414 | |
| 3415 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3416 | &type, sizeof(type)); |
| 3417 | mgmt_pending_remove(cmd); |
| 3418 | |
| 3419 | return err; |
| 3420 | } |
| 3421 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3422 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3423 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3424 | unsigned long timeout = 0; |
| 3425 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3426 | BT_DBG("status %d", status); |
| 3427 | |
| 3428 | if (status) { |
| 3429 | hci_dev_lock(hdev); |
| 3430 | mgmt_start_discovery_failed(hdev, status); |
| 3431 | hci_dev_unlock(hdev); |
| 3432 | return; |
| 3433 | } |
| 3434 | |
| 3435 | hci_dev_lock(hdev); |
| 3436 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3437 | hci_dev_unlock(hdev); |
| 3438 | |
| 3439 | switch (hdev->discovery.type) { |
| 3440 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3441 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3442 | break; |
| 3443 | |
| 3444 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3445 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3446 | break; |
| 3447 | |
| 3448 | case DISCOV_TYPE_BREDR: |
| 3449 | break; |
| 3450 | |
| 3451 | default: |
| 3452 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3453 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3454 | |
| 3455 | if (!timeout) |
| 3456 | return; |
| 3457 | |
| 3458 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3459 | } |
| 3460 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3461 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3462 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3463 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3464 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3465 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3466 | struct hci_cp_le_set_scan_param param_cp; |
| 3467 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3468 | struct hci_cp_inquiry inq_cp; |
| 3469 | struct hci_request req; |
| 3470 | /* General inquiry access code (GIAC) */ |
| 3471 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3472 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3473 | int err; |
| 3474 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3475 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3476 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3477 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3478 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3479 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3480 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3481 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3482 | goto failed; |
| 3483 | } |
| 3484 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3485 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3486 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3487 | MGMT_STATUS_BUSY); |
| 3488 | goto failed; |
| 3489 | } |
| 3490 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3491 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3492 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3493 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3494 | goto failed; |
| 3495 | } |
| 3496 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3497 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3498 | if (!cmd) { |
| 3499 | err = -ENOMEM; |
| 3500 | goto failed; |
| 3501 | } |
| 3502 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3503 | hdev->discovery.type = cp->type; |
| 3504 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3505 | hci_req_init(&req, hdev); |
| 3506 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3507 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3508 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3509 | status = mgmt_bredr_support(hdev); |
| 3510 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3511 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3512 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3513 | mgmt_pending_remove(cmd); |
| 3514 | goto failed; |
| 3515 | } |
| 3516 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3517 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3518 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3519 | MGMT_STATUS_BUSY); |
| 3520 | mgmt_pending_remove(cmd); |
| 3521 | goto failed; |
| 3522 | } |
| 3523 | |
| 3524 | hci_inquiry_cache_flush(hdev); |
| 3525 | |
| 3526 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3527 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3528 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3529 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3530 | break; |
| 3531 | |
| 3532 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3533 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3534 | status = mgmt_le_support(hdev); |
| 3535 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3536 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3537 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3538 | mgmt_pending_remove(cmd); |
| 3539 | goto failed; |
| 3540 | } |
| 3541 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3542 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3543 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3544 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3545 | MGMT_STATUS_NOT_SUPPORTED); |
| 3546 | mgmt_pending_remove(cmd); |
| 3547 | goto failed; |
| 3548 | } |
| 3549 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3550 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3551 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3552 | MGMT_STATUS_REJECTED); |
| 3553 | mgmt_pending_remove(cmd); |
| 3554 | goto failed; |
| 3555 | } |
| 3556 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3557 | /* If controller is scanning, it means the background scanning |
| 3558 | * is running. Thus, we should temporarily stop it in order to |
| 3559 | * set the discovery scanning parameters. |
| 3560 | */ |
| 3561 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3562 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3563 | |
| 3564 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3565 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3566 | /* All active scans will be done with either a resolvable |
| 3567 | * private address (when privacy feature has been enabled) |
| 3568 | * or unresolvable private address. |
| 3569 | */ |
| 3570 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3571 | if (err < 0) { |
| 3572 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3573 | MGMT_STATUS_FAILED); |
| 3574 | mgmt_pending_remove(cmd); |
| 3575 | goto failed; |
| 3576 | } |
| 3577 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3578 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3579 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3580 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3581 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3582 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3583 | ¶m_cp); |
| 3584 | |
| 3585 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3586 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3587 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3588 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3589 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3590 | break; |
| 3591 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3592 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3593 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3594 | MGMT_STATUS_INVALID_PARAMS); |
| 3595 | mgmt_pending_remove(cmd); |
| 3596 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3597 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3598 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3599 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3600 | if (err < 0) |
| 3601 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3602 | else |
| 3603 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3604 | |
| 3605 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3606 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3607 | return err; |
| 3608 | } |
| 3609 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3610 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3611 | { |
| 3612 | struct pending_cmd *cmd; |
| 3613 | int err; |
| 3614 | |
| 3615 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3616 | if (!cmd) |
| 3617 | return -ENOENT; |
| 3618 | |
| 3619 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3620 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3621 | mgmt_pending_remove(cmd); |
| 3622 | |
| 3623 | return err; |
| 3624 | } |
| 3625 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3626 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3627 | { |
| 3628 | BT_DBG("status %d", status); |
| 3629 | |
| 3630 | hci_dev_lock(hdev); |
| 3631 | |
| 3632 | if (status) { |
| 3633 | mgmt_stop_discovery_failed(hdev, status); |
| 3634 | goto unlock; |
| 3635 | } |
| 3636 | |
| 3637 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3638 | |
| 3639 | unlock: |
| 3640 | hci_dev_unlock(hdev); |
| 3641 | } |
| 3642 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3643 | 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] | 3644 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3645 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3646 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3647 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3648 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3649 | int err; |
| 3650 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3651 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3652 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3653 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3654 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3655 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3656 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3657 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3658 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3659 | goto unlock; |
| 3660 | } |
| 3661 | |
| 3662 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3663 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3664 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3665 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3666 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3667 | } |
| 3668 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3669 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3670 | if (!cmd) { |
| 3671 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3672 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3673 | } |
| 3674 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3675 | hci_req_init(&req, hdev); |
| 3676 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3677 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3678 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3679 | err = hci_req_run(&req, stop_discovery_complete); |
| 3680 | if (!err) { |
| 3681 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3682 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3683 | } |
| 3684 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3685 | mgmt_pending_remove(cmd); |
| 3686 | |
| 3687 | /* If no HCI commands were sent we're done */ |
| 3688 | if (err == -ENODATA) { |
| 3689 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3690 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3691 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3692 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3693 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3694 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3695 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3696 | return err; |
| 3697 | } |
| 3698 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3699 | 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] | 3700 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3701 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3702 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3703 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3704 | int err; |
| 3705 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3706 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3707 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3708 | hci_dev_lock(hdev); |
| 3709 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3710 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3711 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3712 | MGMT_STATUS_FAILED, &cp->addr, |
| 3713 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3714 | goto failed; |
| 3715 | } |
| 3716 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3717 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3718 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3719 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3720 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3721 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3722 | goto failed; |
| 3723 | } |
| 3724 | |
| 3725 | if (cp->name_known) { |
| 3726 | e->name_state = NAME_KNOWN; |
| 3727 | list_del(&e->list); |
| 3728 | } else { |
| 3729 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3730 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3731 | } |
| 3732 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3733 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3734 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3735 | |
| 3736 | failed: |
| 3737 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3738 | return err; |
| 3739 | } |
| 3740 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3741 | 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] | 3742 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3743 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3744 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3745 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3746 | int err; |
| 3747 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3748 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3749 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3750 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3751 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3752 | MGMT_STATUS_INVALID_PARAMS, |
| 3753 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3754 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3755 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3756 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3757 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3758 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3759 | status = MGMT_STATUS_FAILED; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3760 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3761 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3762 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3763 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3764 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3765 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3766 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3767 | |
| 3768 | return err; |
| 3769 | } |
| 3770 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3771 | 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] | 3772 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3773 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3774 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3775 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3776 | int err; |
| 3777 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3778 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3779 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3780 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3781 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3782 | MGMT_STATUS_INVALID_PARAMS, |
| 3783 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3784 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3785 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3786 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3787 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3788 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3789 | status = MGMT_STATUS_INVALID_PARAMS; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3790 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3791 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3792 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3793 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3794 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3795 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3796 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3797 | |
| 3798 | return err; |
| 3799 | } |
| 3800 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3801 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3802 | u16 len) |
| 3803 | { |
| 3804 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3805 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3806 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3807 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3808 | |
| 3809 | BT_DBG("%s", hdev->name); |
| 3810 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3811 | source = __le16_to_cpu(cp->source); |
| 3812 | |
| 3813 | if (source > 0x0002) |
| 3814 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3815 | MGMT_STATUS_INVALID_PARAMS); |
| 3816 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3817 | hci_dev_lock(hdev); |
| 3818 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3819 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3820 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3821 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3822 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3823 | |
| 3824 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3825 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3826 | hci_req_init(&req, hdev); |
| 3827 | update_eir(&req); |
| 3828 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3829 | |
| 3830 | hci_dev_unlock(hdev); |
| 3831 | |
| 3832 | return err; |
| 3833 | } |
| 3834 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3835 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3836 | { |
| 3837 | struct cmd_lookup match = { NULL, hdev }; |
| 3838 | |
| 3839 | if (status) { |
| 3840 | u8 mgmt_err = mgmt_status(status); |
| 3841 | |
| 3842 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3843 | cmd_status_rsp, &mgmt_err); |
| 3844 | return; |
| 3845 | } |
| 3846 | |
| 3847 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3848 | &match); |
| 3849 | |
| 3850 | new_settings(hdev, match.sk); |
| 3851 | |
| 3852 | if (match.sk) |
| 3853 | sock_put(match.sk); |
| 3854 | } |
| 3855 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3856 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3857 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3858 | { |
| 3859 | struct mgmt_mode *cp = data; |
| 3860 | struct pending_cmd *cmd; |
| 3861 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3862 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3863 | int err; |
| 3864 | |
| 3865 | BT_DBG("request for %s", hdev->name); |
| 3866 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3867 | status = mgmt_le_support(hdev); |
| 3868 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3869 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3870 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3871 | |
| 3872 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3873 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3874 | MGMT_STATUS_INVALID_PARAMS); |
| 3875 | |
| 3876 | hci_dev_lock(hdev); |
| 3877 | |
| 3878 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3879 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3880 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3881 | /* The following conditions are ones which mean that we should |
| 3882 | * not do any HCI communication but directly send a mgmt |
| 3883 | * response to user space (after toggling the flag if |
| 3884 | * necessary). |
| 3885 | */ |
| 3886 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3887 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3888 | bool changed = false; |
| 3889 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3890 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3891 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3892 | changed = true; |
| 3893 | } |
| 3894 | |
| 3895 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3896 | if (err < 0) |
| 3897 | goto unlock; |
| 3898 | |
| 3899 | if (changed) |
| 3900 | err = new_settings(hdev, sk); |
| 3901 | |
| 3902 | goto unlock; |
| 3903 | } |
| 3904 | |
| 3905 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3906 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3907 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3908 | MGMT_STATUS_BUSY); |
| 3909 | goto unlock; |
| 3910 | } |
| 3911 | |
| 3912 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3913 | if (!cmd) { |
| 3914 | err = -ENOMEM; |
| 3915 | goto unlock; |
| 3916 | } |
| 3917 | |
| 3918 | hci_req_init(&req, hdev); |
| 3919 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3920 | if (val) |
| 3921 | enable_advertising(&req); |
| 3922 | else |
| 3923 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3924 | |
| 3925 | err = hci_req_run(&req, set_advertising_complete); |
| 3926 | if (err < 0) |
| 3927 | mgmt_pending_remove(cmd); |
| 3928 | |
| 3929 | unlock: |
| 3930 | hci_dev_unlock(hdev); |
| 3931 | return err; |
| 3932 | } |
| 3933 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3934 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3935 | void *data, u16 len) |
| 3936 | { |
| 3937 | struct mgmt_cp_set_static_address *cp = data; |
| 3938 | int err; |
| 3939 | |
| 3940 | BT_DBG("%s", hdev->name); |
| 3941 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3942 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3943 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3944 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3945 | |
| 3946 | if (hdev_is_powered(hdev)) |
| 3947 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3948 | MGMT_STATUS_REJECTED); |
| 3949 | |
| 3950 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3951 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3952 | return cmd_status(sk, hdev->id, |
| 3953 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3954 | MGMT_STATUS_INVALID_PARAMS); |
| 3955 | |
| 3956 | /* Two most significant bits shall be set */ |
| 3957 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3958 | return cmd_status(sk, hdev->id, |
| 3959 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3960 | MGMT_STATUS_INVALID_PARAMS); |
| 3961 | } |
| 3962 | |
| 3963 | hci_dev_lock(hdev); |
| 3964 | |
| 3965 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3966 | |
| 3967 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3968 | |
| 3969 | hci_dev_unlock(hdev); |
| 3970 | |
| 3971 | return err; |
| 3972 | } |
| 3973 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3974 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 3975 | void *data, u16 len) |
| 3976 | { |
| 3977 | struct mgmt_cp_set_scan_params *cp = data; |
| 3978 | __u16 interval, window; |
| 3979 | int err; |
| 3980 | |
| 3981 | BT_DBG("%s", hdev->name); |
| 3982 | |
| 3983 | if (!lmp_le_capable(hdev)) |
| 3984 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3985 | MGMT_STATUS_NOT_SUPPORTED); |
| 3986 | |
| 3987 | interval = __le16_to_cpu(cp->interval); |
| 3988 | |
| 3989 | if (interval < 0x0004 || interval > 0x4000) |
| 3990 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3991 | MGMT_STATUS_INVALID_PARAMS); |
| 3992 | |
| 3993 | window = __le16_to_cpu(cp->window); |
| 3994 | |
| 3995 | if (window < 0x0004 || window > 0x4000) |
| 3996 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3997 | MGMT_STATUS_INVALID_PARAMS); |
| 3998 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 3999 | if (window > interval) |
| 4000 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 4001 | MGMT_STATUS_INVALID_PARAMS); |
| 4002 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4003 | hci_dev_lock(hdev); |
| 4004 | |
| 4005 | hdev->le_scan_interval = interval; |
| 4006 | hdev->le_scan_window = window; |
| 4007 | |
| 4008 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 4009 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 4010 | /* If background scan is running, restart it so new parameters are |
| 4011 | * loaded. |
| 4012 | */ |
| 4013 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 4014 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 4015 | struct hci_request req; |
| 4016 | |
| 4017 | hci_req_init(&req, hdev); |
| 4018 | |
| 4019 | hci_req_add_le_scan_disable(&req); |
| 4020 | hci_req_add_le_passive_scan(&req); |
| 4021 | |
| 4022 | hci_req_run(&req, NULL); |
| 4023 | } |
| 4024 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4025 | hci_dev_unlock(hdev); |
| 4026 | |
| 4027 | return err; |
| 4028 | } |
| 4029 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4030 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4031 | { |
| 4032 | struct pending_cmd *cmd; |
| 4033 | |
| 4034 | BT_DBG("status 0x%02x", status); |
| 4035 | |
| 4036 | hci_dev_lock(hdev); |
| 4037 | |
| 4038 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4039 | if (!cmd) |
| 4040 | goto unlock; |
| 4041 | |
| 4042 | if (status) { |
| 4043 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4044 | mgmt_status(status)); |
| 4045 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4046 | struct mgmt_mode *cp = cmd->param; |
| 4047 | |
| 4048 | if (cp->val) |
| 4049 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4050 | else |
| 4051 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4052 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4053 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4054 | new_settings(hdev, cmd->sk); |
| 4055 | } |
| 4056 | |
| 4057 | mgmt_pending_remove(cmd); |
| 4058 | |
| 4059 | unlock: |
| 4060 | hci_dev_unlock(hdev); |
| 4061 | } |
| 4062 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4063 | 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] | 4064 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4065 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4066 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4067 | struct pending_cmd *cmd; |
| 4068 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4069 | int err; |
| 4070 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4071 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4072 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4073 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4074 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4075 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4076 | MGMT_STATUS_NOT_SUPPORTED); |
| 4077 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4078 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4079 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4080 | MGMT_STATUS_INVALID_PARAMS); |
| 4081 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4082 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4083 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4084 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4085 | |
| 4086 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4087 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4088 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4089 | |
| 4090 | hci_dev_lock(hdev); |
| 4091 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4092 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4093 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4094 | MGMT_STATUS_BUSY); |
| 4095 | goto unlock; |
| 4096 | } |
| 4097 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4098 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4099 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4100 | hdev); |
| 4101 | goto unlock; |
| 4102 | } |
| 4103 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4104 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4105 | data, len); |
| 4106 | if (!cmd) { |
| 4107 | err = -ENOMEM; |
| 4108 | goto unlock; |
| 4109 | } |
| 4110 | |
| 4111 | hci_req_init(&req, hdev); |
| 4112 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4113 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4114 | |
| 4115 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4116 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4117 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4118 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4119 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4120 | } |
| 4121 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4122 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4123 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4124 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4125 | return err; |
| 4126 | } |
| 4127 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4128 | static void set_bredr_scan(struct hci_request *req) |
| 4129 | { |
| 4130 | struct hci_dev *hdev = req->hdev; |
| 4131 | u8 scan = 0; |
| 4132 | |
| 4133 | /* Ensure that fast connectable is disabled. This function will |
| 4134 | * not do anything if the page scan parameters are already what |
| 4135 | * they should be. |
| 4136 | */ |
| 4137 | write_fast_connectable(req, false); |
| 4138 | |
| 4139 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4140 | scan |= SCAN_PAGE; |
| 4141 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4142 | scan |= SCAN_INQUIRY; |
| 4143 | |
| 4144 | if (scan) |
| 4145 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4146 | } |
| 4147 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4148 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4149 | { |
| 4150 | struct pending_cmd *cmd; |
| 4151 | |
| 4152 | BT_DBG("status 0x%02x", status); |
| 4153 | |
| 4154 | hci_dev_lock(hdev); |
| 4155 | |
| 4156 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4157 | if (!cmd) |
| 4158 | goto unlock; |
| 4159 | |
| 4160 | if (status) { |
| 4161 | u8 mgmt_err = mgmt_status(status); |
| 4162 | |
| 4163 | /* We need to restore the flag if related HCI commands |
| 4164 | * failed. |
| 4165 | */ |
| 4166 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4167 | |
| 4168 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4169 | } else { |
| 4170 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4171 | new_settings(hdev, cmd->sk); |
| 4172 | } |
| 4173 | |
| 4174 | mgmt_pending_remove(cmd); |
| 4175 | |
| 4176 | unlock: |
| 4177 | hci_dev_unlock(hdev); |
| 4178 | } |
| 4179 | |
| 4180 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4181 | { |
| 4182 | struct mgmt_mode *cp = data; |
| 4183 | struct pending_cmd *cmd; |
| 4184 | struct hci_request req; |
| 4185 | int err; |
| 4186 | |
| 4187 | BT_DBG("request for %s", hdev->name); |
| 4188 | |
| 4189 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4190 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4191 | MGMT_STATUS_NOT_SUPPORTED); |
| 4192 | |
| 4193 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4194 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4195 | MGMT_STATUS_REJECTED); |
| 4196 | |
| 4197 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4198 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4199 | MGMT_STATUS_INVALID_PARAMS); |
| 4200 | |
| 4201 | hci_dev_lock(hdev); |
| 4202 | |
| 4203 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4204 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4205 | goto unlock; |
| 4206 | } |
| 4207 | |
| 4208 | if (!hdev_is_powered(hdev)) { |
| 4209 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4210 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4211 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4212 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4213 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4214 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4215 | } |
| 4216 | |
| 4217 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4218 | |
| 4219 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4220 | if (err < 0) |
| 4221 | goto unlock; |
| 4222 | |
| 4223 | err = new_settings(hdev, sk); |
| 4224 | goto unlock; |
| 4225 | } |
| 4226 | |
| 4227 | /* Reject disabling when powered on */ |
| 4228 | if (!cp->val) { |
| 4229 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4230 | MGMT_STATUS_REJECTED); |
| 4231 | goto unlock; |
| 4232 | } |
| 4233 | |
| 4234 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4235 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4236 | MGMT_STATUS_BUSY); |
| 4237 | goto unlock; |
| 4238 | } |
| 4239 | |
| 4240 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4241 | if (!cmd) { |
| 4242 | err = -ENOMEM; |
| 4243 | goto unlock; |
| 4244 | } |
| 4245 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4246 | /* 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] | 4247 | * generates the correct flags. |
| 4248 | */ |
| 4249 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4250 | |
| 4251 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4252 | |
| 4253 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4254 | set_bredr_scan(&req); |
| 4255 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4256 | /* Since only the advertising data flags will change, there |
| 4257 | * is no need to update the scan response data. |
| 4258 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4259 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4260 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4261 | err = hci_req_run(&req, set_bredr_complete); |
| 4262 | if (err < 0) |
| 4263 | mgmt_pending_remove(cmd); |
| 4264 | |
| 4265 | unlock: |
| 4266 | hci_dev_unlock(hdev); |
| 4267 | return err; |
| 4268 | } |
| 4269 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4270 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4271 | void *data, u16 len) |
| 4272 | { |
| 4273 | struct mgmt_mode *cp = data; |
| 4274 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4275 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4276 | int err; |
| 4277 | |
| 4278 | BT_DBG("request for %s", hdev->name); |
| 4279 | |
| 4280 | status = mgmt_bredr_support(hdev); |
| 4281 | if (status) |
| 4282 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4283 | status); |
| 4284 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4285 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4286 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4287 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4288 | MGMT_STATUS_NOT_SUPPORTED); |
| 4289 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4290 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4291 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4292 | MGMT_STATUS_INVALID_PARAMS); |
| 4293 | |
| 4294 | hci_dev_lock(hdev); |
| 4295 | |
| 4296 | if (!hdev_is_powered(hdev)) { |
| 4297 | bool changed; |
| 4298 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4299 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4300 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4301 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4302 | if (cp->val == 0x02) |
| 4303 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4304 | else |
| 4305 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4306 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4307 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4308 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4309 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4310 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4311 | |
| 4312 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4313 | if (err < 0) |
| 4314 | goto failed; |
| 4315 | |
| 4316 | if (changed) |
| 4317 | err = new_settings(hdev, sk); |
| 4318 | |
| 4319 | goto failed; |
| 4320 | } |
| 4321 | |
| 4322 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4323 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4324 | MGMT_STATUS_BUSY); |
| 4325 | goto failed; |
| 4326 | } |
| 4327 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4328 | val = !!cp->val; |
| 4329 | |
| 4330 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4331 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4332 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4333 | goto failed; |
| 4334 | } |
| 4335 | |
| 4336 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4337 | if (!cmd) { |
| 4338 | err = -ENOMEM; |
| 4339 | goto failed; |
| 4340 | } |
| 4341 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4342 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4343 | if (err < 0) { |
| 4344 | mgmt_pending_remove(cmd); |
| 4345 | goto failed; |
| 4346 | } |
| 4347 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4348 | if (cp->val == 0x02) |
| 4349 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4350 | else |
| 4351 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4352 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4353 | failed: |
| 4354 | hci_dev_unlock(hdev); |
| 4355 | return err; |
| 4356 | } |
| 4357 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4358 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4359 | void *data, u16 len) |
| 4360 | { |
| 4361 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4362 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4363 | int err; |
| 4364 | |
| 4365 | BT_DBG("request for %s", hdev->name); |
| 4366 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4367 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4368 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4369 | MGMT_STATUS_INVALID_PARAMS); |
| 4370 | |
| 4371 | hci_dev_lock(hdev); |
| 4372 | |
| 4373 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4374 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4375 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4376 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4377 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4378 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4379 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4380 | if (cp->val == 0x02) |
| 4381 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4382 | &hdev->dev_flags); |
| 4383 | else |
| 4384 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4385 | &hdev->dev_flags); |
| 4386 | |
| 4387 | if (hdev_is_powered(hdev) && use_changed && |
| 4388 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4389 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4390 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4391 | sizeof(mode), &mode); |
| 4392 | } |
| 4393 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4394 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4395 | if (err < 0) |
| 4396 | goto unlock; |
| 4397 | |
| 4398 | if (changed) |
| 4399 | err = new_settings(hdev, sk); |
| 4400 | |
| 4401 | unlock: |
| 4402 | hci_dev_unlock(hdev); |
| 4403 | return err; |
| 4404 | } |
| 4405 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4406 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4407 | u16 len) |
| 4408 | { |
| 4409 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4410 | bool changed; |
| 4411 | int err; |
| 4412 | |
| 4413 | BT_DBG("request for %s", hdev->name); |
| 4414 | |
| 4415 | if (!lmp_le_capable(hdev)) |
| 4416 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4417 | MGMT_STATUS_NOT_SUPPORTED); |
| 4418 | |
| 4419 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4420 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4421 | MGMT_STATUS_INVALID_PARAMS); |
| 4422 | |
| 4423 | if (hdev_is_powered(hdev)) |
| 4424 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4425 | MGMT_STATUS_REJECTED); |
| 4426 | |
| 4427 | hci_dev_lock(hdev); |
| 4428 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4429 | /* If user space supports this command it is also expected to |
| 4430 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4431 | */ |
| 4432 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4433 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4434 | if (cp->privacy) { |
| 4435 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4436 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4437 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4438 | } else { |
| 4439 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4440 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4441 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4442 | } |
| 4443 | |
| 4444 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4445 | if (err < 0) |
| 4446 | goto unlock; |
| 4447 | |
| 4448 | if (changed) |
| 4449 | err = new_settings(hdev, sk); |
| 4450 | |
| 4451 | unlock: |
| 4452 | hci_dev_unlock(hdev); |
| 4453 | return err; |
| 4454 | } |
| 4455 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4456 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4457 | { |
| 4458 | switch (irk->addr.type) { |
| 4459 | case BDADDR_LE_PUBLIC: |
| 4460 | return true; |
| 4461 | |
| 4462 | case BDADDR_LE_RANDOM: |
| 4463 | /* Two most significant bits shall be set */ |
| 4464 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4465 | return false; |
| 4466 | return true; |
| 4467 | } |
| 4468 | |
| 4469 | return false; |
| 4470 | } |
| 4471 | |
| 4472 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4473 | u16 len) |
| 4474 | { |
| 4475 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4476 | u16 irk_count, expected_len; |
| 4477 | int i, err; |
| 4478 | |
| 4479 | BT_DBG("request for %s", hdev->name); |
| 4480 | |
| 4481 | if (!lmp_le_capable(hdev)) |
| 4482 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4483 | MGMT_STATUS_NOT_SUPPORTED); |
| 4484 | |
| 4485 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4486 | |
| 4487 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4488 | if (expected_len != len) { |
| 4489 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4490 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4491 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4492 | MGMT_STATUS_INVALID_PARAMS); |
| 4493 | } |
| 4494 | |
| 4495 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4496 | |
| 4497 | for (i = 0; i < irk_count; i++) { |
| 4498 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4499 | |
| 4500 | if (!irk_is_valid(key)) |
| 4501 | return cmd_status(sk, hdev->id, |
| 4502 | MGMT_OP_LOAD_IRKS, |
| 4503 | MGMT_STATUS_INVALID_PARAMS); |
| 4504 | } |
| 4505 | |
| 4506 | hci_dev_lock(hdev); |
| 4507 | |
| 4508 | hci_smp_irks_clear(hdev); |
| 4509 | |
| 4510 | for (i = 0; i < irk_count; i++) { |
| 4511 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4512 | u8 addr_type; |
| 4513 | |
| 4514 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4515 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4516 | else |
| 4517 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4518 | |
| 4519 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4520 | BDADDR_ANY); |
| 4521 | } |
| 4522 | |
| 4523 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4524 | |
| 4525 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4526 | |
| 4527 | hci_dev_unlock(hdev); |
| 4528 | |
| 4529 | return err; |
| 4530 | } |
| 4531 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4532 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4533 | { |
| 4534 | if (key->master != 0x00 && key->master != 0x01) |
| 4535 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4536 | |
| 4537 | switch (key->addr.type) { |
| 4538 | case BDADDR_LE_PUBLIC: |
| 4539 | return true; |
| 4540 | |
| 4541 | case BDADDR_LE_RANDOM: |
| 4542 | /* Two most significant bits shall be set */ |
| 4543 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4544 | return false; |
| 4545 | return true; |
| 4546 | } |
| 4547 | |
| 4548 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4549 | } |
| 4550 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4551 | 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] | 4552 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4553 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4554 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4555 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4556 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4557 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4558 | BT_DBG("request for %s", hdev->name); |
| 4559 | |
| 4560 | if (!lmp_le_capable(hdev)) |
| 4561 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4562 | MGMT_STATUS_NOT_SUPPORTED); |
| 4563 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4564 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4565 | |
| 4566 | expected_len = sizeof(*cp) + key_count * |
| 4567 | sizeof(struct mgmt_ltk_info); |
| 4568 | if (expected_len != len) { |
| 4569 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4570 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4571 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4572 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4573 | } |
| 4574 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4575 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4576 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4577 | for (i = 0; i < key_count; i++) { |
| 4578 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4579 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4580 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4581 | return cmd_status(sk, hdev->id, |
| 4582 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4583 | MGMT_STATUS_INVALID_PARAMS); |
| 4584 | } |
| 4585 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4586 | hci_dev_lock(hdev); |
| 4587 | |
| 4588 | hci_smp_ltks_clear(hdev); |
| 4589 | |
| 4590 | for (i = 0; i < key_count; i++) { |
| 4591 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4592 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4593 | |
| 4594 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4595 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4596 | else |
| 4597 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4598 | |
| 4599 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4600 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4601 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4602 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4603 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4604 | switch (key->type) { |
| 4605 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4606 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4607 | break; |
| 4608 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4609 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4610 | break; |
| 4611 | default: |
| 4612 | continue; |
| 4613 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4614 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4615 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4616 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4617 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4618 | } |
| 4619 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4620 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4621 | NULL, 0); |
| 4622 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4623 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4624 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4625 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4626 | } |
| 4627 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4628 | struct cmd_conn_lookup { |
| 4629 | struct hci_conn *conn; |
| 4630 | bool valid_tx_power; |
| 4631 | u8 mgmt_status; |
| 4632 | }; |
| 4633 | |
| 4634 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4635 | { |
| 4636 | struct cmd_conn_lookup *match = data; |
| 4637 | struct mgmt_cp_get_conn_info *cp; |
| 4638 | struct mgmt_rp_get_conn_info rp; |
| 4639 | struct hci_conn *conn = cmd->user_data; |
| 4640 | |
| 4641 | if (conn != match->conn) |
| 4642 | return; |
| 4643 | |
| 4644 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4645 | |
| 4646 | memset(&rp, 0, sizeof(rp)); |
| 4647 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4648 | rp.addr.type = cp->addr.type; |
| 4649 | |
| 4650 | if (!match->mgmt_status) { |
| 4651 | rp.rssi = conn->rssi; |
| 4652 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4653 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4654 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4655 | rp.max_tx_power = conn->max_tx_power; |
| 4656 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4657 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4658 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4659 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4663 | match->mgmt_status, &rp, sizeof(rp)); |
| 4664 | |
| 4665 | hci_conn_drop(conn); |
| 4666 | |
| 4667 | mgmt_pending_remove(cmd); |
| 4668 | } |
| 4669 | |
| 4670 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4671 | { |
| 4672 | struct hci_cp_read_rssi *cp; |
| 4673 | struct hci_conn *conn; |
| 4674 | struct cmd_conn_lookup match; |
| 4675 | u16 handle; |
| 4676 | |
| 4677 | BT_DBG("status 0x%02x", status); |
| 4678 | |
| 4679 | hci_dev_lock(hdev); |
| 4680 | |
| 4681 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4682 | * otherwise we assume it's not valid. At the moment we assume that |
| 4683 | * either both or none of current and max values are valid to keep code |
| 4684 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4685 | */ |
| 4686 | match.valid_tx_power = !status; |
| 4687 | |
| 4688 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4689 | * Level so we check which one was last sent to retrieve connection |
| 4690 | * handle. Both commands have handle as first parameter so it's safe to |
| 4691 | * cast data on the same command struct. |
| 4692 | * |
| 4693 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4694 | * In other case we simply override error to indicate success as we |
| 4695 | * already remembered if TX power value is actually valid. |
| 4696 | */ |
| 4697 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4698 | if (!cp) { |
| 4699 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4700 | status = 0; |
| 4701 | } |
| 4702 | |
| 4703 | if (!cp) { |
| 4704 | BT_ERR("invalid sent_cmd in response"); |
| 4705 | goto unlock; |
| 4706 | } |
| 4707 | |
| 4708 | handle = __le16_to_cpu(cp->handle); |
| 4709 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4710 | if (!conn) { |
| 4711 | BT_ERR("unknown handle (%d) in response", handle); |
| 4712 | goto unlock; |
| 4713 | } |
| 4714 | |
| 4715 | match.conn = conn; |
| 4716 | match.mgmt_status = mgmt_status(status); |
| 4717 | |
| 4718 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4719 | * connection only. |
| 4720 | */ |
| 4721 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4722 | get_conn_info_complete, &match); |
| 4723 | |
| 4724 | unlock: |
| 4725 | hci_dev_unlock(hdev); |
| 4726 | } |
| 4727 | |
| 4728 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4729 | u16 len) |
| 4730 | { |
| 4731 | struct mgmt_cp_get_conn_info *cp = data; |
| 4732 | struct mgmt_rp_get_conn_info rp; |
| 4733 | struct hci_conn *conn; |
| 4734 | unsigned long conn_info_age; |
| 4735 | int err = 0; |
| 4736 | |
| 4737 | BT_DBG("%s", hdev->name); |
| 4738 | |
| 4739 | memset(&rp, 0, sizeof(rp)); |
| 4740 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4741 | rp.addr.type = cp->addr.type; |
| 4742 | |
| 4743 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4744 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4745 | MGMT_STATUS_INVALID_PARAMS, |
| 4746 | &rp, sizeof(rp)); |
| 4747 | |
| 4748 | hci_dev_lock(hdev); |
| 4749 | |
| 4750 | if (!hdev_is_powered(hdev)) { |
| 4751 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4752 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4753 | goto unlock; |
| 4754 | } |
| 4755 | |
| 4756 | if (cp->addr.type == BDADDR_BREDR) |
| 4757 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4758 | &cp->addr.bdaddr); |
| 4759 | else |
| 4760 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4761 | |
| 4762 | if (!conn || conn->state != BT_CONNECTED) { |
| 4763 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4764 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4765 | goto unlock; |
| 4766 | } |
| 4767 | |
| 4768 | /* To avoid client trying to guess when to poll again for information we |
| 4769 | * calculate conn info age as random value between min/max set in hdev. |
| 4770 | */ |
| 4771 | conn_info_age = hdev->conn_info_min_age + |
| 4772 | prandom_u32_max(hdev->conn_info_max_age - |
| 4773 | hdev->conn_info_min_age); |
| 4774 | |
| 4775 | /* Query controller to refresh cached values if they are too old or were |
| 4776 | * never read. |
| 4777 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4778 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4779 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4780 | !conn->conn_info_timestamp) { |
| 4781 | struct hci_request req; |
| 4782 | struct hci_cp_read_tx_power req_txp_cp; |
| 4783 | struct hci_cp_read_rssi req_rssi_cp; |
| 4784 | struct pending_cmd *cmd; |
| 4785 | |
| 4786 | hci_req_init(&req, hdev); |
| 4787 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4788 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4789 | &req_rssi_cp); |
| 4790 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4791 | /* For LE links TX power does not change thus we don't need to |
| 4792 | * query for it once value is known. |
| 4793 | */ |
| 4794 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4795 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4796 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4797 | req_txp_cp.type = 0x00; |
| 4798 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4799 | sizeof(req_txp_cp), &req_txp_cp); |
| 4800 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4801 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4802 | /* Max TX power needs to be read only once per connection */ |
| 4803 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4804 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4805 | req_txp_cp.type = 0x01; |
| 4806 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4807 | sizeof(req_txp_cp), &req_txp_cp); |
| 4808 | } |
| 4809 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4810 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4811 | if (err < 0) |
| 4812 | goto unlock; |
| 4813 | |
| 4814 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4815 | data, len); |
| 4816 | if (!cmd) { |
| 4817 | err = -ENOMEM; |
| 4818 | goto unlock; |
| 4819 | } |
| 4820 | |
| 4821 | hci_conn_hold(conn); |
| 4822 | cmd->user_data = conn; |
| 4823 | |
| 4824 | conn->conn_info_timestamp = jiffies; |
| 4825 | } else { |
| 4826 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4827 | rp.rssi = conn->rssi; |
| 4828 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4829 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4830 | |
| 4831 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4832 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4833 | } |
| 4834 | |
| 4835 | unlock: |
| 4836 | hci_dev_unlock(hdev); |
| 4837 | return err; |
| 4838 | } |
| 4839 | |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame^] | 4840 | static void get_clock_info_complete(struct hci_dev *hdev, u8 status) |
| 4841 | { |
| 4842 | struct mgmt_cp_get_clock_info *cp; |
| 4843 | struct mgmt_rp_get_clock_info rp; |
| 4844 | struct hci_cp_read_clock *hci_cp; |
| 4845 | struct pending_cmd *cmd; |
| 4846 | struct hci_conn *conn; |
| 4847 | |
| 4848 | BT_DBG("%s status %u", hdev->name, status); |
| 4849 | |
| 4850 | hci_dev_lock(hdev); |
| 4851 | |
| 4852 | hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK); |
| 4853 | if (!hci_cp) |
| 4854 | goto unlock; |
| 4855 | |
| 4856 | if (hci_cp->which) { |
| 4857 | u16 handle = __le16_to_cpu(hci_cp->handle); |
| 4858 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4859 | } else { |
| 4860 | conn = NULL; |
| 4861 | } |
| 4862 | |
| 4863 | cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); |
| 4864 | if (!cmd) |
| 4865 | goto unlock; |
| 4866 | |
| 4867 | cp = cmd->param; |
| 4868 | |
| 4869 | memset(&rp, 0, sizeof(rp)); |
| 4870 | memcpy(&rp.addr, &cp->addr, sizeof(rp.addr)); |
| 4871 | |
| 4872 | if (status) |
| 4873 | goto send_rsp; |
| 4874 | |
| 4875 | rp.local_clock = cpu_to_le32(hdev->clock); |
| 4876 | |
| 4877 | if (conn) { |
| 4878 | rp.piconet_clock = cpu_to_le32(conn->clock); |
| 4879 | rp.accuracy = cpu_to_le16(conn->clock_accuracy); |
| 4880 | } |
| 4881 | |
| 4882 | send_rsp: |
| 4883 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 4884 | &rp, sizeof(rp)); |
| 4885 | mgmt_pending_remove(cmd); |
| 4886 | if (conn) |
| 4887 | hci_conn_drop(conn); |
| 4888 | |
| 4889 | unlock: |
| 4890 | hci_dev_unlock(hdev); |
| 4891 | } |
| 4892 | |
| 4893 | static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4894 | u16 len) |
| 4895 | { |
| 4896 | struct mgmt_cp_get_clock_info *cp = data; |
| 4897 | struct mgmt_rp_get_clock_info rp; |
| 4898 | struct hci_cp_read_clock hci_cp; |
| 4899 | struct pending_cmd *cmd; |
| 4900 | struct hci_request req; |
| 4901 | struct hci_conn *conn; |
| 4902 | int err; |
| 4903 | |
| 4904 | BT_DBG("%s", hdev->name); |
| 4905 | |
| 4906 | memset(&rp, 0, sizeof(rp)); |
| 4907 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4908 | rp.addr.type = cp->addr.type; |
| 4909 | |
| 4910 | if (cp->addr.type != BDADDR_BREDR) |
| 4911 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4912 | MGMT_STATUS_INVALID_PARAMS, |
| 4913 | &rp, sizeof(rp)); |
| 4914 | |
| 4915 | hci_dev_lock(hdev); |
| 4916 | |
| 4917 | if (!hdev_is_powered(hdev)) { |
| 4918 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, |
| 4919 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4920 | goto unlock; |
| 4921 | } |
| 4922 | |
| 4923 | if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) { |
| 4924 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4925 | &cp->addr.bdaddr); |
| 4926 | if (!conn || conn->state != BT_CONNECTED) { |
| 4927 | err = cmd_complete(sk, hdev->id, |
| 4928 | MGMT_OP_GET_CLOCK_INFO, |
| 4929 | MGMT_STATUS_NOT_CONNECTED, |
| 4930 | &rp, sizeof(rp)); |
| 4931 | goto unlock; |
| 4932 | } |
| 4933 | } else { |
| 4934 | conn = NULL; |
| 4935 | } |
| 4936 | |
| 4937 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len); |
| 4938 | if (!cmd) { |
| 4939 | err = -ENOMEM; |
| 4940 | goto unlock; |
| 4941 | } |
| 4942 | |
| 4943 | hci_req_init(&req, hdev); |
| 4944 | |
| 4945 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 4946 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4947 | |
| 4948 | if (conn) { |
| 4949 | hci_conn_hold(conn); |
| 4950 | cmd->user_data = conn; |
| 4951 | |
| 4952 | hci_cp.handle = cpu_to_le16(conn->handle); |
| 4953 | hci_cp.which = 0x01; /* Piconet clock */ |
| 4954 | hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp); |
| 4955 | } |
| 4956 | |
| 4957 | err = hci_req_run(&req, get_clock_info_complete); |
| 4958 | if (err < 0) |
| 4959 | mgmt_pending_remove(cmd); |
| 4960 | |
| 4961 | unlock: |
| 4962 | hci_dev_unlock(hdev); |
| 4963 | return err; |
| 4964 | } |
| 4965 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4966 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4967 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 4968 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4969 | bool var_len; |
| 4970 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4971 | } mgmt_handlers[] = { |
| 4972 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4973 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 4974 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 4975 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 4976 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 4977 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 4978 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 4979 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 4980 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 4981 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 4982 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 4983 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 4984 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 4985 | { set_le, false, MGMT_SETTING_SIZE }, |
| 4986 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 4987 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 4988 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 4989 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 4990 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 4991 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 4992 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 4993 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 4994 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 4995 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 4996 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 4997 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 4998 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 4999 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 5000 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 5001 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 5002 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 5003 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 5004 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 5005 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5006 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 5007 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 5008 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 5009 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 5010 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 5011 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 5012 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 5013 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 5014 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5015 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 5016 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5017 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 5018 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 5019 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 5020 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 5021 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 9586842 | 2014-06-28 17:54:07 +0300 | [diff] [blame^] | 5022 | { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5023 | }; |
| 5024 | |
| 5025 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5026 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 5027 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5028 | void *buf; |
| 5029 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5030 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 5031 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5032 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 5033 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5034 | int err; |
| 5035 | |
| 5036 | BT_DBG("got %zu bytes", msglen); |
| 5037 | |
| 5038 | if (msglen < sizeof(*hdr)) |
| 5039 | return -EINVAL; |
| 5040 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 5041 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5042 | if (!buf) |
| 5043 | return -ENOMEM; |
| 5044 | |
| 5045 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 5046 | err = -EFAULT; |
| 5047 | goto done; |
| 5048 | } |
| 5049 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 5050 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 5051 | opcode = __le16_to_cpu(hdr->opcode); |
| 5052 | index = __le16_to_cpu(hdr->index); |
| 5053 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5054 | |
| 5055 | if (len != msglen - sizeof(*hdr)) { |
| 5056 | err = -EINVAL; |
| 5057 | goto done; |
| 5058 | } |
| 5059 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5060 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5061 | hdev = hci_dev_get(index); |
| 5062 | if (!hdev) { |
| 5063 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5064 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5065 | goto done; |
| 5066 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5067 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 5068 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
| 5069 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 5070 | err = cmd_status(sk, index, opcode, |
| 5071 | MGMT_STATUS_INVALID_INDEX); |
| 5072 | goto done; |
| 5073 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5074 | } |
| 5075 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5076 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5077 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5078 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5079 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5080 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5081 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5082 | } |
| 5083 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5084 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5085 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5086 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5087 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5088 | goto done; |
| 5089 | } |
| 5090 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5091 | handler = &mgmt_handlers[opcode]; |
| 5092 | |
| 5093 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5094 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5095 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5096 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5097 | goto done; |
| 5098 | } |
| 5099 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 5100 | if (hdev) |
| 5101 | mgmt_init_hdev(sk, hdev); |
| 5102 | |
| 5103 | cp = buf + sizeof(*hdr); |
| 5104 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 5105 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 5106 | if (err < 0) |
| 5107 | goto done; |
| 5108 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5109 | err = msglen; |
| 5110 | |
| 5111 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 5112 | if (hdev) |
| 5113 | hci_dev_put(hdev); |
| 5114 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 5115 | kfree(buf); |
| 5116 | return err; |
| 5117 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5118 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5119 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5120 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5121 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5122 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5123 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5124 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5125 | } |
| 5126 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5127 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 5128 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 5129 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5130 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 5131 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5132 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 5133 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5134 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5135 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 5136 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5137 | } |
| 5138 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5139 | /* This function requires the caller holds hdev->lock */ |
| 5140 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 5141 | { |
| 5142 | struct hci_conn_params *p; |
| 5143 | |
| 5144 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
| 5145 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) |
| 5146 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
| 5147 | } |
| 5148 | } |
| 5149 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5150 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5151 | { |
| 5152 | struct cmd_lookup match = { NULL, hdev }; |
| 5153 | |
| 5154 | BT_DBG("status 0x%02x", status); |
| 5155 | |
| 5156 | hci_dev_lock(hdev); |
| 5157 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5158 | restart_le_auto_conns(hdev); |
| 5159 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5160 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5161 | |
| 5162 | new_settings(hdev, match.sk); |
| 5163 | |
| 5164 | hci_dev_unlock(hdev); |
| 5165 | |
| 5166 | if (match.sk) |
| 5167 | sock_put(match.sk); |
| 5168 | } |
| 5169 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5170 | static int powered_update_hci(struct hci_dev *hdev) |
| 5171 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5172 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5173 | u8 link_sec; |
| 5174 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5175 | hci_req_init(&req, hdev); |
| 5176 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5177 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5178 | !lmp_host_ssp_capable(hdev)) { |
| 5179 | u8 ssp = 1; |
| 5180 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5181 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5182 | } |
| 5183 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5184 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5185 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5186 | struct hci_cp_write_le_host_supported cp; |
| 5187 | |
| 5188 | cp.le = 1; |
| 5189 | cp.simul = lmp_le_br_capable(hdev); |
| 5190 | |
| 5191 | /* Check first if we already have the right |
| 5192 | * host state (host features set) |
| 5193 | */ |
| 5194 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5195 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5196 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5197 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5198 | } |
| 5199 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5200 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5201 | /* Make sure the controller has a good default for |
| 5202 | * advertising data. This also applies to the case |
| 5203 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5204 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5205 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5206 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5207 | update_scan_rsp_data(&req); |
| 5208 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5209 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5210 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5211 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5212 | } |
| 5213 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5214 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5215 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5216 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5217 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5218 | |
| 5219 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5220 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5221 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5222 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5223 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5224 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5225 | } |
| 5226 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5227 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5228 | } |
| 5229 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5230 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5231 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5232 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5233 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5234 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5235 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5236 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5237 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5238 | return 0; |
| 5239 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5240 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5241 | if (powered_update_hci(hdev) == 0) |
| 5242 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5243 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5244 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5245 | &match); |
| 5246 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5247 | } |
| 5248 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5249 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5250 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5251 | |
| 5252 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5253 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5254 | zero_cod, sizeof(zero_cod), NULL); |
| 5255 | |
| 5256 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5257 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5258 | |
| 5259 | if (match.sk) |
| 5260 | sock_put(match.sk); |
| 5261 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5262 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5263 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5264 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5265 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5266 | { |
| 5267 | struct pending_cmd *cmd; |
| 5268 | u8 status; |
| 5269 | |
| 5270 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5271 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5272 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5273 | |
| 5274 | if (err == -ERFKILL) |
| 5275 | status = MGMT_STATUS_RFKILLED; |
| 5276 | else |
| 5277 | status = MGMT_STATUS_FAILED; |
| 5278 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5279 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5280 | |
| 5281 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5282 | } |
| 5283 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5284 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5285 | { |
| 5286 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5287 | |
| 5288 | hci_dev_lock(hdev); |
| 5289 | |
| 5290 | /* When discoverable timeout triggers, then just make sure |
| 5291 | * the limited discoverable flag is cleared. Even in the case |
| 5292 | * of a timeout triggered from general discoverable, it is |
| 5293 | * safe to unconditionally clear the flag. |
| 5294 | */ |
| 5295 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5296 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5297 | |
| 5298 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5299 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5300 | u8 scan = SCAN_PAGE; |
| 5301 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5302 | sizeof(scan), &scan); |
| 5303 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5304 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5305 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5306 | hci_req_run(&req, NULL); |
| 5307 | |
| 5308 | hdev->discov_timeout = 0; |
| 5309 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5310 | new_settings(hdev, NULL); |
| 5311 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5312 | hci_dev_unlock(hdev); |
| 5313 | } |
| 5314 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5315 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5316 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5317 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5318 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5319 | /* Nothing needed here if there's a pending command since that |
| 5320 | * commands request completion callback takes care of everything |
| 5321 | * necessary. |
| 5322 | */ |
| 5323 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5324 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5325 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5326 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5327 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5328 | return; |
| 5329 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5330 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5331 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5332 | } else { |
| 5333 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5334 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5335 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5336 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5337 | if (changed) { |
| 5338 | struct hci_request req; |
| 5339 | |
| 5340 | /* In case this change in discoverable was triggered by |
| 5341 | * a disabling of connectable there could be a need to |
| 5342 | * update the advertising flags. |
| 5343 | */ |
| 5344 | hci_req_init(&req, hdev); |
| 5345 | update_adv_data(&req); |
| 5346 | hci_req_run(&req, NULL); |
| 5347 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5348 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5349 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5350 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5351 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5352 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5353 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5354 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5355 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5356 | /* Nothing needed here if there's a pending command since that |
| 5357 | * commands request completion callback takes care of everything |
| 5358 | * necessary. |
| 5359 | */ |
| 5360 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5361 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5362 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5363 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5364 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5365 | return; |
| 5366 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5367 | if (connectable) |
| 5368 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5369 | else |
| 5370 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5371 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5372 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5373 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5374 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5375 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5376 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5377 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5378 | /* Powering off may stop advertising - don't let that interfere */ |
| 5379 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5380 | return; |
| 5381 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5382 | if (advertising) |
| 5383 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5384 | else |
| 5385 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5386 | } |
| 5387 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5388 | 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] | 5389 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5390 | u8 mgmt_err = mgmt_status(status); |
| 5391 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5392 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5393 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5394 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5395 | |
| 5396 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5397 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5398 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5399 | } |
| 5400 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5401 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5402 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5403 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5404 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5405 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5406 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5407 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5408 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5409 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5410 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5411 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5412 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5413 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5414 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5415 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5416 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5417 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5418 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5419 | { |
| 5420 | if (ltk->authenticated) |
| 5421 | return MGMT_LTK_AUTHENTICATED; |
| 5422 | |
| 5423 | return MGMT_LTK_UNAUTHENTICATED; |
| 5424 | } |
| 5425 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5426 | void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5427 | { |
| 5428 | struct mgmt_ev_new_long_term_key ev; |
| 5429 | |
| 5430 | memset(&ev, 0, sizeof(ev)); |
| 5431 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5432 | /* Devices using resolvable or non-resolvable random addresses |
| 5433 | * without providing an indentity resolving key don't require |
| 5434 | * to store long term keys. Their addresses will change the |
| 5435 | * next time around. |
| 5436 | * |
| 5437 | * Only when a remote device provides an identity address |
| 5438 | * make sure the long term key is stored. If the remote |
| 5439 | * identity is known, the long term keys are internally |
| 5440 | * mapped to the identity address. So allow static random |
| 5441 | * and public addresses here. |
| 5442 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5443 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5444 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5445 | ev.store_hint = 0x00; |
| 5446 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5447 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5448 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5449 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5450 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5451 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5452 | ev.key.enc_size = key->enc_size; |
| 5453 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5454 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5455 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5456 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5457 | ev.key.master = 1; |
| 5458 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5459 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5460 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5461 | 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] | 5462 | } |
| 5463 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5464 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5465 | { |
| 5466 | struct mgmt_ev_new_irk ev; |
| 5467 | |
| 5468 | memset(&ev, 0, sizeof(ev)); |
| 5469 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5470 | /* For identity resolving keys from devices that are already |
| 5471 | * using a public address or static random address, do not |
| 5472 | * ask for storing this key. The identity resolving key really |
| 5473 | * is only mandatory for devices using resovlable random |
| 5474 | * addresses. |
| 5475 | * |
| 5476 | * Storing all identity resolving keys has the downside that |
| 5477 | * they will be also loaded on next boot of they system. More |
| 5478 | * identity resolving keys, means more time during scanning is |
| 5479 | * needed to actually resolve these addresses. |
| 5480 | */ |
| 5481 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5482 | ev.store_hint = 0x01; |
| 5483 | else |
| 5484 | ev.store_hint = 0x00; |
| 5485 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5486 | bacpy(&ev.rpa, &irk->rpa); |
| 5487 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5488 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5489 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5490 | |
| 5491 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5492 | } |
| 5493 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5494 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5495 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5496 | { |
| 5497 | struct mgmt_ev_new_csrk ev; |
| 5498 | |
| 5499 | memset(&ev, 0, sizeof(ev)); |
| 5500 | |
| 5501 | /* Devices using resolvable or non-resolvable random addresses |
| 5502 | * without providing an indentity resolving key don't require |
| 5503 | * to store signature resolving keys. Their addresses will change |
| 5504 | * the next time around. |
| 5505 | * |
| 5506 | * Only when a remote device provides an identity address |
| 5507 | * make sure the signature resolving key is stored. So allow |
| 5508 | * static random and public addresses here. |
| 5509 | */ |
| 5510 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5511 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5512 | ev.store_hint = 0x00; |
| 5513 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5514 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5515 | |
| 5516 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5517 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5518 | ev.key.master = csrk->master; |
| 5519 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5520 | |
| 5521 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5522 | } |
| 5523 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5524 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5525 | u8 data_len) |
| 5526 | { |
| 5527 | eir[eir_len++] = sizeof(type) + data_len; |
| 5528 | eir[eir_len++] = type; |
| 5529 | memcpy(&eir[eir_len], data, data_len); |
| 5530 | eir_len += data_len; |
| 5531 | |
| 5532 | return eir_len; |
| 5533 | } |
| 5534 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5535 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5536 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5537 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5538 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5539 | char buf[512]; |
| 5540 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5541 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5542 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5543 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5544 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5545 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5546 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5547 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5548 | if (name_len > 0) |
| 5549 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5550 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5551 | |
| 5552 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5553 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5554 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5555 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5556 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5557 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5558 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5559 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5560 | } |
| 5561 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5562 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5563 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5564 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5565 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5566 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5567 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5568 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5569 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5570 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5571 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5572 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5573 | |
| 5574 | *sk = cmd->sk; |
| 5575 | sock_hold(*sk); |
| 5576 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5577 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5578 | } |
| 5579 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5580 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5581 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5582 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5583 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5584 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5585 | |
| 5586 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5587 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5588 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5589 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5590 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5591 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5592 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5593 | |
| 5594 | mgmt_pending_remove(cmd); |
| 5595 | } |
| 5596 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5597 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5598 | u8 link_type, u8 addr_type, u8 reason, |
| 5599 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5600 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5601 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5602 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5603 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5604 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5605 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5606 | if (power_off) { |
| 5607 | struct mgmt_mode *cp = power_off->param; |
| 5608 | |
| 5609 | /* The connection is still in hci_conn_hash so test for 1 |
| 5610 | * instead of 0 to know if this is the last one. |
| 5611 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5612 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5613 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5614 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5615 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5616 | } |
| 5617 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5618 | if (!mgmt_connected) |
| 5619 | return; |
| 5620 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5621 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5622 | return; |
| 5623 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5624 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5625 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5626 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5627 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5628 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5629 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5630 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5631 | |
| 5632 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 5633 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5634 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5635 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5636 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5637 | } |
| 5638 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5639 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5640 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5641 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5642 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 5643 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5644 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5645 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5646 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 5647 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 5648 | hdev); |
| 5649 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5650 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5651 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5652 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5653 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5654 | cp = cmd->param; |
| 5655 | |
| 5656 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 5657 | return; |
| 5658 | |
| 5659 | if (cp->addr.type != bdaddr_type) |
| 5660 | return; |
| 5661 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5662 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5663 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 5664 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5665 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 5666 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5667 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5668 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5669 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5670 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5671 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5672 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5673 | { |
| 5674 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5675 | struct pending_cmd *power_off; |
| 5676 | |
| 5677 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5678 | if (power_off) { |
| 5679 | struct mgmt_mode *cp = power_off->param; |
| 5680 | |
| 5681 | /* The connection is still in hci_conn_hash so test for 1 |
| 5682 | * instead of 0 to know if this is the last one. |
| 5683 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5684 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5685 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5686 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5687 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5688 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5689 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 5690 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5691 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5692 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5693 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5694 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5695 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5696 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5697 | 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] | 5698 | { |
| 5699 | struct mgmt_ev_pin_code_request ev; |
| 5700 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5701 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5702 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 5703 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5704 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5705 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5706 | } |
| 5707 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5708 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5709 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5710 | { |
| 5711 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5712 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5713 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5714 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5715 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5716 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5717 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5718 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5719 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5720 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5721 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 5722 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5723 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5724 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5725 | } |
| 5726 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5727 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5728 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5729 | { |
| 5730 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5731 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5732 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5733 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5734 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5735 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5736 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5737 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5738 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5739 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5740 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 5741 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5742 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5743 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5744 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5745 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5746 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5747 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5748 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5749 | { |
| 5750 | struct mgmt_ev_user_confirm_request ev; |
| 5751 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5752 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5753 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5754 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5755 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 5756 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5757 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5758 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5759 | 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] | 5760 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5761 | } |
| 5762 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5763 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5764 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5765 | { |
| 5766 | struct mgmt_ev_user_passkey_request ev; |
| 5767 | |
| 5768 | BT_DBG("%s", hdev->name); |
| 5769 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5770 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5771 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5772 | |
| 5773 | 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] | 5774 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5775 | } |
| 5776 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 5777 | 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] | 5778 | u8 link_type, u8 addr_type, u8 status, |
| 5779 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5780 | { |
| 5781 | struct pending_cmd *cmd; |
| 5782 | struct mgmt_rp_user_confirm_reply rp; |
| 5783 | int err; |
| 5784 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5785 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5786 | if (!cmd) |
| 5787 | return -ENOENT; |
| 5788 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5789 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5790 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5791 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5792 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5793 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5794 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5795 | |
| 5796 | return err; |
| 5797 | } |
| 5798 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5799 | 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] | 5800 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5801 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5802 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5803 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5804 | } |
| 5805 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5806 | 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] | 5807 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5808 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5809 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5810 | status, |
| 5811 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5812 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5813 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5814 | 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] | 5815 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5816 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5817 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5818 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5819 | } |
| 5820 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5821 | 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] | 5822 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5823 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5824 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5825 | status, |
| 5826 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5827 | } |
| 5828 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 5829 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5830 | u8 link_type, u8 addr_type, u32 passkey, |
| 5831 | u8 entered) |
| 5832 | { |
| 5833 | struct mgmt_ev_passkey_notify ev; |
| 5834 | |
| 5835 | BT_DBG("%s", hdev->name); |
| 5836 | |
| 5837 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5838 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5839 | ev.passkey = __cpu_to_le32(passkey); |
| 5840 | ev.entered = entered; |
| 5841 | |
| 5842 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 5843 | } |
| 5844 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5845 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5846 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5847 | { |
| 5848 | struct mgmt_ev_auth_failed ev; |
| 5849 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 5850 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5851 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5852 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5853 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5854 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5855 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5856 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5857 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5858 | { |
| 5859 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5860 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5861 | |
| 5862 | if (status) { |
| 5863 | u8 mgmt_err = mgmt_status(status); |
| 5864 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5865 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5866 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5867 | } |
| 5868 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5869 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 5870 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 5871 | &hdev->dev_flags); |
| 5872 | else |
| 5873 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 5874 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5875 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5876 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5877 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5878 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5879 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5880 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5881 | |
| 5882 | if (match.sk) |
| 5883 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5884 | } |
| 5885 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5886 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5887 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5888 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5889 | struct hci_cp_write_eir cp; |
| 5890 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 5891 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5892 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5893 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 5894 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 5895 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5896 | memset(&cp, 0, sizeof(cp)); |
| 5897 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5898 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5899 | } |
| 5900 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5901 | 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] | 5902 | { |
| 5903 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5904 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5905 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5906 | |
| 5907 | if (status) { |
| 5908 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5909 | |
| 5910 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5911 | &hdev->dev_flags)) { |
| 5912 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5913 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5914 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5915 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5916 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 5917 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5918 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5919 | } |
| 5920 | |
| 5921 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5922 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5923 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5924 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 5925 | if (!changed) |
| 5926 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 5927 | &hdev->dev_flags); |
| 5928 | else |
| 5929 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5930 | } |
| 5931 | |
| 5932 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 5933 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5934 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5935 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5936 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5937 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5938 | sock_put(match.sk); |
| 5939 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5940 | hci_req_init(&req, hdev); |
| 5941 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 5942 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 5943 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 5944 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 5945 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5946 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 5947 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5948 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 5949 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5950 | |
| 5951 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5952 | } |
| 5953 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5954 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 5955 | { |
| 5956 | struct cmd_lookup match = { NULL, hdev }; |
| 5957 | bool changed = false; |
| 5958 | |
| 5959 | if (status) { |
| 5960 | u8 mgmt_err = mgmt_status(status); |
| 5961 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5962 | if (enable) { |
| 5963 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 5964 | &hdev->dev_flags)) |
| 5965 | new_settings(hdev, NULL); |
| 5966 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5967 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5968 | |
| 5969 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5970 | cmd_status_rsp, &mgmt_err); |
| 5971 | return; |
| 5972 | } |
| 5973 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5974 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5975 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5976 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5977 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5978 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5979 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5980 | |
| 5981 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5982 | settings_rsp, &match); |
| 5983 | |
| 5984 | if (changed) |
| 5985 | new_settings(hdev, match.sk); |
| 5986 | |
| 5987 | if (match.sk) |
| 5988 | sock_put(match.sk); |
| 5989 | } |
| 5990 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5991 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5992 | { |
| 5993 | struct cmd_lookup *match = data; |
| 5994 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5995 | if (match->sk == NULL) { |
| 5996 | match->sk = cmd->sk; |
| 5997 | sock_hold(match->sk); |
| 5998 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5999 | } |
| 6000 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6001 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 6002 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6003 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6004 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6005 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 6006 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 6007 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 6008 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6009 | |
| 6010 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 6011 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 6012 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 6013 | |
| 6014 | if (match.sk) |
| 6015 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 6016 | } |
| 6017 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6018 | 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] | 6019 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6020 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6021 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6022 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6023 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6024 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6025 | |
| 6026 | memset(&ev, 0, sizeof(ev)); |
| 6027 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 6028 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6029 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6030 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6031 | if (!cmd) { |
| 6032 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6033 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 6034 | /* If this is a HCI command related to powering on the |
| 6035 | * HCI dev don't send any mgmt signals. |
| 6036 | */ |
| 6037 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6038 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6039 | } |
| 6040 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 6041 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 6042 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 6043 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6044 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6045 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 6046 | u8 *randomizer192, u8 *hash256, |
| 6047 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6048 | { |
| 6049 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6050 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6051 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6052 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6053 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6054 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6055 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6056 | |
| 6057 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 6058 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 6059 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6060 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6061 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 6062 | hash256 && randomizer256) { |
| 6063 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6064 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6065 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 6066 | memcpy(rp.randomizer192, randomizer192, |
| 6067 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6068 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 6069 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 6070 | memcpy(rp.randomizer256, randomizer256, |
| 6071 | sizeof(rp.randomizer256)); |
| 6072 | |
| 6073 | cmd_complete(cmd->sk, hdev->id, |
| 6074 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6075 | &rp, sizeof(rp)); |
| 6076 | } else { |
| 6077 | struct mgmt_rp_read_local_oob_data rp; |
| 6078 | |
| 6079 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 6080 | memcpy(rp.randomizer, randomizer192, |
| 6081 | sizeof(rp.randomizer)); |
| 6082 | |
| 6083 | cmd_complete(cmd->sk, hdev->id, |
| 6084 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 6085 | &rp, sizeof(rp)); |
| 6086 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6087 | } |
| 6088 | |
| 6089 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 6090 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6091 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6092 | void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
Johan Hedberg | 73cf71d | 2014-03-25 12:06:19 +0200 | [diff] [blame] | 6093 | u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, |
| 6094 | u8 ssp, u8 *eir, u16 eir_len, u8 *scan_rsp, |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6095 | u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6096 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6097 | char buf[512]; |
| 6098 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6099 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6100 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6101 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6102 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6103 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 6104 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6105 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 6106 | * are for the potential CoD field. |
| 6107 | */ |
| 6108 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6109 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 6110 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6111 | memset(buf, 0, sizeof(buf)); |
| 6112 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 6113 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 6114 | if (irk) { |
| 6115 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 6116 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 6117 | } else { |
| 6118 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 6119 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 6120 | } |
| 6121 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6122 | ev->rssi = rssi; |
Johan Hedberg | 9a395a8 | 2012-02-23 00:00:32 +0200 | [diff] [blame] | 6123 | if (cfm_name) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 6124 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME); |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 6125 | if (!ssp) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 6126 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6127 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6128 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 6129 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6130 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6131 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 6132 | 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] | 6133 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 6134 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 6135 | if (scan_rsp_len > 0) |
| 6136 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 6137 | |
| 6138 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 6139 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 6140 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 6141 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 6142 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6143 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6144 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6145 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6146 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6147 | struct mgmt_ev_device_found *ev; |
| 6148 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6149 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6150 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6151 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6152 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6153 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6154 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6155 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6156 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6157 | ev->rssi = rssi; |
| 6158 | |
| 6159 | 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] | 6160 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6161 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6162 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6163 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6164 | 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] | 6165 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6166 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6167 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6168 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6169 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6170 | struct pending_cmd *cmd; |
| 6171 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6172 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6173 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6174 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6175 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6176 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6177 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6178 | |
| 6179 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6180 | u8 type = hdev->discovery.type; |
| 6181 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6182 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6183 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6184 | mgmt_pending_remove(cmd); |
| 6185 | } |
| 6186 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6187 | memset(&ev, 0, sizeof(ev)); |
| 6188 | ev.type = hdev->discovery.type; |
| 6189 | ev.discovering = discovering; |
| 6190 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6191 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6192 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6193 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6194 | 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] | 6195 | { |
| 6196 | struct pending_cmd *cmd; |
| 6197 | struct mgmt_ev_device_blocked ev; |
| 6198 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6199 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6200 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6201 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6202 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6203 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6204 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6205 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6206 | } |
| 6207 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6208 | 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] | 6209 | { |
| 6210 | struct pending_cmd *cmd; |
| 6211 | struct mgmt_ev_device_unblocked ev; |
| 6212 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6213 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6214 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6215 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6216 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6217 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6218 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6219 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6220 | } |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6221 | |
| 6222 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6223 | { |
| 6224 | BT_DBG("%s status %u", hdev->name, status); |
| 6225 | |
| 6226 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6227 | if (status) { |
| 6228 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6229 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6230 | } |
| 6231 | } |
| 6232 | |
| 6233 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6234 | { |
| 6235 | struct hci_request req; |
| 6236 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6237 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6238 | return; |
| 6239 | |
| 6240 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6241 | return; |
| 6242 | |
| 6243 | hci_req_init(&req, hdev); |
| 6244 | enable_advertising(&req); |
| 6245 | |
| 6246 | /* If this fails we have no option but to let user space know |
| 6247 | * that we've disabled advertising. |
| 6248 | */ |
| 6249 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6250 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6251 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6252 | } |
| 6253 | } |