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 | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static const u16 mgmt_events[] = { |
| 91 | MGMT_EV_CONTROLLER_ERROR, |
| 92 | MGMT_EV_INDEX_ADDED, |
| 93 | MGMT_EV_INDEX_REMOVED, |
| 94 | MGMT_EV_NEW_SETTINGS, |
| 95 | MGMT_EV_CLASS_OF_DEV_CHANGED, |
| 96 | MGMT_EV_LOCAL_NAME_CHANGED, |
| 97 | MGMT_EV_NEW_LINK_KEY, |
| 98 | MGMT_EV_NEW_LONG_TERM_KEY, |
| 99 | MGMT_EV_DEVICE_CONNECTED, |
| 100 | MGMT_EV_DEVICE_DISCONNECTED, |
| 101 | MGMT_EV_CONNECT_FAILED, |
| 102 | MGMT_EV_PIN_CODE_REQUEST, |
| 103 | MGMT_EV_USER_CONFIRM_REQUEST, |
| 104 | MGMT_EV_USER_PASSKEY_REQUEST, |
| 105 | MGMT_EV_AUTH_FAILED, |
| 106 | MGMT_EV_DEVICE_FOUND, |
| 107 | MGMT_EV_DISCOVERING, |
| 108 | MGMT_EV_DEVICE_BLOCKED, |
| 109 | MGMT_EV_DEVICE_UNBLOCKED, |
| 110 | MGMT_EV_DEVICE_UNPAIRED, |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 111 | MGMT_EV_PASSKEY_NOTIFY, |
Marcel Holtmann | 1b60ef2 | 2014-02-21 21:35:30 -0800 | [diff] [blame] | 112 | MGMT_EV_NEW_IRK, |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 113 | MGMT_EV_NEW_CSRK, |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
Marcel Holtmann | 17b02e6 | 2012-03-01 14:32:37 -0800 | [diff] [blame] | 116 | #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 117 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 118 | #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \ |
| 119 | !test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) |
| 120 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 121 | struct pending_cmd { |
| 122 | struct list_head list; |
Johan Hedberg | fc2f4b1 | 2011-11-09 13:58:56 +0200 | [diff] [blame] | 123 | u16 opcode; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 124 | int index; |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 125 | void *param; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 126 | struct sock *sk; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 127 | void *user_data; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 128 | }; |
| 129 | |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 130 | /* HCI to MGMT error code conversion table */ |
| 131 | static u8 mgmt_status_table[] = { |
| 132 | MGMT_STATUS_SUCCESS, |
| 133 | MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */ |
| 134 | MGMT_STATUS_NOT_CONNECTED, /* No Connection */ |
| 135 | MGMT_STATUS_FAILED, /* Hardware Failure */ |
| 136 | MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */ |
| 137 | MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */ |
Johan Hedberg | eadd663 | 2014-01-13 17:15:53 +0200 | [diff] [blame] | 138 | MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */ |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 139 | MGMT_STATUS_NO_RESOURCES, /* Memory Full */ |
| 140 | MGMT_STATUS_TIMEOUT, /* Connection Timeout */ |
| 141 | MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */ |
| 142 | MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */ |
| 143 | MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */ |
| 144 | MGMT_STATUS_BUSY, /* Command Disallowed */ |
| 145 | MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */ |
| 146 | MGMT_STATUS_REJECTED, /* Rejected Security */ |
| 147 | MGMT_STATUS_REJECTED, /* Rejected Personal */ |
| 148 | MGMT_STATUS_TIMEOUT, /* Host Timeout */ |
| 149 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */ |
| 150 | MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */ |
| 151 | MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */ |
| 152 | MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */ |
| 153 | MGMT_STATUS_DISCONNECTED, /* OE Power Off */ |
| 154 | MGMT_STATUS_DISCONNECTED, /* Connection Terminated */ |
| 155 | MGMT_STATUS_BUSY, /* Repeated Attempts */ |
| 156 | MGMT_STATUS_REJECTED, /* Pairing Not Allowed */ |
| 157 | MGMT_STATUS_FAILED, /* Unknown LMP PDU */ |
| 158 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */ |
| 159 | MGMT_STATUS_REJECTED, /* SCO Offset Rejected */ |
| 160 | MGMT_STATUS_REJECTED, /* SCO Interval Rejected */ |
| 161 | MGMT_STATUS_REJECTED, /* Air Mode Rejected */ |
| 162 | MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */ |
| 163 | MGMT_STATUS_FAILED, /* Unspecified Error */ |
| 164 | MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */ |
| 165 | MGMT_STATUS_FAILED, /* Role Change Not Allowed */ |
| 166 | MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */ |
| 167 | MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */ |
| 168 | MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */ |
| 169 | MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */ |
| 170 | MGMT_STATUS_FAILED, /* Unit Link Key Used */ |
| 171 | MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */ |
| 172 | MGMT_STATUS_TIMEOUT, /* Instant Passed */ |
| 173 | MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */ |
| 174 | MGMT_STATUS_FAILED, /* Transaction Collision */ |
| 175 | MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */ |
| 176 | MGMT_STATUS_REJECTED, /* QoS Rejected */ |
| 177 | MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */ |
| 178 | MGMT_STATUS_REJECTED, /* Insufficient Security */ |
| 179 | MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */ |
| 180 | MGMT_STATUS_BUSY, /* Role Switch Pending */ |
| 181 | MGMT_STATUS_FAILED, /* Slot Violation */ |
| 182 | MGMT_STATUS_FAILED, /* Role Switch Failed */ |
| 183 | MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */ |
| 184 | MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */ |
| 185 | MGMT_STATUS_BUSY, /* Host Busy Pairing */ |
| 186 | MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */ |
| 187 | MGMT_STATUS_BUSY, /* Controller Busy */ |
| 188 | MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */ |
| 189 | MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */ |
| 190 | MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */ |
| 191 | MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */ |
| 192 | MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ |
| 193 | }; |
| 194 | |
| 195 | static u8 mgmt_status(u8 hci_status) |
| 196 | { |
| 197 | if (hci_status < ARRAY_SIZE(mgmt_status_table)) |
| 198 | return mgmt_status_table[hci_status]; |
| 199 | |
| 200 | return MGMT_STATUS_FAILED; |
| 201 | } |
| 202 | |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 203 | 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] | 204 | { |
| 205 | struct sk_buff *skb; |
| 206 | struct mgmt_hdr *hdr; |
| 207 | struct mgmt_ev_cmd_status *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 208 | int err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 209 | |
Szymon Janc | 34eb525 | 2011-02-28 14:10:08 +0100 | [diff] [blame] | 210 | 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] | 211 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 212 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 213 | if (!skb) |
| 214 | return -ENOMEM; |
| 215 | |
| 216 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 217 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 218 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 219 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 220 | hdr->len = cpu_to_le16(sizeof(*ev)); |
| 221 | |
| 222 | ev = (void *) skb_put(skb, sizeof(*ev)); |
| 223 | ev->status = status; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 224 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 225 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 226 | err = sock_queue_rcv_skb(sk, skb); |
| 227 | if (err < 0) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 228 | kfree_skb(skb); |
| 229 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 230 | return err; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 233 | 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] | 234 | void *rp, size_t rp_len) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 235 | { |
| 236 | struct sk_buff *skb; |
| 237 | struct mgmt_hdr *hdr; |
| 238 | struct mgmt_ev_cmd_complete *ev; |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 239 | int err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 240 | |
| 241 | BT_DBG("sock %p", sk); |
| 242 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 243 | skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 244 | if (!skb) |
| 245 | return -ENOMEM; |
| 246 | |
| 247 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 248 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 249 | hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE); |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 250 | hdr->index = cpu_to_le16(index); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 251 | hdr->len = cpu_to_le16(sizeof(*ev) + rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 252 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 253 | ev = (void *) skb_put(skb, sizeof(*ev) + rp_len); |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 254 | ev->opcode = cpu_to_le16(cmd); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 255 | ev->status = status; |
Szymon Janc | 8020c16 | 2011-02-28 14:09:50 +0100 | [diff] [blame] | 256 | |
| 257 | if (rp) |
| 258 | memcpy(ev->data, rp, rp_len); |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 259 | |
Gustavo F. Padovan | 56b7d13 | 2011-10-14 19:20:01 -0300 | [diff] [blame] | 260 | err = sock_queue_rcv_skb(sk, skb); |
| 261 | if (err < 0) |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 262 | kfree_skb(skb); |
| 263 | |
Marcel Holtmann | e5f0e15 | 2012-02-22 11:59:01 +0100 | [diff] [blame] | 264 | return err; |
Johan Hedberg | 02d9812 | 2010-12-13 21:07:04 +0200 | [diff] [blame] | 265 | } |
| 266 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 267 | static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, |
| 268 | u16 data_len) |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 269 | { |
| 270 | struct mgmt_rp_read_version rp; |
| 271 | |
| 272 | BT_DBG("sock %p", sk); |
| 273 | |
| 274 | rp.version = MGMT_VERSION; |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 275 | rp.revision = cpu_to_le16(MGMT_REVISION); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 276 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 277 | 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] | 278 | sizeof(rp)); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 281 | static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, |
| 282 | u16 data_len) |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 283 | { |
| 284 | struct mgmt_rp_read_commands *rp; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 285 | const u16 num_commands = ARRAY_SIZE(mgmt_commands); |
| 286 | const u16 num_events = ARRAY_SIZE(mgmt_events); |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 287 | __le16 *opcode; |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 288 | size_t rp_size; |
| 289 | int i, err; |
| 290 | |
| 291 | BT_DBG("sock %p", sk); |
| 292 | |
| 293 | rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); |
| 294 | |
| 295 | rp = kmalloc(rp_size, GFP_KERNEL); |
| 296 | if (!rp) |
| 297 | return -ENOMEM; |
| 298 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 299 | rp->num_commands = cpu_to_le16(num_commands); |
| 300 | rp->num_events = cpu_to_le16(num_events); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 301 | |
| 302 | for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) |
| 303 | put_unaligned_le16(mgmt_commands[i], opcode); |
| 304 | |
| 305 | for (i = 0; i < num_events; i++, opcode++) |
| 306 | put_unaligned_le16(mgmt_events[i], opcode); |
| 307 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 308 | 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] | 309 | rp_size); |
Johan Hedberg | e70bb2e | 2012-02-13 16:59:33 +0200 | [diff] [blame] | 310 | kfree(rp); |
| 311 | |
| 312 | return err; |
| 313 | } |
| 314 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 315 | static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data, |
| 316 | u16 data_len) |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 317 | { |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 318 | struct mgmt_rp_read_index_list *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 319 | struct hci_dev *d; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 320 | size_t rp_len; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 321 | u16 count; |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 322 | int err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 323 | |
| 324 | BT_DBG("sock %p", sk); |
| 325 | |
| 326 | read_lock(&hci_dev_list_lock); |
| 327 | |
| 328 | count = 0; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 329 | list_for_each_entry(d, &hci_dev_list, list) { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 330 | if (d->dev_type == HCI_BREDR) |
| 331 | count++; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 334 | rp_len = sizeof(*rp) + (2 * count); |
| 335 | rp = kmalloc(rp_len, GFP_ATOMIC); |
| 336 | if (!rp) { |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 337 | read_unlock(&hci_dev_list_lock); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 338 | return -ENOMEM; |
Jesper Juhl | b2c60d4 | 2011-01-14 00:18:49 +0100 | [diff] [blame] | 339 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 340 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 341 | count = 0; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 342 | list_for_each_entry(d, &hci_dev_list, list) { |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 343 | if (test_bit(HCI_SETUP, &d->dev_flags)) |
Johan Hedberg | ab81cbf | 2010-12-15 13:53:18 +0200 | [diff] [blame] | 344 | continue; |
| 345 | |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 346 | if (test_bit(HCI_USER_CHANNEL, &d->dev_flags)) |
| 347 | continue; |
| 348 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 349 | if (d->dev_type == HCI_BREDR) { |
| 350 | rp->index[count++] = cpu_to_le16(d->id); |
| 351 | BT_DBG("Added hci%u", d->id); |
| 352 | } |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Johan Hedberg | 476e44c | 2012-10-19 20:10:46 +0300 | [diff] [blame] | 355 | rp->num_controllers = cpu_to_le16(count); |
| 356 | rp_len = sizeof(*rp) + (2 * count); |
| 357 | |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 358 | read_unlock(&hci_dev_list_lock); |
| 359 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 360 | 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] | 361 | rp_len); |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 362 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 363 | kfree(rp); |
| 364 | |
| 365 | return err; |
Johan Hedberg | faba42e | 2010-12-13 21:07:05 +0200 | [diff] [blame] | 366 | } |
| 367 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 368 | static u32 get_supported_settings(struct hci_dev *hdev) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 369 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 370 | u32 settings = 0; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 371 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 372 | settings |= MGMT_SETTING_POWERED; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 373 | settings |= MGMT_SETTING_PAIRABLE; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 374 | settings |= MGMT_SETTING_DEBUG_KEYS; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 375 | |
Andre Guedes | ed3fa31 | 2012-07-24 15:03:46 -0300 | [diff] [blame] | 376 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 377 | settings |= MGMT_SETTING_CONNECTABLE; |
Johan Hedberg | 1a47aee | 2013-03-15 17:07:06 -0500 | [diff] [blame] | 378 | if (hdev->hci_ver >= BLUETOOTH_VER_1_2) |
| 379 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 380 | settings |= MGMT_SETTING_DISCOVERABLE; |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 381 | settings |= MGMT_SETTING_BREDR; |
| 382 | settings |= MGMT_SETTING_LINK_SECURITY; |
Marcel Holtmann | a82974c | 2013-10-11 09:48:47 -0700 | [diff] [blame] | 383 | |
| 384 | if (lmp_ssp_capable(hdev)) { |
| 385 | settings |= MGMT_SETTING_SSP; |
| 386 | settings |= MGMT_SETTING_HS; |
| 387 | } |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 388 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 389 | if (lmp_sc_capable(hdev) || |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 390 | test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 391 | settings |= MGMT_SETTING_SECURE_CONN; |
Marcel Holtmann | 848566b | 2013-10-01 22:59:22 -0700 | [diff] [blame] | 392 | } |
Marcel Holtmann | d7b7e79 | 2012-02-20 21:47:49 +0100 | [diff] [blame] | 393 | |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 394 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 9d42820 | 2012-05-03 07:12:31 +0200 | [diff] [blame] | 395 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 396 | settings |= MGMT_SETTING_ADVERTISING; |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 397 | settings |= MGMT_SETTING_PRIVACY; |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 398 | } |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 399 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 400 | return settings; |
| 401 | } |
Johan Hedberg | ebc99fe | 2011-01-04 11:54:26 +0200 | [diff] [blame] | 402 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 403 | static u32 get_current_settings(struct hci_dev *hdev) |
| 404 | { |
| 405 | u32 settings = 0; |
Johan Hedberg | dc4fe30 | 2011-03-16 14:29:36 +0200 | [diff] [blame] | 406 | |
Johan Hedberg | f1f0eb0 | 2012-02-21 17:15:41 +0200 | [diff] [blame] | 407 | if (hdev_is_powered(hdev)) |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 408 | settings |= MGMT_SETTING_POWERED; |
| 409 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 410 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 411 | settings |= MGMT_SETTING_CONNECTABLE; |
| 412 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 413 | if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
| 414 | settings |= MGMT_SETTING_FAST_CONNECTABLE; |
| 415 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 416 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 417 | settings |= MGMT_SETTING_DISCOVERABLE; |
| 418 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 419 | if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 420 | settings |= MGMT_SETTING_PAIRABLE; |
| 421 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 422 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 423 | settings |= MGMT_SETTING_BREDR; |
| 424 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 425 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 426 | settings |= MGMT_SETTING_LE; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 427 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 428 | if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 429 | settings |= MGMT_SETTING_LINK_SECURITY; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 430 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 431 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 432 | settings |= MGMT_SETTING_SSP; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 433 | |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 434 | if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) |
| 435 | settings |= MGMT_SETTING_HS; |
| 436 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 437 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 438 | settings |= MGMT_SETTING_ADVERTISING; |
| 439 | |
Marcel Holtmann | e98d2ce | 2014-01-10 02:07:22 -0800 | [diff] [blame] | 440 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 441 | settings |= MGMT_SETTING_SECURE_CONN; |
| 442 | |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 443 | if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 444 | settings |= MGMT_SETTING_DEBUG_KEYS; |
| 445 | |
Johan Hedberg | 0f4bd94 | 2014-02-22 19:06:35 +0200 | [diff] [blame] | 446 | if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) |
| 447 | settings |= MGMT_SETTING_PRIVACY; |
| 448 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 449 | return settings; |
Johan Hedberg | c542a06 | 2011-01-26 13:11:03 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 452 | #define PNP_INFO_SVCLASS_ID 0x1200 |
| 453 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 454 | static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 455 | { |
| 456 | u8 *ptr = data, *uuids_start = NULL; |
| 457 | struct bt_uuid *uuid; |
| 458 | |
| 459 | if (len < 4) |
| 460 | return ptr; |
| 461 | |
| 462 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 463 | u16 uuid16; |
| 464 | |
| 465 | if (uuid->size != 16) |
| 466 | continue; |
| 467 | |
| 468 | uuid16 = get_unaligned_le16(&uuid->uuid[12]); |
| 469 | if (uuid16 < 0x1100) |
| 470 | continue; |
| 471 | |
| 472 | if (uuid16 == PNP_INFO_SVCLASS_ID) |
| 473 | continue; |
| 474 | |
| 475 | if (!uuids_start) { |
| 476 | uuids_start = ptr; |
| 477 | uuids_start[0] = 1; |
| 478 | uuids_start[1] = EIR_UUID16_ALL; |
| 479 | ptr += 2; |
| 480 | } |
| 481 | |
| 482 | /* Stop if not enough space to put next UUID */ |
| 483 | if ((ptr - data) + sizeof(u16) > len) { |
| 484 | uuids_start[1] = EIR_UUID16_SOME; |
| 485 | break; |
| 486 | } |
| 487 | |
| 488 | *ptr++ = (uuid16 & 0x00ff); |
| 489 | *ptr++ = (uuid16 & 0xff00) >> 8; |
| 490 | uuids_start[0] += sizeof(uuid16); |
| 491 | } |
| 492 | |
| 493 | return ptr; |
| 494 | } |
| 495 | |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 496 | static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 497 | { |
| 498 | u8 *ptr = data, *uuids_start = NULL; |
| 499 | struct bt_uuid *uuid; |
| 500 | |
| 501 | if (len < 6) |
| 502 | return ptr; |
| 503 | |
| 504 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 505 | if (uuid->size != 32) |
| 506 | continue; |
| 507 | |
| 508 | if (!uuids_start) { |
| 509 | uuids_start = ptr; |
| 510 | uuids_start[0] = 1; |
| 511 | uuids_start[1] = EIR_UUID32_ALL; |
| 512 | ptr += 2; |
| 513 | } |
| 514 | |
| 515 | /* Stop if not enough space to put next UUID */ |
| 516 | if ((ptr - data) + sizeof(u32) > len) { |
| 517 | uuids_start[1] = EIR_UUID32_SOME; |
| 518 | break; |
| 519 | } |
| 520 | |
| 521 | memcpy(ptr, &uuid->uuid[12], sizeof(u32)); |
| 522 | ptr += sizeof(u32); |
| 523 | uuids_start[0] += sizeof(u32); |
| 524 | } |
| 525 | |
| 526 | return ptr; |
| 527 | } |
| 528 | |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 529 | static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len) |
| 530 | { |
| 531 | u8 *ptr = data, *uuids_start = NULL; |
| 532 | struct bt_uuid *uuid; |
| 533 | |
| 534 | if (len < 18) |
| 535 | return ptr; |
| 536 | |
| 537 | list_for_each_entry(uuid, &hdev->uuids, list) { |
| 538 | if (uuid->size != 128) |
| 539 | continue; |
| 540 | |
| 541 | if (!uuids_start) { |
| 542 | uuids_start = ptr; |
| 543 | uuids_start[0] = 1; |
| 544 | uuids_start[1] = EIR_UUID128_ALL; |
| 545 | ptr += 2; |
| 546 | } |
| 547 | |
| 548 | /* Stop if not enough space to put next UUID */ |
| 549 | if ((ptr - data) + 16 > len) { |
| 550 | uuids_start[1] = EIR_UUID128_SOME; |
| 551 | break; |
| 552 | } |
| 553 | |
| 554 | memcpy(ptr, uuid->uuid, 16); |
| 555 | ptr += 16; |
| 556 | uuids_start[0] += 16; |
| 557 | } |
| 558 | |
| 559 | return ptr; |
| 560 | } |
| 561 | |
Johan Hedberg | eb2a8d2 | 2013-10-19 23:38:20 +0300 | [diff] [blame] | 562 | static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) |
| 563 | { |
| 564 | struct pending_cmd *cmd; |
| 565 | |
| 566 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 567 | if (cmd->opcode == opcode) |
| 568 | return cmd; |
| 569 | } |
| 570 | |
| 571 | return NULL; |
| 572 | } |
| 573 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 574 | static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) |
| 575 | { |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 576 | u8 ad_len = 0; |
| 577 | size_t name_len; |
| 578 | |
| 579 | name_len = strlen(hdev->dev_name); |
| 580 | if (name_len > 0) { |
| 581 | size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2; |
| 582 | |
| 583 | if (name_len > max_len) { |
| 584 | name_len = max_len; |
| 585 | ptr[1] = EIR_NAME_SHORT; |
| 586 | } else |
| 587 | ptr[1] = EIR_NAME_COMPLETE; |
| 588 | |
| 589 | ptr[0] = name_len + 1; |
| 590 | |
| 591 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 592 | |
| 593 | ad_len += (name_len + 2); |
| 594 | ptr += (name_len + 2); |
| 595 | } |
| 596 | |
| 597 | return ad_len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | static void update_scan_rsp_data(struct hci_request *req) |
| 601 | { |
| 602 | struct hci_dev *hdev = req->hdev; |
| 603 | struct hci_cp_le_set_scan_rsp_data cp; |
| 604 | u8 len; |
| 605 | |
Johan Hedberg | 7751ef1 | 2013-10-19 23:38:15 +0300 | [diff] [blame] | 606 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 607 | return; |
| 608 | |
| 609 | memset(&cp, 0, sizeof(cp)); |
| 610 | |
| 611 | len = create_scan_rsp_data(hdev, cp.data); |
| 612 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 613 | if (hdev->scan_rsp_data_len == len && |
| 614 | memcmp(cp.data, hdev->scan_rsp_data, len) == 0) |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 615 | return; |
| 616 | |
Johan Hedberg | eb438b5 | 2013-10-16 15:31:07 +0300 | [diff] [blame] | 617 | memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); |
| 618 | hdev->scan_rsp_data_len = len; |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 619 | |
| 620 | cp.length = len; |
| 621 | |
| 622 | hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); |
| 623 | } |
| 624 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 625 | static u8 get_adv_discov_flags(struct hci_dev *hdev) |
| 626 | { |
| 627 | struct pending_cmd *cmd; |
| 628 | |
| 629 | /* If there's a pending mgmt command the flags will not yet have |
| 630 | * their final values, so check for this first. |
| 631 | */ |
| 632 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 633 | if (cmd) { |
| 634 | struct mgmt_mode *cp = cmd->param; |
| 635 | if (cp->val == 0x01) |
| 636 | return LE_AD_GENERAL; |
| 637 | else if (cp->val == 0x02) |
| 638 | return LE_AD_LIMITED; |
| 639 | } else { |
| 640 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 641 | return LE_AD_LIMITED; |
| 642 | else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 643 | return LE_AD_GENERAL; |
| 644 | } |
| 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 649 | static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 650 | { |
| 651 | u8 ad_len = 0, flags = 0; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 652 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 653 | flags |= get_adv_discov_flags(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 654 | |
Johan Hedberg | e834004 | 2014-01-30 11:16:50 -0800 | [diff] [blame] | 655 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 656 | flags |= LE_AD_NO_BREDR; |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 657 | |
| 658 | if (flags) { |
| 659 | BT_DBG("adv flags 0x%02x", flags); |
| 660 | |
| 661 | ptr[0] = 2; |
| 662 | ptr[1] = EIR_FLAGS; |
| 663 | ptr[2] = flags; |
| 664 | |
| 665 | ad_len += 3; |
| 666 | ptr += 3; |
| 667 | } |
| 668 | |
| 669 | if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { |
| 670 | ptr[0] = 2; |
| 671 | ptr[1] = EIR_TX_POWER; |
| 672 | ptr[2] = (u8) hdev->adv_tx_power; |
| 673 | |
| 674 | ad_len += 3; |
| 675 | ptr += 3; |
| 676 | } |
| 677 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 678 | return ad_len; |
| 679 | } |
| 680 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 681 | static void update_adv_data(struct hci_request *req) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 682 | { |
| 683 | struct hci_dev *hdev = req->hdev; |
| 684 | struct hci_cp_le_set_adv_data cp; |
| 685 | u8 len; |
| 686 | |
Johan Hedberg | 10994ce | 2013-10-19 23:38:16 +0300 | [diff] [blame] | 687 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 688 | return; |
| 689 | |
| 690 | memset(&cp, 0, sizeof(cp)); |
| 691 | |
Marcel Holtmann | 46cad2e | 2013-10-16 00:16:46 -0700 | [diff] [blame] | 692 | len = create_adv_data(hdev, cp.data); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 693 | |
| 694 | if (hdev->adv_data_len == len && |
| 695 | memcmp(cp.data, hdev->adv_data, len) == 0) |
| 696 | return; |
| 697 | |
| 698 | memcpy(hdev->adv_data, cp.data, sizeof(cp.data)); |
| 699 | hdev->adv_data_len = len; |
| 700 | |
| 701 | cp.length = len; |
| 702 | |
| 703 | hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); |
| 704 | } |
| 705 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 706 | static void create_eir(struct hci_dev *hdev, u8 *data) |
| 707 | { |
| 708 | u8 *ptr = data; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 709 | size_t name_len; |
| 710 | |
| 711 | name_len = strlen(hdev->dev_name); |
| 712 | |
| 713 | if (name_len > 0) { |
| 714 | /* EIR Data type */ |
| 715 | if (name_len > 48) { |
| 716 | name_len = 48; |
| 717 | ptr[1] = EIR_NAME_SHORT; |
| 718 | } else |
| 719 | ptr[1] = EIR_NAME_COMPLETE; |
| 720 | |
| 721 | /* EIR Data length */ |
| 722 | ptr[0] = name_len + 1; |
| 723 | |
| 724 | memcpy(ptr + 2, hdev->dev_name, name_len); |
| 725 | |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 726 | ptr += (name_len + 2); |
| 727 | } |
| 728 | |
Johan Hedberg | bbaf444 | 2012-11-08 01:22:59 +0100 | [diff] [blame] | 729 | if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) { |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 730 | ptr[0] = 2; |
| 731 | ptr[1] = EIR_TX_POWER; |
| 732 | ptr[2] = (u8) hdev->inq_tx_power; |
| 733 | |
Marcel Holtmann | 91c4e9b | 2012-03-11 19:27:21 -0700 | [diff] [blame] | 734 | ptr += 3; |
| 735 | } |
| 736 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 737 | if (hdev->devid_source > 0) { |
| 738 | ptr[0] = 9; |
| 739 | ptr[1] = EIR_DEVICE_ID; |
| 740 | |
| 741 | put_unaligned_le16(hdev->devid_source, ptr + 2); |
| 742 | put_unaligned_le16(hdev->devid_vendor, ptr + 4); |
| 743 | put_unaligned_le16(hdev->devid_product, ptr + 6); |
| 744 | put_unaligned_le16(hdev->devid_version, ptr + 8); |
| 745 | |
Marcel Holtmann | 2b9be13 | 2012-03-11 19:32:12 -0700 | [diff] [blame] | 746 | ptr += 10; |
| 747 | } |
| 748 | |
Johan Hedberg | 213202e | 2013-01-27 00:31:33 +0200 | [diff] [blame] | 749 | ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | cdf1963 | 2013-01-27 00:31:34 +0200 | [diff] [blame] | 750 | ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | c00d575 | 2013-01-27 00:31:35 +0200 | [diff] [blame] | 751 | ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data)); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 752 | } |
| 753 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 754 | static void update_eir(struct hci_request *req) |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 755 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 756 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 757 | struct hci_cp_write_eir cp; |
| 758 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 759 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 760 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 761 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 762 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 763 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 764 | |
Johan Hedberg | 84bde9d | 2012-01-25 14:21:06 +0200 | [diff] [blame] | 765 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 766 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 767 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 768 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 769 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 770 | |
| 771 | memset(&cp, 0, sizeof(cp)); |
| 772 | |
| 773 | create_eir(hdev, cp.data); |
| 774 | |
| 775 | if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 776 | return; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 777 | |
| 778 | memcpy(hdev->eir, cp.data, sizeof(cp.data)); |
| 779 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 780 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 781 | } |
| 782 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 783 | static u8 get_service_classes(struct hci_dev *hdev) |
| 784 | { |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 785 | struct bt_uuid *uuid; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 786 | u8 val = 0; |
| 787 | |
Gustavo F. Padovan | 12dc074 | 2011-10-14 19:32:56 -0300 | [diff] [blame] | 788 | list_for_each_entry(uuid, &hdev->uuids, list) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 789 | val |= uuid->svc_hint; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 790 | |
| 791 | return val; |
| 792 | } |
| 793 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 794 | static void update_class(struct hci_request *req) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 795 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 796 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 797 | u8 cod[3]; |
| 798 | |
| 799 | BT_DBG("%s", hdev->name); |
| 800 | |
Johan Hedberg | 504c8dc | 2012-02-23 13:30:41 +0200 | [diff] [blame] | 801 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 802 | return; |
Johan Hedberg | 7770c4a | 2012-02-22 22:06:38 +0200 | [diff] [blame] | 803 | |
Johan Hedberg | f87ea1d | 2013-10-19 23:38:17 +0300 | [diff] [blame] | 804 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 805 | return; |
| 806 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 807 | if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 808 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 809 | |
| 810 | cod[0] = hdev->minor_class; |
| 811 | cod[1] = hdev->major_class; |
| 812 | cod[2] = get_service_classes(hdev); |
| 813 | |
Marcel Holtmann | 6acd7db | 2013-10-15 06:33:53 -0700 | [diff] [blame] | 814 | if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) |
| 815 | cod[1] |= 0x20; |
| 816 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 817 | if (memcmp(cod, hdev->dev_class, 3) == 0) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 818 | return; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 819 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 820 | 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] | 821 | } |
| 822 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 823 | static bool get_connectable(struct hci_dev *hdev) |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 824 | { |
| 825 | struct pending_cmd *cmd; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 826 | |
| 827 | /* If there's a pending mgmt command the flag will not yet have |
| 828 | * it's final value, so check for this first. |
| 829 | */ |
| 830 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 831 | if (cmd) { |
| 832 | struct mgmt_mode *cp = cmd->param; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 833 | return cp->val; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 834 | } |
| 835 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 836 | return test_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static void enable_advertising(struct hci_request *req) |
| 840 | { |
| 841 | struct hci_dev *hdev = req->hdev; |
| 842 | struct hci_cp_le_set_adv_param cp; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 843 | u8 own_addr_type, enable = 0x01; |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 844 | bool connectable; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 845 | |
Johan Hedberg | 8d97250 | 2014-02-28 12:54:14 +0200 | [diff] [blame] | 846 | /* Clear the HCI_ADVERTISING bit temporarily so that the |
| 847 | * hci_update_random_address knows that it's safe to go ahead |
| 848 | * and write a new random address. The flag will be set back on |
| 849 | * as soon as the SET_ADV_ENABLE HCI command completes. |
| 850 | */ |
| 851 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 852 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 853 | connectable = get_connectable(hdev); |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 854 | |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 855 | /* Set require_privacy to true only when non-connectable |
| 856 | * advertising is used. In that case it is fine to use a |
| 857 | * non-resolvable private address. |
| 858 | */ |
| 859 | if (hci_update_random_address(req, !connectable, &own_addr_type) < 0) |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 860 | return; |
| 861 | |
Marcel Holtmann | 41c90c1 | 2014-02-23 20:25:55 -0800 | [diff] [blame] | 862 | memset(&cp, 0, sizeof(cp)); |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 863 | cp.min_interval = cpu_to_le16(0x0800); |
| 864 | cp.max_interval = cpu_to_le16(0x0800); |
Johan Hedberg | a4858cb | 2014-02-25 19:56:31 +0200 | [diff] [blame] | 865 | cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; |
Johan Hedberg | 8f2a060 | 2014-02-23 19:42:23 +0200 | [diff] [blame] | 866 | cp.own_address_type = own_addr_type; |
Johan Hedberg | 199a2fb | 2014-02-22 19:06:33 +0200 | [diff] [blame] | 867 | cp.channel_map = hdev->le_adv_channel_map; |
| 868 | |
| 869 | hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp); |
| 870 | |
| 871 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 872 | } |
| 873 | |
| 874 | static void disable_advertising(struct hci_request *req) |
| 875 | { |
| 876 | u8 enable = 0x00; |
| 877 | |
| 878 | hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable); |
| 879 | } |
| 880 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 881 | static void service_cache_off(struct work_struct *work) |
| 882 | { |
| 883 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 884 | service_cache.work); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 885 | struct hci_request req; |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 886 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 887 | if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 888 | return; |
| 889 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 890 | hci_req_init(&req, hdev); |
| 891 | |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 892 | hci_dev_lock(hdev); |
| 893 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 894 | update_eir(&req); |
| 895 | update_class(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 896 | |
| 897 | hci_dev_unlock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 898 | |
| 899 | hci_req_run(&req, NULL); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 900 | } |
| 901 | |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 902 | static void rpa_expired(struct work_struct *work) |
| 903 | { |
| 904 | struct hci_dev *hdev = container_of(work, struct hci_dev, |
| 905 | rpa_expired.work); |
| 906 | struct hci_request req; |
| 907 | |
| 908 | BT_DBG(""); |
| 909 | |
| 910 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 911 | |
| 912 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags) || |
| 913 | hci_conn_num(hdev, LE_LINK) > 0) |
| 914 | return; |
| 915 | |
| 916 | /* The generation of a new RPA and programming it into the |
| 917 | * controller happens in the enable_advertising() function. |
| 918 | */ |
| 919 | |
| 920 | hci_req_init(&req, hdev); |
| 921 | |
| 922 | disable_advertising(&req); |
| 923 | enable_advertising(&req); |
| 924 | |
| 925 | hci_req_run(&req, NULL); |
| 926 | } |
| 927 | |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 928 | static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 929 | { |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 930 | if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) |
Johan Hedberg | 6a91908 | 2012-02-28 06:17:26 +0200 | [diff] [blame] | 931 | return; |
| 932 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 933 | INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); |
Johan Hedberg | d6bfd59 | 2014-02-23 19:42:20 +0200 | [diff] [blame] | 934 | INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 935 | |
Johan Hedberg | 4f87da8 | 2012-03-02 19:55:56 +0200 | [diff] [blame] | 936 | /* Non-mgmt controlled devices get this bit set |
| 937 | * implicitly so that pairing works for them, however |
| 938 | * for mgmt we require user-space to explicitly enable |
| 939 | * it |
| 940 | */ |
| 941 | clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 944 | 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] | 945 | void *data, u16 data_len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 946 | { |
| 947 | struct mgmt_rp_read_info rp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 948 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 949 | BT_DBG("sock %p %s", sk, hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 950 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 951 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 952 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 953 | memset(&rp, 0, sizeof(rp)); |
| 954 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 955 | bacpy(&rp.bdaddr, &hdev->bdaddr); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 956 | |
| 957 | rp.version = hdev->hci_ver; |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 958 | rp.manufacturer = cpu_to_le16(hdev->manufacturer); |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 959 | |
| 960 | rp.supported_settings = cpu_to_le32(get_supported_settings(hdev)); |
| 961 | rp.current_settings = cpu_to_le32(get_current_settings(hdev)); |
| 962 | |
| 963 | memcpy(rp.dev_class, hdev->dev_class, 3); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 964 | |
| 965 | memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name)); |
Johan Hedberg | 27fcc36 | 2012-02-22 21:46:22 +0200 | [diff] [blame] | 966 | memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_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_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 969 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 970 | 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] | 971 | sizeof(rp)); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | static void mgmt_pending_free(struct pending_cmd *cmd) |
| 975 | { |
| 976 | sock_put(cmd->sk); |
| 977 | kfree(cmd->param); |
| 978 | kfree(cmd); |
| 979 | } |
| 980 | |
| 981 | 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] | 982 | struct hci_dev *hdev, void *data, |
| 983 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 984 | { |
| 985 | struct pending_cmd *cmd; |
| 986 | |
Johan Hedberg | fca2001 | 2014-06-28 17:54:05 +0300 | [diff] [blame] | 987 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 988 | if (!cmd) |
| 989 | return NULL; |
| 990 | |
| 991 | cmd->opcode = opcode; |
| 992 | cmd->index = hdev->id; |
| 993 | |
Andre Guedes | 12b9456 | 2012-06-07 19:05:45 -0300 | [diff] [blame] | 994 | cmd->param = kmalloc(len, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 995 | if (!cmd->param) { |
| 996 | kfree(cmd); |
| 997 | return NULL; |
| 998 | } |
| 999 | |
| 1000 | if (data) |
| 1001 | memcpy(cmd->param, data, len); |
| 1002 | |
| 1003 | cmd->sk = sk; |
| 1004 | sock_hold(sk); |
| 1005 | |
| 1006 | list_add(&cmd->list, &hdev->mgmt_pending); |
| 1007 | |
| 1008 | return cmd; |
| 1009 | } |
| 1010 | |
| 1011 | static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 1012 | void (*cb)(struct pending_cmd *cmd, |
| 1013 | void *data), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1014 | void *data) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1015 | { |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1016 | struct pending_cmd *cmd, *tmp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1017 | |
Andre Guedes | a3d0935 | 2013-02-01 11:21:30 -0300 | [diff] [blame] | 1018 | list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1019 | if (opcode > 0 && cmd->opcode != opcode) |
| 1020 | continue; |
| 1021 | |
| 1022 | cb(cmd, data); |
| 1023 | } |
| 1024 | } |
| 1025 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1026 | static void mgmt_pending_remove(struct pending_cmd *cmd) |
| 1027 | { |
| 1028 | list_del(&cmd->list); |
| 1029 | mgmt_pending_free(cmd); |
| 1030 | } |
| 1031 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1032 | 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] | 1033 | { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1034 | __le32 settings = cpu_to_le32(get_current_settings(hdev)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1035 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 1036 | return cmd_complete(sk, hdev->id, opcode, 0, &settings, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1037 | sizeof(settings)); |
Johan Hedberg | 8680570 | 2011-11-11 16:18:52 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1040 | static void clean_up_hci_complete(struct hci_dev *hdev, u8 status) |
| 1041 | { |
| 1042 | BT_DBG("%s status 0x%02x", hdev->name, status); |
| 1043 | |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1044 | if (hci_conn_count(hdev) == 0) { |
| 1045 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1046 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1047 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 1050 | static void hci_stop_discovery(struct hci_request *req) |
| 1051 | { |
| 1052 | struct hci_dev *hdev = req->hdev; |
| 1053 | struct hci_cp_remote_name_req_cancel cp; |
| 1054 | struct inquiry_entry *e; |
| 1055 | |
| 1056 | switch (hdev->discovery.state) { |
| 1057 | case DISCOVERY_FINDING: |
| 1058 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 1059 | hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); |
| 1060 | } else { |
| 1061 | cancel_delayed_work(&hdev->le_scan_disable); |
| 1062 | hci_req_add_le_scan_disable(req); |
| 1063 | } |
| 1064 | |
| 1065 | break; |
| 1066 | |
| 1067 | case DISCOVERY_RESOLVING: |
| 1068 | e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, |
| 1069 | NAME_PENDING); |
| 1070 | if (!e) |
| 1071 | return; |
| 1072 | |
| 1073 | bacpy(&cp.bdaddr, &e->data.bdaddr); |
| 1074 | hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp), |
| 1075 | &cp); |
| 1076 | |
| 1077 | break; |
| 1078 | |
| 1079 | default: |
| 1080 | /* Passive scanning */ |
| 1081 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 1082 | hci_req_add_le_scan_disable(req); |
| 1083 | break; |
| 1084 | } |
| 1085 | } |
| 1086 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1087 | static int clean_up_hci_state(struct hci_dev *hdev) |
| 1088 | { |
| 1089 | struct hci_request req; |
| 1090 | struct hci_conn *conn; |
| 1091 | |
| 1092 | hci_req_init(&req, hdev); |
| 1093 | |
| 1094 | if (test_bit(HCI_ISCAN, &hdev->flags) || |
| 1095 | test_bit(HCI_PSCAN, &hdev->flags)) { |
| 1096 | u8 scan = 0x00; |
| 1097 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1098 | } |
| 1099 | |
| 1100 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 1101 | disable_advertising(&req); |
| 1102 | |
Johan Hedberg | f8680f1 | 2014-06-10 14:05:59 +0300 | [diff] [blame] | 1103 | hci_stop_discovery(&req); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1104 | |
| 1105 | list_for_each_entry(conn, &hdev->conn_hash.list, list) { |
| 1106 | struct hci_cp_disconnect dc; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1107 | struct hci_cp_reject_conn_req rej; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1108 | |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 1109 | switch (conn->state) { |
| 1110 | case BT_CONNECTED: |
| 1111 | case BT_CONFIG: |
| 1112 | dc.handle = cpu_to_le16(conn->handle); |
| 1113 | dc.reason = 0x15; /* Terminated due to Power Off */ |
| 1114 | hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 1115 | break; |
| 1116 | case BT_CONNECT: |
| 1117 | if (conn->type == LE_LINK) |
| 1118 | hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 1119 | 0, NULL); |
| 1120 | else if (conn->type == ACL_LINK) |
| 1121 | hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL, |
| 1122 | 6, &conn->dst); |
| 1123 | break; |
| 1124 | case BT_CONNECT2: |
| 1125 | bacpy(&rej.bdaddr, &conn->dst); |
| 1126 | rej.reason = 0x15; /* Terminated due to Power Off */ |
| 1127 | if (conn->type == ACL_LINK) |
| 1128 | hci_req_add(&req, HCI_OP_REJECT_CONN_REQ, |
| 1129 | sizeof(rej), &rej); |
| 1130 | else if (conn->type == SCO_LINK) |
| 1131 | hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 1132 | sizeof(rej), &rej); |
| 1133 | break; |
| 1134 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | return hci_req_run(&req, clean_up_hci_complete); |
| 1138 | } |
| 1139 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1140 | 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] | 1141 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1142 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1143 | struct mgmt_mode *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1144 | struct pending_cmd *cmd; |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1145 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1146 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1147 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1148 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1149 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1150 | return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1151 | MGMT_STATUS_INVALID_PARAMS); |
| 1152 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1153 | hci_dev_lock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1154 | |
Johan Hedberg | 87b95ba | 2013-09-25 13:26:06 +0300 | [diff] [blame] | 1155 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { |
| 1156 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, |
| 1157 | MGMT_STATUS_BUSY); |
| 1158 | goto failed; |
| 1159 | } |
| 1160 | |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1161 | if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { |
| 1162 | cancel_delayed_work(&hdev->power_off); |
| 1163 | |
| 1164 | if (cp->val) { |
Johan Hedberg | a1d7045 | 2013-01-09 15:29:40 +0200 | [diff] [blame] | 1165 | mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, |
| 1166 | data, len); |
| 1167 | err = mgmt_powered(hdev, 1); |
Marcel Holtmann | f0d4b78 | 2012-02-21 12:14:25 +0100 | [diff] [blame] | 1168 | goto failed; |
| 1169 | } |
| 1170 | } |
| 1171 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1172 | if (!!cp->val == hdev_is_powered(hdev)) { |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1173 | err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1174 | goto failed; |
| 1175 | } |
| 1176 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1177 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); |
| 1178 | if (!cmd) { |
| 1179 | err = -ENOMEM; |
| 1180 | goto failed; |
| 1181 | } |
| 1182 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1183 | if (cp->val) { |
Johan Hedberg | 1920257 | 2013-01-14 22:33:51 +0200 | [diff] [blame] | 1184 | queue_work(hdev->req_workqueue, &hdev->power_on); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1185 | err = 0; |
| 1186 | } else { |
| 1187 | /* Disconnect connections, stop scans, etc */ |
| 1188 | err = clean_up_hci_state(hdev); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1189 | if (!err) |
| 1190 | queue_delayed_work(hdev->req_workqueue, &hdev->power_off, |
| 1191 | HCI_POWER_OFF_TIMEOUT); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1192 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1193 | /* ENODATA means there were no HCI commands queued */ |
| 1194 | if (err == -ENODATA) { |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 1195 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 1196 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
| 1197 | err = 0; |
| 1198 | } |
| 1199 | } |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1200 | |
| 1201 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1202 | hci_dev_unlock(hdev); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1203 | return err; |
| 1204 | } |
| 1205 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1206 | static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, |
| 1207 | struct sock *skip_sk) |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1208 | { |
| 1209 | struct sk_buff *skb; |
| 1210 | struct mgmt_hdr *hdr; |
| 1211 | |
Andre Guedes | 790eff4 | 2012-06-07 19:05:46 -0300 | [diff] [blame] | 1212 | skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1213 | if (!skb) |
| 1214 | return -ENOMEM; |
| 1215 | |
| 1216 | hdr = (void *) skb_put(skb, sizeof(*hdr)); |
| 1217 | hdr->opcode = cpu_to_le16(event); |
| 1218 | if (hdev) |
| 1219 | hdr->index = cpu_to_le16(hdev->id); |
| 1220 | else |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1221 | hdr->index = cpu_to_le16(MGMT_INDEX_NONE); |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1222 | hdr->len = cpu_to_le16(data_len); |
| 1223 | |
| 1224 | if (data) |
| 1225 | memcpy(skb_put(skb, data_len), data, data_len); |
| 1226 | |
Marcel Holtmann | 97e0bde | 2012-02-22 13:49:28 +0100 | [diff] [blame] | 1227 | /* Time stamp */ |
| 1228 | __net_timestamp(skb); |
| 1229 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 1230 | hci_send_to_control(skb, skip_sk); |
| 1231 | kfree_skb(skb); |
| 1232 | |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | static int new_settings(struct hci_dev *hdev, struct sock *skip) |
| 1237 | { |
| 1238 | __le32 ev; |
| 1239 | |
| 1240 | ev = cpu_to_le32(get_current_settings(hdev)); |
| 1241 | |
| 1242 | return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); |
| 1243 | } |
| 1244 | |
Johan Hedberg | bd99abd | 2013-09-25 13:26:07 +0300 | [diff] [blame] | 1245 | struct cmd_lookup { |
| 1246 | struct sock *sk; |
| 1247 | struct hci_dev *hdev; |
| 1248 | u8 mgmt_status; |
| 1249 | }; |
| 1250 | |
| 1251 | static void settings_rsp(struct pending_cmd *cmd, void *data) |
| 1252 | { |
| 1253 | struct cmd_lookup *match = data; |
| 1254 | |
| 1255 | send_settings_rsp(cmd->sk, cmd->opcode, match->hdev); |
| 1256 | |
| 1257 | list_del(&cmd->list); |
| 1258 | |
| 1259 | if (match->sk == NULL) { |
| 1260 | match->sk = cmd->sk; |
| 1261 | sock_hold(match->sk); |
| 1262 | } |
| 1263 | |
| 1264 | mgmt_pending_free(cmd); |
| 1265 | } |
| 1266 | |
| 1267 | static void cmd_status_rsp(struct pending_cmd *cmd, void *data) |
| 1268 | { |
| 1269 | u8 *status = data; |
| 1270 | |
| 1271 | cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); |
| 1272 | mgmt_pending_remove(cmd); |
| 1273 | } |
| 1274 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1275 | static u8 mgmt_bredr_support(struct hci_dev *hdev) |
| 1276 | { |
| 1277 | if (!lmp_bredr_capable(hdev)) |
| 1278 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1279 | else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1280 | return MGMT_STATUS_REJECTED; |
| 1281 | else |
| 1282 | return MGMT_STATUS_SUCCESS; |
| 1283 | } |
| 1284 | |
| 1285 | static u8 mgmt_le_support(struct hci_dev *hdev) |
| 1286 | { |
| 1287 | if (!lmp_le_capable(hdev)) |
| 1288 | return MGMT_STATUS_NOT_SUPPORTED; |
| 1289 | else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 1290 | return MGMT_STATUS_REJECTED; |
| 1291 | else |
| 1292 | return MGMT_STATUS_SUCCESS; |
| 1293 | } |
| 1294 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1295 | static void set_discoverable_complete(struct hci_dev *hdev, u8 status) |
| 1296 | { |
| 1297 | struct pending_cmd *cmd; |
| 1298 | struct mgmt_mode *cp; |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1299 | struct hci_request req; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1300 | bool changed; |
| 1301 | |
| 1302 | BT_DBG("status 0x%02x", status); |
| 1303 | |
| 1304 | hci_dev_lock(hdev); |
| 1305 | |
| 1306 | cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1307 | if (!cmd) |
| 1308 | goto unlock; |
| 1309 | |
| 1310 | if (status) { |
| 1311 | u8 mgmt_err = mgmt_status(status); |
| 1312 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1313 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1314 | goto remove_cmd; |
| 1315 | } |
| 1316 | |
| 1317 | cp = cmd->param; |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1318 | if (cp->val) { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1319 | changed = !test_and_set_bit(HCI_DISCOVERABLE, |
| 1320 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1321 | |
| 1322 | if (hdev->discov_timeout > 0) { |
| 1323 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
| 1324 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
| 1325 | to); |
| 1326 | } |
| 1327 | } else { |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1328 | changed = test_and_clear_bit(HCI_DISCOVERABLE, |
| 1329 | &hdev->dev_flags); |
Marcel Holtmann | d4462a0 | 2013-10-15 08:11:02 -0700 | [diff] [blame] | 1330 | } |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1331 | |
| 1332 | send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
| 1333 | |
| 1334 | if (changed) |
| 1335 | new_settings(hdev, cmd->sk); |
| 1336 | |
Marcel Holtmann | 970ba52 | 2013-10-15 06:33:57 -0700 | [diff] [blame] | 1337 | /* When the discoverable mode gets changed, make sure |
| 1338 | * that class of device has the limited discoverable |
| 1339 | * bit correctly set. |
| 1340 | */ |
| 1341 | hci_req_init(&req, hdev); |
| 1342 | update_class(&req); |
| 1343 | hci_req_run(&req, NULL); |
| 1344 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1345 | remove_cmd: |
| 1346 | mgmt_pending_remove(cmd); |
| 1347 | |
| 1348 | unlock: |
| 1349 | hci_dev_unlock(hdev); |
| 1350 | } |
| 1351 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1352 | 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] | 1353 | u16 len) |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1354 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1355 | struct mgmt_cp_set_discoverable *cp = data; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1356 | struct pending_cmd *cmd; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1357 | struct hci_request req; |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1358 | u16 timeout; |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1359 | u8 scan; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1360 | int err; |
| 1361 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1362 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1363 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1364 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1365 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1366 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1367 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1368 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1369 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1370 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
| 1371 | MGMT_STATUS_INVALID_PARAMS); |
| 1372 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 1373 | timeout = __le16_to_cpu(cp->timeout); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1374 | |
| 1375 | /* Disabling discoverable requires that no timeout is set, |
| 1376 | * and enabling limited discoverable requires a timeout. |
| 1377 | */ |
| 1378 | if ((cp->val == 0x00 && timeout > 0) || |
| 1379 | (cp->val == 0x02 && timeout == 0)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1380 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1381 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1382 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1383 | hci_dev_lock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1384 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1385 | if (!hdev_is_powered(hdev) && timeout > 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1386 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1387 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1388 | goto failed; |
| 1389 | } |
| 1390 | |
| 1391 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1392 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1393 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1394 | MGMT_STATUS_BUSY); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1395 | goto failed; |
| 1396 | } |
| 1397 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1398 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1399 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1400 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1401 | goto failed; |
| 1402 | } |
| 1403 | |
| 1404 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1405 | bool changed = false; |
| 1406 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1407 | /* Setting limited discoverable when powered off is |
| 1408 | * not a valid operation since it requires a timeout |
| 1409 | * and so no need to check HCI_LIMITED_DISCOVERABLE. |
| 1410 | */ |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1411 | if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { |
| 1412 | change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1413 | changed = true; |
| 1414 | } |
| 1415 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1416 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | 0224d2f | 2012-02-21 19:40:05 +0200 | [diff] [blame] | 1417 | if (err < 0) |
| 1418 | goto failed; |
| 1419 | |
| 1420 | if (changed) |
| 1421 | err = new_settings(hdev, sk); |
| 1422 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 1423 | goto failed; |
| 1424 | } |
| 1425 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1426 | /* If the current mode is the same, then just update the timeout |
| 1427 | * value with the new value. And if only the timeout gets updated, |
| 1428 | * then no need for any HCI transactions. |
| 1429 | */ |
| 1430 | if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && |
| 1431 | (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, |
| 1432 | &hdev->dev_flags)) { |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1433 | cancel_delayed_work(&hdev->discov_off); |
| 1434 | hdev->discov_timeout = timeout; |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1435 | |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1436 | if (cp->val && hdev->discov_timeout > 0) { |
| 1437 | int to = msecs_to_jiffies(hdev->discov_timeout * 1000); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1438 | queue_delayed_work(hdev->workqueue, &hdev->discov_off, |
Marcel Holtmann | 3626154 | 2013-10-15 08:28:51 -0700 | [diff] [blame] | 1439 | to); |
Marcel Holtmann | 955638e | 2012-02-22 18:21:00 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1442 | err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1443 | goto failed; |
| 1444 | } |
| 1445 | |
| 1446 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len); |
| 1447 | if (!cmd) { |
| 1448 | err = -ENOMEM; |
| 1449 | goto failed; |
| 1450 | } |
| 1451 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1452 | /* Cancel any potential discoverable timeout that might be |
| 1453 | * still active and store new timeout value. The arming of |
| 1454 | * the timeout happens in the complete handler. |
| 1455 | */ |
| 1456 | cancel_delayed_work(&hdev->discov_off); |
| 1457 | hdev->discov_timeout = timeout; |
| 1458 | |
Johan Hedberg | b456f87 | 2013-10-19 23:38:22 +0300 | [diff] [blame] | 1459 | /* Limited discoverable mode */ |
| 1460 | if (cp->val == 0x02) |
| 1461 | set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1462 | else |
| 1463 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1464 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1465 | hci_req_init(&req, hdev); |
| 1466 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1467 | /* The procedure for LE-only controllers is much simpler - just |
| 1468 | * update the advertising data. |
| 1469 | */ |
| 1470 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1471 | goto update_ad; |
| 1472 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1473 | scan = SCAN_PAGE; |
| 1474 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1475 | if (cp->val) { |
| 1476 | struct hci_cp_write_current_iac_lap hci_cp; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1477 | |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1478 | if (cp->val == 0x02) { |
| 1479 | /* Limited discoverable mode */ |
Marcel Holtmann | 33337dc | 2013-10-23 08:28:01 -0700 | [diff] [blame] | 1480 | hci_cp.num_iac = min_t(u8, hdev->num_iac, 2); |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1481 | hci_cp.iac_lap[0] = 0x00; /* LIAC */ |
| 1482 | hci_cp.iac_lap[1] = 0x8b; |
| 1483 | hci_cp.iac_lap[2] = 0x9e; |
| 1484 | hci_cp.iac_lap[3] = 0x33; /* GIAC */ |
| 1485 | hci_cp.iac_lap[4] = 0x8b; |
| 1486 | hci_cp.iac_lap[5] = 0x9e; |
| 1487 | } else { |
| 1488 | /* General discoverable mode */ |
Marcel Holtmann | 310a3d4 | 2013-10-15 09:13:39 -0700 | [diff] [blame] | 1489 | hci_cp.num_iac = 1; |
| 1490 | hci_cp.iac_lap[0] = 0x33; /* GIAC */ |
| 1491 | hci_cp.iac_lap[1] = 0x8b; |
| 1492 | hci_cp.iac_lap[2] = 0x9e; |
| 1493 | } |
| 1494 | |
| 1495 | hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP, |
| 1496 | (hci_cp.num_iac * 3) + 1, &hci_cp); |
| 1497 | |
| 1498 | scan |= SCAN_INQUIRY; |
| 1499 | } else { |
| 1500 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1501 | } |
| 1502 | |
| 1503 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1504 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1505 | update_ad: |
| 1506 | update_adv_data(&req); |
| 1507 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 1508 | err = hci_req_run(&req, set_discoverable_complete); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1509 | if (err < 0) |
| 1510 | mgmt_pending_remove(cmd); |
| 1511 | |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1512 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1513 | hci_dev_unlock(hdev); |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1514 | return err; |
| 1515 | } |
| 1516 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1517 | static void write_fast_connectable(struct hci_request *req, bool enable) |
| 1518 | { |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1519 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1520 | struct hci_cp_write_page_scan_activity acp; |
| 1521 | u8 type; |
| 1522 | |
Johan Hedberg | 547003b | 2013-10-21 16:51:53 +0300 | [diff] [blame] | 1523 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 1524 | return; |
| 1525 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1526 | if (hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 1527 | return; |
| 1528 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1529 | if (enable) { |
| 1530 | type = PAGE_SCAN_TYPE_INTERLACED; |
| 1531 | |
| 1532 | /* 160 msec page scan interval */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1533 | acp.interval = cpu_to_le16(0x0100); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1534 | } else { |
| 1535 | type = PAGE_SCAN_TYPE_STANDARD; /* default */ |
| 1536 | |
| 1537 | /* default 1.28 sec page scan */ |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1538 | acp.interval = cpu_to_le16(0x0800); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1539 | } |
| 1540 | |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 1541 | acp.window = cpu_to_le16(0x0012); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1542 | |
Johan Hedberg | bd98b99 | 2013-03-15 17:07:13 -0500 | [diff] [blame] | 1543 | if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval || |
| 1544 | __cpu_to_le16(hdev->page_scan_window) != acp.window) |
| 1545 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY, |
| 1546 | sizeof(acp), &acp); |
| 1547 | |
| 1548 | if (hdev->page_scan_type != type) |
| 1549 | hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 1550 | } |
| 1551 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1552 | static void set_connectable_complete(struct hci_dev *hdev, u8 status) |
| 1553 | { |
| 1554 | struct pending_cmd *cmd; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1555 | struct mgmt_mode *cp; |
| 1556 | bool changed; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1557 | |
| 1558 | BT_DBG("status 0x%02x", status); |
| 1559 | |
| 1560 | hci_dev_lock(hdev); |
| 1561 | |
| 1562 | cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); |
| 1563 | if (!cmd) |
| 1564 | goto unlock; |
| 1565 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1566 | if (status) { |
| 1567 | u8 mgmt_err = mgmt_status(status); |
| 1568 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 1569 | goto remove_cmd; |
| 1570 | } |
| 1571 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1572 | cp = cmd->param; |
| 1573 | if (cp->val) |
| 1574 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1575 | else |
| 1576 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1577 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1578 | send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1579 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 1580 | if (changed) |
| 1581 | new_settings(hdev, cmd->sk); |
| 1582 | |
Johan Hedberg | 37438c1 | 2013-10-14 16:20:05 +0300 | [diff] [blame] | 1583 | remove_cmd: |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1584 | mgmt_pending_remove(cmd); |
| 1585 | |
| 1586 | unlock: |
| 1587 | hci_dev_unlock(hdev); |
| 1588 | } |
| 1589 | |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1590 | static int set_connectable_update_settings(struct hci_dev *hdev, |
| 1591 | struct sock *sk, u8 val) |
| 1592 | { |
| 1593 | bool changed = false; |
| 1594 | int err; |
| 1595 | |
| 1596 | if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 1597 | changed = true; |
| 1598 | |
| 1599 | if (val) { |
| 1600 | set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1601 | } else { |
| 1602 | clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 1603 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1604 | } |
| 1605 | |
| 1606 | err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); |
| 1607 | if (err < 0) |
| 1608 | return err; |
| 1609 | |
| 1610 | if (changed) |
| 1611 | return new_settings(hdev, sk); |
| 1612 | |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1616 | 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] | 1617 | u16 len) |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1618 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1619 | struct mgmt_mode *cp = data; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1620 | struct pending_cmd *cmd; |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1621 | struct hci_request req; |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1622 | u8 scan; |
Johan Hedberg | f7b64e69 | 2010-12-13 21:07:06 +0200 | [diff] [blame] | 1623 | int err; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 1624 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1625 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 1626 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1627 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 1628 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1629 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1630 | MGMT_STATUS_REJECTED); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1631 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1632 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1633 | return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
| 1634 | MGMT_STATUS_INVALID_PARAMS); |
| 1635 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1636 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1637 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1638 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | e8ba3a1f | 2013-10-19 23:38:18 +0300 | [diff] [blame] | 1639 | err = set_connectable_update_settings(hdev, sk, cp->val); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1640 | goto failed; |
| 1641 | } |
| 1642 | |
| 1643 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1644 | mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1645 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1646 | MGMT_STATUS_BUSY); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1647 | goto failed; |
| 1648 | } |
| 1649 | |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1650 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); |
| 1651 | if (!cmd) { |
| 1652 | err = -ENOMEM; |
| 1653 | goto failed; |
| 1654 | } |
| 1655 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1656 | hci_req_init(&req, hdev); |
| 1657 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 1658 | /* If BR/EDR is not enabled and we disable advertising as a |
| 1659 | * by-product of disabling connectable, we need to update the |
| 1660 | * advertising flags. |
| 1661 | */ |
| 1662 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 1663 | if (!cp->val) { |
| 1664 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
| 1665 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 1666 | } |
| 1667 | update_adv_data(&req); |
| 1668 | } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1669 | if (cp->val) { |
| 1670 | scan = SCAN_PAGE; |
| 1671 | } else { |
| 1672 | scan = 0; |
| 1673 | |
| 1674 | if (test_bit(HCI_ISCAN, &hdev->flags) && |
Marcel Holtmann | 8d6083f | 2013-10-14 16:38:45 -0700 | [diff] [blame] | 1675 | hdev->discov_timeout > 0) |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1676 | cancel_delayed_work(&hdev->discov_off); |
| 1677 | } |
| 1678 | |
| 1679 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 1680 | } |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1681 | |
Johan Hedberg | 4c01f8b | 2013-03-15 17:07:14 -0500 | [diff] [blame] | 1682 | /* If we're going from non-connectable to connectable or |
| 1683 | * vice-versa when fast connectable is enabled ensure that fast |
| 1684 | * connectable gets disabled. write_fast_connectable won't do |
| 1685 | * anything if the page scan parameters are already what they |
| 1686 | * should be. |
| 1687 | */ |
| 1688 | if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | e36a376 | 2013-03-15 17:07:10 -0500 | [diff] [blame] | 1689 | write_fast_connectable(&req, false); |
| 1690 | |
Johan Hedberg | 1987fdc | 2013-10-14 21:15:24 +0300 | [diff] [blame] | 1691 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) && |
| 1692 | hci_conn_num(hdev, LE_LINK) == 0) { |
| 1693 | disable_advertising(&req); |
| 1694 | enable_advertising(&req); |
| 1695 | } |
| 1696 | |
Johan Hedberg | 2b76f45 | 2013-03-15 17:07:04 -0500 | [diff] [blame] | 1697 | err = hci_req_run(&req, set_connectable_complete); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1698 | if (err < 0) { |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1699 | mgmt_pending_remove(cmd); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1700 | if (err == -ENODATA) |
Johan Hedberg | a81070b | 2013-10-19 23:38:19 +0300 | [diff] [blame] | 1701 | err = set_connectable_update_settings(hdev, sk, |
| 1702 | cp->val); |
Johan Hedberg | 9b74246 | 2013-10-14 16:20:03 +0300 | [diff] [blame] | 1703 | goto failed; |
| 1704 | } |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1705 | |
| 1706 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1707 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1708 | return err; |
| 1709 | } |
| 1710 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1711 | 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] | 1712 | u16 len) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1713 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 1714 | struct mgmt_mode *cp = data; |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1715 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 1716 | int err; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1717 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1718 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1719 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1720 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1721 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PAIRABLE, |
| 1722 | MGMT_STATUS_INVALID_PARAMS); |
| 1723 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1724 | hci_dev_lock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1725 | |
| 1726 | if (cp->val) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1727 | changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1728 | else |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1729 | changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1730 | |
Johan Hedberg | 69ab39e | 2011-12-15 00:47:35 +0200 | [diff] [blame] | 1731 | err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1732 | if (err < 0) |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1733 | goto unlock; |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1734 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1735 | if (changed) |
| 1736 | err = new_settings(hdev, sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1737 | |
Marcel Holtmann | 5559435 | 2013-10-06 16:11:57 -0700 | [diff] [blame] | 1738 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 1739 | hci_dev_unlock(hdev); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 1740 | return err; |
| 1741 | } |
Johan Hedberg | 72a734e | 2010-12-30 00:38:22 +0200 | [diff] [blame] | 1742 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1743 | static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data, |
| 1744 | u16 len) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1745 | { |
| 1746 | struct mgmt_mode *cp = data; |
| 1747 | struct pending_cmd *cmd; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1748 | u8 val, status; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1749 | int err; |
| 1750 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1751 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1752 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1753 | status = mgmt_bredr_support(hdev); |
| 1754 | if (status) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1755 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1756 | status); |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 1757 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1758 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1759 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
| 1760 | MGMT_STATUS_INVALID_PARAMS); |
| 1761 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1762 | hci_dev_lock(hdev); |
| 1763 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1764 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1765 | bool changed = false; |
| 1766 | |
| 1767 | if (!!cp->val != test_bit(HCI_LINK_SECURITY, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 1768 | &hdev->dev_flags)) { |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 1769 | change_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 1770 | changed = true; |
| 1771 | } |
| 1772 | |
| 1773 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1774 | if (err < 0) |
| 1775 | goto failed; |
| 1776 | |
| 1777 | if (changed) |
| 1778 | err = new_settings(hdev, sk); |
| 1779 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1780 | goto failed; |
| 1781 | } |
| 1782 | |
| 1783 | if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1784 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 1785 | MGMT_STATUS_BUSY); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1786 | goto failed; |
| 1787 | } |
| 1788 | |
| 1789 | val = !!cp->val; |
| 1790 | |
| 1791 | if (test_bit(HCI_AUTH, &hdev->flags) == val) { |
| 1792 | err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev); |
| 1793 | goto failed; |
| 1794 | } |
| 1795 | |
| 1796 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len); |
| 1797 | if (!cmd) { |
| 1798 | err = -ENOMEM; |
| 1799 | goto failed; |
| 1800 | } |
| 1801 | |
| 1802 | err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val); |
| 1803 | if (err < 0) { |
| 1804 | mgmt_pending_remove(cmd); |
| 1805 | goto failed; |
| 1806 | } |
| 1807 | |
| 1808 | failed: |
| 1809 | hci_dev_unlock(hdev); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 1810 | return err; |
| 1811 | } |
| 1812 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1813 | 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] | 1814 | { |
| 1815 | struct mgmt_mode *cp = data; |
| 1816 | struct pending_cmd *cmd; |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1817 | u8 status; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1818 | int err; |
| 1819 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1820 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1821 | |
Marcel Holtmann | cdba528 | 2013-10-02 21:31:52 -0700 | [diff] [blame] | 1822 | status = mgmt_bredr_support(hdev); |
| 1823 | if (status) |
| 1824 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); |
| 1825 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1826 | if (!lmp_ssp_capable(hdev)) |
| 1827 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1828 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1829 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1830 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1831 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1832 | MGMT_STATUS_INVALID_PARAMS); |
| 1833 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1834 | hci_dev_lock(hdev); |
Johan Hedberg | 6c8f12c | 2012-02-22 16:35:26 +0200 | [diff] [blame] | 1835 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 1836 | if (!hdev_is_powered(hdev)) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1837 | bool changed; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1838 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1839 | if (cp->val) { |
| 1840 | changed = !test_and_set_bit(HCI_SSP_ENABLED, |
| 1841 | &hdev->dev_flags); |
| 1842 | } else { |
| 1843 | changed = test_and_clear_bit(HCI_SSP_ENABLED, |
| 1844 | &hdev->dev_flags); |
| 1845 | if (!changed) |
| 1846 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 1847 | &hdev->dev_flags); |
| 1848 | else |
| 1849 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1853 | if (err < 0) |
| 1854 | goto failed; |
| 1855 | |
| 1856 | if (changed) |
| 1857 | err = new_settings(hdev, sk); |
| 1858 | |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1859 | goto failed; |
| 1860 | } |
| 1861 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1862 | if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || |
| 1863 | mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 1864 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, |
| 1865 | MGMT_STATUS_BUSY); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1866 | goto failed; |
| 1867 | } |
| 1868 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1869 | if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1870 | err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); |
| 1871 | goto failed; |
| 1872 | } |
| 1873 | |
| 1874 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len); |
| 1875 | if (!cmd) { |
| 1876 | err = -ENOMEM; |
| 1877 | goto failed; |
| 1878 | } |
| 1879 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 1880 | if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 1881 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 1882 | sizeof(cp->val), &cp->val); |
| 1883 | |
Marcel Holtmann | 72ef0c1 | 2013-10-10 03:08:10 -0700 | [diff] [blame] | 1884 | 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] | 1885 | if (err < 0) { |
| 1886 | mgmt_pending_remove(cmd); |
| 1887 | goto failed; |
| 1888 | } |
| 1889 | |
| 1890 | failed: |
| 1891 | hci_dev_unlock(hdev); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 1892 | return err; |
| 1893 | } |
| 1894 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1895 | 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] | 1896 | { |
| 1897 | struct mgmt_mode *cp = data; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1898 | bool changed; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1899 | u8 status; |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1900 | int err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1901 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1902 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1903 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 1904 | status = mgmt_bredr_support(hdev); |
| 1905 | if (status) |
| 1906 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1907 | |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 1908 | if (!lmp_ssp_capable(hdev)) |
| 1909 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1910 | MGMT_STATUS_NOT_SUPPORTED); |
| 1911 | |
| 1912 | if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) |
| 1913 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1914 | MGMT_STATUS_REJECTED); |
| 1915 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1916 | if (cp->val != 0x00 && cp->val != 0x01) |
| 1917 | return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1918 | MGMT_STATUS_INVALID_PARAMS); |
| 1919 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1920 | hci_dev_lock(hdev); |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1921 | |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1922 | if (cp->val) { |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1923 | changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1924 | } else { |
| 1925 | if (hdev_is_powered(hdev)) { |
| 1926 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, |
| 1927 | MGMT_STATUS_REJECTED); |
| 1928 | goto unlock; |
| 1929 | } |
| 1930 | |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1931 | changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | a0cdf960 | 2013-10-02 00:27:02 -0700 | [diff] [blame] | 1932 | } |
Marcel Holtmann | ee39269 | 2013-10-01 22:59:23 -0700 | [diff] [blame] | 1933 | |
| 1934 | err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); |
| 1935 | if (err < 0) |
| 1936 | goto unlock; |
| 1937 | |
| 1938 | if (changed) |
| 1939 | err = new_settings(hdev, sk); |
| 1940 | |
| 1941 | unlock: |
| 1942 | hci_dev_unlock(hdev); |
| 1943 | return err; |
Johan Hedberg | 6d80dfd | 2012-02-20 23:50:38 +0200 | [diff] [blame] | 1944 | } |
| 1945 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1946 | static void le_enable_complete(struct hci_dev *hdev, u8 status) |
| 1947 | { |
| 1948 | struct cmd_lookup match = { NULL, hdev }; |
| 1949 | |
| 1950 | if (status) { |
| 1951 | u8 mgmt_err = mgmt_status(status); |
| 1952 | |
| 1953 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp, |
| 1954 | &mgmt_err); |
| 1955 | return; |
| 1956 | } |
| 1957 | |
| 1958 | mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match); |
| 1959 | |
| 1960 | new_settings(hdev, match.sk); |
| 1961 | |
| 1962 | if (match.sk) |
| 1963 | sock_put(match.sk); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1964 | |
| 1965 | /* Make sure the controller has a good default for |
| 1966 | * advertising data. Restrict the update to when LE |
| 1967 | * has actually been enabled. During power on, the |
| 1968 | * update in powered_update_hci will take care of it. |
| 1969 | */ |
| 1970 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 1971 | struct hci_request req; |
| 1972 | |
| 1973 | hci_dev_lock(hdev); |
| 1974 | |
| 1975 | hci_req_init(&req, hdev); |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 1976 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 1977 | update_scan_rsp_data(&req); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 1978 | hci_req_run(&req, NULL); |
| 1979 | |
| 1980 | hci_dev_unlock(hdev); |
| 1981 | } |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1982 | } |
| 1983 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1984 | 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] | 1985 | { |
| 1986 | struct mgmt_mode *cp = data; |
| 1987 | struct hci_cp_write_le_host_supported hci_cp; |
| 1988 | struct pending_cmd *cmd; |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 1989 | struct hci_request req; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1990 | int err; |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 1991 | u8 val, enabled; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1992 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 1993 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 1994 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 1995 | if (!lmp_le_capable(hdev)) |
| 1996 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 1997 | MGMT_STATUS_NOT_SUPPORTED); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 1998 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 1999 | if (cp->val != 0x00 && cp->val != 0x01) |
| 2000 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2001 | MGMT_STATUS_INVALID_PARAMS); |
| 2002 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2003 | /* LE-only devices do not allow toggling LE on/off */ |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 2004 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 2005 | return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
| 2006 | MGMT_STATUS_REJECTED); |
| 2007 | |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2008 | hci_dev_lock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2009 | |
| 2010 | val = !!cp->val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2011 | enabled = lmp_host_le_capable(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2012 | |
Johan Hedberg | 0b60eba | 2012-02-28 00:57:24 +0200 | [diff] [blame] | 2013 | if (!hdev_is_powered(hdev) || val == enabled) { |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2014 | bool changed = false; |
| 2015 | |
| 2016 | if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
| 2017 | change_bit(HCI_LE_ENABLED, &hdev->dev_flags); |
| 2018 | changed = true; |
| 2019 | } |
| 2020 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 2021 | if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 2022 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 2023 | changed = true; |
| 2024 | } |
| 2025 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2026 | err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); |
| 2027 | if (err < 0) |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2028 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2029 | |
| 2030 | if (changed) |
| 2031 | err = new_settings(hdev, sk); |
| 2032 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2033 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2034 | } |
| 2035 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 2036 | if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || |
| 2037 | mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2038 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2039 | MGMT_STATUS_BUSY); |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2040 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len); |
| 2044 | if (!cmd) { |
| 2045 | err = -ENOMEM; |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2046 | goto unlock; |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2047 | } |
| 2048 | |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2049 | hci_req_init(&req, hdev); |
| 2050 | |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2051 | memset(&hci_cp, 0, sizeof(hci_cp)); |
| 2052 | |
| 2053 | if (val) { |
| 2054 | hci_cp.le = val; |
Gustavo Padovan | ffa88e0 | 2012-11-23 16:50:51 -0200 | [diff] [blame] | 2055 | hci_cp.simul = lmp_le_br_capable(hdev); |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 2056 | } else { |
| 2057 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 2058 | disable_advertising(&req); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2059 | } |
| 2060 | |
Johan Hedberg | 416a4ae | 2013-09-25 13:26:08 +0300 | [diff] [blame] | 2061 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), |
| 2062 | &hci_cp); |
| 2063 | |
| 2064 | err = hci_req_run(&req, le_enable_complete); |
Syam Sidhardhan | 0c01bc4 | 2012-04-12 20:33:21 +0530 | [diff] [blame] | 2065 | if (err < 0) |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2066 | mgmt_pending_remove(cmd); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2067 | |
Johan Hedberg | 1de028c | 2012-02-29 19:55:35 -0800 | [diff] [blame] | 2068 | unlock: |
| 2069 | hci_dev_unlock(hdev); |
Johan Hedberg | 06199cf | 2012-02-22 16:37:11 +0200 | [diff] [blame] | 2070 | return err; |
| 2071 | } |
| 2072 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2073 | /* This is a helper function to test for pending mgmt commands that can |
| 2074 | * cause CoD or EIR HCI commands. We can only allow one such pending |
| 2075 | * mgmt command at a time since otherwise we cannot easily track what |
| 2076 | * the current values are, will be, and based on that calculate if a new |
| 2077 | * HCI command needs to be sent and if yes with what value. |
| 2078 | */ |
| 2079 | static bool pending_eir_or_class(struct hci_dev *hdev) |
| 2080 | { |
| 2081 | struct pending_cmd *cmd; |
| 2082 | |
| 2083 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
| 2084 | switch (cmd->opcode) { |
| 2085 | case MGMT_OP_ADD_UUID: |
| 2086 | case MGMT_OP_REMOVE_UUID: |
| 2087 | case MGMT_OP_SET_DEV_CLASS: |
| 2088 | case MGMT_OP_SET_POWERED: |
| 2089 | return true; |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | return false; |
| 2094 | } |
| 2095 | |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2096 | static const u8 bluetooth_base_uuid[] = { |
| 2097 | 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, |
| 2098 | 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 2099 | }; |
| 2100 | |
| 2101 | static u8 get_uuid_size(const u8 *uuid) |
| 2102 | { |
| 2103 | u32 val; |
| 2104 | |
| 2105 | if (memcmp(uuid, bluetooth_base_uuid, 12)) |
| 2106 | return 128; |
| 2107 | |
| 2108 | val = get_unaligned_le32(&uuid[12]); |
| 2109 | if (val > 0xffff) |
| 2110 | return 32; |
| 2111 | |
| 2112 | return 16; |
| 2113 | } |
| 2114 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2115 | static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) |
| 2116 | { |
| 2117 | struct pending_cmd *cmd; |
| 2118 | |
| 2119 | hci_dev_lock(hdev); |
| 2120 | |
| 2121 | cmd = mgmt_pending_find(mgmt_op, hdev); |
| 2122 | if (!cmd) |
| 2123 | goto unlock; |
| 2124 | |
| 2125 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), |
| 2126 | hdev->dev_class, 3); |
| 2127 | |
| 2128 | mgmt_pending_remove(cmd); |
| 2129 | |
| 2130 | unlock: |
| 2131 | hci_dev_unlock(hdev); |
| 2132 | } |
| 2133 | |
| 2134 | static void add_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2135 | { |
| 2136 | BT_DBG("status 0x%02x", status); |
| 2137 | |
| 2138 | mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status); |
| 2139 | } |
| 2140 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2141 | 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] | 2142 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2143 | struct mgmt_cp_add_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2144 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2145 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2146 | struct bt_uuid *uuid; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2147 | int err; |
| 2148 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2149 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2150 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2151 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2152 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2153 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2154 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2155 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2156 | goto failed; |
| 2157 | } |
| 2158 | |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2159 | uuid = kmalloc(sizeof(*uuid), GFP_KERNEL); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2160 | if (!uuid) { |
| 2161 | err = -ENOMEM; |
| 2162 | goto failed; |
| 2163 | } |
| 2164 | |
| 2165 | memcpy(uuid->uuid, cp->uuid, 16); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2166 | uuid->svc_hint = cp->svc_hint; |
Johan Hedberg | 83be8ec | 2013-01-27 00:31:29 +0200 | [diff] [blame] | 2167 | uuid->size = get_uuid_size(cp->uuid); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2168 | |
Johan Hedberg | de66aa6 | 2013-01-27 00:31:27 +0200 | [diff] [blame] | 2169 | list_add_tail(&uuid->list, &hdev->uuids); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2170 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2171 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2172 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2173 | update_class(&req); |
| 2174 | update_eir(&req); |
| 2175 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2176 | err = hci_req_run(&req, add_uuid_complete); |
| 2177 | if (err < 0) { |
| 2178 | if (err != -ENODATA) |
| 2179 | goto failed; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2180 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2181 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2182 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2183 | goto failed; |
| 2184 | } |
| 2185 | |
| 2186 | cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2187 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2188 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2189 | goto failed; |
| 2190 | } |
| 2191 | |
| 2192 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2193 | |
| 2194 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2195 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2196 | return err; |
| 2197 | } |
| 2198 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2199 | static bool enable_service_cache(struct hci_dev *hdev) |
| 2200 | { |
| 2201 | if (!hdev_is_powered(hdev)) |
| 2202 | return false; |
| 2203 | |
| 2204 | if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 46818ed | 2013-01-14 22:33:52 +0200 | [diff] [blame] | 2205 | queue_delayed_work(hdev->workqueue, &hdev->service_cache, |
| 2206 | CACHE_TIMEOUT); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2207 | return true; |
| 2208 | } |
| 2209 | |
| 2210 | return false; |
| 2211 | } |
| 2212 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2213 | static void remove_uuid_complete(struct hci_dev *hdev, u8 status) |
| 2214 | { |
| 2215 | BT_DBG("status 0x%02x", status); |
| 2216 | |
| 2217 | mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status); |
| 2218 | } |
| 2219 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2220 | 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] | 2221 | u16 len) |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2222 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2223 | struct mgmt_cp_remove_uuid *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2224 | struct pending_cmd *cmd; |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2225 | struct bt_uuid *match, *tmp; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2226 | 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] | 2227 | struct hci_request req; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2228 | int err, found; |
| 2229 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2230 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2231 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2232 | hci_dev_lock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2233 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2234 | if (pending_eir_or_class(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2235 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2236 | MGMT_STATUS_BUSY); |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 2237 | goto unlock; |
| 2238 | } |
| 2239 | |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2240 | if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) { |
Johan Hedberg | 35f7498 | 2014-02-18 17:14:32 +0200 | [diff] [blame] | 2241 | hci_uuids_clear(hdev); |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2242 | |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2243 | if (enable_service_cache(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2244 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2245 | 0, hdev->dev_class, 3); |
Johan Hedberg | 24b78d0 | 2012-02-23 23:24:30 +0200 | [diff] [blame] | 2246 | goto unlock; |
| 2247 | } |
Johan Hedberg | 4004b6d | 2012-02-23 21:30:12 +0200 | [diff] [blame] | 2248 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2249 | goto update_class; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | found = 0; |
| 2253 | |
Johan Hedberg | 056341c | 2013-01-27 00:31:30 +0200 | [diff] [blame] | 2254 | list_for_each_entry_safe(match, tmp, &hdev->uuids, list) { |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2255 | if (memcmp(match->uuid, cp->uuid, 16) != 0) |
| 2256 | continue; |
| 2257 | |
| 2258 | list_del(&match->list); |
Johan Hedberg | 482049f | 2012-11-08 10:25:26 +0100 | [diff] [blame] | 2259 | kfree(match); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2260 | found++; |
| 2261 | } |
| 2262 | |
| 2263 | if (found == 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2264 | err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2265 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2266 | goto unlock; |
| 2267 | } |
| 2268 | |
Johan Hedberg | 9246a86 | 2012-02-23 21:33:16 +0200 | [diff] [blame] | 2269 | update_class: |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2270 | hci_req_init(&req, hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2271 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2272 | update_class(&req); |
| 2273 | update_eir(&req); |
| 2274 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2275 | err = hci_req_run(&req, remove_uuid_complete); |
| 2276 | if (err < 0) { |
| 2277 | if (err != -ENODATA) |
| 2278 | goto unlock; |
Johan Hedberg | 80a1e1d | 2011-03-28 14:07:23 +0300 | [diff] [blame] | 2279 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2280 | err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2281 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2282 | goto unlock; |
| 2283 | } |
| 2284 | |
| 2285 | cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2286 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2287 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2288 | goto unlock; |
| 2289 | } |
| 2290 | |
| 2291 | err = 0; |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2292 | |
| 2293 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2294 | hci_dev_unlock(hdev); |
Johan Hedberg | 2aeb9a1 | 2011-01-04 12:08:51 +0200 | [diff] [blame] | 2295 | return err; |
| 2296 | } |
| 2297 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2298 | static void set_class_complete(struct hci_dev *hdev, u8 status) |
| 2299 | { |
| 2300 | BT_DBG("status 0x%02x", status); |
| 2301 | |
| 2302 | mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status); |
| 2303 | } |
| 2304 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2305 | 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] | 2306 | u16 len) |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2307 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2308 | struct mgmt_cp_set_dev_class *cp = data; |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2309 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2310 | struct hci_request req; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2311 | int err; |
| 2312 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2313 | BT_DBG("request for %s", hdev->name); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2314 | |
Marcel Holtmann | 6203fc9 | 2013-10-02 23:37:29 -0700 | [diff] [blame] | 2315 | if (!lmp_bredr_capable(hdev)) |
Johan Hedberg | 13ecd8b | 2013-01-09 15:29:38 +0200 | [diff] [blame] | 2316 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2317 | MGMT_STATUS_NOT_SUPPORTED); |
| 2318 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2319 | hci_dev_lock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2320 | |
Johan Hedberg | 0cab9c8 | 2013-03-15 17:06:54 -0500 | [diff] [blame] | 2321 | if (pending_eir_or_class(hdev)) { |
| 2322 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2323 | MGMT_STATUS_BUSY); |
| 2324 | goto unlock; |
| 2325 | } |
| 2326 | |
| 2327 | if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { |
| 2328 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, |
| 2329 | MGMT_STATUS_INVALID_PARAMS); |
| 2330 | goto unlock; |
| 2331 | } |
| 2332 | |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2333 | hdev->major_class = cp->major; |
| 2334 | hdev->minor_class = cp->minor; |
| 2335 | |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2336 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2337 | 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] | 2338 | hdev->dev_class, 3); |
Johan Hedberg | 932f5ff | 2012-02-22 22:11:32 +0200 | [diff] [blame] | 2339 | goto unlock; |
| 2340 | } |
| 2341 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2342 | hci_req_init(&req, hdev); |
| 2343 | |
Johan Hedberg | a8b2d5c | 2012-01-08 23:11:15 +0200 | [diff] [blame] | 2344 | if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2345 | hci_dev_unlock(hdev); |
| 2346 | cancel_delayed_work_sync(&hdev->service_cache); |
| 2347 | hci_dev_lock(hdev); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2348 | update_eir(&req); |
Johan Hedberg | 7d78525 | 2011-12-15 00:47:39 +0200 | [diff] [blame] | 2349 | } |
Johan Hedberg | 14c0b60 | 2011-12-15 00:47:37 +0200 | [diff] [blame] | 2350 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2351 | update_class(&req); |
| 2352 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 2353 | err = hci_req_run(&req, set_class_complete); |
| 2354 | if (err < 0) { |
| 2355 | if (err != -ENODATA) |
| 2356 | goto unlock; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2357 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2358 | 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] | 2359 | hdev->dev_class, 3); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2360 | goto unlock; |
| 2361 | } |
| 2362 | |
| 2363 | 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] | 2364 | if (!cmd) { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 2365 | err = -ENOMEM; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 2366 | goto unlock; |
| 2367 | } |
| 2368 | |
| 2369 | err = 0; |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2370 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 2371 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2372 | hci_dev_unlock(hdev); |
Johan Hedberg | 1aff6f0 | 2011-01-13 21:56:52 +0200 | [diff] [blame] | 2373 | return err; |
| 2374 | } |
| 2375 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2376 | 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] | 2377 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2378 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2379 | struct mgmt_cp_load_link_keys *cp = data; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 2380 | u16 key_count, expected_len; |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2381 | bool changed; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2382 | int i; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2383 | |
Marcel Holtmann | 9060d5c | 2013-10-02 21:16:07 -0700 | [diff] [blame] | 2384 | BT_DBG("request for %s", hdev->name); |
| 2385 | |
| 2386 | if (!lmp_bredr_capable(hdev)) |
| 2387 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2388 | MGMT_STATUS_NOT_SUPPORTED); |
| 2389 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 2390 | key_count = __le16_to_cpu(cp->key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2391 | |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2392 | expected_len = sizeof(*cp) + key_count * |
| 2393 | sizeof(struct mgmt_link_key_info); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2394 | if (expected_len != len) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2395 | BT_ERR("load_link_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 2396 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2397 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2398 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2399 | } |
| 2400 | |
Johan Hedberg | 4ae14301 | 2013-01-20 14:27:13 +0200 | [diff] [blame] | 2401 | if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) |
| 2402 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2403 | MGMT_STATUS_INVALID_PARAMS); |
| 2404 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2405 | 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] | 2406 | key_count); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2407 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2408 | for (i = 0; i < key_count; i++) { |
| 2409 | struct mgmt_link_key_info *key = &cp->keys[i]; |
| 2410 | |
Marcel Holtmann | 8e99113 | 2014-01-10 02:07:25 -0800 | [diff] [blame] | 2411 | if (key->addr.type != BDADDR_BREDR || key->type > 0x08) |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2412 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, |
| 2413 | MGMT_STATUS_INVALID_PARAMS); |
| 2414 | } |
| 2415 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2416 | hci_dev_lock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2417 | |
| 2418 | hci_link_keys_clear(hdev); |
| 2419 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2420 | if (cp->debug_keys) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2421 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 2422 | &hdev->dev_flags); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2423 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 2424 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 2425 | &hdev->dev_flags); |
Marcel Holtmann | b1de97d | 2014-01-31 11:55:21 -0800 | [diff] [blame] | 2426 | |
| 2427 | if (changed) |
| 2428 | new_settings(hdev, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2429 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 2430 | for (i = 0; i < key_count; i++) { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 2431 | struct mgmt_link_key_info *key = &cp->keys[i]; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2432 | |
Johan Hedberg | 58e9293 | 2014-06-24 14:00:26 +0300 | [diff] [blame] | 2433 | /* Always ignore debug keys and require a new pairing if |
| 2434 | * the user wants to use them. |
| 2435 | */ |
| 2436 | if (key->type == HCI_LK_DEBUG_COMBINATION) |
| 2437 | continue; |
| 2438 | |
Johan Hedberg | 7652ff6 | 2014-06-24 13:15:49 +0300 | [diff] [blame] | 2439 | hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val, |
| 2440 | key->type, key->pin_len, NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2441 | } |
| 2442 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2443 | 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] | 2444 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2445 | hci_dev_unlock(hdev); |
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 | return 0; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2448 | } |
| 2449 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2450 | static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2451 | u8 addr_type, struct sock *skip_sk) |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2452 | { |
| 2453 | struct mgmt_ev_device_unpaired ev; |
| 2454 | |
| 2455 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 2456 | ev.addr.type = addr_type; |
| 2457 | |
| 2458 | return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2459 | skip_sk); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2460 | } |
| 2461 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2462 | 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] | 2463 | u16 len) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2464 | { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2465 | struct mgmt_cp_unpair_device *cp = data; |
| 2466 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2467 | struct hci_cp_disconnect dc; |
| 2468 | struct pending_cmd *cmd; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2469 | struct hci_conn *conn; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2470 | int err; |
| 2471 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2472 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2473 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2474 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2475 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2476 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2477 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2478 | MGMT_STATUS_INVALID_PARAMS, |
| 2479 | &rp, sizeof(rp)); |
| 2480 | |
Johan Hedberg | 118da70 | 2013-01-20 14:27:20 +0200 | [diff] [blame] | 2481 | if (cp->disconnect != 0x00 && cp->disconnect != 0x01) |
| 2482 | return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
| 2483 | MGMT_STATUS_INVALID_PARAMS, |
| 2484 | &rp, sizeof(rp)); |
| 2485 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2486 | hci_dev_lock(hdev); |
| 2487 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2488 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2489 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2490 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2491 | goto unlock; |
| 2492 | } |
| 2493 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2494 | if (cp->addr.type == BDADDR_BREDR) { |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2495 | err = hci_remove_link_key(hdev, &cp->addr.bdaddr); |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2496 | } else { |
| 2497 | u8 addr_type; |
| 2498 | |
| 2499 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2500 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2501 | else |
| 2502 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2503 | |
Johan Hedberg | a7ec733 | 2014-02-18 17:14:35 +0200 | [diff] [blame] | 2504 | hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type); |
| 2505 | |
Andre Guedes | a9b0a04 | 2014-02-26 20:21:52 -0300 | [diff] [blame] | 2506 | hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type); |
| 2507 | |
Johan Hedberg | e0b2b27 | 2014-02-18 17:14:31 +0200 | [diff] [blame] | 2508 | err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type); |
| 2509 | } |
Vinicius Costa Gomes | b0dbfb4 | 2012-02-02 21:08:03 -0300 | [diff] [blame] | 2510 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2511 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2512 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2513 | MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2514 | goto unlock; |
| 2515 | } |
| 2516 | |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2517 | if (cp->disconnect) { |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2518 | if (cp->addr.type == BDADDR_BREDR) |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2519 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2520 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2521 | else |
| 2522 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2523 | &cp->addr.bdaddr); |
Johan Hedberg | 86a8cfc | 2012-02-22 22:53:34 +0200 | [diff] [blame] | 2524 | } else { |
| 2525 | conn = NULL; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2526 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2527 | |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2528 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2529 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2530 | &rp, sizeof(rp)); |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 2531 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2532 | goto unlock; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2533 | } |
| 2534 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 2535 | cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2536 | sizeof(*cp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2537 | if (!cmd) { |
| 2538 | err = -ENOMEM; |
| 2539 | goto unlock; |
| 2540 | } |
| 2541 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2542 | dc.handle = cpu_to_le16(conn->handle); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 2543 | dc.reason = 0x13; /* Remote User Terminated Connection */ |
| 2544 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2545 | if (err < 0) |
| 2546 | mgmt_pending_remove(cmd); |
| 2547 | |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2548 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2549 | hci_dev_unlock(hdev); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 2550 | return err; |
| 2551 | } |
| 2552 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2553 | 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] | 2554 | u16 len) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2555 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2556 | struct mgmt_cp_disconnect *cp = data; |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2557 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2558 | struct hci_cp_disconnect dc; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2559 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2560 | struct hci_conn *conn; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2561 | int err; |
| 2562 | |
| 2563 | BT_DBG(""); |
| 2564 | |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2565 | memset(&rp, 0, sizeof(rp)); |
| 2566 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2567 | rp.addr.type = cp->addr.type; |
| 2568 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2569 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2570 | return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2571 | MGMT_STATUS_INVALID_PARAMS, |
| 2572 | &rp, sizeof(rp)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2573 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2574 | hci_dev_lock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2575 | |
| 2576 | if (!test_bit(HCI_UP, &hdev->flags)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2577 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2578 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2579 | goto failed; |
| 2580 | } |
| 2581 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2582 | if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2583 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2584 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2585 | goto failed; |
| 2586 | } |
| 2587 | |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2588 | if (cp->addr.type == BDADDR_BREDR) |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 2589 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 2590 | &cp->addr.bdaddr); |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 2591 | else |
| 2592 | 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] | 2593 | |
Vishal Agarwal | f960727 | 2012-06-13 05:32:43 +0530 | [diff] [blame] | 2594 | if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { |
Johan Hedberg | 06a63b1 | 2013-01-20 14:27:21 +0200 | [diff] [blame] | 2595 | err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, |
| 2596 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2597 | goto failed; |
| 2598 | } |
| 2599 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2600 | cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len); |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2601 | if (!cmd) { |
| 2602 | err = -ENOMEM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2603 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2604 | } |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2605 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2606 | dc.handle = cpu_to_le16(conn->handle); |
Andre Guedes | 3701f94 | 2012-06-11 18:41:12 -0300 | [diff] [blame] | 2607 | dc.reason = HCI_ERROR_REMOTE_USER_TERM; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2608 | |
| 2609 | err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 2610 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2611 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2612 | |
| 2613 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2614 | hci_dev_unlock(hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 2615 | return err; |
| 2616 | } |
| 2617 | |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2618 | static u8 link_to_bdaddr(u8 link_type, u8 addr_type) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2619 | { |
| 2620 | switch (link_type) { |
| 2621 | case LE_LINK: |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2622 | switch (addr_type) { |
| 2623 | case ADDR_LE_DEV_PUBLIC: |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2624 | return BDADDR_LE_PUBLIC; |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2625 | |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2626 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2627 | /* Fallback to LE Random address type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2628 | return BDADDR_LE_RANDOM; |
Johan Hedberg | 48264f0 | 2011-11-09 13:58:58 +0200 | [diff] [blame] | 2629 | } |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2630 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2631 | default: |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2632 | /* Fallback to BR/EDR type */ |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 2633 | return BDADDR_BREDR; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2634 | } |
| 2635 | } |
| 2636 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2637 | static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2638 | u16 data_len) |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2639 | { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2640 | struct mgmt_rp_get_connections *rp; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2641 | struct hci_conn *c; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2642 | size_t rp_len; |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2643 | int err; |
| 2644 | u16 i; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2645 | |
| 2646 | BT_DBG(""); |
| 2647 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2648 | hci_dev_lock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2649 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2650 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2651 | err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2652 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2653 | goto unlock; |
| 2654 | } |
| 2655 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2656 | i = 0; |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2657 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
| 2658 | if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2659 | i++; |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2660 | } |
| 2661 | |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2662 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Andre Guedes | 92c4c20 | 2012-06-07 19:05:44 -0300 | [diff] [blame] | 2663 | rp = kmalloc(rp_len, GFP_KERNEL); |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2664 | if (!rp) { |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2665 | err = -ENOMEM; |
| 2666 | goto unlock; |
| 2667 | } |
| 2668 | |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2669 | i = 0; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2670 | list_for_each_entry(c, &hdev->conn_hash.list, list) { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 2671 | if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags)) |
| 2672 | continue; |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2673 | bacpy(&rp->addr[i].bdaddr, &c->dst); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 2674 | rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type); |
Andre Guedes | 0ed0914 | 2012-04-03 08:46:54 -0300 | [diff] [blame] | 2675 | if (c->type == SCO_LINK || c->type == ESCO_LINK) |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2676 | continue; |
| 2677 | i++; |
| 2678 | } |
| 2679 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 2680 | rp->conn_count = cpu_to_le16(i); |
Johan Hedberg | 60fc5fb6 | 2012-02-23 09:52:28 +0200 | [diff] [blame] | 2681 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 2682 | /* Recalculate length in case of filtered SCO connections, etc */ |
| 2683 | rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2684 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2685 | 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] | 2686 | rp_len); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2687 | |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 2688 | kfree(rp); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2689 | |
| 2690 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2691 | hci_dev_unlock(hdev); |
Johan Hedberg | 2784eb4 | 2011-01-21 13:56:35 +0200 | [diff] [blame] | 2692 | return err; |
| 2693 | } |
| 2694 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2695 | 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] | 2696 | struct mgmt_cp_pin_code_neg_reply *cp) |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2697 | { |
| 2698 | struct pending_cmd *cmd; |
| 2699 | int err; |
| 2700 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2701 | 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] | 2702 | sizeof(*cp)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2703 | if (!cmd) |
| 2704 | return -ENOMEM; |
| 2705 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2706 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2707 | sizeof(cp->addr.bdaddr), &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2708 | if (err < 0) |
| 2709 | mgmt_pending_remove(cmd); |
| 2710 | |
| 2711 | return err; |
| 2712 | } |
| 2713 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2714 | 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] | 2715 | u16 len) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2716 | { |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2717 | struct hci_conn *conn; |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2718 | struct mgmt_cp_pin_code_reply *cp = data; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2719 | struct hci_cp_pin_code_reply reply; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2720 | struct pending_cmd *cmd; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2721 | int err; |
| 2722 | |
| 2723 | BT_DBG(""); |
| 2724 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2725 | hci_dev_lock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2726 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 2727 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2728 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2729 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2730 | goto failed; |
| 2731 | } |
| 2732 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2733 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2734 | if (!conn) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2735 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2736 | MGMT_STATUS_NOT_CONNECTED); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2737 | goto failed; |
| 2738 | } |
| 2739 | |
| 2740 | if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) { |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2741 | struct mgmt_cp_pin_code_neg_reply ncp; |
| 2742 | |
| 2743 | memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr)); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2744 | |
| 2745 | BT_ERR("PIN code is not 16 bytes long"); |
| 2746 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2747 | err = send_pin_code_neg_reply(sk, hdev, &ncp); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2748 | if (err >= 0) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2749 | err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2750 | MGMT_STATUS_INVALID_PARAMS); |
Waldemar Rymarkiewicz | 96d97a6 | 2011-06-01 17:28:48 +0200 | [diff] [blame] | 2751 | |
| 2752 | goto failed; |
| 2753 | } |
| 2754 | |
Gustavo F. Padovan | 00abfe4 | 2012-03-01 00:37:10 -0300 | [diff] [blame] | 2755 | 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] | 2756 | if (!cmd) { |
| 2757 | err = -ENOMEM; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2758 | goto failed; |
Johan Hedberg | 366a033 | 2011-02-19 12:05:55 -0300 | [diff] [blame] | 2759 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2760 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 2761 | bacpy(&reply.bdaddr, &cp->addr.bdaddr); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2762 | reply.pin_len = cp->pin_len; |
Waldemar Rymarkiewicz | 24718ca | 2011-06-01 17:28:47 +0200 | [diff] [blame] | 2763 | memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2764 | |
| 2765 | err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply); |
| 2766 | if (err < 0) |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2767 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2768 | |
| 2769 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2770 | hci_dev_unlock(hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 2771 | return err; |
| 2772 | } |
| 2773 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2774 | static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2775 | u16 len) |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2776 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2777 | struct mgmt_cp_set_io_capability *cp = data; |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2778 | |
| 2779 | BT_DBG(""); |
| 2780 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2781 | if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) |
| 2782 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, |
| 2783 | MGMT_STATUS_INVALID_PARAMS, NULL, 0); |
| 2784 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2785 | hci_dev_lock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2786 | |
| 2787 | hdev->io_capability = cp->io_capability; |
| 2788 | |
| 2789 | BT_DBG("%s IO capability set to 0x%02x", hdev->name, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2790 | hdev->io_capability); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2791 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2792 | hci_dev_unlock(hdev); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2793 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2794 | return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, |
| 2795 | 0); |
Johan Hedberg | 17fa4b9 | 2011-01-25 13:28:33 +0200 | [diff] [blame] | 2796 | } |
| 2797 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 2798 | static struct pending_cmd *find_pairing(struct hci_conn *conn) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2799 | { |
| 2800 | struct hci_dev *hdev = conn->hdev; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 2801 | struct pending_cmd *cmd; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2802 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2803 | list_for_each_entry(cmd, &hdev->mgmt_pending, list) { |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2804 | if (cmd->opcode != MGMT_OP_PAIR_DEVICE) |
| 2805 | continue; |
| 2806 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2807 | if (cmd->user_data != conn) |
| 2808 | continue; |
| 2809 | |
| 2810 | return cmd; |
| 2811 | } |
| 2812 | |
| 2813 | return NULL; |
| 2814 | } |
| 2815 | |
| 2816 | static void pairing_complete(struct pending_cmd *cmd, u8 status) |
| 2817 | { |
| 2818 | struct mgmt_rp_pair_device rp; |
| 2819 | struct hci_conn *conn = cmd->user_data; |
| 2820 | |
Johan Hedberg | 61b1a7f | 2014-03-20 12:54:16 +0200 | [diff] [blame] | 2821 | bacpy(&rp.addr.bdaddr, &conn->dst); |
| 2822 | rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2823 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 2824 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2825 | &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2826 | |
| 2827 | /* So we don't get further callbacks for this connection */ |
| 2828 | conn->connect_cfm_cb = NULL; |
| 2829 | conn->security_cfm_cb = NULL; |
| 2830 | conn->disconn_cfm_cb = NULL; |
| 2831 | |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2832 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2833 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 2834 | mgmt_pending_remove(cmd); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2835 | } |
| 2836 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2837 | void mgmt_smp_complete(struct hci_conn *conn, bool complete) |
| 2838 | { |
| 2839 | u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; |
| 2840 | struct pending_cmd *cmd; |
| 2841 | |
| 2842 | cmd = find_pairing(conn); |
| 2843 | if (cmd) |
| 2844 | pairing_complete(cmd, status); |
| 2845 | } |
| 2846 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2847 | static void pairing_complete_cb(struct hci_conn *conn, u8 status) |
| 2848 | { |
| 2849 | struct pending_cmd *cmd; |
| 2850 | |
| 2851 | BT_DBG("status %u", status); |
| 2852 | |
Johan Hedberg | 56e5cb8 | 2011-11-08 20:40:16 +0200 | [diff] [blame] | 2853 | cmd = find_pairing(conn); |
| 2854 | if (!cmd) |
| 2855 | BT_DBG("Unable to find a pending command"); |
| 2856 | else |
Johan Hedberg | e211326 | 2012-02-18 15:20:03 +0200 | [diff] [blame] | 2857 | pairing_complete(cmd, mgmt_status(status)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2858 | } |
| 2859 | |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2860 | static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) |
Vishal Agarwal | 4c47d73 | 2012-06-07 20:27:35 +0530 | [diff] [blame] | 2861 | { |
| 2862 | struct pending_cmd *cmd; |
| 2863 | |
| 2864 | BT_DBG("status %u", status); |
| 2865 | |
| 2866 | if (!status) |
| 2867 | return; |
| 2868 | |
| 2869 | cmd = find_pairing(conn); |
| 2870 | if (!cmd) |
| 2871 | BT_DBG("Unable to find a pending command"); |
| 2872 | else |
| 2873 | pairing_complete(cmd, mgmt_status(status)); |
| 2874 | } |
| 2875 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2876 | 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] | 2877 | u16 len) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2878 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 2879 | struct mgmt_cp_pair_device *cp = data; |
Johan Hedberg | 1425acb | 2011-11-11 00:07:35 +0200 | [diff] [blame] | 2880 | struct mgmt_rp_pair_device rp; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2881 | struct pending_cmd *cmd; |
| 2882 | u8 sec_level, auth_type; |
| 2883 | struct hci_conn *conn; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2884 | int err; |
| 2885 | |
| 2886 | BT_DBG(""); |
| 2887 | |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2888 | memset(&rp, 0, sizeof(rp)); |
| 2889 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 2890 | rp.addr.type = cp->addr.type; |
| 2891 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 2892 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 2893 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2894 | MGMT_STATUS_INVALID_PARAMS, |
| 2895 | &rp, sizeof(rp)); |
| 2896 | |
Johan Hedberg | 4ec86d4 | 2014-06-17 15:14:48 +0300 | [diff] [blame] | 2897 | if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) |
| 2898 | return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2899 | MGMT_STATUS_INVALID_PARAMS, |
| 2900 | &rp, sizeof(rp)); |
| 2901 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2902 | hci_dev_lock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2903 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2904 | if (!hdev_is_powered(hdev)) { |
Szymon Janc | f950a30e | 2013-01-18 12:48:07 +0100 | [diff] [blame] | 2905 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
| 2906 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2907 | goto unlock; |
| 2908 | } |
| 2909 | |
Vinicius Costa Gomes | c908df3 | 2011-09-02 14:51:22 -0300 | [diff] [blame] | 2910 | sec_level = BT_SECURITY_MEDIUM; |
Mikel Astiz | 6fd6b91 | 2014-04-08 14:21:32 +0200 | [diff] [blame] | 2911 | auth_type = HCI_AT_DEDICATED_BONDING; |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2912 | |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2913 | if (cp->addr.type == BDADDR_BREDR) { |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2914 | conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level, |
| 2915 | auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2916 | } else { |
| 2917 | u8 addr_type; |
| 2918 | |
| 2919 | /* Convert from L2CAP channel address type to HCI address type |
| 2920 | */ |
| 2921 | if (cp->addr.type == BDADDR_LE_PUBLIC) |
| 2922 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 2923 | else |
| 2924 | addr_type = ADDR_LE_DEV_RANDOM; |
| 2925 | |
| 2926 | conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type, |
Andre Guedes | 04a6c58 | 2014-02-26 20:21:44 -0300 | [diff] [blame] | 2927 | sec_level, auth_type); |
Andre Guedes | 6f77d8c | 2014-02-26 20:21:45 -0300 | [diff] [blame] | 2928 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2929 | |
Ville Tervo | 30e7627 | 2011-02-22 16:10:53 -0300 | [diff] [blame] | 2930 | if (IS_ERR(conn)) { |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2931 | int status; |
| 2932 | |
| 2933 | if (PTR_ERR(conn) == -EBUSY) |
| 2934 | status = MGMT_STATUS_BUSY; |
| 2935 | else |
| 2936 | status = MGMT_STATUS_CONNECT_FAILED; |
| 2937 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2938 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Andrzej Kaczmarek | 489dc48 | 2012-05-30 15:39:22 +0200 | [diff] [blame] | 2939 | status, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2940 | sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2941 | goto unlock; |
| 2942 | } |
| 2943 | |
| 2944 | if (conn->connect_cfm_cb) { |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2945 | hci_conn_drop(conn); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2946 | err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2947 | MGMT_STATUS_BUSY, &rp, sizeof(rp)); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2948 | goto unlock; |
| 2949 | } |
| 2950 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 2951 | cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2952 | if (!cmd) { |
| 2953 | err = -ENOMEM; |
David Herrmann | 76a68ba | 2013-04-06 20:28:37 +0200 | [diff] [blame] | 2954 | hci_conn_drop(conn); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2955 | goto unlock; |
| 2956 | } |
| 2957 | |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2958 | /* For LE, just connecting isn't a proof that the pairing finished */ |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2959 | if (cp->addr.type == BDADDR_BREDR) { |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2960 | conn->connect_cfm_cb = pairing_complete_cb; |
Johan Hedberg | f4a407b | 2014-02-18 21:41:34 +0200 | [diff] [blame] | 2961 | conn->security_cfm_cb = pairing_complete_cb; |
| 2962 | conn->disconn_cfm_cb = pairing_complete_cb; |
| 2963 | } else { |
| 2964 | conn->connect_cfm_cb = le_pairing_complete_cb; |
| 2965 | conn->security_cfm_cb = le_pairing_complete_cb; |
| 2966 | conn->disconn_cfm_cb = le_pairing_complete_cb; |
| 2967 | } |
Vinicius Costa Gomes | 7a512d0 | 2011-08-19 21:06:54 -0300 | [diff] [blame] | 2968 | |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2969 | conn->io_capability = cp->io_cap; |
| 2970 | cmd->user_data = conn; |
| 2971 | |
| 2972 | if (conn->state == BT_CONNECTED && |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 2973 | hci_conn_security(conn, sec_level, auth_type)) |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2974 | pairing_complete(cmd, 0); |
| 2975 | |
| 2976 | err = 0; |
| 2977 | |
| 2978 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 2979 | hci_dev_unlock(hdev); |
Johan Hedberg | e9a416b | 2011-02-19 12:05:56 -0300 | [diff] [blame] | 2980 | return err; |
| 2981 | } |
| 2982 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2983 | static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data, |
| 2984 | u16 len) |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2985 | { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 2986 | struct mgmt_addr_info *addr = data; |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2987 | struct pending_cmd *cmd; |
| 2988 | struct hci_conn *conn; |
| 2989 | int err; |
| 2990 | |
| 2991 | BT_DBG(""); |
| 2992 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 2993 | hci_dev_lock(hdev); |
| 2994 | |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2995 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 2996 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 2997 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5f97c1d | 2012-02-22 22:41:18 +0200 | [diff] [blame] | 2998 | goto unlock; |
| 2999 | } |
| 3000 | |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3001 | cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); |
| 3002 | if (!cmd) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3003 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3004 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3005 | goto unlock; |
| 3006 | } |
| 3007 | |
| 3008 | conn = cmd->user_data; |
| 3009 | |
| 3010 | if (bacmp(&addr->bdaddr, &conn->dst) != 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3011 | err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3012 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3013 | goto unlock; |
| 3014 | } |
| 3015 | |
| 3016 | pairing_complete(cmd, MGMT_STATUS_CANCELLED); |
| 3017 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3018 | 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] | 3019 | addr, sizeof(*addr)); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3020 | unlock: |
| 3021 | hci_dev_unlock(hdev); |
Johan Hedberg | 2842470 | 2012-02-02 04:02:29 +0200 | [diff] [blame] | 3022 | return err; |
| 3023 | } |
| 3024 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3025 | static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev, |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3026 | struct mgmt_addr_info *addr, u16 mgmt_op, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3027 | u16 hci_op, __le32 passkey) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3028 | { |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3029 | struct pending_cmd *cmd; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3030 | struct hci_conn *conn; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3031 | int err; |
| 3032 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3033 | hci_dev_lock(hdev); |
Johan Hedberg | 08ba538 | 2011-03-16 14:29:34 +0200 | [diff] [blame] | 3034 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3035 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3036 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3037 | MGMT_STATUS_NOT_POWERED, addr, |
| 3038 | sizeof(*addr)); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3039 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3040 | } |
| 3041 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3042 | if (addr->type == BDADDR_BREDR) |
| 3043 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3044 | else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3045 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3046 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3047 | if (!conn) { |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3048 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3049 | MGMT_STATUS_NOT_CONNECTED, addr, |
| 3050 | sizeof(*addr)); |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 3051 | goto done; |
| 3052 | } |
| 3053 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3054 | if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3055 | err = smp_user_confirm_reply(conn, mgmt_op, passkey); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3056 | if (!err) |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3057 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3058 | MGMT_STATUS_SUCCESS, addr, |
| 3059 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3060 | else |
Johan Hedberg | feb94d3 | 2013-03-15 17:07:16 -0500 | [diff] [blame] | 3061 | err = cmd_complete(sk, hdev->id, mgmt_op, |
| 3062 | MGMT_STATUS_FAILED, addr, |
| 3063 | sizeof(*addr)); |
Brian Gix | 5fe57d9 | 2011-12-21 16:12:13 -0800 | [diff] [blame] | 3064 | |
Brian Gix | 47c15e2 | 2011-11-16 13:53:14 -0800 | [diff] [blame] | 3065 | goto done; |
| 3066 | } |
| 3067 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3068 | cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3069 | if (!cmd) { |
| 3070 | err = -ENOMEM; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3071 | goto done; |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3072 | } |
| 3073 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3074 | /* Continue with pairing via HCI */ |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3075 | if (hci_op == HCI_OP_USER_PASSKEY_REPLY) { |
| 3076 | struct hci_cp_user_passkey_reply cp; |
| 3077 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3078 | bacpy(&cp.bdaddr, &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3079 | cp.passkey = passkey; |
| 3080 | err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp); |
| 3081 | } else |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3082 | err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr), |
| 3083 | &addr->bdaddr); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3084 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 3085 | if (err < 0) |
| 3086 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3087 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3088 | done: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3089 | hci_dev_unlock(hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 3090 | return err; |
| 3091 | } |
| 3092 | |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3093 | static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, |
| 3094 | void *data, u16 len) |
| 3095 | { |
| 3096 | struct mgmt_cp_pin_code_neg_reply *cp = data; |
| 3097 | |
| 3098 | BT_DBG(""); |
| 3099 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3100 | return user_pairing_resp(sk, hdev, &cp->addr, |
Jaganath Kanakkassery | afeb019 | 2012-07-09 16:11:51 +0530 | [diff] [blame] | 3101 | MGMT_OP_PIN_CODE_NEG_REPLY, |
| 3102 | HCI_OP_PIN_CODE_NEG_REPLY, 0); |
| 3103 | } |
| 3104 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3105 | static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3106 | u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3107 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3108 | struct mgmt_cp_user_confirm_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3109 | |
| 3110 | BT_DBG(""); |
| 3111 | |
| 3112 | if (len != sizeof(*cp)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3113 | return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3114 | MGMT_STATUS_INVALID_PARAMS); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3115 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3116 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3117 | MGMT_OP_USER_CONFIRM_REPLY, |
| 3118 | HCI_OP_USER_CONFIRM_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3119 | } |
| 3120 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3121 | 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] | 3122 | void *data, u16 len) |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3123 | { |
Johan Hedberg | c9c2659 | 2011-12-15 00:47:41 +0200 | [diff] [blame] | 3124 | struct mgmt_cp_user_confirm_neg_reply *cp = data; |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3125 | |
| 3126 | BT_DBG(""); |
| 3127 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3128 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3129 | MGMT_OP_USER_CONFIRM_NEG_REPLY, |
| 3130 | HCI_OP_USER_CONFIRM_NEG_REPLY, 0); |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 3131 | } |
| 3132 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3133 | static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3134 | u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3135 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3136 | struct mgmt_cp_user_passkey_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3137 | |
| 3138 | BT_DBG(""); |
| 3139 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3140 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3141 | MGMT_OP_USER_PASSKEY_REPLY, |
| 3142 | HCI_OP_USER_PASSKEY_REPLY, cp->passkey); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3143 | } |
| 3144 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3145 | 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] | 3146 | void *data, u16 len) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3147 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3148 | struct mgmt_cp_user_passkey_neg_reply *cp = data; |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3149 | |
| 3150 | BT_DBG(""); |
| 3151 | |
Johan Hedberg | 1707c60 | 2013-03-15 17:07:15 -0500 | [diff] [blame] | 3152 | return user_pairing_resp(sk, hdev, &cp->addr, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3153 | MGMT_OP_USER_PASSKEY_NEG_REPLY, |
| 3154 | HCI_OP_USER_PASSKEY_NEG_REPLY, 0); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 3155 | } |
| 3156 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3157 | static void update_name(struct hci_request *req) |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3158 | { |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3159 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3160 | struct hci_cp_write_local_name cp; |
| 3161 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3162 | memcpy(cp.name, hdev->dev_name, sizeof(cp.name)); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3163 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3164 | hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp); |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3165 | } |
| 3166 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3167 | static void set_name_complete(struct hci_dev *hdev, u8 status) |
| 3168 | { |
| 3169 | struct mgmt_cp_set_local_name *cp; |
| 3170 | struct pending_cmd *cmd; |
| 3171 | |
| 3172 | BT_DBG("status 0x%02x", status); |
| 3173 | |
| 3174 | hci_dev_lock(hdev); |
| 3175 | |
| 3176 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
| 3177 | if (!cmd) |
| 3178 | goto unlock; |
| 3179 | |
| 3180 | cp = cmd->param; |
| 3181 | |
| 3182 | if (status) |
| 3183 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, |
| 3184 | mgmt_status(status)); |
| 3185 | else |
| 3186 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3187 | cp, sizeof(*cp)); |
| 3188 | |
| 3189 | mgmt_pending_remove(cmd); |
| 3190 | |
| 3191 | unlock: |
| 3192 | hci_dev_unlock(hdev); |
| 3193 | } |
| 3194 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3195 | 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] | 3196 | u16 len) |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3197 | { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3198 | struct mgmt_cp_set_local_name *cp = data; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3199 | struct pending_cmd *cmd; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3200 | struct hci_request req; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3201 | int err; |
| 3202 | |
| 3203 | BT_DBG(""); |
| 3204 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3205 | hci_dev_lock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3206 | |
Johan Hedberg | b3f2ca9 | 2013-03-15 17:07:03 -0500 | [diff] [blame] | 3207 | /* If the old values are the same as the new ones just return a |
| 3208 | * direct command complete event. |
| 3209 | */ |
| 3210 | if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && |
| 3211 | !memcmp(hdev->short_name, cp->short_name, |
| 3212 | sizeof(hdev->short_name))) { |
| 3213 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, |
| 3214 | data, len); |
| 3215 | goto failed; |
| 3216 | } |
| 3217 | |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3218 | memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3219 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3220 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | 2b4bf39 | 2012-03-03 00:19:06 +0200 | [diff] [blame] | 3221 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3222 | |
| 3223 | 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] | 3224 | data, len); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3225 | if (err < 0) |
| 3226 | goto failed; |
| 3227 | |
| 3228 | err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3229 | sk); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3230 | |
Johan Hedberg | b5235a6 | 2012-02-21 14:32:24 +0200 | [diff] [blame] | 3231 | goto failed; |
| 3232 | } |
| 3233 | |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 3234 | 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] | 3235 | if (!cmd) { |
| 3236 | err = -ENOMEM; |
| 3237 | goto failed; |
| 3238 | } |
| 3239 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3240 | memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); |
| 3241 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3242 | hci_req_init(&req, hdev); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3243 | |
| 3244 | if (lmp_bredr_capable(hdev)) { |
| 3245 | update_name(&req); |
| 3246 | update_eir(&req); |
| 3247 | } |
| 3248 | |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3249 | /* The name is stored in the scan response data and so |
| 3250 | * no need to udpate the advertising data here. |
| 3251 | */ |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3252 | if (lmp_le_capable(hdev)) |
Marcel Holtmann | 7a5f499 | 2013-10-16 00:16:49 -0700 | [diff] [blame] | 3253 | update_scan_rsp_data(&req); |
Johan Hedberg | 3f98505 | 2013-03-15 17:07:02 -0500 | [diff] [blame] | 3254 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 3255 | err = hci_req_run(&req, set_name_complete); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3256 | if (err < 0) |
| 3257 | mgmt_pending_remove(cmd); |
| 3258 | |
| 3259 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3260 | hci_dev_unlock(hdev); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 3261 | return err; |
| 3262 | } |
| 3263 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 3264 | 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] | 3265 | void *data, u16 data_len) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3266 | { |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3267 | struct pending_cmd *cmd; |
| 3268 | int err; |
| 3269 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3270 | BT_DBG("%s", hdev->name); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3271 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3272 | hci_dev_lock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3273 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3274 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3275 | 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] | 3276 | MGMT_STATUS_NOT_POWERED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3277 | goto unlock; |
| 3278 | } |
| 3279 | |
Andre Guedes | 9a1a199 | 2012-07-24 15:03:48 -0300 | [diff] [blame] | 3280 | if (!lmp_ssp_capable(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3281 | 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] | 3282 | MGMT_STATUS_NOT_SUPPORTED); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3283 | goto unlock; |
| 3284 | } |
| 3285 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3286 | if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3287 | 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] | 3288 | MGMT_STATUS_BUSY); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3289 | goto unlock; |
| 3290 | } |
| 3291 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3292 | 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] | 3293 | if (!cmd) { |
| 3294 | err = -ENOMEM; |
| 3295 | goto unlock; |
| 3296 | } |
| 3297 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 3298 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) |
| 3299 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, |
| 3300 | 0, NULL); |
| 3301 | else |
| 3302 | err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); |
| 3303 | |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3304 | if (err < 0) |
| 3305 | mgmt_pending_remove(cmd); |
| 3306 | |
| 3307 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3308 | hci_dev_unlock(hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 3309 | return err; |
| 3310 | } |
| 3311 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3312 | 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] | 3313 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3314 | { |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3315 | int err; |
| 3316 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3317 | BT_DBG("%s ", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3318 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3319 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3320 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3321 | if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) { |
| 3322 | struct mgmt_cp_add_remote_oob_data *cp = data; |
| 3323 | u8 status; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3324 | |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 3325 | err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr, |
| 3326 | cp->hash, cp->randomizer); |
| 3327 | if (err < 0) |
| 3328 | status = MGMT_STATUS_FAILED; |
| 3329 | else |
| 3330 | status = MGMT_STATUS_SUCCESS; |
| 3331 | |
| 3332 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3333 | status, &cp->addr, sizeof(cp->addr)); |
| 3334 | } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { |
| 3335 | struct mgmt_cp_add_remote_oob_ext_data *cp = data; |
| 3336 | u8 status; |
| 3337 | |
| 3338 | err = hci_add_remote_oob_ext_data(hdev, &cp->addr.bdaddr, |
| 3339 | cp->hash192, |
| 3340 | cp->randomizer192, |
| 3341 | cp->hash256, |
| 3342 | cp->randomizer256); |
| 3343 | if (err < 0) |
| 3344 | status = MGMT_STATUS_FAILED; |
| 3345 | else |
| 3346 | status = MGMT_STATUS_SUCCESS; |
| 3347 | |
| 3348 | err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3349 | status, &cp->addr, sizeof(cp->addr)); |
| 3350 | } else { |
| 3351 | BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); |
| 3352 | err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, |
| 3353 | MGMT_STATUS_INVALID_PARAMS); |
| 3354 | } |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3355 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3356 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3357 | return err; |
| 3358 | } |
| 3359 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3360 | 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] | 3361 | void *data, u16 len) |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3362 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3363 | struct mgmt_cp_remove_remote_oob_data *cp = data; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3364 | u8 status; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3365 | int err; |
| 3366 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3367 | BT_DBG("%s", hdev->name); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3368 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3369 | hci_dev_lock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3370 | |
Johan Hedberg | 664ce4c | 2012-02-09 15:44:09 +0200 | [diff] [blame] | 3371 | err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3372 | if (err < 0) |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3373 | status = MGMT_STATUS_INVALID_PARAMS; |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3374 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3375 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | bf1e354 | 2012-02-19 13:16:14 +0200 | [diff] [blame] | 3376 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3377 | 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] | 3378 | status, &cp->addr, sizeof(cp->addr)); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3379 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3380 | hci_dev_unlock(hdev); |
Szymon Janc | 2763eda | 2011-03-22 13:12:22 +0100 | [diff] [blame] | 3381 | return err; |
| 3382 | } |
| 3383 | |
Andre Guedes | 41dc2bd | 2013-04-30 15:29:30 -0300 | [diff] [blame] | 3384 | static int mgmt_start_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3385 | { |
| 3386 | struct pending_cmd *cmd; |
| 3387 | u8 type; |
| 3388 | int err; |
| 3389 | |
| 3390 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3391 | |
| 3392 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
| 3393 | if (!cmd) |
| 3394 | return -ENOENT; |
| 3395 | |
| 3396 | type = hdev->discovery.type; |
| 3397 | |
| 3398 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3399 | &type, sizeof(type)); |
| 3400 | mgmt_pending_remove(cmd); |
| 3401 | |
| 3402 | return err; |
| 3403 | } |
| 3404 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3405 | static void start_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3406 | { |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3407 | unsigned long timeout = 0; |
| 3408 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3409 | BT_DBG("status %d", status); |
| 3410 | |
| 3411 | if (status) { |
| 3412 | hci_dev_lock(hdev); |
| 3413 | mgmt_start_discovery_failed(hdev, status); |
| 3414 | hci_dev_unlock(hdev); |
| 3415 | return; |
| 3416 | } |
| 3417 | |
| 3418 | hci_dev_lock(hdev); |
| 3419 | hci_discovery_set_state(hdev, DISCOVERY_FINDING); |
| 3420 | hci_dev_unlock(hdev); |
| 3421 | |
| 3422 | switch (hdev->discovery.type) { |
| 3423 | case DISCOV_TYPE_LE: |
Lukasz Rymanowski | 3d5a76f | 2014-03-27 20:55:21 +0100 | [diff] [blame] | 3424 | timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3425 | break; |
| 3426 | |
| 3427 | case DISCOV_TYPE_INTERLEAVED: |
Lukasz Rymanowski | b9a7a61 | 2014-03-27 20:55:20 +0100 | [diff] [blame] | 3428 | timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3429 | break; |
| 3430 | |
| 3431 | case DISCOV_TYPE_BREDR: |
| 3432 | break; |
| 3433 | |
| 3434 | default: |
| 3435 | BT_ERR("Invalid discovery type %d", hdev->discovery.type); |
| 3436 | } |
Lukasz Rymanowski | ae55f59 | 2014-03-27 20:55:19 +0100 | [diff] [blame] | 3437 | |
| 3438 | if (!timeout) |
| 3439 | return; |
| 3440 | |
| 3441 | queue_delayed_work(hdev->workqueue, &hdev->le_scan_disable, timeout); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3442 | } |
| 3443 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3444 | static int start_discovery(struct sock *sk, struct hci_dev *hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3445 | void *data, u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3446 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3447 | struct mgmt_cp_start_discovery *cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3448 | struct pending_cmd *cmd; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3449 | struct hci_cp_le_set_scan_param param_cp; |
| 3450 | struct hci_cp_le_set_scan_enable enable_cp; |
| 3451 | struct hci_cp_inquiry inq_cp; |
| 3452 | struct hci_request req; |
| 3453 | /* General inquiry access code (GIAC) */ |
| 3454 | u8 lap[3] = { 0x33, 0x8b, 0x9e }; |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3455 | u8 status, own_addr_type; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3456 | int err; |
| 3457 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3458 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3459 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3460 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3461 | |
Johan Hedberg | 4b34ee78 | 2012-02-21 14:13:02 +0200 | [diff] [blame] | 3462 | if (!hdev_is_powered(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3463 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3464 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | bd2d133 | 2011-11-07 23:13:37 +0200 | [diff] [blame] | 3465 | goto failed; |
| 3466 | } |
| 3467 | |
Andre Guedes | 642be6c | 2012-03-21 00:03:37 -0300 | [diff] [blame] | 3468 | if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { |
| 3469 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3470 | MGMT_STATUS_BUSY); |
| 3471 | goto failed; |
| 3472 | } |
| 3473 | |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3474 | if (hdev->discovery.state != DISCOVERY_STOPPED) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3475 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3476 | MGMT_STATUS_BUSY); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3477 | goto failed; |
| 3478 | } |
| 3479 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3480 | cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3481 | if (!cmd) { |
| 3482 | err = -ENOMEM; |
| 3483 | goto failed; |
| 3484 | } |
| 3485 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3486 | hdev->discovery.type = cp->type; |
| 3487 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3488 | hci_req_init(&req, hdev); |
| 3489 | |
Andre Guedes | 4aab14e | 2012-02-17 20:39:36 -0300 | [diff] [blame] | 3490 | switch (hdev->discovery.type) { |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3491 | case DISCOV_TYPE_BREDR: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3492 | status = mgmt_bredr_support(hdev); |
| 3493 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3494 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3495 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3496 | mgmt_pending_remove(cmd); |
| 3497 | goto failed; |
| 3498 | } |
| 3499 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3500 | if (test_bit(HCI_INQUIRY, &hdev->flags)) { |
| 3501 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3502 | MGMT_STATUS_BUSY); |
| 3503 | mgmt_pending_remove(cmd); |
| 3504 | goto failed; |
| 3505 | } |
| 3506 | |
| 3507 | hci_inquiry_cache_flush(hdev); |
| 3508 | |
| 3509 | memset(&inq_cp, 0, sizeof(inq_cp)); |
| 3510 | memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3511 | inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3512 | hci_req_add(&req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3513 | break; |
| 3514 | |
| 3515 | case DISCOV_TYPE_LE: |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3516 | case DISCOV_TYPE_INTERLEAVED: |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3517 | status = mgmt_le_support(hdev); |
| 3518 | if (status) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3519 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3520 | status); |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3521 | mgmt_pending_remove(cmd); |
| 3522 | goto failed; |
| 3523 | } |
| 3524 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3525 | if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 3526 | !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3527 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3528 | MGMT_STATUS_NOT_SUPPORTED); |
| 3529 | mgmt_pending_remove(cmd); |
| 3530 | goto failed; |
| 3531 | } |
| 3532 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3533 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3534 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3535 | MGMT_STATUS_REJECTED); |
| 3536 | mgmt_pending_remove(cmd); |
| 3537 | goto failed; |
| 3538 | } |
| 3539 | |
Andre Guedes | c54c386 | 2014-02-26 20:21:50 -0300 | [diff] [blame] | 3540 | /* If controller is scanning, it means the background scanning |
| 3541 | * is running. Thus, we should temporarily stop it in order to |
| 3542 | * set the discovery scanning parameters. |
| 3543 | */ |
| 3544 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) |
| 3545 | hci_req_add_le_scan_disable(&req); |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3546 | |
| 3547 | memset(¶m_cp, 0, sizeof(param_cp)); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3548 | |
Marcel Holtmann | 94b1fc9 | 2014-02-23 20:25:54 -0800 | [diff] [blame] | 3549 | /* All active scans will be done with either a resolvable |
| 3550 | * private address (when privacy feature has been enabled) |
| 3551 | * or unresolvable private address. |
| 3552 | */ |
| 3553 | err = hci_update_random_address(&req, true, &own_addr_type); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3554 | if (err < 0) { |
| 3555 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3556 | MGMT_STATUS_FAILED); |
| 3557 | mgmt_pending_remove(cmd); |
| 3558 | goto failed; |
| 3559 | } |
| 3560 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3561 | param_cp.type = LE_SCAN_ACTIVE; |
Andre Guedes | 0d8cc93 | 2013-04-30 15:29:31 -0300 | [diff] [blame] | 3562 | param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); |
| 3563 | param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); |
Johan Hedberg | d948394 | 2014-02-23 19:42:24 +0200 | [diff] [blame] | 3564 | param_cp.own_address_type = own_addr_type; |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3565 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 3566 | ¶m_cp); |
| 3567 | |
| 3568 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 3569 | enable_cp.enable = LE_SCAN_ENABLE; |
| 3570 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 3571 | hci_req_add(&req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 3572 | &enable_cp); |
Andre Guedes | 5e0452c | 2012-02-17 20:39:38 -0300 | [diff] [blame] | 3573 | break; |
| 3574 | |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3575 | default: |
Johan Hedberg | 0410675 | 2013-01-10 14:54:09 +0200 | [diff] [blame] | 3576 | err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, |
| 3577 | MGMT_STATUS_INVALID_PARAMS); |
| 3578 | mgmt_pending_remove(cmd); |
| 3579 | goto failed; |
Andre Guedes | f39799f | 2012-02-17 20:39:35 -0300 | [diff] [blame] | 3580 | } |
Andre Guedes | 3fd2415 | 2012-02-03 17:48:01 -0300 | [diff] [blame] | 3581 | |
Andre Guedes | 7c30772 | 2013-04-30 15:29:28 -0300 | [diff] [blame] | 3582 | err = hci_req_run(&req, start_discovery_complete); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3583 | if (err < 0) |
| 3584 | mgmt_pending_remove(cmd); |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3585 | else |
| 3586 | hci_discovery_set_state(hdev, DISCOVERY_STARTING); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3587 | |
| 3588 | failed: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3589 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3590 | return err; |
| 3591 | } |
| 3592 | |
Andre Guedes | 1183fdc | 2013-04-30 15:29:35 -0300 | [diff] [blame] | 3593 | static int mgmt_stop_discovery_failed(struct hci_dev *hdev, u8 status) |
| 3594 | { |
| 3595 | struct pending_cmd *cmd; |
| 3596 | int err; |
| 3597 | |
| 3598 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
| 3599 | if (!cmd) |
| 3600 | return -ENOENT; |
| 3601 | |
| 3602 | err = cmd_complete(cmd->sk, hdev->id, cmd->opcode, mgmt_status(status), |
| 3603 | &hdev->discovery.type, sizeof(hdev->discovery.type)); |
| 3604 | mgmt_pending_remove(cmd); |
| 3605 | |
| 3606 | return err; |
| 3607 | } |
| 3608 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3609 | static void stop_discovery_complete(struct hci_dev *hdev, u8 status) |
| 3610 | { |
| 3611 | BT_DBG("status %d", status); |
| 3612 | |
| 3613 | hci_dev_lock(hdev); |
| 3614 | |
| 3615 | if (status) { |
| 3616 | mgmt_stop_discovery_failed(hdev, status); |
| 3617 | goto unlock; |
| 3618 | } |
| 3619 | |
| 3620 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3621 | |
| 3622 | unlock: |
| 3623 | hci_dev_unlock(hdev); |
| 3624 | } |
| 3625 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3626 | 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] | 3627 | u16 len) |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3628 | { |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3629 | struct mgmt_cp_stop_discovery *mgmt_cp = data; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3630 | struct pending_cmd *cmd; |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3631 | struct hci_request req; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3632 | int err; |
| 3633 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3634 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3635 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3636 | hci_dev_lock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3637 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3638 | if (!hci_discovery_active(hdev)) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3639 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3640 | MGMT_STATUS_REJECTED, &mgmt_cp->type, |
| 3641 | sizeof(mgmt_cp->type)); |
Johan Hedberg | d930650 | 2012-02-20 23:25:18 +0200 | [diff] [blame] | 3642 | goto unlock; |
| 3643 | } |
| 3644 | |
| 3645 | if (hdev->discovery.type != mgmt_cp->type) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3646 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3647 | MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, |
| 3648 | sizeof(mgmt_cp->type)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3649 | goto unlock; |
Johan Hedberg | ff9ef57 | 2012-01-04 14:23:45 +0200 | [diff] [blame] | 3650 | } |
| 3651 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 3652 | cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, NULL, 0); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3653 | if (!cmd) { |
| 3654 | err = -ENOMEM; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3655 | goto unlock; |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3656 | } |
| 3657 | |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3658 | hci_req_init(&req, hdev); |
| 3659 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3660 | hci_stop_discovery(&req); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3661 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3662 | err = hci_req_run(&req, stop_discovery_complete); |
| 3663 | if (!err) { |
| 3664 | hci_discovery_set_state(hdev, DISCOVERY_STOPPING); |
Andre Guedes | 0e05bba | 2013-04-30 15:29:33 -0300 | [diff] [blame] | 3665 | goto unlock; |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3666 | } |
| 3667 | |
Johan Hedberg | 21a60d3 | 2014-06-10 14:05:58 +0300 | [diff] [blame] | 3668 | mgmt_pending_remove(cmd); |
| 3669 | |
| 3670 | /* If no HCI commands were sent we're done */ |
| 3671 | if (err == -ENODATA) { |
| 3672 | err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, |
| 3673 | &mgmt_cp->type, sizeof(mgmt_cp->type)); |
| 3674 | hci_discovery_set_state(hdev, DISCOVERY_STOPPED); |
| 3675 | } |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3676 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3677 | unlock: |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3678 | hci_dev_unlock(hdev); |
Johan Hedberg | 14a5366 | 2011-04-27 10:29:56 -0400 | [diff] [blame] | 3679 | return err; |
| 3680 | } |
| 3681 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3682 | 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] | 3683 | u16 len) |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3684 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3685 | struct mgmt_cp_confirm_name *cp = data; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3686 | struct inquiry_entry *e; |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3687 | int err; |
| 3688 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3689 | BT_DBG("%s", hdev->name); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3690 | |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3691 | hci_dev_lock(hdev); |
| 3692 | |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3693 | if (!hci_discovery_active(hdev)) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3694 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3695 | MGMT_STATUS_FAILED, &cp->addr, |
| 3696 | sizeof(cp->addr)); |
Johan Hedberg | 30dc78e | 2012-01-04 15:44:20 +0200 | [diff] [blame] | 3697 | goto failed; |
| 3698 | } |
| 3699 | |
Johan Hedberg | a198e7b | 2012-02-17 14:27:06 +0200 | [diff] [blame] | 3700 | e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3701 | if (!e) { |
Lukasz Rymanowski | d3a2541 | 2014-02-27 16:47:28 +0100 | [diff] [blame] | 3702 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, |
| 3703 | MGMT_STATUS_INVALID_PARAMS, &cp->addr, |
| 3704 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3705 | goto failed; |
| 3706 | } |
| 3707 | |
| 3708 | if (cp->name_known) { |
| 3709 | e->name_state = NAME_KNOWN; |
| 3710 | list_del(&e->list); |
| 3711 | } else { |
| 3712 | e->name_state = NAME_NEEDED; |
Johan Hedberg | a3d4e20a | 2012-01-09 00:53:02 +0200 | [diff] [blame] | 3713 | hci_inquiry_cache_update_resolve(hdev, e); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3714 | } |
| 3715 | |
Johan Hedberg | e384662 | 2013-01-09 15:29:33 +0200 | [diff] [blame] | 3716 | err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, |
| 3717 | sizeof(cp->addr)); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3718 | |
| 3719 | failed: |
| 3720 | hci_dev_unlock(hdev); |
Johan Hedberg | 561aafb | 2012-01-04 13:31:59 +0200 | [diff] [blame] | 3721 | return err; |
| 3722 | } |
| 3723 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3724 | 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] | 3725 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3726 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3727 | struct mgmt_cp_block_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3728 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3729 | int err; |
| 3730 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3731 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3732 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3733 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3734 | return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, |
| 3735 | MGMT_STATUS_INVALID_PARAMS, |
| 3736 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3737 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3738 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3739 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3740 | err = hci_blacklist_add(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3741 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3742 | status = MGMT_STATUS_FAILED; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3743 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3744 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3745 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3746 | err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3747 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3748 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3749 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3750 | |
| 3751 | return err; |
| 3752 | } |
| 3753 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3754 | 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] | 3755 | u16 len) |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3756 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 3757 | struct mgmt_cp_unblock_device *cp = data; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3758 | u8 status; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3759 | int err; |
| 3760 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3761 | BT_DBG("%s", hdev->name); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3762 | |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3763 | if (!bdaddr_type_is_valid(cp->addr.type)) |
Johan Hedberg | 5d0846d | 2013-01-20 14:27:22 +0200 | [diff] [blame] | 3764 | return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, |
| 3765 | MGMT_STATUS_INVALID_PARAMS, |
| 3766 | &cp->addr, sizeof(cp->addr)); |
Johan Hedberg | 4ee71b2 | 2013-01-20 14:27:19 +0200 | [diff] [blame] | 3767 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3768 | hci_dev_lock(hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3769 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 3770 | err = hci_blacklist_del(hdev, &cp->addr.bdaddr, cp->addr.type); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3771 | if (err < 0) |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3772 | status = MGMT_STATUS_INVALID_PARAMS; |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3773 | else |
Szymon Janc | a6785be | 2012-12-13 15:11:21 +0100 | [diff] [blame] | 3774 | status = MGMT_STATUS_SUCCESS; |
Johan Hedberg | f0eeea8 | 2012-02-19 12:58:54 +0200 | [diff] [blame] | 3775 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 3776 | err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 3777 | &cp->addr, sizeof(cp->addr)); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 3778 | |
Gustavo F. Padovan | 09fd0de | 2011-06-17 13:03:21 -0300 | [diff] [blame] | 3779 | hci_dev_unlock(hdev); |
Antti Julku | 7fbec22 | 2011-06-15 12:01:15 +0300 | [diff] [blame] | 3780 | |
| 3781 | return err; |
| 3782 | } |
| 3783 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3784 | static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3785 | u16 len) |
| 3786 | { |
| 3787 | struct mgmt_cp_set_device_id *cp = data; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3788 | struct hci_request req; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3789 | int err; |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3790 | __u16 source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3791 | |
| 3792 | BT_DBG("%s", hdev->name); |
| 3793 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3794 | source = __le16_to_cpu(cp->source); |
| 3795 | |
| 3796 | if (source > 0x0002) |
| 3797 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, |
| 3798 | MGMT_STATUS_INVALID_PARAMS); |
| 3799 | |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3800 | hci_dev_lock(hdev); |
| 3801 | |
Szymon Janc | c72d4b8 | 2012-03-16 16:02:57 +0100 | [diff] [blame] | 3802 | hdev->devid_source = source; |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3803 | hdev->devid_vendor = __le16_to_cpu(cp->vendor); |
| 3804 | hdev->devid_product = __le16_to_cpu(cp->product); |
| 3805 | hdev->devid_version = __le16_to_cpu(cp->version); |
| 3806 | |
| 3807 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); |
| 3808 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 3809 | hci_req_init(&req, hdev); |
| 3810 | update_eir(&req); |
| 3811 | hci_req_run(&req, NULL); |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 3812 | |
| 3813 | hci_dev_unlock(hdev); |
| 3814 | |
| 3815 | return err; |
| 3816 | } |
| 3817 | |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3818 | static void set_advertising_complete(struct hci_dev *hdev, u8 status) |
| 3819 | { |
| 3820 | struct cmd_lookup match = { NULL, hdev }; |
| 3821 | |
| 3822 | if (status) { |
| 3823 | u8 mgmt_err = mgmt_status(status); |
| 3824 | |
| 3825 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, |
| 3826 | cmd_status_rsp, &mgmt_err); |
| 3827 | return; |
| 3828 | } |
| 3829 | |
| 3830 | mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, |
| 3831 | &match); |
| 3832 | |
| 3833 | new_settings(hdev, match.sk); |
| 3834 | |
| 3835 | if (match.sk) |
| 3836 | sock_put(match.sk); |
| 3837 | } |
| 3838 | |
Marcel Holtmann | 21b5187 | 2013-10-10 09:47:53 -0700 | [diff] [blame] | 3839 | static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data, |
| 3840 | u16 len) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3841 | { |
| 3842 | struct mgmt_mode *cp = data; |
| 3843 | struct pending_cmd *cmd; |
| 3844 | struct hci_request req; |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3845 | u8 val, enabled, status; |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3846 | int err; |
| 3847 | |
| 3848 | BT_DBG("request for %s", hdev->name); |
| 3849 | |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3850 | status = mgmt_le_support(hdev); |
| 3851 | if (status) |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3852 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
Johan Hedberg | e6fe798 | 2013-10-02 15:45:22 +0300 | [diff] [blame] | 3853 | status); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3854 | |
| 3855 | if (cp->val != 0x00 && cp->val != 0x01) |
| 3856 | return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3857 | MGMT_STATUS_INVALID_PARAMS); |
| 3858 | |
| 3859 | hci_dev_lock(hdev); |
| 3860 | |
| 3861 | val = !!cp->val; |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3862 | enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3863 | |
Johan Hedberg | f74ca9b | 2013-10-08 15:52:18 +0200 | [diff] [blame] | 3864 | /* The following conditions are ones which mean that we should |
| 3865 | * not do any HCI communication but directly send a mgmt |
| 3866 | * response to user space (after toggling the flag if |
| 3867 | * necessary). |
| 3868 | */ |
| 3869 | if (!hdev_is_powered(hdev) || val == enabled || |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 3870 | hci_conn_num(hdev, LE_LINK) > 0) { |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3871 | bool changed = false; |
| 3872 | |
Johan Hedberg | f3d3444a | 2013-10-05 12:01:04 +0200 | [diff] [blame] | 3873 | if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { |
| 3874 | change_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3875 | changed = true; |
| 3876 | } |
| 3877 | |
| 3878 | err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); |
| 3879 | if (err < 0) |
| 3880 | goto unlock; |
| 3881 | |
| 3882 | if (changed) |
| 3883 | err = new_settings(hdev, sk); |
| 3884 | |
| 3885 | goto unlock; |
| 3886 | } |
| 3887 | |
| 3888 | if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || |
| 3889 | mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { |
| 3890 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, |
| 3891 | MGMT_STATUS_BUSY); |
| 3892 | goto unlock; |
| 3893 | } |
| 3894 | |
| 3895 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len); |
| 3896 | if (!cmd) { |
| 3897 | err = -ENOMEM; |
| 3898 | goto unlock; |
| 3899 | } |
| 3900 | |
| 3901 | hci_req_init(&req, hdev); |
| 3902 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 3903 | if (val) |
| 3904 | enable_advertising(&req); |
| 3905 | else |
| 3906 | disable_advertising(&req); |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 3907 | |
| 3908 | err = hci_req_run(&req, set_advertising_complete); |
| 3909 | if (err < 0) |
| 3910 | mgmt_pending_remove(cmd); |
| 3911 | |
| 3912 | unlock: |
| 3913 | hci_dev_unlock(hdev); |
| 3914 | return err; |
| 3915 | } |
| 3916 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3917 | static int set_static_address(struct sock *sk, struct hci_dev *hdev, |
| 3918 | void *data, u16 len) |
| 3919 | { |
| 3920 | struct mgmt_cp_set_static_address *cp = data; |
| 3921 | int err; |
| 3922 | |
| 3923 | BT_DBG("%s", hdev->name); |
| 3924 | |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3925 | if (!lmp_le_capable(hdev)) |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3926 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
Marcel Holtmann | 62af444 | 2013-10-02 22:10:32 -0700 | [diff] [blame] | 3927 | MGMT_STATUS_NOT_SUPPORTED); |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 3928 | |
| 3929 | if (hdev_is_powered(hdev)) |
| 3930 | return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, |
| 3931 | MGMT_STATUS_REJECTED); |
| 3932 | |
| 3933 | if (bacmp(&cp->bdaddr, BDADDR_ANY)) { |
| 3934 | if (!bacmp(&cp->bdaddr, BDADDR_NONE)) |
| 3935 | return cmd_status(sk, hdev->id, |
| 3936 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3937 | MGMT_STATUS_INVALID_PARAMS); |
| 3938 | |
| 3939 | /* Two most significant bits shall be set */ |
| 3940 | if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) |
| 3941 | return cmd_status(sk, hdev->id, |
| 3942 | MGMT_OP_SET_STATIC_ADDRESS, |
| 3943 | MGMT_STATUS_INVALID_PARAMS); |
| 3944 | } |
| 3945 | |
| 3946 | hci_dev_lock(hdev); |
| 3947 | |
| 3948 | bacpy(&hdev->static_addr, &cp->bdaddr); |
| 3949 | |
| 3950 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); |
| 3951 | |
| 3952 | hci_dev_unlock(hdev); |
| 3953 | |
| 3954 | return err; |
| 3955 | } |
| 3956 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3957 | static int set_scan_params(struct sock *sk, struct hci_dev *hdev, |
| 3958 | void *data, u16 len) |
| 3959 | { |
| 3960 | struct mgmt_cp_set_scan_params *cp = data; |
| 3961 | __u16 interval, window; |
| 3962 | int err; |
| 3963 | |
| 3964 | BT_DBG("%s", hdev->name); |
| 3965 | |
| 3966 | if (!lmp_le_capable(hdev)) |
| 3967 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3968 | MGMT_STATUS_NOT_SUPPORTED); |
| 3969 | |
| 3970 | interval = __le16_to_cpu(cp->interval); |
| 3971 | |
| 3972 | if (interval < 0x0004 || interval > 0x4000) |
| 3973 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3974 | MGMT_STATUS_INVALID_PARAMS); |
| 3975 | |
| 3976 | window = __le16_to_cpu(cp->window); |
| 3977 | |
| 3978 | if (window < 0x0004 || window > 0x4000) |
| 3979 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3980 | MGMT_STATUS_INVALID_PARAMS); |
| 3981 | |
Marcel Holtmann | 899e107 | 2013-10-14 09:55:32 -0700 | [diff] [blame] | 3982 | if (window > interval) |
| 3983 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, |
| 3984 | MGMT_STATUS_INVALID_PARAMS); |
| 3985 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 3986 | hci_dev_lock(hdev); |
| 3987 | |
| 3988 | hdev->le_scan_interval = interval; |
| 3989 | hdev->le_scan_window = window; |
| 3990 | |
| 3991 | err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); |
| 3992 | |
Andre Guedes | dd2ef8e | 2014-02-26 20:21:56 -0300 | [diff] [blame] | 3993 | /* If background scan is running, restart it so new parameters are |
| 3994 | * loaded. |
| 3995 | */ |
| 3996 | if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && |
| 3997 | hdev->discovery.state == DISCOVERY_STOPPED) { |
| 3998 | struct hci_request req; |
| 3999 | |
| 4000 | hci_req_init(&req, hdev); |
| 4001 | |
| 4002 | hci_req_add_le_scan_disable(&req); |
| 4003 | hci_req_add_le_passive_scan(&req); |
| 4004 | |
| 4005 | hci_req_run(&req, NULL); |
| 4006 | } |
| 4007 | |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4008 | hci_dev_unlock(hdev); |
| 4009 | |
| 4010 | return err; |
| 4011 | } |
| 4012 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4013 | static void fast_connectable_complete(struct hci_dev *hdev, u8 status) |
| 4014 | { |
| 4015 | struct pending_cmd *cmd; |
| 4016 | |
| 4017 | BT_DBG("status 0x%02x", status); |
| 4018 | |
| 4019 | hci_dev_lock(hdev); |
| 4020 | |
| 4021 | cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4022 | if (!cmd) |
| 4023 | goto unlock; |
| 4024 | |
| 4025 | if (status) { |
| 4026 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4027 | mgmt_status(status)); |
| 4028 | } else { |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4029 | struct mgmt_mode *cp = cmd->param; |
| 4030 | |
| 4031 | if (cp->val) |
| 4032 | set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4033 | else |
| 4034 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4035 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4036 | send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); |
| 4037 | new_settings(hdev, cmd->sk); |
| 4038 | } |
| 4039 | |
| 4040 | mgmt_pending_remove(cmd); |
| 4041 | |
| 4042 | unlock: |
| 4043 | hci_dev_unlock(hdev); |
| 4044 | } |
| 4045 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4046 | 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] | 4047 | void *data, u16 len) |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4048 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4049 | struct mgmt_mode *cp = data; |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4050 | struct pending_cmd *cmd; |
| 4051 | struct hci_request req; |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4052 | int err; |
| 4053 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4054 | BT_DBG("%s", hdev->name); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4055 | |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 4056 | if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || |
| 4057 | hdev->hci_ver < BLUETOOTH_VER_1_2) |
Johan Hedberg | 33c525c | 2012-10-24 21:11:58 +0300 | [diff] [blame] | 4058 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4059 | MGMT_STATUS_NOT_SUPPORTED); |
| 4060 | |
Johan Hedberg | a7e80f2 | 2013-01-09 16:05:19 +0200 | [diff] [blame] | 4061 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4062 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4063 | MGMT_STATUS_INVALID_PARAMS); |
| 4064 | |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4065 | if (!hdev_is_powered(hdev)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4066 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4067 | MGMT_STATUS_NOT_POWERED); |
Johan Hedberg | 5400c04 | 2012-02-21 16:40:33 +0200 | [diff] [blame] | 4068 | |
| 4069 | if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4070 | return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4071 | MGMT_STATUS_REJECTED); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4072 | |
| 4073 | hci_dev_lock(hdev); |
| 4074 | |
Johan Hedberg | 05cbf29 | 2013-03-15 17:07:07 -0500 | [diff] [blame] | 4075 | if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { |
| 4076 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4077 | MGMT_STATUS_BUSY); |
| 4078 | goto unlock; |
| 4079 | } |
| 4080 | |
Johan Hedberg | 1a4d3c4 | 2013-03-15 17:07:08 -0500 | [diff] [blame] | 4081 | if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { |
| 4082 | err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, |
| 4083 | hdev); |
| 4084 | goto unlock; |
| 4085 | } |
| 4086 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4087 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, |
| 4088 | data, len); |
| 4089 | if (!cmd) { |
| 4090 | err = -ENOMEM; |
| 4091 | goto unlock; |
| 4092 | } |
| 4093 | |
| 4094 | hci_req_init(&req, hdev); |
| 4095 | |
Johan Hedberg | 406d780 | 2013-03-15 17:07:09 -0500 | [diff] [blame] | 4096 | write_fast_connectable(&req, cp->val); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4097 | |
| 4098 | err = hci_req_run(&req, fast_connectable_complete); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4099 | if (err < 0) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4100 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4101 | MGMT_STATUS_FAILED); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4102 | mgmt_pending_remove(cmd); |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4103 | } |
| 4104 | |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4105 | unlock: |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4106 | hci_dev_unlock(hdev); |
Johan Hedberg | 33e38b3 | 2013-03-15 17:07:05 -0500 | [diff] [blame] | 4107 | |
Antti Julku | f6422ec | 2011-06-22 13:11:56 +0300 | [diff] [blame] | 4108 | return err; |
| 4109 | } |
| 4110 | |
Johan Hedberg | 67e5a7a | 2013-10-14 21:15:25 +0300 | [diff] [blame] | 4111 | static void set_bredr_scan(struct hci_request *req) |
| 4112 | { |
| 4113 | struct hci_dev *hdev = req->hdev; |
| 4114 | u8 scan = 0; |
| 4115 | |
| 4116 | /* Ensure that fast connectable is disabled. This function will |
| 4117 | * not do anything if the page scan parameters are already what |
| 4118 | * they should be. |
| 4119 | */ |
| 4120 | write_fast_connectable(req, false); |
| 4121 | |
| 4122 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4123 | scan |= SCAN_PAGE; |
| 4124 | if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) |
| 4125 | scan |= SCAN_INQUIRY; |
| 4126 | |
| 4127 | if (scan) |
| 4128 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 4129 | } |
| 4130 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4131 | static void set_bredr_complete(struct hci_dev *hdev, u8 status) |
| 4132 | { |
| 4133 | struct pending_cmd *cmd; |
| 4134 | |
| 4135 | BT_DBG("status 0x%02x", status); |
| 4136 | |
| 4137 | hci_dev_lock(hdev); |
| 4138 | |
| 4139 | cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); |
| 4140 | if (!cmd) |
| 4141 | goto unlock; |
| 4142 | |
| 4143 | if (status) { |
| 4144 | u8 mgmt_err = mgmt_status(status); |
| 4145 | |
| 4146 | /* We need to restore the flag if related HCI commands |
| 4147 | * failed. |
| 4148 | */ |
| 4149 | clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4150 | |
| 4151 | cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); |
| 4152 | } else { |
| 4153 | send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); |
| 4154 | new_settings(hdev, cmd->sk); |
| 4155 | } |
| 4156 | |
| 4157 | mgmt_pending_remove(cmd); |
| 4158 | |
| 4159 | unlock: |
| 4160 | hci_dev_unlock(hdev); |
| 4161 | } |
| 4162 | |
| 4163 | static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) |
| 4164 | { |
| 4165 | struct mgmt_mode *cp = data; |
| 4166 | struct pending_cmd *cmd; |
| 4167 | struct hci_request req; |
| 4168 | int err; |
| 4169 | |
| 4170 | BT_DBG("request for %s", hdev->name); |
| 4171 | |
| 4172 | if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) |
| 4173 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4174 | MGMT_STATUS_NOT_SUPPORTED); |
| 4175 | |
| 4176 | if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) |
| 4177 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4178 | MGMT_STATUS_REJECTED); |
| 4179 | |
| 4180 | if (cp->val != 0x00 && cp->val != 0x01) |
| 4181 | return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4182 | MGMT_STATUS_INVALID_PARAMS); |
| 4183 | |
| 4184 | hci_dev_lock(hdev); |
| 4185 | |
| 4186 | if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 4187 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4188 | goto unlock; |
| 4189 | } |
| 4190 | |
| 4191 | if (!hdev_is_powered(hdev)) { |
| 4192 | if (!cp->val) { |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4193 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
| 4194 | clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 4195 | clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 4196 | clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); |
| 4197 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
| 4198 | } |
| 4199 | |
| 4200 | change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4201 | |
| 4202 | err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); |
| 4203 | if (err < 0) |
| 4204 | goto unlock; |
| 4205 | |
| 4206 | err = new_settings(hdev, sk); |
| 4207 | goto unlock; |
| 4208 | } |
| 4209 | |
| 4210 | /* Reject disabling when powered on */ |
| 4211 | if (!cp->val) { |
| 4212 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4213 | MGMT_STATUS_REJECTED); |
| 4214 | goto unlock; |
| 4215 | } |
| 4216 | |
| 4217 | if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { |
| 4218 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, |
| 4219 | MGMT_STATUS_BUSY); |
| 4220 | goto unlock; |
| 4221 | } |
| 4222 | |
| 4223 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len); |
| 4224 | if (!cmd) { |
| 4225 | err = -ENOMEM; |
| 4226 | goto unlock; |
| 4227 | } |
| 4228 | |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4229 | /* 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] | 4230 | * generates the correct flags. |
| 4231 | */ |
| 4232 | set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); |
| 4233 | |
| 4234 | hci_req_init(&req, hdev); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4235 | |
| 4236 | if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) |
| 4237 | set_bredr_scan(&req); |
| 4238 | |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 4239 | /* Since only the advertising data flags will change, there |
| 4240 | * is no need to update the scan response data. |
| 4241 | */ |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 4242 | update_adv_data(&req); |
Johan Hedberg | aa8af46 | 2013-10-14 21:15:26 +0300 | [diff] [blame] | 4243 | |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4244 | err = hci_req_run(&req, set_bredr_complete); |
| 4245 | if (err < 0) |
| 4246 | mgmt_pending_remove(cmd); |
| 4247 | |
| 4248 | unlock: |
| 4249 | hci_dev_unlock(hdev); |
| 4250 | return err; |
| 4251 | } |
| 4252 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4253 | static int set_secure_conn(struct sock *sk, struct hci_dev *hdev, |
| 4254 | void *data, u16 len) |
| 4255 | { |
| 4256 | struct mgmt_mode *cp = data; |
| 4257 | struct pending_cmd *cmd; |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4258 | u8 val, status; |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4259 | int err; |
| 4260 | |
| 4261 | BT_DBG("request for %s", hdev->name); |
| 4262 | |
| 4263 | status = mgmt_bredr_support(hdev); |
| 4264 | if (status) |
| 4265 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4266 | status); |
| 4267 | |
Marcel Holtmann | 5afeac1 | 2014-01-10 02:07:27 -0800 | [diff] [blame] | 4268 | if (!lmp_sc_capable(hdev) && |
Marcel Holtmann | 111902f | 2014-06-21 04:53:17 +0200 | [diff] [blame] | 4269 | !test_bit(HCI_FORCE_SC, &hdev->dbg_flags)) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4270 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4271 | MGMT_STATUS_NOT_SUPPORTED); |
| 4272 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4273 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4274 | return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4275 | MGMT_STATUS_INVALID_PARAMS); |
| 4276 | |
| 4277 | hci_dev_lock(hdev); |
| 4278 | |
| 4279 | if (!hdev_is_powered(hdev)) { |
| 4280 | bool changed; |
| 4281 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4282 | if (cp->val) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4283 | changed = !test_and_set_bit(HCI_SC_ENABLED, |
| 4284 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4285 | if (cp->val == 0x02) |
| 4286 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4287 | else |
| 4288 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4289 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4290 | changed = test_and_clear_bit(HCI_SC_ENABLED, |
| 4291 | &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4292 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4293 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4294 | |
| 4295 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4296 | if (err < 0) |
| 4297 | goto failed; |
| 4298 | |
| 4299 | if (changed) |
| 4300 | err = new_settings(hdev, sk); |
| 4301 | |
| 4302 | goto failed; |
| 4303 | } |
| 4304 | |
| 4305 | if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { |
| 4306 | err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, |
| 4307 | MGMT_STATUS_BUSY); |
| 4308 | goto failed; |
| 4309 | } |
| 4310 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4311 | val = !!cp->val; |
| 4312 | |
| 4313 | if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 4314 | (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4315 | err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); |
| 4316 | goto failed; |
| 4317 | } |
| 4318 | |
| 4319 | cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len); |
| 4320 | if (!cmd) { |
| 4321 | err = -ENOMEM; |
| 4322 | goto failed; |
| 4323 | } |
| 4324 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4325 | err = hci_send_cmd(hdev, HCI_OP_WRITE_SC_SUPPORT, 1, &val); |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4326 | if (err < 0) { |
| 4327 | mgmt_pending_remove(cmd); |
| 4328 | goto failed; |
| 4329 | } |
| 4330 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 4331 | if (cp->val == 0x02) |
| 4332 | set_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4333 | else |
| 4334 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 4335 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4336 | failed: |
| 4337 | hci_dev_unlock(hdev); |
| 4338 | return err; |
| 4339 | } |
| 4340 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4341 | static int set_debug_keys(struct sock *sk, struct hci_dev *hdev, |
| 4342 | void *data, u16 len) |
| 4343 | { |
| 4344 | struct mgmt_mode *cp = data; |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4345 | bool changed, use_changed; |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4346 | int err; |
| 4347 | |
| 4348 | BT_DBG("request for %s", hdev->name); |
| 4349 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4350 | if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4351 | return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, |
| 4352 | MGMT_STATUS_INVALID_PARAMS); |
| 4353 | |
| 4354 | hci_dev_lock(hdev); |
| 4355 | |
| 4356 | if (cp->val) |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4357 | changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, |
| 4358 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4359 | else |
Johan Hedberg | 0663b29 | 2014-06-24 13:15:50 +0300 | [diff] [blame] | 4360 | changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, |
| 4361 | &hdev->dev_flags); |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4362 | |
Johan Hedberg | b9710979 | 2014-06-24 14:00:28 +0300 | [diff] [blame] | 4363 | if (cp->val == 0x02) |
| 4364 | use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, |
| 4365 | &hdev->dev_flags); |
| 4366 | else |
| 4367 | use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, |
| 4368 | &hdev->dev_flags); |
| 4369 | |
| 4370 | if (hdev_is_powered(hdev) && use_changed && |
| 4371 | test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 4372 | u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; |
| 4373 | hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 4374 | sizeof(mode), &mode); |
| 4375 | } |
| 4376 | |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4377 | err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev); |
| 4378 | if (err < 0) |
| 4379 | goto unlock; |
| 4380 | |
| 4381 | if (changed) |
| 4382 | err = new_settings(hdev, sk); |
| 4383 | |
| 4384 | unlock: |
| 4385 | hci_dev_unlock(hdev); |
| 4386 | return err; |
| 4387 | } |
| 4388 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4389 | static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4390 | u16 len) |
| 4391 | { |
| 4392 | struct mgmt_cp_set_privacy *cp = cp_data; |
| 4393 | bool changed; |
| 4394 | int err; |
| 4395 | |
| 4396 | BT_DBG("request for %s", hdev->name); |
| 4397 | |
| 4398 | if (!lmp_le_capable(hdev)) |
| 4399 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4400 | MGMT_STATUS_NOT_SUPPORTED); |
| 4401 | |
| 4402 | if (cp->privacy != 0x00 && cp->privacy != 0x01) |
| 4403 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4404 | MGMT_STATUS_INVALID_PARAMS); |
| 4405 | |
| 4406 | if (hdev_is_powered(hdev)) |
| 4407 | return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, |
| 4408 | MGMT_STATUS_REJECTED); |
| 4409 | |
| 4410 | hci_dev_lock(hdev); |
| 4411 | |
Johan Hedberg | c21c0ea | 2014-02-24 11:10:30 +0200 | [diff] [blame] | 4412 | /* If user space supports this command it is also expected to |
| 4413 | * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. |
| 4414 | */ |
| 4415 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4416 | |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4417 | if (cp->privacy) { |
| 4418 | changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4419 | memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); |
| 4420 | set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4421 | } else { |
| 4422 | changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); |
| 4423 | memset(hdev->irk, 0, sizeof(hdev->irk)); |
| 4424 | clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); |
| 4425 | } |
| 4426 | |
| 4427 | err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); |
| 4428 | if (err < 0) |
| 4429 | goto unlock; |
| 4430 | |
| 4431 | if (changed) |
| 4432 | err = new_settings(hdev, sk); |
| 4433 | |
| 4434 | unlock: |
| 4435 | hci_dev_unlock(hdev); |
| 4436 | return err; |
| 4437 | } |
| 4438 | |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4439 | static bool irk_is_valid(struct mgmt_irk_info *irk) |
| 4440 | { |
| 4441 | switch (irk->addr.type) { |
| 4442 | case BDADDR_LE_PUBLIC: |
| 4443 | return true; |
| 4444 | |
| 4445 | case BDADDR_LE_RANDOM: |
| 4446 | /* Two most significant bits shall be set */ |
| 4447 | if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4448 | return false; |
| 4449 | return true; |
| 4450 | } |
| 4451 | |
| 4452 | return false; |
| 4453 | } |
| 4454 | |
| 4455 | static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data, |
| 4456 | u16 len) |
| 4457 | { |
| 4458 | struct mgmt_cp_load_irks *cp = cp_data; |
| 4459 | u16 irk_count, expected_len; |
| 4460 | int i, err; |
| 4461 | |
| 4462 | BT_DBG("request for %s", hdev->name); |
| 4463 | |
| 4464 | if (!lmp_le_capable(hdev)) |
| 4465 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4466 | MGMT_STATUS_NOT_SUPPORTED); |
| 4467 | |
| 4468 | irk_count = __le16_to_cpu(cp->irk_count); |
| 4469 | |
| 4470 | expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); |
| 4471 | if (expected_len != len) { |
| 4472 | BT_ERR("load_irks: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4473 | expected_len, len); |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4474 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, |
| 4475 | MGMT_STATUS_INVALID_PARAMS); |
| 4476 | } |
| 4477 | |
| 4478 | BT_DBG("%s irk_count %u", hdev->name, irk_count); |
| 4479 | |
| 4480 | for (i = 0; i < irk_count; i++) { |
| 4481 | struct mgmt_irk_info *key = &cp->irks[i]; |
| 4482 | |
| 4483 | if (!irk_is_valid(key)) |
| 4484 | return cmd_status(sk, hdev->id, |
| 4485 | MGMT_OP_LOAD_IRKS, |
| 4486 | MGMT_STATUS_INVALID_PARAMS); |
| 4487 | } |
| 4488 | |
| 4489 | hci_dev_lock(hdev); |
| 4490 | |
| 4491 | hci_smp_irks_clear(hdev); |
| 4492 | |
| 4493 | for (i = 0; i < irk_count; i++) { |
| 4494 | struct mgmt_irk_info *irk = &cp->irks[i]; |
| 4495 | u8 addr_type; |
| 4496 | |
| 4497 | if (irk->addr.type == BDADDR_LE_PUBLIC) |
| 4498 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4499 | else |
| 4500 | addr_type = ADDR_LE_DEV_RANDOM; |
| 4501 | |
| 4502 | hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val, |
| 4503 | BDADDR_ANY); |
| 4504 | } |
| 4505 | |
| 4506 | set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); |
| 4507 | |
| 4508 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); |
| 4509 | |
| 4510 | hci_dev_unlock(hdev); |
| 4511 | |
| 4512 | return err; |
| 4513 | } |
| 4514 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4515 | static bool ltk_is_valid(struct mgmt_ltk_info *key) |
| 4516 | { |
| 4517 | if (key->master != 0x00 && key->master != 0x01) |
| 4518 | return false; |
Marcel Holtmann | 490cb0b | 2014-02-16 12:59:05 -0800 | [diff] [blame] | 4519 | |
| 4520 | switch (key->addr.type) { |
| 4521 | case BDADDR_LE_PUBLIC: |
| 4522 | return true; |
| 4523 | |
| 4524 | case BDADDR_LE_RANDOM: |
| 4525 | /* Two most significant bits shall be set */ |
| 4526 | if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0) |
| 4527 | return false; |
| 4528 | return true; |
| 4529 | } |
| 4530 | |
| 4531 | return false; |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4532 | } |
| 4533 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4534 | 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] | 4535 | void *cp_data, u16 len) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4536 | { |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4537 | struct mgmt_cp_load_long_term_keys *cp = cp_data; |
| 4538 | u16 key_count, expected_len; |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4539 | int i, err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4540 | |
Marcel Holtmann | cf99ba1 | 2013-10-02 21:16:08 -0700 | [diff] [blame] | 4541 | BT_DBG("request for %s", hdev->name); |
| 4542 | |
| 4543 | if (!lmp_le_capable(hdev)) |
| 4544 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4545 | MGMT_STATUS_NOT_SUPPORTED); |
| 4546 | |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4547 | key_count = __le16_to_cpu(cp->key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4548 | |
| 4549 | expected_len = sizeof(*cp) + key_count * |
| 4550 | sizeof(struct mgmt_ltk_info); |
| 4551 | if (expected_len != len) { |
| 4552 | BT_ERR("load_keys: expected %u bytes, got %u bytes", |
Johan Hedberg | 2606ecb | 2014-03-07 15:04:13 +0200 | [diff] [blame] | 4553 | expected_len, len); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4554 | return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, |
Johan Hedberg | e57e619 | 2013-01-20 14:27:14 +0200 | [diff] [blame] | 4555 | MGMT_STATUS_INVALID_PARAMS); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4556 | } |
| 4557 | |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4558 | BT_DBG("%s key_count %u", hdev->name, key_count); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4559 | |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4560 | for (i = 0; i < key_count; i++) { |
| 4561 | struct mgmt_ltk_info *key = &cp->keys[i]; |
| 4562 | |
Johan Hedberg | 3f706b7 | 2013-01-20 14:27:16 +0200 | [diff] [blame] | 4563 | if (!ltk_is_valid(key)) |
Johan Hedberg | 54ad6d8 | 2013-01-20 14:27:15 +0200 | [diff] [blame] | 4564 | return cmd_status(sk, hdev->id, |
| 4565 | MGMT_OP_LOAD_LONG_TERM_KEYS, |
| 4566 | MGMT_STATUS_INVALID_PARAMS); |
| 4567 | } |
| 4568 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4569 | hci_dev_lock(hdev); |
| 4570 | |
| 4571 | hci_smp_ltks_clear(hdev); |
| 4572 | |
| 4573 | for (i = 0; i < key_count; i++) { |
| 4574 | struct mgmt_ltk_info *key = &cp->keys[i]; |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4575 | u8 type, addr_type, authenticated; |
Marcel Holtmann | 79d95a1 | 2013-10-13 03:57:38 -0700 | [diff] [blame] | 4576 | |
| 4577 | if (key->addr.type == BDADDR_LE_PUBLIC) |
| 4578 | addr_type = ADDR_LE_DEV_PUBLIC; |
| 4579 | else |
| 4580 | addr_type = ADDR_LE_DEV_RANDOM; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4581 | |
| 4582 | if (key->master) |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4583 | type = SMP_LTK; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4584 | else |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 4585 | type = SMP_LTK_SLAVE; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4586 | |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4587 | switch (key->type) { |
| 4588 | case MGMT_LTK_UNAUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4589 | authenticated = 0x00; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4590 | break; |
| 4591 | case MGMT_LTK_AUTHENTICATED: |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4592 | authenticated = 0x01; |
Johan Hedberg | 61b4335 | 2014-05-29 19:36:53 +0300 | [diff] [blame] | 4593 | break; |
| 4594 | default: |
| 4595 | continue; |
| 4596 | } |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4597 | |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4598 | hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type, |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 4599 | authenticated, key->val, key->enc_size, key->ediv, |
Johan Hedberg | 35d7027 | 2014-02-19 14:57:47 +0200 | [diff] [blame] | 4600 | key->rand); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4601 | } |
| 4602 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4603 | err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, |
| 4604 | NULL, 0); |
| 4605 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4606 | hci_dev_unlock(hdev); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4607 | |
Johan Hedberg | 715a5bf | 2013-01-09 15:29:34 +0200 | [diff] [blame] | 4608 | return err; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 4609 | } |
| 4610 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4611 | struct cmd_conn_lookup { |
| 4612 | struct hci_conn *conn; |
| 4613 | bool valid_tx_power; |
| 4614 | u8 mgmt_status; |
| 4615 | }; |
| 4616 | |
| 4617 | static void get_conn_info_complete(struct pending_cmd *cmd, void *data) |
| 4618 | { |
| 4619 | struct cmd_conn_lookup *match = data; |
| 4620 | struct mgmt_cp_get_conn_info *cp; |
| 4621 | struct mgmt_rp_get_conn_info rp; |
| 4622 | struct hci_conn *conn = cmd->user_data; |
| 4623 | |
| 4624 | if (conn != match->conn) |
| 4625 | return; |
| 4626 | |
| 4627 | cp = (struct mgmt_cp_get_conn_info *) cmd->param; |
| 4628 | |
| 4629 | memset(&rp, 0, sizeof(rp)); |
| 4630 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4631 | rp.addr.type = cp->addr.type; |
| 4632 | |
| 4633 | if (!match->mgmt_status) { |
| 4634 | rp.rssi = conn->rssi; |
| 4635 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4636 | if (match->valid_tx_power) { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4637 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4638 | rp.max_tx_power = conn->max_tx_power; |
| 4639 | } else { |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4640 | rp.tx_power = HCI_TX_POWER_INVALID; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4641 | rp.max_tx_power = HCI_TX_POWER_INVALID; |
| 4642 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4643 | } |
| 4644 | |
| 4645 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, |
| 4646 | match->mgmt_status, &rp, sizeof(rp)); |
| 4647 | |
| 4648 | hci_conn_drop(conn); |
| 4649 | |
| 4650 | mgmt_pending_remove(cmd); |
| 4651 | } |
| 4652 | |
| 4653 | static void conn_info_refresh_complete(struct hci_dev *hdev, u8 status) |
| 4654 | { |
| 4655 | struct hci_cp_read_rssi *cp; |
| 4656 | struct hci_conn *conn; |
| 4657 | struct cmd_conn_lookup match; |
| 4658 | u16 handle; |
| 4659 | |
| 4660 | BT_DBG("status 0x%02x", status); |
| 4661 | |
| 4662 | hci_dev_lock(hdev); |
| 4663 | |
| 4664 | /* TX power data is valid in case request completed successfully, |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4665 | * otherwise we assume it's not valid. At the moment we assume that |
| 4666 | * either both or none of current and max values are valid to keep code |
| 4667 | * simple. |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4668 | */ |
| 4669 | match.valid_tx_power = !status; |
| 4670 | |
| 4671 | /* Commands sent in request are either Read RSSI or Read Transmit Power |
| 4672 | * Level so we check which one was last sent to retrieve connection |
| 4673 | * handle. Both commands have handle as first parameter so it's safe to |
| 4674 | * cast data on the same command struct. |
| 4675 | * |
| 4676 | * First command sent is always Read RSSI and we fail only if it fails. |
| 4677 | * In other case we simply override error to indicate success as we |
| 4678 | * already remembered if TX power value is actually valid. |
| 4679 | */ |
| 4680 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI); |
| 4681 | if (!cp) { |
| 4682 | cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER); |
| 4683 | status = 0; |
| 4684 | } |
| 4685 | |
| 4686 | if (!cp) { |
| 4687 | BT_ERR("invalid sent_cmd in response"); |
| 4688 | goto unlock; |
| 4689 | } |
| 4690 | |
| 4691 | handle = __le16_to_cpu(cp->handle); |
| 4692 | conn = hci_conn_hash_lookup_handle(hdev, handle); |
| 4693 | if (!conn) { |
| 4694 | BT_ERR("unknown handle (%d) in response", handle); |
| 4695 | goto unlock; |
| 4696 | } |
| 4697 | |
| 4698 | match.conn = conn; |
| 4699 | match.mgmt_status = mgmt_status(status); |
| 4700 | |
| 4701 | /* Cache refresh is complete, now reply for mgmt request for given |
| 4702 | * connection only. |
| 4703 | */ |
| 4704 | mgmt_pending_foreach(MGMT_OP_GET_CONN_INFO, hdev, |
| 4705 | get_conn_info_complete, &match); |
| 4706 | |
| 4707 | unlock: |
| 4708 | hci_dev_unlock(hdev); |
| 4709 | } |
| 4710 | |
| 4711 | static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data, |
| 4712 | u16 len) |
| 4713 | { |
| 4714 | struct mgmt_cp_get_conn_info *cp = data; |
| 4715 | struct mgmt_rp_get_conn_info rp; |
| 4716 | struct hci_conn *conn; |
| 4717 | unsigned long conn_info_age; |
| 4718 | int err = 0; |
| 4719 | |
| 4720 | BT_DBG("%s", hdev->name); |
| 4721 | |
| 4722 | memset(&rp, 0, sizeof(rp)); |
| 4723 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 4724 | rp.addr.type = cp->addr.type; |
| 4725 | |
| 4726 | if (!bdaddr_type_is_valid(cp->addr.type)) |
| 4727 | return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4728 | MGMT_STATUS_INVALID_PARAMS, |
| 4729 | &rp, sizeof(rp)); |
| 4730 | |
| 4731 | hci_dev_lock(hdev); |
| 4732 | |
| 4733 | if (!hdev_is_powered(hdev)) { |
| 4734 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4735 | MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); |
| 4736 | goto unlock; |
| 4737 | } |
| 4738 | |
| 4739 | if (cp->addr.type == BDADDR_BREDR) |
| 4740 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, |
| 4741 | &cp->addr.bdaddr); |
| 4742 | else |
| 4743 | conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); |
| 4744 | |
| 4745 | if (!conn || conn->state != BT_CONNECTED) { |
| 4746 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4747 | MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); |
| 4748 | goto unlock; |
| 4749 | } |
| 4750 | |
| 4751 | /* To avoid client trying to guess when to poll again for information we |
| 4752 | * calculate conn info age as random value between min/max set in hdev. |
| 4753 | */ |
| 4754 | conn_info_age = hdev->conn_info_min_age + |
| 4755 | prandom_u32_max(hdev->conn_info_max_age - |
| 4756 | hdev->conn_info_min_age); |
| 4757 | |
| 4758 | /* Query controller to refresh cached values if they are too old or were |
| 4759 | * never read. |
| 4760 | */ |
Andrzej Kaczmarek | f4e2dd5 | 2014-05-16 16:48:57 +0200 | [diff] [blame] | 4761 | if (time_after(jiffies, conn->conn_info_timestamp + |
| 4762 | msecs_to_jiffies(conn_info_age)) || |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4763 | !conn->conn_info_timestamp) { |
| 4764 | struct hci_request req; |
| 4765 | struct hci_cp_read_tx_power req_txp_cp; |
| 4766 | struct hci_cp_read_rssi req_rssi_cp; |
| 4767 | struct pending_cmd *cmd; |
| 4768 | |
| 4769 | hci_req_init(&req, hdev); |
| 4770 | req_rssi_cp.handle = cpu_to_le16(conn->handle); |
| 4771 | hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp), |
| 4772 | &req_rssi_cp); |
| 4773 | |
Andrzej Kaczmarek | f7faab0 | 2014-05-14 13:43:04 +0200 | [diff] [blame] | 4774 | /* For LE links TX power does not change thus we don't need to |
| 4775 | * query for it once value is known. |
| 4776 | */ |
| 4777 | if (!bdaddr_type_is_le(cp->addr.type) || |
| 4778 | conn->tx_power == HCI_TX_POWER_INVALID) { |
| 4779 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4780 | req_txp_cp.type = 0x00; |
| 4781 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4782 | sizeof(req_txp_cp), &req_txp_cp); |
| 4783 | } |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4784 | |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4785 | /* Max TX power needs to be read only once per connection */ |
| 4786 | if (conn->max_tx_power == HCI_TX_POWER_INVALID) { |
| 4787 | req_txp_cp.handle = cpu_to_le16(conn->handle); |
| 4788 | req_txp_cp.type = 0x01; |
| 4789 | hci_req_add(&req, HCI_OP_READ_TX_POWER, |
| 4790 | sizeof(req_txp_cp), &req_txp_cp); |
| 4791 | } |
| 4792 | |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4793 | err = hci_req_run(&req, conn_info_refresh_complete); |
| 4794 | if (err < 0) |
| 4795 | goto unlock; |
| 4796 | |
| 4797 | cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev, |
| 4798 | data, len); |
| 4799 | if (!cmd) { |
| 4800 | err = -ENOMEM; |
| 4801 | goto unlock; |
| 4802 | } |
| 4803 | |
| 4804 | hci_conn_hold(conn); |
| 4805 | cmd->user_data = conn; |
| 4806 | |
| 4807 | conn->conn_info_timestamp = jiffies; |
| 4808 | } else { |
| 4809 | /* Cache is valid, just reply with values cached in hci_conn */ |
| 4810 | rp.rssi = conn->rssi; |
| 4811 | rp.tx_power = conn->tx_power; |
Andrzej Kaczmarek | eed5daf | 2014-05-14 13:43:06 +0200 | [diff] [blame] | 4812 | rp.max_tx_power = conn->max_tx_power; |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4813 | |
| 4814 | err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, |
| 4815 | MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); |
| 4816 | } |
| 4817 | |
| 4818 | unlock: |
| 4819 | hci_dev_unlock(hdev); |
| 4820 | return err; |
| 4821 | } |
| 4822 | |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4823 | static const struct mgmt_handler { |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4824 | int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, |
| 4825 | u16 data_len); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4826 | bool var_len; |
| 4827 | size_t data_len; |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4828 | } mgmt_handlers[] = { |
| 4829 | { NULL }, /* 0x0000 (no command) */ |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4830 | { read_version, false, MGMT_READ_VERSION_SIZE }, |
| 4831 | { read_commands, false, MGMT_READ_COMMANDS_SIZE }, |
| 4832 | { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE }, |
| 4833 | { read_controller_info, false, MGMT_READ_INFO_SIZE }, |
| 4834 | { set_powered, false, MGMT_SETTING_SIZE }, |
| 4835 | { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE }, |
| 4836 | { set_connectable, false, MGMT_SETTING_SIZE }, |
| 4837 | { set_fast_connectable, false, MGMT_SETTING_SIZE }, |
| 4838 | { set_pairable, false, MGMT_SETTING_SIZE }, |
| 4839 | { set_link_security, false, MGMT_SETTING_SIZE }, |
| 4840 | { set_ssp, false, MGMT_SETTING_SIZE }, |
| 4841 | { set_hs, false, MGMT_SETTING_SIZE }, |
| 4842 | { set_le, false, MGMT_SETTING_SIZE }, |
| 4843 | { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE }, |
| 4844 | { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE }, |
| 4845 | { add_uuid, false, MGMT_ADD_UUID_SIZE }, |
| 4846 | { remove_uuid, false, MGMT_REMOVE_UUID_SIZE }, |
| 4847 | { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE }, |
| 4848 | { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE }, |
| 4849 | { disconnect, false, MGMT_DISCONNECT_SIZE }, |
| 4850 | { get_connections, false, MGMT_GET_CONNECTIONS_SIZE }, |
| 4851 | { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE }, |
| 4852 | { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE }, |
| 4853 | { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE }, |
| 4854 | { pair_device, false, MGMT_PAIR_DEVICE_SIZE }, |
| 4855 | { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE }, |
| 4856 | { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE }, |
| 4857 | { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE }, |
| 4858 | { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE }, |
| 4859 | { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE }, |
| 4860 | { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE }, |
| 4861 | { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE }, |
Marcel Holtmann | ec10911 | 2014-01-10 02:07:30 -0800 | [diff] [blame] | 4862 | { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE }, |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4863 | { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE }, |
| 4864 | { start_discovery, false, MGMT_START_DISCOVERY_SIZE }, |
| 4865 | { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE }, |
| 4866 | { confirm_name, false, MGMT_CONFIRM_NAME_SIZE }, |
| 4867 | { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, |
| 4868 | { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, |
Marcel Holtmann | cdbaccc | 2012-03-11 20:00:29 -0700 | [diff] [blame] | 4869 | { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, |
Johan Hedberg | 4375f10 | 2013-09-25 13:26:10 +0300 | [diff] [blame] | 4870 | { set_advertising, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 0663ca2 | 2013-10-02 13:43:14 +0300 | [diff] [blame] | 4871 | { set_bredr, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 4872 | { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE }, |
Marcel Holtmann | 14b49b9 | 2013-10-11 08:23:20 -0700 | [diff] [blame] | 4873 | { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE }, |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 4874 | { set_secure_conn, false, MGMT_SETTING_SIZE }, |
Marcel Holtmann | 4e39ac8 | 2014-01-31 11:55:22 -0800 | [diff] [blame] | 4875 | { set_debug_keys, false, MGMT_SETTING_SIZE }, |
Johan Hedberg | 62b04cd | 2014-02-23 19:42:27 +0200 | [diff] [blame] | 4876 | { set_privacy, false, MGMT_SET_PRIVACY_SIZE }, |
Johan Hedberg | 41edf16 | 2014-02-18 10:19:35 +0200 | [diff] [blame] | 4877 | { load_irks, true, MGMT_LOAD_IRKS_SIZE }, |
Andrzej Kaczmarek | dd98380 | 2014-05-14 13:43:03 +0200 | [diff] [blame] | 4878 | { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE }, |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4879 | }; |
| 4880 | |
| 4881 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4882 | int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) |
| 4883 | { |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4884 | void *buf; |
| 4885 | u8 *cp; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4886 | struct mgmt_hdr *hdr; |
Szymon Janc | 4e51eae | 2011-02-25 19:05:48 +0100 | [diff] [blame] | 4887 | u16 opcode, index, len; |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4888 | struct hci_dev *hdev = NULL; |
Andrei Emeltchenko | 2e3c35e | 2012-03-14 18:54:15 +0200 | [diff] [blame] | 4889 | const struct mgmt_handler *handler; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4890 | int err; |
| 4891 | |
| 4892 | BT_DBG("got %zu bytes", msglen); |
| 4893 | |
| 4894 | if (msglen < sizeof(*hdr)) |
| 4895 | return -EINVAL; |
| 4896 | |
Gustavo F. Padovan | e63a15e | 2011-04-04 18:56:53 -0300 | [diff] [blame] | 4897 | buf = kmalloc(msglen, GFP_KERNEL); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4898 | if (!buf) |
| 4899 | return -ENOMEM; |
| 4900 | |
| 4901 | if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) { |
| 4902 | err = -EFAULT; |
| 4903 | goto done; |
| 4904 | } |
| 4905 | |
Vinicius Costa Gomes | 650f726 | 2012-02-02 21:07:59 -0300 | [diff] [blame] | 4906 | hdr = buf; |
Marcel Holtmann | 1f350c8 | 2012-03-12 20:31:08 -0700 | [diff] [blame] | 4907 | opcode = __le16_to_cpu(hdr->opcode); |
| 4908 | index = __le16_to_cpu(hdr->index); |
| 4909 | len = __le16_to_cpu(hdr->len); |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4910 | |
| 4911 | if (len != msglen - sizeof(*hdr)) { |
| 4912 | err = -EINVAL; |
| 4913 | goto done; |
| 4914 | } |
| 4915 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4916 | if (index != MGMT_INDEX_NONE) { |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4917 | hdev = hci_dev_get(index); |
| 4918 | if (!hdev) { |
| 4919 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4920 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4921 | goto done; |
| 4922 | } |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4923 | |
Johan Hedberg | cebf4cf | 2013-10-10 18:06:04 +0200 | [diff] [blame] | 4924 | if (test_bit(HCI_SETUP, &hdev->dev_flags) || |
| 4925 | test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { |
Marcel Holtmann | 0736cfa | 2013-08-26 21:40:51 -0700 | [diff] [blame] | 4926 | err = cmd_status(sk, index, opcode, |
| 4927 | MGMT_STATUS_INVALID_INDEX); |
| 4928 | goto done; |
| 4929 | } |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4930 | } |
| 4931 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4932 | if (opcode >= ARRAY_SIZE(mgmt_handlers) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4933 | mgmt_handlers[opcode].func == NULL) { |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4934 | BT_DBG("Unknown op %u", opcode); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 4935 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4936 | MGMT_STATUS_UNKNOWN_COMMAND); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4937 | goto done; |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4938 | } |
| 4939 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4940 | if ((hdev && opcode < MGMT_OP_READ_INFO) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4941 | (!hdev && opcode >= MGMT_OP_READ_INFO)) { |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4942 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4943 | MGMT_STATUS_INVALID_INDEX); |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4944 | goto done; |
| 4945 | } |
| 4946 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4947 | handler = &mgmt_handlers[opcode]; |
| 4948 | |
| 4949 | if ((handler->var_len && len < handler->data_len) || |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 4950 | (!handler->var_len && len != handler->data_len)) { |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4951 | err = cmd_status(sk, index, opcode, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 4952 | MGMT_STATUS_INVALID_PARAMS); |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4953 | goto done; |
| 4954 | } |
| 4955 | |
Johan Hedberg | 0f4e68c | 2012-02-28 17:18:30 +0200 | [diff] [blame] | 4956 | if (hdev) |
| 4957 | mgmt_init_hdev(sk, hdev); |
| 4958 | |
| 4959 | cp = buf + sizeof(*hdr); |
| 4960 | |
Johan Hedberg | be22b54 | 2012-03-01 22:24:41 +0200 | [diff] [blame] | 4961 | err = handler->func(sk, hdev, cp, len); |
Johan Hedberg | e41d8b4 | 2010-12-13 21:07:03 +0200 | [diff] [blame] | 4962 | if (err < 0) |
| 4963 | goto done; |
| 4964 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4965 | err = msglen; |
| 4966 | |
| 4967 | done: |
Johan Hedberg | bdb6d97 | 2012-02-28 06:13:32 +0200 | [diff] [blame] | 4968 | if (hdev) |
| 4969 | hci_dev_put(hdev); |
| 4970 | |
Johan Hedberg | 0381101 | 2010-12-08 00:21:06 +0200 | [diff] [blame] | 4971 | kfree(buf); |
| 4972 | return err; |
| 4973 | } |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4974 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4975 | void mgmt_index_added(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4976 | { |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4977 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4978 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4979 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4980 | mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4981 | } |
| 4982 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4983 | void mgmt_index_removed(struct hci_dev *hdev) |
Johan Hedberg | c71e97b | 2010-12-13 21:07:07 +0200 | [diff] [blame] | 4984 | { |
Johan Hedberg | 5f15903 | 2012-03-02 03:13:19 +0200 | [diff] [blame] | 4985 | u8 status = MGMT_STATUS_INVALID_INDEX; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4986 | |
Marcel Holtmann | 1514b89 | 2013-10-06 08:25:01 -0700 | [diff] [blame] | 4987 | if (hdev->dev_type != HCI_BREDR) |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4988 | return; |
Andrei Emeltchenko | bb4b2a9 | 2012-07-19 17:03:40 +0300 | [diff] [blame] | 4989 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 4990 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 4991 | |
Marcel Holtmann | bf6b56d | 2013-10-06 23:55:45 -0700 | [diff] [blame] | 4992 | mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 4993 | } |
| 4994 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 4995 | /* This function requires the caller holds hdev->lock */ |
| 4996 | static void restart_le_auto_conns(struct hci_dev *hdev) |
| 4997 | { |
| 4998 | struct hci_conn_params *p; |
| 4999 | |
| 5000 | list_for_each_entry(p, &hdev->le_conn_params, list) { |
| 5001 | if (p->auto_connect == HCI_AUTO_CONN_ALWAYS) |
| 5002 | hci_pend_le_conn_add(hdev, &p->addr, p->addr_type); |
| 5003 | } |
| 5004 | } |
| 5005 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5006 | static void powered_complete(struct hci_dev *hdev, u8 status) |
| 5007 | { |
| 5008 | struct cmd_lookup match = { NULL, hdev }; |
| 5009 | |
| 5010 | BT_DBG("status 0x%02x", status); |
| 5011 | |
| 5012 | hci_dev_lock(hdev); |
| 5013 | |
Andre Guedes | 6046dc3 | 2014-02-26 20:21:51 -0300 | [diff] [blame] | 5014 | restart_le_auto_conns(hdev); |
| 5015 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5016 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5017 | |
| 5018 | new_settings(hdev, match.sk); |
| 5019 | |
| 5020 | hci_dev_unlock(hdev); |
| 5021 | |
| 5022 | if (match.sk) |
| 5023 | sock_put(match.sk); |
| 5024 | } |
| 5025 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5026 | static int powered_update_hci(struct hci_dev *hdev) |
| 5027 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5028 | struct hci_request req; |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5029 | u8 link_sec; |
| 5030 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5031 | hci_req_init(&req, hdev); |
| 5032 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5033 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && |
| 5034 | !lmp_host_ssp_capable(hdev)) { |
| 5035 | u8 ssp = 1; |
| 5036 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5037 | hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, 1, &ssp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5038 | } |
| 5039 | |
Johan Hedberg | c73eee9 | 2013-04-19 18:35:21 +0300 | [diff] [blame] | 5040 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && |
| 5041 | lmp_bredr_capable(hdev)) { |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5042 | struct hci_cp_write_le_host_supported cp; |
| 5043 | |
| 5044 | cp.le = 1; |
| 5045 | cp.simul = lmp_le_br_capable(hdev); |
| 5046 | |
| 5047 | /* Check first if we already have the right |
| 5048 | * host state (host features set) |
| 5049 | */ |
| 5050 | if (cp.le != lmp_host_le_capable(hdev) || |
| 5051 | cp.simul != lmp_host_le_br_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5052 | hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, |
| 5053 | sizeof(cp), &cp); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5054 | } |
| 5055 | |
Marcel Holtmann | d13eafc | 2013-10-02 04:41:30 -0700 | [diff] [blame] | 5056 | if (lmp_le_capable(hdev)) { |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5057 | /* Make sure the controller has a good default for |
| 5058 | * advertising data. This also applies to the case |
| 5059 | * where BR/EDR was toggled during the AUTO_OFF phase. |
| 5060 | */ |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5061 | if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { |
Marcel Holtmann | 5947f4b | 2013-10-16 00:16:50 -0700 | [diff] [blame] | 5062 | update_adv_data(&req); |
Marcel Holtmann | f14d8f6 | 2013-10-16 00:16:48 -0700 | [diff] [blame] | 5063 | update_scan_rsp_data(&req); |
| 5064 | } |
Marcel Holtmann | 441ad2d | 2013-10-15 06:33:52 -0700 | [diff] [blame] | 5065 | |
Marcel Holtmann | bba3aa5 | 2013-10-06 02:55:21 -0700 | [diff] [blame] | 5066 | if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 5067 | enable_advertising(&req); |
Johan Hedberg | eeca6f8 | 2013-09-25 13:26:09 +0300 | [diff] [blame] | 5068 | } |
| 5069 | |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5070 | link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); |
| 5071 | if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5072 | hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, |
| 5073 | sizeof(link_sec), &link_sec); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5074 | |
| 5075 | if (lmp_bredr_capable(hdev)) { |
Johan Hedberg | 56f8790 | 2013-10-02 13:43:13 +0300 | [diff] [blame] | 5076 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) |
| 5077 | set_bredr_scan(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5078 | update_class(&req); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5079 | update_name(&req); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5080 | update_eir(&req); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5081 | } |
| 5082 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5083 | return hci_req_run(&req, powered_complete); |
Johan Hedberg | 70da624 | 2013-03-15 17:06:51 -0500 | [diff] [blame] | 5084 | } |
| 5085 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5086 | int mgmt_powered(struct hci_dev *hdev, u8 powered) |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5087 | { |
Johan Hedberg | 76a7f3a | 2012-02-17 00:34:40 +0200 | [diff] [blame] | 5088 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5089 | u8 status_not_powered = MGMT_STATUS_NOT_POWERED; |
| 5090 | u8 zero_cod[] = { 0, 0, 0 }; |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5091 | int err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5092 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5093 | if (!test_bit(HCI_MGMT, &hdev->dev_flags)) |
| 5094 | return 0; |
| 5095 | |
Johan Hedberg | 5e5282b | 2012-02-21 16:01:30 +0200 | [diff] [blame] | 5096 | if (powered) { |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5097 | if (powered_update_hci(hdev) == 0) |
| 5098 | return 0; |
Johan Hedberg | fe03888 | 2013-01-16 16:15:34 +0200 | [diff] [blame] | 5099 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5100 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, |
| 5101 | &match); |
| 5102 | goto new_settings; |
Johan Hedberg | b24752f | 2011-11-03 14:40:33 +0200 | [diff] [blame] | 5103 | } |
| 5104 | |
Johan Hedberg | 229ab39 | 2013-03-15 17:06:53 -0500 | [diff] [blame] | 5105 | mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match); |
| 5106 | mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status_not_powered); |
| 5107 | |
| 5108 | if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) |
| 5109 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, |
| 5110 | zero_cod, sizeof(zero_cod), NULL); |
| 5111 | |
| 5112 | new_settings: |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5113 | err = new_settings(hdev, match.sk); |
Johan Hedberg | eec8d2b | 2010-12-16 10:17:38 +0200 | [diff] [blame] | 5114 | |
| 5115 | if (match.sk) |
| 5116 | sock_put(match.sk); |
| 5117 | |
Johan Hedberg | 7bb895d | 2012-02-17 01:20:00 +0200 | [diff] [blame] | 5118 | return err; |
Johan Hedberg | 5add6af | 2010-12-16 10:00:37 +0200 | [diff] [blame] | 5119 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5120 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5121 | void mgmt_set_powered_failed(struct hci_dev *hdev, int err) |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5122 | { |
| 5123 | struct pending_cmd *cmd; |
| 5124 | u8 status; |
| 5125 | |
| 5126 | cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5127 | if (!cmd) |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5128 | return; |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5129 | |
| 5130 | if (err == -ERFKILL) |
| 5131 | status = MGMT_STATUS_RFKILLED; |
| 5132 | else |
| 5133 | status = MGMT_STATUS_FAILED; |
| 5134 | |
Marcel Holtmann | 3eec705 | 2013-10-06 23:55:46 -0700 | [diff] [blame] | 5135 | cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5136 | |
| 5137 | mgmt_pending_remove(cmd); |
Johan Hedberg | 96570ff | 2013-05-29 09:51:29 +0300 | [diff] [blame] | 5138 | } |
| 5139 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5140 | void mgmt_discoverable_timeout(struct hci_dev *hdev) |
| 5141 | { |
| 5142 | struct hci_request req; |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5143 | |
| 5144 | hci_dev_lock(hdev); |
| 5145 | |
| 5146 | /* When discoverable timeout triggers, then just make sure |
| 5147 | * the limited discoverable flag is cleared. Even in the case |
| 5148 | * of a timeout triggered from general discoverable, it is |
| 5149 | * safe to unconditionally clear the flag. |
| 5150 | */ |
| 5151 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5152 | clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5153 | |
| 5154 | hci_req_init(&req, hdev); |
Johan Hedberg | 4b58061 | 2013-10-19 23:38:21 +0300 | [diff] [blame] | 5155 | if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { |
| 5156 | u8 scan = SCAN_PAGE; |
| 5157 | hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, |
| 5158 | sizeof(scan), &scan); |
| 5159 | } |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5160 | update_class(&req); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5161 | update_adv_data(&req); |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5162 | hci_req_run(&req, NULL); |
| 5163 | |
| 5164 | hdev->discov_timeout = 0; |
| 5165 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5166 | new_settings(hdev, NULL); |
| 5167 | |
Marcel Holtmann | d1967ff | 2013-10-15 10:57:40 -0700 | [diff] [blame] | 5168 | hci_dev_unlock(hdev); |
| 5169 | } |
| 5170 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5171 | void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5172 | { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5173 | bool changed; |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5174 | |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5175 | /* Nothing needed here if there's a pending command since that |
| 5176 | * commands request completion callback takes care of everything |
| 5177 | * necessary. |
| 5178 | */ |
| 5179 | if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev)) |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5180 | return; |
Johan Hedberg | bfaf8c9 | 2013-10-14 21:15:27 +0300 | [diff] [blame] | 5181 | |
Johan Hedberg | bd10799 | 2014-02-24 14:52:19 +0200 | [diff] [blame] | 5182 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5183 | if (!discoverable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5184 | return; |
| 5185 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5186 | if (discoverable) { |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5187 | changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5188 | } else { |
| 5189 | clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5190 | changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5191 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5192 | |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5193 | if (changed) { |
| 5194 | struct hci_request req; |
| 5195 | |
| 5196 | /* In case this change in discoverable was triggered by |
| 5197 | * a disabling of connectable there could be a need to |
| 5198 | * update the advertising flags. |
| 5199 | */ |
| 5200 | hci_req_init(&req, hdev); |
| 5201 | update_adv_data(&req); |
| 5202 | hci_req_run(&req, NULL); |
| 5203 | |
Marcel Holtmann | 86a7564 | 2013-10-15 06:33:54 -0700 | [diff] [blame] | 5204 | new_settings(hdev, NULL); |
Johan Hedberg | 9a43e25 | 2013-10-20 19:00:07 +0300 | [diff] [blame] | 5205 | } |
Johan Hedberg | 73f22f6 | 2010-12-29 16:00:25 +0200 | [diff] [blame] | 5206 | } |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5207 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5208 | void mgmt_connectable(struct hci_dev *hdev, u8 connectable) |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5209 | { |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5210 | bool changed; |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5211 | |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5212 | /* Nothing needed here if there's a pending command since that |
| 5213 | * commands request completion callback takes care of everything |
| 5214 | * necessary. |
| 5215 | */ |
| 5216 | if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5217 | return; |
Johan Hedberg | d7b856f | 2013-10-14 16:20:04 +0300 | [diff] [blame] | 5218 | |
Johan Hedberg | ce3f24c | 2014-02-24 14:52:20 +0200 | [diff] [blame] | 5219 | /* Powering off may clear the scan mode - don't let that interfere */ |
| 5220 | if (!connectable && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5221 | return; |
| 5222 | |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5223 | if (connectable) |
| 5224 | changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
| 5225 | else |
| 5226 | changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5227 | |
Johan Hedberg | beadb2b | 2012-02-21 16:55:31 +0200 | [diff] [blame] | 5228 | if (changed) |
Marcel Holtmann | a330916 | 2013-10-15 06:33:55 -0700 | [diff] [blame] | 5229 | new_settings(hdev, NULL); |
Johan Hedberg | 9fbcbb4 | 2010-12-30 00:18:33 +0200 | [diff] [blame] | 5230 | } |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5231 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5232 | void mgmt_advertising(struct hci_dev *hdev, u8 advertising) |
| 5233 | { |
Johan Hedberg | 7c4cfab | 2014-02-24 14:52:21 +0200 | [diff] [blame] | 5234 | /* Powering off may stop advertising - don't let that interfere */ |
| 5235 | if (!advertising && mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
| 5236 | return; |
| 5237 | |
Johan Hedberg | 778b235 | 2014-02-24 14:52:17 +0200 | [diff] [blame] | 5238 | if (advertising) |
| 5239 | set_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5240 | else |
| 5241 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
| 5242 | } |
| 5243 | |
Marcel Holtmann | 4796e8a | 2013-10-15 06:33:56 -0700 | [diff] [blame] | 5244 | 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] | 5245 | { |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5246 | u8 mgmt_err = mgmt_status(status); |
| 5247 | |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5248 | if (scan & SCAN_PAGE) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5249 | mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5250 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5251 | |
| 5252 | if (scan & SCAN_INQUIRY) |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5253 | mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5254 | cmd_status_rsp, &mgmt_err); |
Johan Hedberg | 2d7cee5 | 2011-11-07 22:16:03 +0200 | [diff] [blame] | 5255 | } |
| 5256 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5257 | void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, |
| 5258 | bool persistent) |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5259 | { |
Johan Hedberg | 86742e1 | 2011-11-07 23:13:38 +0200 | [diff] [blame] | 5260 | struct mgmt_ev_new_link_key ev; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5261 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5262 | memset(&ev, 0, sizeof(ev)); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5263 | |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5264 | ev.store_hint = persistent; |
Johan Hedberg | d753fdc | 2012-02-17 14:06:34 +0200 | [diff] [blame] | 5265 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5266 | ev.key.addr.type = BDADDR_BREDR; |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5267 | ev.key.type = key->type; |
Andrei Emeltchenko | 9b3b446 | 2012-05-23 11:31:20 +0300 | [diff] [blame] | 5268 | memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); |
Vinicius Costa Gomes | a492cd5 | 2011-08-25 20:02:29 -0300 | [diff] [blame] | 5269 | ev.key.pin_len = key->pin_len; |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5270 | |
Marcel Holtmann | dc4a5ee | 2013-10-15 10:15:57 -0700 | [diff] [blame] | 5271 | mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 55ed8ca1 | 2011-01-17 14:41:05 +0200 | [diff] [blame] | 5272 | } |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5273 | |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5274 | static u8 mgmt_ltk_type(struct smp_ltk *ltk) |
| 5275 | { |
| 5276 | if (ltk->authenticated) |
| 5277 | return MGMT_LTK_AUTHENTICATED; |
| 5278 | |
| 5279 | return MGMT_LTK_UNAUTHENTICATED; |
| 5280 | } |
| 5281 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5282 | 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] | 5283 | { |
| 5284 | struct mgmt_ev_new_long_term_key ev; |
| 5285 | |
| 5286 | memset(&ev, 0, sizeof(ev)); |
| 5287 | |
Marcel Holtmann | 5192d30 | 2014-02-19 17:11:58 -0800 | [diff] [blame] | 5288 | /* Devices using resolvable or non-resolvable random addresses |
| 5289 | * without providing an indentity resolving key don't require |
| 5290 | * to store long term keys. Their addresses will change the |
| 5291 | * next time around. |
| 5292 | * |
| 5293 | * Only when a remote device provides an identity address |
| 5294 | * make sure the long term key is stored. If the remote |
| 5295 | * identity is known, the long term keys are internally |
| 5296 | * mapped to the identity address. So allow static random |
| 5297 | * and public addresses here. |
| 5298 | */ |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5299 | if (key->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5300 | (key->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5301 | ev.store_hint = 0x00; |
| 5302 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5303 | ev.store_hint = persistent; |
Johan Hedberg | ba74b66 | 2014-02-19 14:57:45 +0200 | [diff] [blame] | 5304 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5305 | bacpy(&ev.key.addr.bdaddr, &key->bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5306 | ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type); |
Johan Hedberg | d7b2545 | 2014-05-23 13:19:53 +0300 | [diff] [blame] | 5307 | ev.key.type = mgmt_ltk_type(key); |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5308 | ev.key.enc_size = key->enc_size; |
| 5309 | ev.key.ediv = key->ediv; |
Marcel Holtmann | fe39c7b | 2014-02-27 16:00:28 -0800 | [diff] [blame] | 5310 | ev.key.rand = key->rand; |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5311 | |
Johan Hedberg | 2ceba53 | 2014-06-16 19:25:16 +0300 | [diff] [blame] | 5312 | if (key->type == SMP_LTK) |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5313 | ev.key.master = 1; |
| 5314 | |
Vinicius Costa Gomes | 346af67 | 2012-02-02 21:08:02 -0300 | [diff] [blame] | 5315 | memcpy(ev.key.val, key->val, sizeof(key->val)); |
| 5316 | |
Marcel Holtmann | 083368f | 2013-10-15 14:26:29 -0700 | [diff] [blame] | 5317 | 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] | 5318 | } |
| 5319 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5320 | void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk) |
| 5321 | { |
| 5322 | struct mgmt_ev_new_irk ev; |
| 5323 | |
| 5324 | memset(&ev, 0, sizeof(ev)); |
| 5325 | |
Marcel Holtmann | bab6d1e | 2014-02-19 11:51:54 -0800 | [diff] [blame] | 5326 | /* For identity resolving keys from devices that are already |
| 5327 | * using a public address or static random address, do not |
| 5328 | * ask for storing this key. The identity resolving key really |
| 5329 | * is only mandatory for devices using resovlable random |
| 5330 | * addresses. |
| 5331 | * |
| 5332 | * Storing all identity resolving keys has the downside that |
| 5333 | * they will be also loaded on next boot of they system. More |
| 5334 | * identity resolving keys, means more time during scanning is |
| 5335 | * needed to actually resolve these addresses. |
| 5336 | */ |
| 5337 | if (bacmp(&irk->rpa, BDADDR_ANY)) |
| 5338 | ev.store_hint = 0x01; |
| 5339 | else |
| 5340 | ev.store_hint = 0x00; |
| 5341 | |
Johan Hedberg | 95fbac8 | 2014-02-19 15:18:31 +0200 | [diff] [blame] | 5342 | bacpy(&ev.rpa, &irk->rpa); |
| 5343 | bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr); |
| 5344 | ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type); |
| 5345 | memcpy(ev.irk.val, irk->val, sizeof(irk->val)); |
| 5346 | |
| 5347 | mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL); |
| 5348 | } |
| 5349 | |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5350 | void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk, |
| 5351 | bool persistent) |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5352 | { |
| 5353 | struct mgmt_ev_new_csrk ev; |
| 5354 | |
| 5355 | memset(&ev, 0, sizeof(ev)); |
| 5356 | |
| 5357 | /* Devices using resolvable or non-resolvable random addresses |
| 5358 | * without providing an indentity resolving key don't require |
| 5359 | * to store signature resolving keys. Their addresses will change |
| 5360 | * the next time around. |
| 5361 | * |
| 5362 | * Only when a remote device provides an identity address |
| 5363 | * make sure the signature resolving key is stored. So allow |
| 5364 | * static random and public addresses here. |
| 5365 | */ |
| 5366 | if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM && |
| 5367 | (csrk->bdaddr.b[5] & 0xc0) != 0xc0) |
| 5368 | ev.store_hint = 0x00; |
| 5369 | else |
Marcel Holtmann | 53ac6ab | 2014-03-09 23:38:42 -0700 | [diff] [blame] | 5370 | ev.store_hint = persistent; |
Marcel Holtmann | 7ee4ea3 | 2014-03-09 12:19:17 -0700 | [diff] [blame] | 5371 | |
| 5372 | bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); |
| 5373 | ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); |
| 5374 | ev.key.master = csrk->master; |
| 5375 | memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); |
| 5376 | |
| 5377 | mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); |
| 5378 | } |
| 5379 | |
Marcel Holtmann | 9493399 | 2013-10-15 10:26:39 -0700 | [diff] [blame] | 5380 | static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data, |
| 5381 | u8 data_len) |
| 5382 | { |
| 5383 | eir[eir_len++] = sizeof(type) + data_len; |
| 5384 | eir[eir_len++] = type; |
| 5385 | memcpy(&eir[eir_len], data, data_len); |
| 5386 | eir_len += data_len; |
| 5387 | |
| 5388 | return eir_len; |
| 5389 | } |
| 5390 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5391 | void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5392 | u8 addr_type, u32 flags, u8 *name, u8 name_len, |
| 5393 | u8 *dev_class) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5394 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5395 | char buf[512]; |
| 5396 | struct mgmt_ev_device_connected *ev = (void *) buf; |
| 5397 | u16 eir_len = 0; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5398 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5399 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5400 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5401 | |
Johan Hedberg | c95f0ba | 2012-02-23 22:54:38 +0200 | [diff] [blame] | 5402 | ev->flags = __cpu_to_le32(flags); |
Johan Hedberg | 08c79b6 | 2012-02-23 22:31:51 +0200 | [diff] [blame] | 5403 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5404 | if (name_len > 0) |
| 5405 | eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5406 | name, name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5407 | |
| 5408 | if (dev_class && memcmp(dev_class, "\0\0\0", 3) != 0) |
Brian Gix | 5315638 | 2012-03-09 14:07:03 -0800 | [diff] [blame] | 5409 | eir_len = eir_append_data(ev->eir, eir_len, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5410 | EIR_CLASS_OF_DEV, dev_class, 3); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5411 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 5412 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 5413 | |
Marcel Holtmann | ecd90ae | 2013-10-06 23:55:49 -0700 | [diff] [blame] | 5414 | mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, |
| 5415 | sizeof(*ev) + eir_len, NULL); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5416 | } |
| 5417 | |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5418 | static void disconnect_rsp(struct pending_cmd *cmd, void *data) |
| 5419 | { |
Szymon Janc | c68fb7f | 2011-03-22 13:12:19 +0100 | [diff] [blame] | 5420 | struct mgmt_cp_disconnect *cp = cmd->param; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5421 | struct sock **sk = data; |
Johan Hedberg | a38528f | 2011-01-22 06:46:43 +0200 | [diff] [blame] | 5422 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5423 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5424 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5425 | rp.addr.type = cp->addr.type; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5426 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5427 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 0, &rp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5428 | sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5429 | |
| 5430 | *sk = cmd->sk; |
| 5431 | sock_hold(*sk); |
| 5432 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5433 | mgmt_pending_remove(cmd); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5434 | } |
| 5435 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5436 | static void unpair_device_rsp(struct pending_cmd *cmd, void *data) |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5437 | { |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5438 | struct hci_dev *hdev = data; |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5439 | struct mgmt_cp_unpair_device *cp = cmd->param; |
| 5440 | struct mgmt_rp_unpair_device rp; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5441 | |
| 5442 | memset(&rp, 0, sizeof(rp)); |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5443 | bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr); |
| 5444 | rp.addr.type = cp->addr.type; |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5445 | |
Johan Hedberg | b1078ad | 2012-02-09 17:21:16 +0200 | [diff] [blame] | 5446 | device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk); |
| 5447 | |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5448 | cmd_complete(cmd->sk, cmd->index, cmd->opcode, 0, &rp, sizeof(rp)); |
Johan Hedberg | a8a1d19 | 2011-11-10 15:54:38 +0200 | [diff] [blame] | 5449 | |
| 5450 | mgmt_pending_remove(cmd); |
| 5451 | } |
| 5452 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5453 | void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5454 | u8 link_type, u8 addr_type, u8 reason, |
| 5455 | bool mgmt_connected) |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5456 | { |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5457 | struct mgmt_ev_device_disconnected ev; |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5458 | struct pending_cmd *power_off; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5459 | struct sock *sk = NULL; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5460 | |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5461 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5462 | if (power_off) { |
| 5463 | struct mgmt_mode *cp = power_off->param; |
| 5464 | |
| 5465 | /* The connection is still in hci_conn_hash so test for 1 |
| 5466 | * instead of 0 to know if this is the last one. |
| 5467 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5468 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5469 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5470 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5471 | } |
Johan Hedberg | 8b064a3 | 2014-02-24 14:52:22 +0200 | [diff] [blame] | 5472 | } |
| 5473 | |
Johan Hedberg | 12d4a3b | 2014-02-24 14:52:18 +0200 | [diff] [blame] | 5474 | if (!mgmt_connected) |
| 5475 | return; |
| 5476 | |
Andre Guedes | 57eb776 | 2013-10-30 19:01:41 -0300 | [diff] [blame] | 5477 | if (link_type != ACL_LINK && link_type != LE_LINK) |
| 5478 | return; |
| 5479 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5480 | mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5481 | |
Mikel Astiz | f0d6a0e | 2012-08-09 09:52:30 +0200 | [diff] [blame] | 5482 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5483 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5484 | ev.reason = reason; |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5485 | |
Marcel Holtmann | 9b80ec5 | 2013-10-06 23:55:50 -0700 | [diff] [blame] | 5486 | mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5487 | |
| 5488 | if (sk) |
Szymon Janc | d97dcb6 | 2012-03-16 16:02:56 +0100 | [diff] [blame] | 5489 | sock_put(sk); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5490 | |
Johan Hedberg | 124f6e3 | 2012-02-09 13:50:12 +0200 | [diff] [blame] | 5491 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5492 | hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5493 | } |
| 5494 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5495 | void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5496 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5497 | { |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5498 | u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); |
| 5499 | struct mgmt_cp_disconnect *cp; |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5500 | struct mgmt_rp_disconnect rp; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5501 | struct pending_cmd *cmd; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5502 | |
Jefferson Delfes | 36a75f1 | 2012-09-18 13:36:54 -0400 | [diff] [blame] | 5503 | mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, |
| 5504 | hdev); |
| 5505 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5506 | cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5507 | if (!cmd) |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5508 | return; |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5509 | |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5510 | cp = cmd->param; |
| 5511 | |
| 5512 | if (bacmp(bdaddr, &cp->addr.bdaddr)) |
| 5513 | return; |
| 5514 | |
| 5515 | if (cp->addr.type != bdaddr_type) |
| 5516 | return; |
| 5517 | |
Johan Hedberg | 88c3df1 | 2012-02-09 14:27:38 +0200 | [diff] [blame] | 5518 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 3655bba | 2013-10-30 19:01:40 -0300 | [diff] [blame] | 5519 | rp.addr.type = bdaddr_type; |
Johan Hedberg | 37d9ef7 | 2011-11-10 15:54:39 +0200 | [diff] [blame] | 5520 | |
Marcel Holtmann | 7892924 | 2013-10-06 23:55:47 -0700 | [diff] [blame] | 5521 | cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, |
| 5522 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 8962ee7 | 2011-01-20 12:40:27 +0200 | [diff] [blame] | 5523 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5524 | mgmt_pending_remove(cmd); |
Johan Hedberg | f752054 | 2011-01-20 12:34:39 +0200 | [diff] [blame] | 5525 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5526 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5527 | void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5528 | u8 addr_type, u8 status) |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5529 | { |
| 5530 | struct mgmt_ev_connect_failed ev; |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5531 | struct pending_cmd *power_off; |
| 5532 | |
| 5533 | power_off = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); |
| 5534 | if (power_off) { |
| 5535 | struct mgmt_mode *cp = power_off->param; |
| 5536 | |
| 5537 | /* The connection is still in hci_conn_hash so test for 1 |
| 5538 | * instead of 0 to know if this is the last one. |
| 5539 | */ |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5540 | if (!cp->val && hci_conn_count(hdev) == 1) { |
| 5541 | cancel_delayed_work(&hdev->power_off); |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5542 | queue_work(hdev->req_workqueue, &hdev->power_off.work); |
Johan Hedberg | a3172b7 | 2014-02-28 09:33:44 +0200 | [diff] [blame] | 5543 | } |
Johan Hedberg | c9910d0 | 2014-02-27 14:35:12 +0200 | [diff] [blame] | 5544 | } |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5545 | |
Johan Hedberg | 4c659c3 | 2011-11-07 23:13:39 +0200 | [diff] [blame] | 5546 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5547 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5548 | ev.status = mgmt_status(status); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5549 | |
Marcel Holtmann | 445608d | 2013-10-06 23:55:48 -0700 | [diff] [blame] | 5550 | mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 17d5c04 | 2011-01-22 06:09:08 +0200 | [diff] [blame] | 5551 | } |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5552 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5553 | 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] | 5554 | { |
| 5555 | struct mgmt_ev_pin_code_request ev; |
| 5556 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5557 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5558 | ev.addr.type = BDADDR_BREDR; |
Waldemar Rymarkiewicz | a770bb5 | 2011-04-28 12:07:59 +0200 | [diff] [blame] | 5559 | ev.secure = secure; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5560 | |
Marcel Holtmann | ce0e4a0 | 2013-10-15 14:26:20 -0700 | [diff] [blame] | 5561 | mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5562 | } |
| 5563 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5564 | void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5565 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5566 | { |
| 5567 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5568 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5569 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5570 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5571 | if (!cmd) |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5572 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5573 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5574 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5575 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5576 | |
Marcel Holtmann | e669cf8 | 2013-10-15 14:26:21 -0700 | [diff] [blame] | 5577 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, |
| 5578 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5579 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5580 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5581 | } |
| 5582 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5583 | void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5584 | u8 status) |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5585 | { |
| 5586 | struct pending_cmd *cmd; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5587 | struct mgmt_rp_pin_code_reply rp; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5588 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5589 | cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5590 | if (!cmd) |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5591 | return; |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5592 | |
Johan Hedberg | d845769 | 2012-02-17 14:24:57 +0200 | [diff] [blame] | 5593 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 591f47f | 2012-04-24 21:02:49 -0300 | [diff] [blame] | 5594 | rp.addr.type = BDADDR_BREDR; |
Johan Hedberg | ac56fb1 | 2011-02-19 12:05:59 -0300 | [diff] [blame] | 5595 | |
Marcel Holtmann | 3eb3852 | 2013-10-15 14:26:22 -0700 | [diff] [blame] | 5596 | cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, |
| 5597 | mgmt_status(status), &rp, sizeof(rp)); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5598 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5599 | mgmt_pending_remove(cmd); |
Johan Hedberg | 980e1a5 | 2011-01-22 06:10:07 +0200 | [diff] [blame] | 5600 | } |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5601 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5602 | int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5603 | u8 link_type, u8 addr_type, u32 value, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5604 | u8 confirm_hint) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5605 | { |
| 5606 | struct mgmt_ev_user_confirm_request ev; |
| 5607 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5608 | BT_DBG("%s", hdev->name); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5609 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5610 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5611 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | 55bc1a3 | 2011-04-28 11:28:56 -0700 | [diff] [blame] | 5612 | ev.confirm_hint = confirm_hint; |
Johan Hedberg | 39adbff | 2014-03-20 08:18:14 +0200 | [diff] [blame] | 5613 | ev.value = cpu_to_le32(value); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5614 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5615 | 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] | 5616 | NULL); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5617 | } |
| 5618 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5619 | int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr, |
Gustavo Padovan | 8ce8e2b | 2012-05-17 00:36:20 -0300 | [diff] [blame] | 5620 | u8 link_type, u8 addr_type) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5621 | { |
| 5622 | struct mgmt_ev_user_passkey_request ev; |
| 5623 | |
| 5624 | BT_DBG("%s", hdev->name); |
| 5625 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5626 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5627 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5628 | |
| 5629 | 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] | 5630 | NULL); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5631 | } |
| 5632 | |
Brian Gix | 0df4c18 | 2011-11-16 13:53:13 -0800 | [diff] [blame] | 5633 | 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] | 5634 | u8 link_type, u8 addr_type, u8 status, |
| 5635 | u8 opcode) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5636 | { |
| 5637 | struct pending_cmd *cmd; |
| 5638 | struct mgmt_rp_user_confirm_reply rp; |
| 5639 | int err; |
| 5640 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5641 | cmd = mgmt_pending_find(opcode, hdev); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5642 | if (!cmd) |
| 5643 | return -ENOENT; |
| 5644 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5645 | bacpy(&rp.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5646 | rp.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | aee9b21 | 2012-02-18 15:07:59 +0200 | [diff] [blame] | 5647 | err = cmd_complete(cmd->sk, hdev->id, opcode, mgmt_status(status), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5648 | &rp, sizeof(rp)); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5649 | |
Johan Hedberg | a664b5b | 2011-02-19 12:06:02 -0300 | [diff] [blame] | 5650 | mgmt_pending_remove(cmd); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5651 | |
| 5652 | return err; |
| 5653 | } |
| 5654 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5655 | 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] | 5656 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5657 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5658 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5659 | status, MGMT_OP_USER_CONFIRM_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5660 | } |
| 5661 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5662 | 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] | 5663 | u8 link_type, u8 addr_type, u8 status) |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5664 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5665 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5666 | status, |
| 5667 | MGMT_OP_USER_CONFIRM_NEG_REPLY); |
Johan Hedberg | a5c2968 | 2011-02-19 12:05:57 -0300 | [diff] [blame] | 5668 | } |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5669 | |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5670 | 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] | 5671 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5672 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5673 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5674 | status, MGMT_OP_USER_PASSKEY_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5675 | } |
| 5676 | |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5677 | 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] | 5678 | u8 link_type, u8 addr_type, u8 status) |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5679 | { |
Johan Hedberg | 272d90d | 2012-02-09 15:26:12 +0200 | [diff] [blame] | 5680 | return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type, |
Gustavo Padovan | 8fc9ced | 2012-05-23 04:04:21 -0300 | [diff] [blame] | 5681 | status, |
| 5682 | MGMT_OP_USER_PASSKEY_NEG_REPLY); |
Brian Gix | 604086b | 2011-11-23 08:28:33 -0800 | [diff] [blame] | 5683 | } |
| 5684 | |
Johan Hedberg | 92a2525 | 2012-09-06 18:39:26 +0300 | [diff] [blame] | 5685 | int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr, |
| 5686 | u8 link_type, u8 addr_type, u32 passkey, |
| 5687 | u8 entered) |
| 5688 | { |
| 5689 | struct mgmt_ev_passkey_notify ev; |
| 5690 | |
| 5691 | BT_DBG("%s", hdev->name); |
| 5692 | |
| 5693 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 5694 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
| 5695 | ev.passkey = __cpu_to_le32(passkey); |
| 5696 | ev.entered = entered; |
| 5697 | |
| 5698 | return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); |
| 5699 | } |
| 5700 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5701 | void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 5702 | u8 addr_type, u8 status) |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5703 | { |
| 5704 | struct mgmt_ev_auth_failed ev; |
| 5705 | |
Johan Hedberg | bab73cb | 2012-02-09 16:07:29 +0200 | [diff] [blame] | 5706 | bacpy(&ev.addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 5707 | ev.addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | ca69b79 | 2011-11-11 18:10:00 +0200 | [diff] [blame] | 5708 | ev.status = mgmt_status(status); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5709 | |
Marcel Holtmann | e546099 | 2013-10-15 14:26:23 -0700 | [diff] [blame] | 5710 | mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 2a61169 | 2011-02-19 12:06:00 -0300 | [diff] [blame] | 5711 | } |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5712 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5713 | void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5714 | { |
| 5715 | struct cmd_lookup match = { NULL, hdev }; |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5716 | bool changed; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5717 | |
| 5718 | if (status) { |
| 5719 | u8 mgmt_err = mgmt_status(status); |
| 5720 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5721 | cmd_status_rsp, &mgmt_err); |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5722 | return; |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5723 | } |
| 5724 | |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5725 | if (test_bit(HCI_AUTH, &hdev->flags)) |
| 5726 | changed = !test_and_set_bit(HCI_LINK_SECURITY, |
| 5727 | &hdev->dev_flags); |
| 5728 | else |
| 5729 | changed = test_and_clear_bit(HCI_LINK_SECURITY, |
| 5730 | &hdev->dev_flags); |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5731 | |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5732 | mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5733 | &match); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5734 | |
Johan Hedberg | 47990ea | 2012-02-22 11:58:37 +0200 | [diff] [blame] | 5735 | if (changed) |
Marcel Holtmann | 464996a | 2013-10-15 14:26:24 -0700 | [diff] [blame] | 5736 | new_settings(hdev, match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5737 | |
| 5738 | if (match.sk) |
| 5739 | sock_put(match.sk); |
Johan Hedberg | 33ef95e | 2012-02-16 23:56:27 +0200 | [diff] [blame] | 5740 | } |
| 5741 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5742 | static void clear_eir(struct hci_request *req) |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5743 | { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5744 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5745 | struct hci_cp_write_eir cp; |
| 5746 | |
Johan Hedberg | 976eb20 | 2012-10-24 21:12:01 +0300 | [diff] [blame] | 5747 | if (!lmp_ext_inq_capable(hdev)) |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5748 | return; |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5749 | |
Johan Hedberg | c80da27 | 2012-02-22 15:38:48 +0200 | [diff] [blame] | 5750 | memset(hdev->eir, 0, sizeof(hdev->eir)); |
| 5751 | |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5752 | memset(&cp, 0, sizeof(cp)); |
| 5753 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5754 | hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); |
Johan Hedberg | cacaf52 | 2012-02-21 00:52:42 +0200 | [diff] [blame] | 5755 | } |
| 5756 | |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5757 | 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] | 5758 | { |
| 5759 | struct cmd_lookup match = { NULL, hdev }; |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5760 | struct hci_request req; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5761 | bool changed = false; |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5762 | |
| 5763 | if (status) { |
| 5764 | u8 mgmt_err = mgmt_status(status); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5765 | |
| 5766 | if (enable && test_and_clear_bit(HCI_SSP_ENABLED, |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5767 | &hdev->dev_flags)) { |
| 5768 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5769 | new_settings(hdev, NULL); |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5770 | } |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5771 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 5772 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, |
| 5773 | &mgmt_err); |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5774 | return; |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5775 | } |
| 5776 | |
| 5777 | if (enable) { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5778 | changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5779 | } else { |
Marcel Holtmann | 9ecb3e2 | 2013-10-10 03:08:11 -0700 | [diff] [blame] | 5780 | changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); |
| 5781 | if (!changed) |
| 5782 | changed = test_and_clear_bit(HCI_HS_ENABLED, |
| 5783 | &hdev->dev_flags); |
| 5784 | else |
| 5785 | clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5786 | } |
| 5787 | |
| 5788 | mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); |
| 5789 | |
Johan Hedberg | c0ecddc | 2012-02-22 12:38:31 +0200 | [diff] [blame] | 5790 | if (changed) |
Marcel Holtmann | 3e24856 | 2013-10-15 14:26:25 -0700 | [diff] [blame] | 5791 | new_settings(hdev, match.sk); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5792 | |
Johan Hedberg | 5fc6ebb | 2012-02-22 15:10:59 +0200 | [diff] [blame] | 5793 | if (match.sk) |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5794 | sock_put(match.sk); |
| 5795 | |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5796 | hci_req_init(&req, hdev); |
| 5797 | |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 5798 | if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { |
| 5799 | if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) |
| 5800 | hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, |
| 5801 | sizeof(enable), &enable); |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5802 | update_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 5803 | } else { |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5804 | clear_eir(&req); |
Johan Hedberg | 3769972 | 2014-06-24 14:00:27 +0300 | [diff] [blame] | 5805 | } |
Johan Hedberg | 890ea89 | 2013-03-15 17:06:52 -0500 | [diff] [blame] | 5806 | |
| 5807 | hci_req_run(&req, NULL); |
Johan Hedberg | ed2c4ee | 2012-02-17 00:56:28 +0200 | [diff] [blame] | 5808 | } |
| 5809 | |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5810 | void mgmt_sc_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) |
| 5811 | { |
| 5812 | struct cmd_lookup match = { NULL, hdev }; |
| 5813 | bool changed = false; |
| 5814 | |
| 5815 | if (status) { |
| 5816 | u8 mgmt_err = mgmt_status(status); |
| 5817 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5818 | if (enable) { |
| 5819 | if (test_and_clear_bit(HCI_SC_ENABLED, |
| 5820 | &hdev->dev_flags)) |
| 5821 | new_settings(hdev, NULL); |
| 5822 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5823 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5824 | |
| 5825 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5826 | cmd_status_rsp, &mgmt_err); |
| 5827 | return; |
| 5828 | } |
| 5829 | |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5830 | if (enable) { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5831 | changed = !test_and_set_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5832 | } else { |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5833 | changed = test_and_clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); |
Marcel Holtmann | 0ab04a9 | 2014-02-01 09:19:57 -0800 | [diff] [blame] | 5834 | clear_bit(HCI_SC_ONLY, &hdev->dev_flags); |
| 5835 | } |
Marcel Holtmann | eac83dc | 2014-01-10 02:07:23 -0800 | [diff] [blame] | 5836 | |
| 5837 | mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN, hdev, |
| 5838 | settings_rsp, &match); |
| 5839 | |
| 5840 | if (changed) |
| 5841 | new_settings(hdev, match.sk); |
| 5842 | |
| 5843 | if (match.sk) |
| 5844 | sock_put(match.sk); |
| 5845 | } |
| 5846 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5847 | static void sk_lookup(struct pending_cmd *cmd, void *data) |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5848 | { |
| 5849 | struct cmd_lookup *match = data; |
| 5850 | |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5851 | if (match->sk == NULL) { |
| 5852 | match->sk = cmd->sk; |
| 5853 | sock_hold(match->sk); |
| 5854 | } |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5855 | } |
| 5856 | |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5857 | void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, |
| 5858 | u8 status) |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5859 | { |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5860 | struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5861 | |
Johan Hedberg | 92da609 | 2013-03-15 17:06:55 -0500 | [diff] [blame] | 5862 | mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); |
| 5863 | mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); |
| 5864 | mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5865 | |
| 5866 | if (!status) |
Marcel Holtmann | 4e1b024 | 2013-10-15 14:26:26 -0700 | [diff] [blame] | 5867 | mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, |
| 5868 | NULL); |
Johan Hedberg | 90e7045 | 2012-02-23 23:09:40 +0200 | [diff] [blame] | 5869 | |
| 5870 | if (match.sk) |
| 5871 | sock_put(match.sk); |
Marcel Holtmann | 7f9a903 | 2012-02-22 18:38:01 +0100 | [diff] [blame] | 5872 | } |
| 5873 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5874 | 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] | 5875 | { |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5876 | struct mgmt_cp_set_local_name ev; |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5877 | struct pending_cmd *cmd; |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5878 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5879 | if (status) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5880 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5881 | |
| 5882 | memset(&ev, 0, sizeof(ev)); |
| 5883 | memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); |
Johan Hedberg | 28cc7bd | 2012-02-22 21:06:55 +0200 | [diff] [blame] | 5884 | memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5885 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5886 | cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5887 | if (!cmd) { |
| 5888 | memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5889 | |
Johan Hedberg | 1392897 | 2013-03-15 17:07:00 -0500 | [diff] [blame] | 5890 | /* If this is a HCI command related to powering on the |
| 5891 | * HCI dev don't send any mgmt signals. |
| 5892 | */ |
| 5893 | if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5894 | return; |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5895 | } |
| 5896 | |
Marcel Holtmann | 7667da3 | 2013-10-15 14:26:27 -0700 | [diff] [blame] | 5897 | mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), |
| 5898 | cmd ? cmd->sk : NULL); |
Johan Hedberg | b312b161 | 2011-03-16 14:29:37 +0200 | [diff] [blame] | 5899 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5900 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5901 | void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192, |
| 5902 | u8 *randomizer192, u8 *hash256, |
| 5903 | u8 *randomizer256, u8 status) |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5904 | { |
| 5905 | struct pending_cmd *cmd; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5906 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 5907 | BT_DBG("%s status %u", hdev->name, status); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5908 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 5909 | cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5910 | if (!cmd) |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5911 | return; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5912 | |
| 5913 | if (status) { |
Marcel Holtmann | 3edaf09 | 2013-10-15 14:26:28 -0700 | [diff] [blame] | 5914 | cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, |
| 5915 | mgmt_status(status)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5916 | } else { |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5917 | if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && |
| 5918 | hash256 && randomizer256) { |
| 5919 | struct mgmt_rp_read_local_oob_ext_data rp; |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5920 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5921 | memcpy(rp.hash192, hash192, sizeof(rp.hash192)); |
| 5922 | memcpy(rp.randomizer192, randomizer192, |
| 5923 | sizeof(rp.randomizer192)); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5924 | |
Marcel Holtmann | 4d2d279 | 2014-01-10 02:07:26 -0800 | [diff] [blame] | 5925 | memcpy(rp.hash256, hash256, sizeof(rp.hash256)); |
| 5926 | memcpy(rp.randomizer256, randomizer256, |
| 5927 | sizeof(rp.randomizer256)); |
| 5928 | |
| 5929 | cmd_complete(cmd->sk, hdev->id, |
| 5930 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5931 | &rp, sizeof(rp)); |
| 5932 | } else { |
| 5933 | struct mgmt_rp_read_local_oob_data rp; |
| 5934 | |
| 5935 | memcpy(rp.hash, hash192, sizeof(rp.hash)); |
| 5936 | memcpy(rp.randomizer, randomizer192, |
| 5937 | sizeof(rp.randomizer)); |
| 5938 | |
| 5939 | cmd_complete(cmd->sk, hdev->id, |
| 5940 | MGMT_OP_READ_LOCAL_OOB_DATA, 0, |
| 5941 | &rp, sizeof(rp)); |
| 5942 | } |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5943 | } |
| 5944 | |
| 5945 | mgmt_pending_remove(cmd); |
Szymon Janc | c35938b | 2011-03-22 13:12:21 +0100 | [diff] [blame] | 5946 | } |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5947 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5948 | 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] | 5949 | u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, |
| 5950 | u8 ssp, u8 *eir, u16 eir_len, u8 *scan_rsp, |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 5951 | u8 scan_rsp_len) |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5952 | { |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5953 | char buf[512]; |
| 5954 | struct mgmt_ev_device_found *ev = (void *) buf; |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 5955 | struct smp_irk *irk; |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5956 | size_t ev_size; |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5957 | |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5958 | if (!hci_discovery_active(hdev)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5959 | return; |
Andre Guedes | 12602d0 | 2013-04-30 15:29:40 -0300 | [diff] [blame] | 5960 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 5961 | /* Make sure that the buffer is big enough. The 5 extra bytes |
| 5962 | * are for the potential CoD field. |
| 5963 | */ |
| 5964 | if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf)) |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5965 | return; |
Andre Guedes | 7d262f8 | 2012-01-10 18:20:49 -0300 | [diff] [blame] | 5966 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5967 | memset(buf, 0, sizeof(buf)); |
| 5968 | |
Johan Hedberg | 5cedbb8 | 2014-02-18 21:41:37 +0200 | [diff] [blame] | 5969 | irk = hci_get_irk(hdev, bdaddr, addr_type); |
| 5970 | if (irk) { |
| 5971 | bacpy(&ev->addr.bdaddr, &irk->bdaddr); |
| 5972 | ev->addr.type = link_to_bdaddr(link_type, irk->addr_type); |
| 5973 | } else { |
| 5974 | bacpy(&ev->addr.bdaddr, bdaddr); |
| 5975 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
| 5976 | } |
| 5977 | |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5978 | ev->rssi = rssi; |
Johan Hedberg | 9a395a8 | 2012-02-23 00:00:32 +0200 | [diff] [blame] | 5979 | if (cfm_name) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5980 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME); |
Johan Hedberg | 388fc8f | 2012-02-23 00:38:59 +0200 | [diff] [blame] | 5981 | if (!ssp) |
Joe Perches | dcf4adb | 2014-03-12 10:52:35 -0700 | [diff] [blame] | 5982 | ev->flags |= cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5983 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5984 | if (eir_len > 0) |
Johan Hedberg | e319d2e | 2012-01-15 19:51:59 +0200 | [diff] [blame] | 5985 | memcpy(ev->eir, eir, eir_len); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5986 | |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5987 | if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) |
| 5988 | 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] | 5989 | dev_class, 3); |
Johan Hedberg | 1dc0609 | 2012-01-15 21:01:23 +0200 | [diff] [blame] | 5990 | |
Johan Hedberg | 5d2e9fa | 2014-03-25 10:30:47 +0200 | [diff] [blame] | 5991 | if (scan_rsp_len > 0) |
| 5992 | memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); |
| 5993 | |
| 5994 | ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); |
| 5995 | ev_size = sizeof(*ev) + eir_len + scan_rsp_len; |
Andre Guedes | f852359 | 2011-09-09 18:56:26 -0300 | [diff] [blame] | 5996 | |
Marcel Holtmann | 901801b | 2013-10-06 23:55:51 -0700 | [diff] [blame] | 5997 | mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); |
Johan Hedberg | e17acd4 | 2011-03-30 23:57:16 +0300 | [diff] [blame] | 5998 | } |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 5999 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6000 | void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, |
| 6001 | u8 addr_type, s8 rssi, u8 *name, u8 name_len) |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6002 | { |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6003 | struct mgmt_ev_device_found *ev; |
| 6004 | char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; |
| 6005 | u16 eir_len; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6006 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6007 | ev = (struct mgmt_ev_device_found *) buf; |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6008 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6009 | memset(buf, 0, sizeof(buf)); |
Johan Hedberg | a88a965 | 2011-03-30 13:18:12 +0300 | [diff] [blame] | 6010 | |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6011 | bacpy(&ev->addr.bdaddr, bdaddr); |
Andre Guedes | 57c1477 | 2012-04-24 21:02:50 -0300 | [diff] [blame] | 6012 | ev->addr.type = link_to_bdaddr(link_type, addr_type); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6013 | ev->rssi = rssi; |
| 6014 | |
| 6015 | 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] | 6016 | name_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6017 | |
Marcel Holtmann | eb55ef0 | 2012-03-14 18:08:46 +0200 | [diff] [blame] | 6018 | ev->eir_len = cpu_to_le16(eir_len); |
Johan Hedberg | b644ba3 | 2012-01-17 21:48:47 +0200 | [diff] [blame] | 6019 | |
Marcel Holtmann | 9cf12ae | 2013-10-06 23:55:52 -0700 | [diff] [blame] | 6020 | 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] | 6021 | } |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6022 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6023 | void mgmt_discovering(struct hci_dev *hdev, u8 discovering) |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6024 | { |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6025 | struct mgmt_ev_discovering ev; |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6026 | struct pending_cmd *cmd; |
| 6027 | |
Andre Guedes | 343fb14 | 2011-11-22 17:14:19 -0300 | [diff] [blame] | 6028 | BT_DBG("%s discovering %u", hdev->name, discovering); |
| 6029 | |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6030 | if (discovering) |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6031 | cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6032 | else |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6033 | cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6034 | |
| 6035 | if (cmd != NULL) { |
Johan Hedberg | f808e16 | 2012-02-19 12:52:07 +0200 | [diff] [blame] | 6036 | u8 type = hdev->discovery.type; |
| 6037 | |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6038 | cmd_complete(cmd->sk, hdev->id, cmd->opcode, 0, &type, |
| 6039 | sizeof(type)); |
Johan Hedberg | 164a6e7 | 2011-11-01 17:06:44 +0200 | [diff] [blame] | 6040 | mgmt_pending_remove(cmd); |
| 6041 | } |
| 6042 | |
Johan Hedberg | f963e8e | 2012-02-20 23:30:44 +0200 | [diff] [blame] | 6043 | memset(&ev, 0, sizeof(ev)); |
| 6044 | ev.type = hdev->discovery.type; |
| 6045 | ev.discovering = discovering; |
| 6046 | |
Marcel Holtmann | 2f1e063 | 2013-10-06 23:55:53 -0700 | [diff] [blame] | 6047 | mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); |
Johan Hedberg | 314b238 | 2011-04-27 10:29:57 -0400 | [diff] [blame] | 6048 | } |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6049 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6050 | 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] | 6051 | { |
| 6052 | struct pending_cmd *cmd; |
| 6053 | struct mgmt_ev_device_blocked ev; |
| 6054 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6055 | cmd = mgmt_pending_find(MGMT_OP_BLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6056 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6057 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6058 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6059 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6060 | return mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6061 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6062 | } |
| 6063 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6064 | 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] | 6065 | { |
| 6066 | struct pending_cmd *cmd; |
| 6067 | struct mgmt_ev_device_unblocked ev; |
| 6068 | |
Johan Hedberg | 2e58ef3 | 2011-11-08 20:40:15 +0200 | [diff] [blame] | 6069 | cmd = mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE, hdev); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6070 | |
Johan Hedberg | 88c1fe4 | 2012-02-09 15:56:11 +0200 | [diff] [blame] | 6071 | bacpy(&ev.addr.bdaddr, bdaddr); |
| 6072 | ev.addr.type = type; |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6073 | |
Johan Hedberg | 744cf19 | 2011-11-08 20:40:14 +0200 | [diff] [blame] | 6074 | return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &ev, sizeof(ev), |
Gustavo F. Padovan | 0412468 | 2012-03-08 01:25:00 -0300 | [diff] [blame] | 6075 | cmd ? cmd->sk : NULL); |
Antti Julku | 5e76244 | 2011-08-25 16:48:02 +0300 | [diff] [blame] | 6076 | } |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6077 | |
| 6078 | static void adv_enable_complete(struct hci_dev *hdev, u8 status) |
| 6079 | { |
| 6080 | BT_DBG("%s status %u", hdev->name, status); |
| 6081 | |
| 6082 | /* Clear the advertising mgmt setting if we failed to re-enable it */ |
| 6083 | if (status) { |
| 6084 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6085 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6086 | } |
| 6087 | } |
| 6088 | |
| 6089 | void mgmt_reenable_advertising(struct hci_dev *hdev) |
| 6090 | { |
| 6091 | struct hci_request req; |
| 6092 | |
Marcel Holtmann | b145edc | 2013-10-10 09:47:54 -0700 | [diff] [blame] | 6093 | if (hci_conn_num(hdev, LE_LINK) > 0) |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6094 | return; |
| 6095 | |
| 6096 | if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) |
| 6097 | return; |
| 6098 | |
| 6099 | hci_req_init(&req, hdev); |
| 6100 | enable_advertising(&req); |
| 6101 | |
| 6102 | /* If this fails we have no option but to let user space know |
| 6103 | * that we've disabled advertising. |
| 6104 | */ |
| 6105 | if (hci_req_run(&req, adv_enable_complete) < 0) { |
| 6106 | clear_bit(HCI_ADVERTISING, &hdev->dev_flags); |
Marcel Holtmann | a6d811e | 2013-10-06 04:11:12 -0700 | [diff] [blame] | 6107 | new_settings(hdev, NULL); |
Marcel Holtmann | 5976e60 | 2013-10-06 04:08:14 -0700 | [diff] [blame] | 6108 | } |
| 6109 | } |